Skip to content

Commit b438c16

Browse files
committed
Exclude scala2macros from experimental checking
Also, some refactorings for simplicity.
1 parent d9d4c1c commit b438c16

File tree

6 files changed

+40
-34
lines changed

6 files changed

+40
-34
lines changed

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Driver {
7777
val ictx = rootCtx.fresh
7878
val summary = command.distill(args, ictx.settings)(ictx.settingsState)(using ictx)
7979
ictx.setSettings(summary.sstate)
80-
Feature.checkExperimentalFlags(using ictx)
80+
Feature.checkExperimentalSettings(using ictx)
8181
MacroClassLoader.init(ictx)
8282
Positioned.init(using ictx)
8383

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,6 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
277277
case _ => None
278278
case _ => None
279279

280-
def isLanguageImport(path: Tree): Boolean = languageImport(path).isDefined
281-
282-
def isExperimentalImport(path: Tree): Boolean = languageImport(path) match
283-
case Some(nme.experimental) => true
284-
case _ => false
285-
286280
/** The underlying pattern ignoring any bindings */
287281
def unbind(x: Tree): Tree = unsplice(x) match {
288282
case Bind(_, y) => unbind(y)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,10 @@ object Config {
214214
* reduces the number of allocated denotations by ~50%.
215215
*/
216216
inline val reuseSymDenotations = true
217+
218+
/** Are experimental imports are allowed? By default same as
219+
* `Properties.experimental`. Can be re-assigned, e.g. to allow tests
220+
* of experimental features.
221+
*/
222+
private[dotty] var allowExperimentalFeatures = Properties.experimental
217223
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import core._
66
import Contexts._, Symbols._, Names._, NameOps._, Phases._
77
import StdNames.nme
88
import Decorators.{_, given}
9-
import util.SrcPos
9+
import util.{SrcPos, NoSourcePosition}
1010
import SourceVersion._
1111
import reporting.Message
1212
import NameKinds.QualifiedName
1313

1414
object Feature:
1515

16-
private def experimental(str: String): TermName =
16+
def experimental(str: PreName): TermName =
1717
QualifiedName(nme.experimental, str.toTermName)
1818

19-
private def deprecated(str: String): TermName =
19+
private def deprecated(str: PreName): TermName =
2020
QualifiedName(nme.deprecated, str.toTermName)
2121

2222
private val namedTypeArguments = experimental("namedTypeArguments")
2323
private val genericNumberLiterals = experimental("genericNumberLiterals")
24-
private val scala2macros = experimental("macros")
24+
val scala2macros = experimental("macros")
2525

2626
val dependent = experimental("dependent")
2727
val erasedDefinitions = experimental("erasedDefinitions")
@@ -97,15 +97,17 @@ object Feature:
9797
else
9898
false
9999

100-
val experimentalWarningMessage = "Experimental features may only be used with nightly or snapshot version of compiler."
100+
def allowExperimentalFeatures(using Context) =
101+
Config.allowExperimentalFeatures
102+
103+
def checkExperimentalFeature(which: String, srcPos: SrcPos = NoSourcePosition)(using Context) =
104+
if !allowExperimentalFeatures then
105+
report.error(i"Experimental feature$which may only be used with nightly or snapshot version of compiler", srcPos)
101106

102107
/** Check that experimental compiler options are only set for snapshot or nightly compiler versions. */
103-
def checkExperimentalFlags(using Context): Unit =
104-
if !Properties.experimental then
105-
val features = ctx.settings.language.value.filter(_.startsWith("experimental."))
106-
if features.nonEmpty then
107-
report.error(
108-
i"""$experimentalWarningMessage
109-
|The experimental language features were enabled via -language:$features%,%""")
108+
def checkExperimentalSettings(using Context): Unit =
109+
ctx.settings.language.value
110+
.filter(str => str.startsWith("experimental.") && str != "experimental.scala2macros")
111+
.foreach(str => checkExperimentalFeature(s" $str"))
110112

111113
end Feature

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,20 +3078,24 @@ object Parsers {
30783078
/** Create an import node and handle source version imports */
30793079
def mkImport(outermost: Boolean = false): ImportConstr = (tree, selectors) =>
30803080
val imp = Import(tree, selectors)
3081-
if isLanguageImport(tree) then
3082-
in.languageImportContext = in.languageImportContext.importContext(imp, NoSymbol)
3083-
if isExperimentalImport(tree) && !Properties.experimental then
3084-
report.error(Feature.experimentalWarningMessage, imp.srcPos)
3085-
for
3086-
case ImportSelector(id @ Ident(imported), EmptyTree, _) <- selectors
3087-
if allSourceVersionNames.contains(imported)
3088-
do
3089-
if !outermost then
3090-
syntaxError(i"source version import is only allowed at the toplevel", id.span)
3091-
else if ctx.compilationUnit.sourceVersion.isDefined then
3092-
syntaxError(i"duplicate source version import", id.span)
3093-
else
3094-
ctx.compilationUnit.sourceVersion = Some(SourceVersion.valueOf(imported.toString))
3081+
languageImport(tree) match
3082+
case Some(prefix) =>
3083+
in.languageImportContext = in.languageImportContext.importContext(imp, NoSymbol)
3084+
if prefix == nme.experimental
3085+
&& selectors.exists(sel => Feature.experimental(sel.name) != Feature.scala2macros)
3086+
then
3087+
Feature.checkExperimentalFeature("s", imp.srcPos)
3088+
for
3089+
case ImportSelector(id @ Ident(imported), EmptyTree, _) <- selectors
3090+
if allSourceVersionNames.contains(imported)
3091+
do
3092+
if !outermost then
3093+
syntaxError(i"source version import is only allowed at the toplevel", id.span)
3094+
else if ctx.compilationUnit.sourceVersion.isDefined then
3095+
syntaxError(i"duplicate source version import", id.span)
3096+
else
3097+
ctx.compilationUnit.sourceVersion = Some(SourceVersion.valueOf(imported.toString))
3098+
case None =>
30953099
imp
30963100

30973101
/** ImportExpr ::= SimpleRef {‘.’ id} ‘.’ ImportSpec

compiler/src/dotty/tools/dotc/typer/ImportInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class ImportInfo(symf: Context ?=> Symbol,
181181
assert(myUnimported != null)
182182
myUnimported
183183

184-
private val isLanguageImport: Boolean = untpd.isLanguageImport(qualifier)
184+
private val isLanguageImport: Boolean = untpd.languageImport(qualifier).isDefined
185185

186186
private var myUnimported: Symbol = _
187187

0 commit comments

Comments
 (0)