Skip to content

Commit cd5b1d8

Browse files
committed
Fix scala#8841: Improve inline val error message
1 parent d7c0574 commit cd5b1d8

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,12 @@ trait Checking {
888888
// final vals can be marked inline even if they're not pure, see Typer#patchFinalVals
889889
val purityLevel = if (sym.is(Final)) Idempotent else Pure
890890
tpt.tpe.widenTermRefExpr match
891-
case tp: ConstantType if exprPurity(tree) >= purityLevel => // ok
891+
case tp: ConstantType =>
892+
if !(exprPurity(tree) >= purityLevel) then
893+
ctx.error(em"inline value must be pure", tree.sourcePos)
892894
case _ =>
893-
ctx.error(em"type of inline must be a known value", tree.sourcePos)
895+
val pos = if tpt.span.isZeroExtent then tree.sourcePos else tpt.sourcePos
896+
ctx.error(em"inline value must have a literal constant type", pos)
894897
}
895898

896899
/** A hook to exclude selected symbols from double declaration check */

tests/neg/i8841.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Error: tests/neg/i8841.scala:2:20 -----------------------------------------------------------------------------------
2+
2 | inline val log1 : Boolean = false // error
3+
| ^^^^^^^
4+
| inline value must have a literal constant type
5+
-- Error: tests/neg/i8841.scala:3:20 -----------------------------------------------------------------------------------
6+
3 | inline val log2 = true: Boolean // error
7+
| ^^^^^^^^^^^^^
8+
| inline value must have a literal constant type
9+
-- Error: tests/neg/i8841.scala:4:28 -----------------------------------------------------------------------------------
10+
4 | inline val log3: false = { println(); false } // error
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
| inline value must be pure

tests/neg/i8841.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Foo {
2+
inline val log1 : Boolean = false // error
3+
inline val log2 = true: Boolean // error
4+
inline val log3: false = { println(); false } // error
5+
}

0 commit comments

Comments
 (0)