Skip to content

Commit 632daed

Browse files
committed
Minor tweaks in Types/Scopes.
All methods to do with handling imports more uniformly and early filtering of symbols which cannot be imported. Also make TypeBounds treat a Wildcard in lower or upper bounds as an empty bound, so we don't see all these method signatures like def f[T >: ? <: ?] because that's not helpful.
1 parent 2a1d020 commit 632daed

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

src/compiler/scala/tools/nsc/typechecker/Contexts.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ trait Contexts { self: Analyzer =>
10061006
}
10071007

10081008
def allImportedSymbols: Iterable[Symbol] =
1009-
qual.tpe.members flatMap (transformImport(tree.selectors, _))
1009+
importableMembers(qual.tpe) flatMap (transformImport(tree.selectors, _))
10101010

10111011
private def transformImport(selectors: List[ImportSelector], sym: Symbol): List[Symbol] = selectors match {
10121012
case List() => List()

src/reflect/scala/reflect/internal/Definitions.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,26 @@ trait Definitions extends api.StandardDefinitions {
235235
scope.sorted foreach fullyInitializeSymbol
236236
scope
237237
}
238+
/** Is this symbol a member of Object or Any? */
239+
def isUniversalMember(sym: Symbol) = (
240+
(sym ne NoSymbol)
241+
&& (ObjectClass isSubClass sym.owner)
242+
)
243+
244+
/** Is this symbol unimportable? Unimportable symbols include:
245+
* - constructors, because <init> is not a real name
246+
* - private[this] members, which cannot be referenced from anywhere else
247+
* - members of Any or Object, because every instance will inherit a
248+
* definition which supersedes the imported one
249+
*/
250+
def isUnimportable(sym: Symbol) = (
251+
(sym eq NoSymbol)
252+
|| sym.isConstructor
253+
|| sym.isPrivateLocal
254+
|| isUniversalMember(sym)
255+
)
256+
def isImportable(sym: Symbol) = !isUnimportable(sym)
257+
238258
/** Is this type equivalent to Any, AnyVal, or AnyRef? */
239259
def isTrivialTopType(tp: Type) = (
240260
tp =:= AnyClass.tpe

src/reflect/scala/reflect/internal/Scopes.scala

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -341,36 +341,19 @@ trait Scopes extends api.Scopes { self: SymbolTable =>
341341
*/
342342
def iterator: Iterator[Symbol] = toList.iterator
343343

344-
/*
345-
/** Does this scope contain an entry for `sym`?
346-
*/
347-
def contains(sym: Symbol): Boolean = lookupAll(sym.name) contains sym
348-
349-
/** A scope that contains all symbols of this scope and that also contains `sym`.
350-
*/
351-
def +(sym: Symbol): Scope =
352-
if (contains(sym)) this
353-
else {
354-
val result = cloneScope
355-
result enter sym
356-
result
357-
}
344+
def containsName(name: Name) = lookupEntry(name) != null
345+
def containsSymbol(s: Symbol) = lookupAll(s.name) contains s
358346

359-
/** A scope that contains all symbols of this scope except `sym`.
360-
*/
361-
def -(sym: Symbol): Scope =
362-
if (!contains(sym)) this
363-
else {
364-
val result = cloneScope
365-
result unlink sym
366-
result
367-
}
368-
*/
369347
override def foreach[U](p: Symbol => U): Unit = toList foreach p
370348

371-
override def filter(p: Symbol => Boolean): Scope =
372-
if (!(toList forall p)) newScopeWith(toList filter p: _*) else this
373-
349+
override def filterNot(p: Symbol => Boolean): Scope = (
350+
if (toList exists p) newScopeWith(toList filterNot p: _*)
351+
else this
352+
)
353+
override def filter(p: Symbol => Boolean): Scope = (
354+
if (toList forall p) this
355+
else newScopeWith(toList filter p: _*)
356+
)
374357
@deprecated("Use `toList.reverse` instead", "2.10.0")
375358
def reverse: List[Symbol] = toList.reverse
376359

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,8 +1516,8 @@ trait Types extends api.Types { self: SymbolTable =>
15161516
}
15171517
private def lowerString = if (emptyLowerBound) "" else " >: " + lo
15181518
private def upperString = if (emptyUpperBound) "" else " <: " + hi
1519-
private def emptyLowerBound = typeIsNothing(lo)
1520-
private def emptyUpperBound = typeIsAny(hi)
1519+
private def emptyLowerBound = typeIsNothing(lo) || lo.isWildcard
1520+
private def emptyUpperBound = typeIsAny(hi) || hi.isWildcard
15211521
def isEmptyBounds = emptyLowerBound && emptyUpperBound
15221522

15231523
// override def isNullable: Boolean = NullClass.tpe <:< lo;
@@ -7240,8 +7240,12 @@ trait Types extends api.Types { self: SymbolTable =>
72407240
/** Members of the given class, other than those inherited
72417241
* from Any or AnyRef.
72427242
*/
7243-
def nonTrivialMembers(clazz: Symbol): Iterable[Symbol] =
7244-
clazz.info.members filterNot (sym => sym.owner == ObjectClass || sym.owner == AnyClass)
7243+
def nonTrivialMembers(clazz: Symbol): Scope = clazz.info.members filterNot isUniversalMember
7244+
7245+
/** Members which can be imported into other scopes.
7246+
*/
7247+
def importableMembers(clazz: Symbol): Scope = importableMembers(clazz.info)
7248+
def importableMembers(pre: Type): Scope = pre.members filter isImportable
72457249

72467250
def objToAny(tp: Type): Type =
72477251
if (!phase.erasedTypes && tp.typeSymbol == ObjectClass) AnyClass.tpe

0 commit comments

Comments
 (0)