Skip to content

Commit e2ea418

Browse files
authored
Merge pull request #12192 from dotty-staging/fix-#12177
Improve error message for inline val null and Unit
2 parents 08dda0d + abf9906 commit e2ea418

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
958958

959959
/** Extractors for splices */
960960
object Spliced {
961-
/** Extracts the content of a spliced expresion tree.
961+
/** Extracts the content of a spliced expression tree.
962962
* The result can be the contents of a term splice, which
963963
* will return a term tree.
964964
*/

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ class InlineVals extends MiniPhase:
3939
val details = if enclosingInlineds.isEmpty then "" else em"but was: $rhs"
4040
report.error(s"inline value must be pure$details", rhs.srcPos)
4141
case tp =>
42-
if tp.derivesFrom(defn.StringClass) || defn.ScalaValueClasses().exists(tp.derivesFrom(_)) then
42+
if tp.derivesFrom(defn.UnitClass) then
43+
report.error(em"`inline val` of type `Unit` is not supported.\n\nTo inline a `Unit` consider using `inline def`", rhs)
44+
else if tp.derivesFrom(defn.StringClass) || defn.ScalaValueClasses().exists(tp.derivesFrom(_)) then
4345
val pos = if tpt.span.isZeroExtent then rhs.srcPos else tpt.srcPos
4446
report.error(em"inline value must have a literal constant type", pos)
47+
else if tp.derivesFrom(defn.NullClass) then
48+
report.error(em"`inline val` with `null` is not supported.\n\nTo inline a `null` consider using `inline def`", rhs)
4549
else
4650
report.error(em"inline value must contain a literal constant value.\n\nTo inline more complex types consider using `inline def`", rhs)
4751
}

tests/neg/i12177.check

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Error: tests/neg/i12177.scala:2:17 ----------------------------------------------------------------------------------
2+
2 | inline val v = null // error
3+
| ^^^^
4+
| `inline val` with `null` is not supported.
5+
|
6+
| To inline a `null` consider using `inline def`
7+
-- Error: tests/neg/i12177.scala:4:17 ----------------------------------------------------------------------------------
8+
4 | inline val u = () // error
9+
| ^^
10+
| `inline val` of type `Unit` is not supported.
11+
|
12+
| To inline a `Unit` consider using `inline def`
13+
-- Error: tests/neg/i12177.scala:6:18 ----------------------------------------------------------------------------------
14+
6 | inline val u2 = { println(); () } // error
15+
| ^^^^^^^^^^^^^^^^^
16+
| `inline val` of type `Unit` is not supported.
17+
|
18+
| To inline a `Unit` consider using `inline def`
19+
-- Error: tests/neg/i12177.scala:7:18 ----------------------------------------------------------------------------------
20+
7 | inline val u3 = { } // error
21+
| ^^^
22+
| `inline val` of type `Unit` is not supported.
23+
|
24+
| To inline a `Unit` consider using `inline def`

tests/neg/i12177.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test1 {
2+
inline val v = null // error
3+
inline def d = null
4+
inline val u = () // error
5+
inline def e = ()
6+
inline val u2 = { println(); () } // error
7+
inline val u3 = { } // error
8+
}

0 commit comments

Comments
 (0)