Skip to content

Commit 45b7f14

Browse files
committed
Suppress inline checking after erasure
After erasure, the rhs of an inline val like inline val n = 3 no longer has a constant type. Therefore, we need to disable the check for it after erasure. Test case (discovered by accident) in harmonize.scala.
1 parent 28481bf commit 45b7f14

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,8 @@ trait Checking {
557557
case tp => tp.widenTermRefExpr match {
558558
case tp: ConstantType if isPureExpr(tree) => // ok
559559
case tp if defn.isFunctionType(tp) && isPureExpr(tree) => // ok
560-
case _ => ctx.error(em"$what must be a constant expression or a function", tree.pos)
560+
case _ =>
561+
if (!ctx.erasedTypes) ctx.error(em"$what must be a constant expression or a function", tree.pos)
561562
}
562563
}
563564

tests/pos/harmonize.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,40 @@ object Test {
33
def main(args: Array[String]) = {
44
val x = true
55
val n = 1
6+
inline val nn = 2
67
val y = if (x) 'A' else n
78
val z: Int = y
89

9-
val yy = n match {
10+
val yy1 = n match {
1011
case 1 => 'A'
1112
case 2 => n
1213
case 3 => 1.0
1314
}
14-
val zz: Double = yy
15+
val zz1: AnyVal = yy1 // no widening
16+
17+
val yy2 = n match {
18+
case 1 => 'A'
19+
case 2 => nn
20+
case 3 => 1.0f
21+
}
22+
val zz2: Float = yy2 // widening to Float
23+
24+
val yy3 = n match {
25+
case 1 => 'A'
26+
case 2 => 3L
27+
case 3 => 1.0f
28+
}
29+
val zz3: Double = yy3 // widening to Double
1530

1631
val a = try {
1732
'A'
1833
} catch {
19-
case ex: Exception => n
34+
case ex: Exception => nn
2035
case ex: Error => 3L
2136
}
2237
val b: Long = a
2338

24-
val xs = List(1.0, n, 'c')
39+
val xs = List(1.0, nn, 'c')
2540
val ys: List[Double] = xs
2641
}
2742

0 commit comments

Comments
 (0)