Skip to content

Commit 3ad054c

Browse files
committed
Improve error message when using experimental definitions
When an experimental definition is used, the error message shows how to enable the experimental mode. Previously we only did this for experimental language features, but not for experimental definitions.
1 parent 98efdab commit 3ad054c

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

+10-11
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,16 @@ object Feature:
144144
ccEnabled && defn.ccExperimental.contains(sym)
145145

146146
def checkExperimentalDef(sym: Symbol, srcPos: SrcPos)(using Context) =
147-
if !isExperimentalEnabled then
148-
val experimentalSym =
149-
if sym.hasAnnotation(defn.ExperimentalAnnot) then sym
150-
else if sym.owner.hasAnnotation(defn.ExperimentalAnnot) then sym.owner
151-
else NoSymbol
152-
if !ccException(experimentalSym) then
153-
val symMsg =
154-
if experimentalSym.exists
155-
then i"$experimentalSym is marked @experimental"
156-
else i"$sym inherits @experimental"
157-
report.error(em"$symMsg and therefore may only be used in an experimental scope.", srcPos)
147+
val experimentalSym =
148+
if sym.hasAnnotation(defn.ExperimentalAnnot) then sym
149+
else if sym.owner.hasAnnotation(defn.ExperimentalAnnot) then sym.owner
150+
else NoSymbol
151+
if !ccException(experimentalSym) then
152+
val note =
153+
if experimentalSym.exists
154+
then i"$experimentalSym is marked @experimental"
155+
else i"$sym inherits @experimental"
156+
checkExperimentalFeature("definition", srcPos, s"\n\n$note")
158157

159158
/** Check that experimental compiler options are only set for snapshot or nightly compiler versions. */
160159
def checkExperimentalSettings(using Context): Unit =

tests/neg/use-experimental-def.check

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Error: tests/neg/use-experimental-def.scala:7:15 --------------------------------------------------------------------
2+
7 |def bar: Int = foo // error
3+
| ^^^
4+
| Experimental definition may only be used under experimental mode:
5+
| 1. In a definition marked as @experimental
6+
| 2. Compiling with the -experimental compiler flag
7+
| 3. With a nightly or snapshot version of the compiler
8+
|
9+
| method foo is marked @experimental
10+
|

tests/neg/use-experimental-def.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//> using options -Yno-experimental
2+
3+
import scala.annotation.experimental
4+
5+
@experimental def foo: Int = 1
6+
7+
def bar: Int = foo // error

0 commit comments

Comments
 (0)