Skip to content

Commit 0ebdbde

Browse files
committed
Rename baseType -> baseTypeRef
What was `baseType` and is now `baseTypeRef` only computes the prefix of of basetype, not the type arguments. If type arguments need to be included there is `baseTypeWithArgs`. The reason is that type arguments are usually already encoded as member types. But this was a source of errors because in Scala 2, baseType includes the type arguements. (also added test file structural.scala forgotten from last commit)
1 parent cba210d commit 0ebdbde

File tree

9 files changed

+54
-31
lines changed

9 files changed

+54
-31
lines changed

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ object SymDenotations {
449449
| ${owner.showLocated} where target is defined""".stripMargin)
450450
else if (
451451
!( isType // allow accesses to types from arbitrary subclasses fixes #4737
452-
|| pre.baseType(cls).exists
452+
|| pre.baseTypeRef(cls).exists // ??? why not use derivesFrom ???
453453
|| isConstructor
454454
|| (owner is ModuleClass) // don't perform this check for static members
455455
))
@@ -850,12 +850,12 @@ object SymDenotations {
850850
private[this] var myBaseClasses: List[ClassSymbol] = null
851851
private[this] var mySuperClassBits: BitSet = null
852852

853-
/** Invalidate baseTypeCache and superClassBits on new run */
853+
/** Invalidate baseTypeRefCache and superClassBits on new run */
854854
private def checkBasesUpToDate()(implicit ctx: Context) =
855-
if (baseTypeValid != ctx.runId) {
856-
baseTypeCache = new java.util.HashMap[CachedType, Type]
855+
if (baseTypeRefValid != ctx.runId) {
856+
baseTypeRefCache = new java.util.HashMap[CachedType, Type]
857857
mySuperClassBits = null
858-
baseTypeValid = ctx.runId
858+
baseTypeRefValid = ctx.runId
859859
}
860860

861861
private def computeBases(implicit ctx: Context): Unit = {
@@ -1065,18 +1065,18 @@ object SymDenotations {
10651065
raw.filterExcluded(excluded).asSeenFrom(pre).toDenot(pre)
10661066
}
10671067

1068-
private[this] var baseTypeCache: java.util.HashMap[CachedType, Type] = null
1069-
private[this] var baseTypeValid: RunId = NoRunId
1068+
private[this] var baseTypeRefCache: java.util.HashMap[CachedType, Type] = null
1069+
private[this] var baseTypeRefValid: RunId = NoRunId
10701070

1071-
/** Compute tp.baseType(this) */
1072-
final def baseTypeOf(tp: Type)(implicit ctx: Context): Type = {
1071+
/** Compute tp.baseTypeRef(this) */
1072+
final def baseTypeRefOf(tp: Type)(implicit ctx: Context): Type = {
10731073

10741074
def foldGlb(bt: Type, ps: List[Type]): Type = ps match {
1075-
case p :: ps1 => foldGlb(bt & baseTypeOf(p), ps1)
1075+
case p :: ps1 => foldGlb(bt & baseTypeRefOf(p), ps1)
10761076
case _ => bt
10771077
}
10781078

1079-
def computeBaseTypeOf(tp: Type): Type = {
1079+
def computeBaseTypeRefOf(tp: Type): Type = {
10801080
Stats.record("computeBaseTypeOf")
10811081
if (symbol.isStatic && tp.derivesFrom(symbol))
10821082
symbol.typeRef
@@ -1090,34 +1090,34 @@ object SymDenotations {
10901090
if (cdenot.superClassBits contains symbol.superId) foldGlb(NoType, tp.parents)
10911091
else NoType
10921092
case _ =>
1093-
baseTypeOf(tp.underlying)
1093+
baseTypeRefOf(tp.underlying)
10941094
}
10951095
case tp: TypeProxy =>
1096-
baseTypeOf(tp.underlying)
1096+
baseTypeRefOf(tp.underlying)
10971097
case AndType(tp1, tp2) =>
1098-
baseTypeOf(tp1) & baseTypeOf(tp2)
1098+
baseTypeRefOf(tp1) & baseTypeRefOf(tp2)
10991099
case OrType(tp1, tp2) =>
1100-
baseTypeOf(tp1) | baseTypeOf(tp2)
1100+
baseTypeRefOf(tp1) | baseTypeRefOf(tp2)
11011101
case _ =>
11021102
NoType
11031103
}
11041104
}
11051105

1106-
/*>|>*/ ctx.debugTraceIndented(s"$tp.baseType($this)") /*<|<*/ {
1106+
/*>|>*/ ctx.debugTraceIndented(s"$tp.baseTypeRef($this)") /*<|<*/ {
11071107
tp match {
11081108
case tp: CachedType =>
11091109
checkBasesUpToDate()
1110-
var basetp = baseTypeCache get tp
1110+
var basetp = baseTypeRefCache get tp
11111111
if (basetp == null) {
1112-
baseTypeCache.put(tp, NoPrefix)
1113-
basetp = computeBaseTypeOf(tp)
1114-
baseTypeCache.put(tp, basetp)
1112+
baseTypeRefCache.put(tp, NoPrefix)
1113+
basetp = computeBaseTypeRefOf(tp)
1114+
baseTypeRefCache.put(tp, basetp)
11151115
} else if (basetp == NoPrefix) {
11161116
throw new CyclicReference(this)
11171117
}
11181118
basetp
11191119
case _ =>
1120-
computeBaseTypeOf(tp)
1120+
computeBaseTypeRefOf(tp)
11211121
}
11221122
}
11231123
}

src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class TypeApplications(val self: Type) extends AnyVal {
152152

153153
/** The base type including all type arguments of this type */
154154
final def baseTypeWithArgs(base: Symbol)(implicit ctx: Context): Type =
155-
self.baseType(base).appliedTo(baseTypeArgs(base))
155+
self.baseTypeRef(base).appliedTo(baseTypeArgs(base))
156156

157157
/** Translate a type of the form From[T] to To[T], keep other types as they are.
158158
* `from` and `to` must be static classes, both with one type parameter, and the same variance.

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ class TypeComparer(initctx: Context) extends DotClass {
405405
case _ =>
406406
val cls2 = tp2.symbol
407407
if (cls2.isClass) {
408-
val base = tp1.baseType(cls2)
408+
val base = tp1.baseTypeRef(cls2)
409409
if (base.exists && (base ne tp1)) return isSubType(base, tp2)
410410
if ( cls2 == defn.SingletonClass && tp1.isStable
411411
|| cls2 == defn.NotNullClass && tp1.isNotNull

src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ trait TypeOps { this: Context =>
1212
def toPrefix(pre: Type, cls: Symbol, thiscls: ClassSymbol): Type = /*>|>*/ ctx.debugTraceIndented(s"toPrefix($pre, $cls, $thiscls)") /*<|<*/ {
1313
if ((pre eq NoType) || (pre eq NoPrefix) || (cls is PackageClass))
1414
tp
15-
else if (thiscls.derivesFrom(cls) && pre.baseType(thiscls).exists)
15+
else if (thiscls.derivesFrom(cls) && pre.baseTypeRef(thiscls).exists)
1616
pre match {
1717
case SuperType(thispre, _) => thispre
1818
case _ => pre
1919
}
2020
else
21-
toPrefix(pre.baseType(cls).normalizedPrefix, cls.owner, thiscls)
21+
toPrefix(pre.baseTypeRef(cls).normalizedPrefix, cls.owner, thiscls)
2222
}
2323

2424
/*>|>*/ ctx.conditionalTraceIndented(TypeOps.track , s"asSeen ${tp.show} from (${pre.show}, ${cls.show})", show = true) /*<|<*/ { // !!! DEBUG

src/dotty/tools/dotc/core/Types.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,12 @@ object Types {
495495
this, that, alwaysMatchSimple = !ctx.phase.erasedTypes)
496496
}
497497

498-
/** The basetype of this type with given class symbol */
499-
final def baseType(base: Symbol)(implicit ctx: Context): Type = /*ctx.traceIndented(s"$this baseType $base")*/ /*>|>*/ track("baseType") /*<|<*/ {
498+
/** The basetype TypeRef of this type with given class symbol,
499+
* but without including any type arguments
500+
*/
501+
final def baseTypeRef(base: Symbol)(implicit ctx: Context): Type = /*ctx.traceIndented(s"$this baseTypeRef $base")*/ /*>|>*/ track("baseTypeRef") /*<|<*/ {
500502
base.denot match {
501-
case classd: ClassDenotation => classd.baseTypeOf(this)//widen.dealias)
503+
case classd: ClassDenotation => classd.baseTypeRefOf(this)//widen.dealias)
502504
case _ => NoType
503505
}
504506
}

src/dotty/tools/dotc/core/pickling/UnPickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ class UnPickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClassRoot:
631631
if (sym.owner != cls) {
632632
val overriding = cls.decls.lookup(sym.name)
633633
if (overriding.exists && overriding != sym) {
634-
val base = pre.baseType(sym.owner)
634+
val base = pre.baseTypeWithArgs(sym.owner)
635635
assert(base.exists)
636636
pre = SuperType(pre, base)
637637
}

src/dotty/tools/dotc/core/transform/Erasure.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ object Erasure {
5151
case AndType(tp1, tp2) =>
5252
erasure(tp1)
5353
case OrType(tp1, tp2) =>
54-
erasure(tp.baseType(lubClass(tp1, tp2)))
54+
erasure(tp.baseTypeRef(lubClass(tp1, tp2)))
5555
case tp: MethodType =>
5656
tp.derivedMethodType(
5757
tp.paramNames, tp.paramTypes.mapConserve(erasure), resultErasure(tp.resultType))

src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ trait Applications extends Compatibility { self: Typer =>
222222
val pre =
223223
if (meth.isClassConstructor) {
224224
// default getters for class constructors are found in the companion object
225-
mpre.baseType(cls) match {
225+
mpre.baseTypeRef(cls) match {
226226
case tp: TypeRef => ref(tp.prefix, cls.companionModule)
227227
case _ => NoType
228228
}

tests/pos/structural.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
object test123 {
2+
type A = { def a: Int }
3+
def f(a: A): A = a
4+
}
5+
6+
object structural2 {
7+
type A = { def a: Int }
8+
9+
type B = {
10+
def b: Int
11+
}
12+
13+
type AB = A & B
14+
15+
def f(ab: AB): AB = ab
16+
17+
f(new {
18+
def a = 43
19+
def b = 42
20+
})
21+
}

0 commit comments

Comments
 (0)