Skip to content

Commit 53c759d

Browse files
committed
Properly find constructors needed by the compiler
The compiler sometimes needs to invoke `ClassCastExceptionClass_stringConstructor` in generated code. Handle this properly in the presence of explicit nulls.
1 parent 1495c5c commit 53c759d

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,12 +660,16 @@ class Definitions {
660660
lazy val BoxedNumberClass: ClassSymbol = ctx.requiredClass("java.lang.Number")
661661
lazy val ClassCastExceptionClass: ClassSymbol = ctx.requiredClass("java.lang.ClassCastException")
662662
lazy val ClassCastExceptionClass_stringConstructor: TermSymbol = ClassCastExceptionClass.info.member(nme.CONSTRUCTOR).suchThat(_.info.firstParamTypes match {
663-
case List(pt) => (pt isRef StringClass)
663+
case List(pt) =>
664+
val pt1 = if (ctx.settings.YexplicitNulls.value) pt.stripNull else pt
665+
pt1 isRef StringClass
664666
case _ => false
665667
}).symbol.asTerm
666668
lazy val ArithmeticExceptionClass: ClassSymbol = ctx.requiredClass("java.lang.ArithmeticException")
667669
lazy val ArithmeticExceptionClass_stringConstructor: TermSymbol = ArithmeticExceptionClass.info.member(nme.CONSTRUCTOR).suchThat(_.info.firstParamTypes match {
668-
case List(pt) => (pt isRef StringClass)
670+
case List(pt) =>
671+
val pt1 = if (ctx.settings.YexplicitNulls.value) pt.stripNull else pt
672+
pt1 isRef StringClass
669673
case _ => false
670674
}).symbol.asTerm
671675

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Check that calling `asInstanceOf[Nothing]` throws a ClassCastException.
2+
// In particular, the compiler needs access to the right method to throw
3+
// the exception, and identifying the method uses some explicit nulls related
4+
// logic (see ClassCastExceptionClass in Definitions.scala).
5+
object Test {
6+
def main(args: Array[String]): Unit = {
7+
val x: String = "hello"
8+
try {
9+
val y: Nothing = x.asInstanceOf[Nothing]
10+
assert(false)
11+
} catch {
12+
case e: ClassCastException =>
13+
// ok
14+
}
15+
16+
val n: Null = null
17+
try {
18+
val y: Nothing = n.asInstanceOf[Nothing]
19+
assert(false)
20+
} catch {
21+
case e: ClassCastException =>
22+
// ok
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)