Skip to content

Commit 9aacccd

Browse files
committed
Improve error message for inline val null and Unit
Fixes #12177
1 parent 6f1846a commit 9aacccd

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class InlineVals extends MiniPhase:
4242
if tp.derivesFrom(defn.StringClass) || defn.ScalaValueClasses().exists(tp.derivesFrom(_)) then
4343
val pos = if tpt.span.isZeroExtent then rhs.srcPos else tpt.srcPos
4444
report.error(em"inline value must have a literal constant type", pos)
45+
else if tp.derivesFrom(defn.NullClass) then
46+
report.error(em"`inline val` with `null` is no supported.\n\nTo inline a `null` consider using `inline def`", rhs)
4547
else
4648
report.error(em"inline value must contain a literal constant value.\n\nTo inline more complex types consider using `inline def`", rhs)
4749
}

tests/neg/i12177.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Error: tests/neg/i12177.scala:2:17 ----------------------------------------------------------------------------------
2+
2 | inline val v = null // error
3+
| ^^^^
4+
| `inline val` with `null` is no supported.
5+
|
6+
| To inline a `null` consider using `inline def`

tests/neg/i12177.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test1 {
2+
inline val v = null // error
3+
inline def d = null
4+
}

0 commit comments

Comments
 (0)