Skip to content

Commit e34555f

Browse files
committed
Disallow taking a class tag of Nothing or Null.
It seems in most cases this leads to weird behavior and cause confusing error messages later. It also means we cannot create an Array[Nothing], except by passing the classtag explicitly.
1 parent b11e6d6 commit e34555f

File tree

7 files changed

+21
-7
lines changed

7 files changed

+21
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ trait Implicits { self: Typer =>
521521
val etag = inferImplicitArg(defn.ClassTagType.appliedTo(elemTp), error, pos)
522522
if (etag.isEmpty) etag else etag.select(nme.wrap)
523523
case tp if hasStableErasure(tp) =>
524+
if (defn.isBottomClass(tp.typeSymbol))
525+
error(where => i"attempt to take ClassTag of undetermined type for $where")
524526
ref(defn.ClassTagModule)
525527
.select(nme.apply)
526528
.appliedToType(tp)

tests/neg/i1802.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ object Exception {
1414
def apply(x: Throwable): T = f(downcast(x).get)
1515
}
1616

17-
def mkThrowableCatcher[T](isDef: Throwable => Boolean, f: Throwable => T) = mkCatcher(isDef, f)
17+
def mkThrowableCatcher[T](isDef: Throwable => Boolean, f: Throwable => T) = mkCatcher(isDef, f) // error: undetermined ClassTag
1818

19-
implicit def throwableSubtypeToCatcher[Ex <: Throwable: ClassTag, T](pf: PartialFunction[Ex, T]) = // error: cyclic reference
19+
implicit def throwableSubtypeToCatcher[Ex <: Throwable: ClassTag, T](pf: PartialFunction[Ex, T]) =
2020
mkCatcher(pf.isDefinedAt _, pf.apply _)
2121
}

tests/neg/i1907.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import java.io.File
2+
3+
object Test {
4+
Some(new File("."))
5+
.map(_.listFiles).getOrElse(Array.empty) // error: undetermined ClassTag
6+
.map(_.listFiles)
7+
}

tests/neg/undet-classtag.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
def f[T: reflect.ClassTag](x: T) = ???
3+
4+
f(???) // error: undetermined ClassTag
5+
}

tests/pos/t3859.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Test {
2-
def foo: Unit = bar(Array(): _*)
2+
def foo: Unit = bar(Array[AnyRef](): _*)
33
def bar(values: AnyRef*): Unit = ()
44
}

tests/pos/t5859.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class A {
77
f(List[AnyRef](): _*)
88
f(List(): _*)
99
f(Nil: _*)
10-
f(Array(): _*)
10+
// f(Array(): _*) // undetermined ClassTag
1111
f(Array[AnyRef](): _*)
1212
f(List(1))
1313
f(List(1), Nil: _*)
14-
f(List(1), Array(): _*)
14+
// f(List(1), Array(): _*) // undetermined ClassTag
1515
}

tests/run/array-addition.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ object Test {
44
def main(args: Array[String]): Unit = {
55
prettyPrintArray(Array(1,2,3) :+ 4)
66
prettyPrintArray(1 +: Array(2,3,4))
7-
prettyPrintArray(Array() :+ 1)
8-
prettyPrintArray(1 +: Array())
7+
prettyPrintArray(Array[Int]() :+ 1)
8+
prettyPrintArray(1 +: Array[Int]())
99
}
1010
}
1111

0 commit comments

Comments
 (0)