Skip to content

Commit 1b0315c

Browse files
authored
Merge pull request #1412 from dotty-staging/fix-#1284
Fix #1284: Make classTag depend directly on erasure
2 parents 0ba8d7d + 15fb8e1 commit 1b0315c

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ object TypeErasure {
277277
else tp1
278278
}
279279
}
280+
281+
/** Does the (possibly generic) type `tp` have the same erasure in all its
282+
* possible instantiations?
283+
*/
284+
def hasStableErasure(tp: Type)(implicit ctx: Context): Boolean = tp match {
285+
case tp: TypeRef =>
286+
tp.info match {
287+
case TypeAlias(alias) => hasStableErasure(alias)
288+
case _: ClassInfo => true
289+
case _ => false
290+
}
291+
case tp: PolyParam => false
292+
case tp: TypeProxy => hasStableErasure(tp.superType)
293+
case tp: AndOrType => hasStableErasure(tp.tp1) && hasStableErasure(tp.tp2)
294+
case _ => false
295+
}
280296
}
281297
import TypeErasure._
282298

@@ -493,4 +509,6 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
493509
println(s"no sig for $tp")
494510
throw ex
495511
}
512+
513+
496514
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import printing.Showable
1010
import Contexts._
1111
import Types._
1212
import Flags._
13+
import TypeErasure.{erasure, hasStableErasure}
1314
import Mode.ImplicitsEnabled
1415
import Denotations._
1516
import NameOps._
@@ -479,15 +480,12 @@ trait Implicits { self: Typer =>
479480
formal.argTypes match {
480481
case arg :: Nil =>
481482
val tp = fullyDefinedType(arg, "ClassTag argument", pos)
482-
tp.underlyingClassRef(refinementOK = false) match {
483-
case tref: TypeRef =>
484-
return ref(defn.ClassTagModule)
485-
.select(nme.apply)
486-
.appliedToType(tp)
487-
.appliedTo(clsOf(tref))
488-
.withPos(pos)
489-
case _ =>
490-
}
483+
if (hasStableErasure(tp))
484+
return ref(defn.ClassTagModule)
485+
.select(nme.apply)
486+
.appliedToType(tp)
487+
.appliedTo(clsOf(erasure(tp)))
488+
.withPos(pos)
491489
case _ =>
492490
}
493491
EmptyTree

tests/run/i1284.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case object A
2+
case object B
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
assert(Array(A, B).deep.toString == "Array(A, B)")
7+
}
8+
}

0 commit comments

Comments
 (0)