Skip to content

Commit 6080d06

Browse files
committed
Fix #4205: Select ClassTag directly for Unit, Any and AnyVal
1 parent b094bb0 commit 6080d06

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,15 @@ trait Implicits { self: Typer =>
554554
val etag = inferImplicitArg(defn.ClassTagType.appliedTo(elemTp), pos)
555555
if (etag.tpe.isError) EmptyTree else etag.select(nme.wrap)
556556
case tp if hasStableErasure(tp) && !defn.isBottomClass(tp.typeSymbol) =>
557-
ref(defn.ClassTagModule)
558-
.select(nme.apply)
559-
.appliedToType(tp)
560-
.appliedTo(clsOf(erasure(tp)))
561-
.withPos(pos)
557+
val sym = tp.typeSymbol
558+
if (sym == defn.UnitClass || sym == defn.AnyClass || sym == defn.AnyValClass)
559+
ref(defn.ClassTagModule).select(sym.name.toTermName).withPos(pos)
560+
else
561+
ref(defn.ClassTagModule)
562+
.select(nme.apply)
563+
.appliedToType(tp)
564+
.appliedTo(clsOf(erasure(tp)))
565+
.withPos(pos)
562566
case tp =>
563567
EmptyTree
564568
}

tests/run/i4205.check

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
true
2+
true
3+
true
4+
true
5+
true
6+
true
7+
true
8+
true
9+
true
10+
true
11+
true
12+
true
13+
true
14+
true

tests/run/i4205.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import scala.reflect.ClassTag
2+
import scala.runtime.BoxedUnit
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
println(implicitly[ClassTag[Unit]] == ClassTag.Unit)
7+
println(implicitly[ClassTag[Boolean]] == ClassTag.Boolean)
8+
println(implicitly[ClassTag[Byte]] == ClassTag.Byte)
9+
println(implicitly[ClassTag[Char]] == ClassTag.Char)
10+
println(implicitly[ClassTag[Short]] == ClassTag.Short)
11+
println(implicitly[ClassTag[Int]] == ClassTag.Int)
12+
println(implicitly[ClassTag[Long]] == ClassTag.Long)
13+
println(implicitly[ClassTag[Float]] == ClassTag.Float)
14+
println(implicitly[ClassTag[Double]] == ClassTag.Double)
15+
println(implicitly[ClassTag[Object]] == ClassTag.Object)
16+
println(implicitly[ClassTag[Any]] == ClassTag.Any)
17+
println(implicitly[ClassTag[AnyRef]] == ClassTag.AnyRef)
18+
println(implicitly[ClassTag[AnyVal]] == ClassTag.AnyVal)
19+
20+
println(implicitly[ClassTag[BoxedUnit]] != ClassTag.Unit)
21+
}
22+
}

0 commit comments

Comments
 (0)