From abf99060b6c5e985802174c4bcc43cdb7daadfe7 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 22 Apr 2021 15:06:21 +0200 Subject: [PATCH] Improve error message for inline val null and Unit Fixes #12177 --- .../src/dotty/tools/dotc/ast/TreeInfo.scala | 2 +- .../tools/dotc/transform/InlineVals.scala | 6 ++++- tests/neg/i12177.check | 24 +++++++++++++++++++ tests/neg/i12177.scala | 8 +++++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i12177.check create mode 100644 tests/neg/i12177.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index 73f1585427ac..7d04f23d9e8d 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -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. */ diff --git a/compiler/src/dotty/tools/dotc/transform/InlineVals.scala b/compiler/src/dotty/tools/dotc/transform/InlineVals.scala index 9c3dc295d207..0e3f4139c27b 100644 --- a/compiler/src/dotty/tools/dotc/transform/InlineVals.scala +++ b/compiler/src/dotty/tools/dotc/transform/InlineVals.scala @@ -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) } \ No newline at end of file diff --git a/tests/neg/i12177.check b/tests/neg/i12177.check new file mode 100644 index 000000000000..2918dbfe0533 --- /dev/null +++ b/tests/neg/i12177.check @@ -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` diff --git a/tests/neg/i12177.scala b/tests/neg/i12177.scala new file mode 100644 index 000000000000..cf8d0a7ffe56 --- /dev/null +++ b/tests/neg/i12177.scala @@ -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 +}