Skip to content

Commit 35a4a2b

Browse files
Improve error message when using experimental definitions (#19782)
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.
2 parents 2c2e8af + 89d6104 commit 35a4a2b

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

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

+13-14
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,25 @@ object Feature:
135135
if !isExperimentalEnabled then
136136
report.error(
137137
em"""Experimental $which may only be used under experimental mode:
138-
| 1. In a definition marked as @experimental
139-
| 2. Compiling with the -experimental compiler flag
140-
| 3. With a nightly or snapshot version of the compiler$note
138+
| 1. in a definition marked as @experimental, or
139+
| 2. compiling with the -experimental compiler flag, or
140+
| 3. with a nightly or snapshot version of the compiler.$note
141141
""", srcPos)
142142

143143
private def ccException(sym: Symbol)(using Context): Boolean =
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, or
6+
| 2. compiling with the -experimental compiler flag, or
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)