Skip to content

Commit f52385a

Browse files
committed
Add -Yimports compiler flag
1 parent 45ac398 commit f52385a

File tree

8 files changed

+28
-2
lines changed

8 files changed

+28
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ private sealed trait YSettings:
282282
val Yscala2Unpickler: Setting[String] = StringSetting("-Yscala2-unpickler", "", "Control where we may get Scala 2 symbols from. This is either \"always\", \"never\", or a classpath.", "always")
283283

284284
val YnoImports: Setting[Boolean] = BooleanSetting("-Yno-imports", "Compile without importing scala.*, java.lang.*, or Predef.")
285+
val Yimports: Setting[List[String]] = MultiStringSetting("-Yimports", helpArg="", "Custom root imports. If set, none of scala.*, java.lang.*, or Predef.* will be imported unless explicitly included.")
285286
val YnoGenericSig: Setting[Boolean] = BooleanSetting("-Yno-generic-signatures", "Suppress generation of generic signatures for Java.")
286287
val YnoPredef: Setting[Boolean] = BooleanSetting("-Yno-predef", "Compile without importing Predef.")
287288
val Yskip: Setting[List[String]] = PhasesSetting("-Yskip", "Skip")

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,12 +1541,18 @@ class Definitions {
15411541
private val PredefImportFns: RootRef =
15421542
RootRef(() => ScalaPredefModule.termRef, isPredef=true)
15431543

1544+
@tu private lazy val YimportsImportFns: List[RootRef] = ctx.settings.Yimports.value.map { imp =>
1545+
RootRef(() => requiredPackageRef(imp), isPredef = false)
1546+
}
1547+
15441548
@tu private lazy val JavaRootImportFns: List[RootRef] =
1545-
if ctx.settings.YnoImports.value then Nil
1549+
if !ctx.settings.Yimports.isDefault then YimportsImportFns
1550+
else if ctx.settings.YnoImports.value then Nil
15461551
else JavaImportFns
15471552

15481553
@tu private lazy val ScalaRootImportFns: List[RootRef] =
1549-
if ctx.settings.YnoImports.value then Nil
1554+
if !ctx.settings.Yimports.isDefault then YimportsImportFns
1555+
else if ctx.settings.YnoImports.value then Nil
15501556
else if ctx.settings.YnoPredef.value then ScalaImportFns
15511557
else ScalaImportFns :+ PredefImportFns
15521558

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class CompilationTests {
6565
compileFile("tests/pos-custom-args/i10383.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
6666
compileFile("tests/pos-custom-args/i13044.scala", defaultOptions.and("-Xmax-inlines:33")),
6767
compileFile("tests/pos-custom-args/jdk-8-app.scala", defaultOptions.and("-release:8")),
68+
compileFile("tests/pos-custom-args/single-additional-import.scala", defaultOptions.and("-Yimports:scala,java.lang,scala.annotation")),
69+
compileFile("tests/pos-custom-args/multiple-additional-imports.scala", defaultOptions.and("-Yimports:scala,java.lang,scala.Predef,scala.annotation,scala.util.matching")),
6870
).checkCompile()
6971
}
7072

@@ -154,6 +156,8 @@ class CompilationTests {
154156
compileFile("tests/neg-custom-args/nopredef.scala", defaultOptions.and("-Yno-predef")),
155157
compileFile("tests/neg-custom-args/noimports.scala", defaultOptions.and("-Yno-imports")),
156158
compileFile("tests/neg-custom-args/noimports2.scala", defaultOptions.and("-Yno-imports")),
159+
compileFile("tests/neg-custom-args/noimports-additional.scala", defaultOptions.and("-Yimports:scala.annotation,scala.util.matching")),
160+
compileFile("tests/neg-custom-args/nopredef-additional.scala", defaultOptions.and("-Yimports:java.lang,scala.annotation,scala.util.matching")),
157161
compileFile("tests/neg-custom-args/i1650.scala", allowDeepSubtypes),
158162
compileFile("tests/neg-custom-args/i3882.scala", allowDeepSubtypes),
159163
compileFile("tests/neg-custom-args/i4372.scala", allowDeepSubtypes),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
class annotation extends Annotation
3+
val s: String = "str" // error
4+
val regex: Regex = new Regex("str")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class annotation extends Annotation
2+
val s: String = "str"
3+
val regex: Regex = s.r // error

tests/neg/missing-import.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class annotation extends Annotation // error
2+
val s: String = "str"
3+
val regex: Regex = s.r // error
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
class annotation extends Annotation
3+
val s: String = "str"
4+
val regex: Regex = s.r
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class annotation extends Annotation

0 commit comments

Comments
 (0)