Skip to content

Improve error message for inline val null and Unit #12192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>

/** Extractors for splices */
object Spliced {
/** Extracts the content of a spliced expresion tree.
/** Extracts the content of a spliced expression tree.
* The result can be the contents of a term splice, which
* will return a term tree.
*/
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/InlineVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ class InlineVals extends MiniPhase:
val details = if enclosingInlineds.isEmpty then "" else em"but was: $rhs"
report.error(s"inline value must be pure$details", rhs.srcPos)
case tp =>
if tp.derivesFrom(defn.StringClass) || defn.ScalaValueClasses().exists(tp.derivesFrom(_)) then
if tp.derivesFrom(defn.UnitClass) then
report.error(em"`inline val` of type `Unit` is not supported.\n\nTo inline a `Unit` consider using `inline def`", rhs)
else if tp.derivesFrom(defn.StringClass) || defn.ScalaValueClasses().exists(tp.derivesFrom(_)) then
val pos = if tpt.span.isZeroExtent then rhs.srcPos else tpt.srcPos
report.error(em"inline value must have a literal constant type", pos)
else if tp.derivesFrom(defn.NullClass) then
report.error(em"`inline val` with `null` is not supported.\n\nTo inline a `null` consider using `inline def`", rhs)
else
report.error(em"inline value must contain a literal constant value.\n\nTo inline more complex types consider using `inline def`", rhs)
}
24 changes: 24 additions & 0 deletions tests/neg/i12177.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Error: tests/neg/i12177.scala:2:17 ----------------------------------------------------------------------------------
2 | inline val v = null // error
| ^^^^
| `inline val` with `null` is not supported.
|
| To inline a `null` consider using `inline def`
-- Error: tests/neg/i12177.scala:4:17 ----------------------------------------------------------------------------------
4 | inline val u = () // error
| ^^
| `inline val` of type `Unit` is not supported.
|
| To inline a `Unit` consider using `inline def`
-- Error: tests/neg/i12177.scala:6:18 ----------------------------------------------------------------------------------
6 | inline val u2 = { println(); () } // error
| ^^^^^^^^^^^^^^^^^
| `inline val` of type `Unit` is not supported.
|
| To inline a `Unit` consider using `inline def`
-- Error: tests/neg/i12177.scala:7:18 ----------------------------------------------------------------------------------
7 | inline val u3 = { } // error
| ^^^
| `inline val` of type `Unit` is not supported.
|
| To inline a `Unit` consider using `inline def`
8 changes: 8 additions & 0 deletions tests/neg/i12177.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test1 {
inline val v = null // error
inline def d = null
inline val u = () // error
inline def e = ()
inline val u2 = { println(); () } // error
inline val u3 = { } // error
}