Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit beadafa

Browse files
committed
Selective dealiasing when printing errors.
*** Important note for busy commit log skimmers *** Symbol method "fullName" has been trying to serve the dual role of "how to print a symbol" and "how to find a class file." It cannot serve both these roles simultaneously, primarily because of package objects but other little things as well. Since in the majority of situations we want the one which corresponds to the idealized scala world, not the grubby bytecode, I went with that for fullName. When you require the path to a class (e.g. you are calling Class.forName) you should use javaClassName. package foo { package object bar { class Bippy } } If sym is Bippy's symbol, then sym.fullName == foo.bar.Bippy sym.javaClassName == foo.bar.package.Bippy *** End important note *** There are many situations where we (until now) forewent revealing everything we knew about a type mismatch. For instance, this isn't very helpful of scalac (at least in those more common cases where you didn't define type X on the previous repl line.) scala> type X = Int defined type alias X scala> def f(x: X): Byte = x <console>:8: error: type mismatch; found : X required: Byte def f(x: X): Byte = x ^ Now it says: found : X (which expands to) Int required: Byte def f(x: X): Byte = x ^ In addition I rearchitected a number of methods involving: - finding a symbol's owner - calculating a symbol's name - determining whether to print a prefix No review.
1 parent 55109d0 commit beadafa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+366
-321
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,13 @@ trait Definitions extends reflect.api.StandardDefinitions {
142142
lazy val EmptyPackage = RootClass.newPackage(NoPosition, nme.EMPTY_PACKAGE_NAME).setFlag(FINAL)
143143
lazy val EmptyPackageClass = EmptyPackage.moduleClass
144144

145-
lazy val JavaLangPackage = getModule(sn.JavaLang)
146-
lazy val ScalaPackage = getModule("scala")
147-
lazy val ScalaPackageClass = ScalaPackage.tpe.typeSymbol
145+
lazy val JavaLangPackage = getModule(sn.JavaLang)
146+
lazy val JavaLangPackageClass = JavaLangPackage.moduleClass
147+
lazy val ScalaPackage = getModule(nme.scala_)
148+
lazy val ScalaPackageClass = ScalaPackage.moduleClass
148149

149150
lazy val RuntimePackage = getModule("scala.runtime")
150-
lazy val RuntimePackageClass = RuntimePackage.tpe.typeSymbol
151+
lazy val RuntimePackageClass = RuntimePackage.moduleClass
151152

152153
// convenient one-argument parameter lists
153154
lazy val anyparam = List(AnyClass.typeConstructor)
@@ -221,7 +222,7 @@ trait Definitions extends reflect.api.StandardDefinitions {
221222
lazy val BridgeClass = getClass("scala.annotation.bridge")
222223

223224
// fundamental reference classes
224-
lazy val ScalaObjectClass = getClass("scala.ScalaObject")
225+
lazy val ScalaObjectClass = getMember(ScalaPackageClass, tpnme.ScalaObject)
225226
lazy val PartialFunctionClass = getClass("scala.PartialFunction")
226227
lazy val SymbolClass = getClass("scala.Symbol")
227228
lazy val StringClass = getClass(sn.String)
@@ -233,9 +234,17 @@ trait Definitions extends reflect.api.StandardDefinitions {
233234
// fundamental modules
234235
lazy val SysPackage = getPackageObject("scala.sys")
235236
def Sys_error = getMember(SysPackage, nme.error)
237+
238+
// Modules whose members are in the default namespace
239+
lazy val UnqualifiedModules = List(PredefModule, ScalaPackage, JavaLangPackage)
240+
// Those modules and their module classes
241+
lazy val UnqualifiedOwners = UnqualifiedModules.toSet ++ UnqualifiedModules.map(_.moduleClass)
242+
236243
lazy val PredefModule: Symbol = getModule("scala.Predef")
237-
lazy val PredefModuleClass = PredefModule.tpe.typeSymbol
238-
def Predef_AnyRef = getMember(PredefModule, "AnyRef") // used by the specialization annotation
244+
lazy val PredefModuleClass = PredefModule.moduleClass
245+
// Note: this is not the type alias AnyRef, it's a val defined in Predef
246+
// used by the @specialize annotation.
247+
def Predef_AnyRef = getMember(PredefModule, tpnme.AnyRef.toTermName)
239248
def Predef_classOf = getMember(PredefModule, nme.classOf)
240249
def Predef_identity = getMember(PredefModule, nme.identity)
241250
def Predef_conforms = getMember(PredefModule, nme.conforms)

src/compiler/scala/reflect/internal/Symbols.scala

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -395,34 +395,40 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
395395
final def isAnonymousFunction = isSynthetic && (name containsName tpnme.ANON_FUN_NAME)
396396
final def isAnonOrRefinementClass = isAnonymousClass || isRefinementClass
397397

398-
final def isPackageObject = isModule && name == nme.PACKAGEkw && owner.isPackageClass
399-
final def isPackageObjectClass = isModuleClass && name.toTermName == nme.PACKAGEkw && owner.isPackageClass
400-
final def definedInPackage = owner.isPackageClass || owner.isPackageObjectClass
398+
def isPackageObjectOrClass = (name.toTermName == nme.PACKAGEkw) && owner.isPackageClass
399+
final def isPackageObject = isModule && isPackageObjectOrClass
400+
final def isPackageObjectClass = isModuleClass && isPackageObjectOrClass
401+
final def isDefinedInPackage = effectiveOwner.isPackageClass
401402
final def isJavaInterface = isJavaDefined && isTrait
402403
final def needsFlatClasses: Boolean = phase.flatClasses && rawowner != NoSymbol && !rawowner.isPackageClass
403404

404-
// not printed as prefixes
405-
final def isPredefModule = this == PredefModule
406-
final def isScalaPackage = (this == ScalaPackage) || (isPackageObject && owner == ScalaPackageClass)
407-
final def isScalaPackageClass = skipPackageObject == ScalaPackageClass
408-
def inDefaultNamespace = owner.isPredefModule || owner.isScalaPackageClass
405+
// In java.lang, Predef, or scala package/package object
406+
def isInDefaultNamespace = UnqualifiedOwners(effectiveOwner)
409407

410408
/** If this is a package object or package object class, its owner: otherwise this.
411409
*/
412410
final def skipPackageObject: Symbol = if (isPackageObjectClass) owner else this
413411

412+
/** The owner, skipping package objects.
413+
*/
414+
def effectiveOwner = owner.skipPackageObject
415+
414416
/** If this is a constructor, its owner: otherwise this.
415417
*/
416418
final def skipConstructor: Symbol = if (isConstructor) owner else this
417419

418420
/** Conditions where we omit the prefix when printing a symbol, to avoid
419421
* unpleasantries like Predef.String, $iw.$iw.Foo and <empty>.Bippy.
420422
*/
421-
final def printWithoutPrefix = !settings.debug.value && (
422-
isScalaPackageClass || isPredefModule || isEffectiveRoot || isAnonOrRefinementClass ||
423-
nme.isReplWrapperName(name) // not isInterpreterWrapper due to nesting
423+
final def isOmittablePrefix = !settings.debug.value && (
424+
UnqualifiedOwners(skipPackageObject)
425+
|| isEmptyPrefix
426+
)
427+
def isEmptyPrefix = (
428+
isEffectiveRoot // has no prefix for real, <empty> or <root>
429+
|| isAnonOrRefinementClass // has uninteresting <anon> or <refinement> prefix
430+
|| nme.isReplWrapperName(name) // has ugly $iw. prefix (doesn't call isInterpreterWrapper due to nesting)
424431
)
425-
426432
def isFBounded = info.baseTypeSeq exists (_ contains this)
427433

428434
/** Is symbol a monomorphic type?
@@ -723,7 +729,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
723729

724730
/** The decoded name of the symbol, e.g. `==` instead of `\$eq\$eq`.
725731
*/
726-
def decodedName: String = stripLocalSuffix(NameTransformer.decode(encodedName))
732+
def decodedName: String = stripNameString(NameTransformer.decode(encodedName))
727733

728734
def moduleSuffix: String = (
729735
if (hasModuleFlag && !isMethod && !isImplClass && !isJavaDefined) nme.MODULE_SUFFIX_STRING
@@ -732,22 +738,32 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
732738

733739
/** These should be moved somewhere like JavaPlatform.
734740
*/
735-
def javaSimpleName = stripLocalSuffix("" + simpleName) + moduleSuffix
736-
def javaBinaryName = fullName('/') + moduleSuffix
737-
def javaClassName = fullName('.') + moduleSuffix
741+
def javaSimpleName = ("" + simpleName).trim + moduleSuffix
742+
def javaBinaryName = fullNameInternal('/') + moduleSuffix
743+
def javaClassName = fullNameInternal('.') + moduleSuffix
738744

739745
/** The encoded full path name of this symbol, where outer names and inner names
740746
* are separated by `separator` characters.
741747
* Never translates expansions of operators back to operator symbol.
742748
* Never adds id.
749+
* Drops package objects.
750+
*/
751+
final def fullName(separator: Char): String = stripNameString(fullNameInternal(separator))
752+
753+
/** Doesn't drop package objects, for those situations (e.g. classloading)
754+
* where the true path is needed.
743755
*/
744-
final def fullName(separator: Char): String = stripLocalSuffix {
756+
private def fullNameInternal(separator: Char): String = (
745757
if (isRoot || isRootPackage || this == NoSymbol) this.toString
746758
else if (owner.isEffectiveRoot) encodedName
747-
else owner.enclClass.fullName(separator) + separator + encodedName
748-
}
759+
else effectiveOwner.enclClass.fullName(separator) + separator + encodedName
760+
)
749761

750-
private def stripLocalSuffix(s: String) = s stripSuffix nme.LOCAL_SUFFIX_STRING
762+
/** Strip package objects and any local suffix.
763+
*/
764+
private def stripNameString(s: String) =
765+
if (settings.debug.value) s
766+
else s.replaceAllLiterally(".package", "").trim
751767

752768
/** The encoded full path name of this symbol, where outer names and inner names
753769
* are separated by periods.
@@ -1358,11 +1374,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
13581374

13591375
/** The package containing this symbol, or NoSymbol if there
13601376
* is not one. */
1361-
def enclosingPackage: Symbol = {
1362-
val packSym = enclosingPackageClass
1363-
if (packSym != NoSymbol) packSym.companionModule
1364-
else packSym
1365-
}
1377+
def enclosingPackage: Symbol = enclosingPackageClass.companionModule
13661378

13671379
/** Return the original enclosing method of this symbol. It should return
13681380
* the same thing as enclMethod when called before lambda lift,
@@ -1420,8 +1432,8 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
14201432
/** Is this symbol defined in the same scope and compilation unit as `that` symbol? */
14211433
def isCoDefinedWith(that: Symbol) = (
14221434
(this.rawInfo ne NoType) &&
1423-
(this.owner == that.owner) && {
1424-
!this.owner.isPackageClass ||
1435+
(this.effectiveOwner == that.effectiveOwner) && {
1436+
!this.effectiveOwner.isPackageClass ||
14251437
(this.sourceFile eq null) ||
14261438
(that.sourceFile eq null) ||
14271439
(this.sourceFile == that.sourceFile) || {
@@ -1784,11 +1796,10 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
17841796
* to the name of the owner.
17851797
*/
17861798
def hasMeaninglessName = (
1787-
isSetterParameter // x$1
1788-
|| isClassConstructor // this
1789-
|| isPackageObject // package
1790-
|| isPackageObjectClass // package$
1791-
|| isRefinementClass // <refinement>
1799+
isSetterParameter // x$1
1800+
|| isClassConstructor // this
1801+
|| isPackageObjectOrClass // package
1802+
|| isRefinementClass // <refinement>
17921803
)
17931804

17941805
/** String representation of symbol's simple name.
@@ -1812,9 +1823,8 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
18121823
/** String representation of location.
18131824
*/
18141825
def ownsString = {
1815-
val owns = owner.skipPackageObject
1816-
if (owns.isClass && !owns.printWithoutPrefix && !isScalaPackageClass) "" + owns
1817-
else ""
1826+
val owns = effectiveOwner
1827+
if (owns.isClass && !owns.isEmptyPrefix) "" + owns else ""
18181828
}
18191829

18201830
/** String representation of location, plus a preposition. Doesn't do much,
@@ -1963,7 +1973,7 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
19631973

19641974
protected def doCookJavaRawInfo() {
19651975
def cook(sym: Symbol) {
1966-
require(sym hasFlag JAVA)
1976+
require(sym.isJavaDefined, sym)
19671977
// @M: I think this is more desirable, but Martin prefers to leave raw-types as-is as much as possible
19681978
// object rawToExistentialInJava extends TypeMap {
19691979
// def apply(tp: Type): Type = tp match {
@@ -1985,9 +1995,9 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
19851995

19861996
if (isJavaDefined)
19871997
cook(this)
1988-
else if (hasFlag(OVERLOADED))
1998+
else if (isOverloaded)
19891999
for (sym2 <- alternatives)
1990-
if (sym2 hasFlag JAVA)
2000+
if (sym2.isJavaDefined)
19912001
cook(sym2)
19922002
}
19932003
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ trait Types extends api.Types { self: SymbolTable =>
11571157
override def prefixString =
11581158
if (settings.debug.value) sym.nameString + ".this."
11591159
else if (sym.isAnonOrRefinementClass) "this."
1160-
else if (sym.printWithoutPrefix) ""
1160+
else if (sym.isOmittablePrefix) ""
11611161
else if (sym.isModuleClass) sym.fullName + "."
11621162
else sym.nameString + ".this."
11631163
override def safeToString: String =
@@ -1220,8 +1220,9 @@ trait Types extends api.Types { self: SymbolTable =>
12201220

12211221
override def termSymbol = sym
12221222
override def prefix: Type = pre
1223-
override def prefixString: String =
1224-
if ((sym.isEmptyPackage || sym.isInterpreterWrapper || sym.isPredefModule || sym.isScalaPackage) && !settings.debug.value) ""
1223+
override def prefixString =
1224+
if (sym.isPackageObjectOrClass) pre.prefixString
1225+
else if (sym.isOmittablePrefix) ""
12251226
else pre.prefixString + sym.nameString + "."
12261227
override def kind = "SingleType"
12271228
}
@@ -2050,8 +2051,10 @@ A type's typeSymbol should never be inspected directly.
20502051
override def prefixString = "" + (
20512052
if (settings.debug.value)
20522053
super.prefixString
2053-
else if (sym.printWithoutPrefix)
2054+
else if (sym.isOmittablePrefix)
20542055
""
2056+
else if (sym.isPackageObjectOrClass)
2057+
sym.owner.fullName + "."
20552058
else if (sym.isPackageClass)
20562059
sym.fullName + "."
20572060
else if (isStable && nme.isSingletonName(sym.name))
@@ -4080,7 +4083,7 @@ A type's typeSymbol should never be inspected directly.
40804083
def corresponds(sym1: Symbol, sym2: Symbol): Boolean =
40814084
sym1.name == sym2.name && (sym1.isPackageClass || corresponds(sym1.owner, sym2.owner))
40824085
if (!corresponds(sym.owner, rebind0.owner)) {
4083-
debuglog("ADAPT1 pre = "+pre+", sym = "+sym+sym.locationString+", rebind = "+rebind0+rebind0.locationString)
4086+
debuglog("ADAPT1 pre = "+pre+", sym = "+sym.fullLocationString+", rebind = "+rebind0.fullLocationString)
40844087
val bcs = pre.baseClasses.dropWhile(bc => !corresponds(bc, sym.owner));
40854088
if (bcs.isEmpty)
40864089
assert(pre.typeSymbol.isRefinementClass, pre) // if pre is a refinementclass it might be a structural type => OK to leave it in.
@@ -4089,11 +4092,8 @@ A type's typeSymbol should never be inspected directly.
40894092
debuglog(
40904093
"ADAPT2 pre = " + pre +
40914094
", bcs.head = " + bcs.head +
4092-
", sym = " + sym+sym.locationString +
4093-
", rebind = " + rebind0 + (
4094-
if (rebind0 == NoSymbol) ""
4095-
else rebind0.locationString
4096-
)
4095+
", sym = " + sym.fullLocationString +
4096+
", rebind = " + rebind0.fullLocationString
40974097
)
40984098
}
40994099
val rebind = rebind0.suchThat(sym => sym.isType || sym.isStable)

src/compiler/scala/reflect/runtime/Loaders.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trait Loaders { self: SymbolTable =>
3535
assert(sym == clazz || sym == module || sym == module.moduleClass)
3636
// try {
3737
atPhaseNotLaterThan(picklerPhase) {
38-
unpickleClass(clazz, module, javaClass(clazz.fullName))
38+
unpickleClass(clazz, module, javaClass(clazz.javaClassName))
3939
// } catch {
4040
// case ex: ClassNotFoundException => makePackage()
4141
// case ex: NoClassDefFoundError => makePackage()

src/compiler/scala/reflect/runtime/ScalaToJava.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ trait ScalaToJava extends ConversionUtil { self: SymbolTable =>
4343
else if (clazz == ArrayClass)
4444
noClass
4545
else if (clazz.owner.isPackageClass)
46-
javaClass(clazz.fullName)
46+
javaClass(clazz.javaClassName)
4747
else if (clazz.owner.isClass)
4848
classToJava(clazz.owner)
4949
.getDeclaredClasses

src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ModelFactory(val global: Global, val settings: doc.Settings) {
3939
"memberSym " + memberSym + " templateSym " + templateSym + " encls = " +
4040
closestPackage(memberSym) + ", " + closestPackage(templateSym)
4141
)
42-
memberSym.inDefaultNamespace || (closestPackage(memberSym) == closestPackage(templateSym))
42+
memberSym.isOmittablePrefix || (closestPackage(memberSym) == closestPackage(templateSym))
4343
}
4444

4545
private lazy val noSubclassCache = Set(AnyClass, AnyRefClass, ObjectClass, ScalaObjectClass)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ trait Implicits {
335335
pre1: String, pre2: String, trailer: String) =
336336
if (!info1.tpe.isErroneous && !info2.tpe.isErroneous) {
337337
val coreMsg =
338-
pre1+" "+info1.sym+info1.sym.locationString+" of type "+info1.tpe+"\n "+
339-
pre2+" "+info2.sym+info2.sym.locationString+" of type "+info2.tpe+"\n "+
338+
pre1+" "+info1.sym.fullLocationString+" of type "+info1.tpe+"\n "+
339+
pre2+" "+info2.sym.fullLocationString+" of type "+info2.tpe+"\n "+
340340
trailer
341341
error(tree.pos,
342342
if (isView) {
@@ -408,7 +408,7 @@ trait Implicits {
408408
if (!(pt.isErroneous))
409409
context.unit.error(
410410
tree.pos, "diverging implicit expansion for type "+pt+"\nstarting with "+
411-
info.sym+info.sym.locationString)
411+
info.sym.fullLocationString)
412412
SearchFailure
413413
} else {
414414
throw DivergentImplicit
@@ -545,7 +545,7 @@ trait Implicits {
545545
SearchFailure
546546
else if (!hasMatchingSymbol(itree1))
547547
fail("candidate implicit %s is shadowed by other implicit %s".format(
548-
info.sym + info.sym.locationString, itree1.symbol + itree1.symbol.locationString))
548+
info.sym.fullLocationString, itree1.symbol.fullLocationString))
549549
else {
550550
val tvars = undetParams map freshVar
551551

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ abstract class SuperAccessors extends transform.Transform with transform.TypingT
403403
val isCandidate = (
404404
sym.isProtected
405405
&& sym.isJavaDefined
406-
&& !sym.definedInPackage
406+
&& !sym.isDefinedInPackage
407407
&& !accessibleThroughSubclassing
408408
&& (sym.owner.enclosingPackageClass != currentOwner.enclosingPackageClass)
409409
&& (sym.owner.enclosingPackageClass == packageAccessBoundry(sym))

0 commit comments

Comments
 (0)