Skip to content

Commit 6438a74

Browse files
oderskynicolasstucki
authored andcommitted
Allow experimental language imports in experimental scopes
1 parent 9696219 commit 6438a74

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,10 +3114,6 @@ object Parsers {
31143114
languageImport(tree) match
31153115
case Some(prefix) =>
31163116
in.languageImportContext = in.languageImportContext.importContext(imp, NoSymbol)
3117-
if prefix == nme.experimental
3118-
&& selectors.exists(sel => Feature.experimental(sel.name) != Feature.scala2macros)
3119-
then
3120-
Feature.checkExperimentalFeature("features", imp.srcPos)
31213117
for
31223118
case ImportSelector(id @ Ident(imported), EmptyTree, _) <- selectors
31233119
if allSourceVersionNames.contains(imported)

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
448448
throw ex
449449
}
450450

451+
override def transformStats(trees: List[Tree], exprOwner: Symbol)(using Context): List[Tree] =
452+
try super.transformStats(trees, exprOwner)
453+
finally Checking.checkExperimentalImports(trees)
454+
451455
/** Transforms the rhs tree into a its default tree if it is in an `erased` val/def.
452456
* Performed to shrink the tree that is known to be erased later.
453457
*/

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,19 @@ object Checking {
719719
checkValue(tree)
720720
case _ =>
721721
tree
722+
723+
/** Check that experimental language imports in `trees`
724+
* are done only in experimental scopes.
725+
*/
726+
def checkExperimentalImports(trees: List[Tree])(using Context): Unit =
727+
for case imp @ Import(qual, selectors) <- trees do
728+
languageImport(qual) match
729+
case Some(nme.experimental) =>
730+
if !ctx.owner.isInExperimentalScope
731+
&& selectors.exists(sel => experimental(sel.name) != scala2macros)
732+
then checkExperimentalFeature("features", imp.srcPos)
733+
case _ =>
734+
end checkExperimentalImports
722735
}
723736

724737
trait Checking {

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class CompilationTests {
178178
compileFile("tests/neg-custom-args/matchable.scala", defaultOptions.and("-Xfatal-warnings", "-source", "future")),
179179
compileFile("tests/neg-custom-args/i7314.scala", defaultOptions.and("-Xfatal-warnings", "-source", "future")),
180180
compileFile("tests/neg-custom-args/feature-shadowing.scala", defaultOptions.and("-Xfatal-warnings", "-feature")),
181-
compileDir("tests/neg-custom-args/hidden-type-errors", defaultOptions.and("-explain")),
181+
compileDir("tests/neg-custom-args/hidden-type-errors", defaultOptions.and("-explain")),
182182
).checkExpectedErrors()
183183
}
184184

tests/neg-custom-args/no-experimental/experimental.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,4 @@ class Test2 {
2525

2626
class Test6 {
2727
import scala.language.experimental // ok
28-
}
29-
30-
class Test7 {
31-
import scala.language.experimental
32-
import experimental.genericNumberLiterals // error: no aliases can be used to refer to a language import
33-
val x: BigInt = 13232202002020202020202 // error
3428
}

0 commit comments

Comments
 (0)