Skip to content

Commit 85dc1cb

Browse files
committed
Improve error message for inline vals with non-constants
Closes scala#11854
1 parent 75f7cb3 commit 85dc1cb

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

compiler/src/dotty/tools/dotc/transform/InlineVals.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package transform
44

55
import dotty.tools.dotc.core.Contexts._
66
import dotty.tools.dotc.core.Decorators._
7+
import dotty.tools.dotc.core.Symbols._
78
import dotty.tools.dotc.core.Flags._
89
import dotty.tools.dotc.core.Types._
910
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
@@ -37,7 +38,10 @@ class InlineVals extends MiniPhase:
3738
if !isPureExpr(rhs) then
3839
val details = if enclosingInlineds.isEmpty then "" else em"but was: $rhs"
3940
report.error(s"inline value must be pure$details", rhs.srcPos)
40-
case _ =>
41-
val pos = if tpt.span.isZeroExtent then rhs.srcPos else tpt.srcPos
42-
report.error(em"inline value must have a literal constant type", pos)
41+
case tp =>
42+
if tp.derivesFrom(defn.StringClass) || defn.ScalaValueClasses().exists(tp.derivesFrom(_)) then
43+
val pos = if tpt.span.isZeroExtent then rhs.srcPos else tpt.srcPos
44+
report.error(em"inline value must have a literal constant type", pos)
45+
else
46+
report.error(em"inline value must contain a literal constant value.\n\nTo inline more complex types consider using `inline def`", rhs)
4347
}

tests/neg/i11854.check

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Error: tests/neg/i11854.scala:4:14 ----------------------------------------------------------------------------------
2+
4 |inline val j: Int = 2 // error
3+
| ^^^
4+
| inline value must have a literal constant type
5+
-- Error: tests/neg/i11854.scala:5:14 ----------------------------------------------------------------------------------
6+
5 |inline val b: Boolean = true // error
7+
| ^^^^^^^
8+
| inline value must have a literal constant type
9+
-- Error: tests/neg/i11854.scala:6:14 ----------------------------------------------------------------------------------
10+
6 |inline val s: String = "" // error
11+
| ^^^^^^
12+
| inline value must have a literal constant type
13+
-- Error: tests/neg/i11854.scala:7:18 ----------------------------------------------------------------------------------
14+
7 |inline val bagA = new Bag(Seq('a', 'b', 'c')) // error
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| inline value must contain a literal constant value.
17+
|
18+
| To inline more complex types consider using `inline def`
19+
-- Error: tests/neg/i11854.scala:8:23 ----------------------------------------------------------------------------------
20+
8 |inline val bagB: Bag = new Bag(Seq('a', 'b', 'c')) // error
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
| inline value must contain a literal constant value.
23+
|
24+
| To inline more complex types consider using `inline def`

tests/neg/i11854.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Bag(seq: Seq[Char])
2+
3+
inline val i = 2
4+
inline val j: Int = 2 // error
5+
inline val b: Boolean = true // error
6+
inline val s: String = "" // error
7+
inline val bagA = new Bag(Seq('a', 'b', 'c')) // error
8+
inline val bagB: Bag = new Bag(Seq('a', 'b', 'c')) // error

0 commit comments

Comments
 (0)