Skip to content

Commit f1830f5

Browse files
committed
Fix #5006: Normalize prefixes of TypeApply in Erasure
1 parent a35d724 commit f1830f5

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,15 +1253,15 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
12531253
def desugarIdent(tree: Ident)(implicit ctx: Context): Tree = {
12541254
val qual = desugarIdentPrefix(tree)
12551255
if (qual.isEmpty) tree
1256-
else qual.select(tree.symbol)
1256+
else qual.select(tree.symbol).withSpan(tree.span)
12571257
}
12581258

12591259
/** Recover identifier prefix (e.g. this) if it exists */
12601260
def desugarIdentPrefix(tree: Ident)(implicit ctx: Context): Tree = tree.tpe match {
12611261
case TermRef(prefix: TermRef, _) =>
1262-
ref(prefix)
1262+
ref(prefix).withSpan(tree.span)
12631263
case TermRef(prefix: ThisType, _) =>
1264-
This(prefix.cls)
1264+
This(prefix.cls).withSpan(tree.span)
12651265
case _ =>
12661266
EmptyTree
12671267
}

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ object TypeTestsCasts {
156156
}
157157

158158
def interceptTypeApply(tree: TypeApply)(implicit ctx: Context): Tree = trace(s"transforming ${tree.show}", show = true) {
159-
tree.fun match {
160-
case fun @ Select(expr, selector) =>
159+
/** Intercept `expr.xyz[XYZ]` */
160+
def interceptWith(expr: Tree): Tree = {
161+
if (expr.isEmpty) tree
162+
else {
161163
val sym = tree.symbol
162164

163165
def isPrimitive(tp: Type) = tp.classSymbol.isPrimitiveValueClass
@@ -172,7 +174,7 @@ object TypeTestsCasts {
172174
def foundCls = effectiveClass(expr.tpe.widen)
173175

174176
def inMatch =
175-
fun.symbol == defn.Any_typeTest || // new scheme
177+
tree.fun.symbol == defn.Any_typeTest || // new scheme
176178
expr.symbol.is(Case) // old scheme
177179

178180
def transformIsInstanceOf(expr: Tree, testType: Type, flagUnrelated: Boolean): Tree = {
@@ -306,9 +308,13 @@ object TypeTestsCasts {
306308
else if (sym.isTypeCast)
307309
transformAsInstanceOf(erasure(tree.args.head.tpe))
308310
else tree
309-
310-
case _ =>
311-
tree
311+
}
312+
}
313+
val expr = tree.fun match {
314+
case Select(expr, _) => expr
315+
case i: Ident => desugarIdentPrefix(i)
316+
case _ => EmptyTree
312317
}
318+
interceptWith(expr)
313319
}
314320
}

tests/pos/i5006.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object i0 {
2+
def f: Int = asInstanceOf[Int].toInt
3+
}
4+
5+
class i2 {
6+
def f: Int = asInstanceOf[Int].toInt
7+
}
8+
9+
trait i3 {
10+
def f: Int = asInstanceOf[Int].toInt
11+
}

0 commit comments

Comments
 (0)