Skip to content

Commit b5f68b1

Browse files
adampaulsdwijnand
authored andcommitted
Add -Yimports compiler flag
1 parent bfa800e commit b5f68b1

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
@@ -281,6 +281,7 @@ private sealed trait YSettings:
281281
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")
282282

283283
val YnoImports: Setting[Boolean] = BooleanSetting("-Yno-imports", "Compile without importing scala.*, java.lang.*, or Predef.")
284+
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.")
284285
val YnoGenericSig: Setting[Boolean] = BooleanSetting("-Yno-generic-signatures", "Suppress generation of generic signatures for Java.")
285286
val YnoPredef: Setting[Boolean] = BooleanSetting("-Yno-predef", "Compile without importing Predef.")
286287
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
@@ -1558,12 +1558,18 @@ class Definitions {
15581558
private val PredefImportFns: RootRef =
15591559
RootRef(() => ScalaPredefModule.termRef, isPredef=true)
15601560

1561+
@tu private lazy val YimportsImportFns: List[RootRef] = ctx.settings.Yimports.value.map { imp =>
1562+
RootRef(() => requiredPackageRef(imp), isPredef = false)
1563+
}
1564+
15611565
@tu private lazy val JavaRootImportFns: List[RootRef] =
1562-
if ctx.settings.YnoImports.value then Nil
1566+
if !ctx.settings.Yimports.isDefault then YimportsImportFns
1567+
else if ctx.settings.YnoImports.value then Nil
15631568
else JavaImportFns
15641569

15651570
@tu private lazy val ScalaRootImportFns: List[RootRef] =
1566-
if ctx.settings.YnoImports.value then Nil
1571+
if !ctx.settings.Yimports.isDefault then YimportsImportFns
1572+
else if ctx.settings.YnoImports.value then Nil
15671573
else if ctx.settings.YnoPredef.value then ScalaImportFns
15681574
else ScalaImportFns :+ PredefImportFns
15691575

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class CompilationTests {
6868
compileFile("tests/pos-custom-args/i10383.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
6969
compileFile("tests/pos-custom-args/i13044.scala", defaultOptions.and("-Xmax-inlines:33")),
7070
compileFile("tests/pos-custom-args/jdk-8-app.scala", defaultOptions.and("-release:8")),
71+
compileFile("tests/pos-custom-args/single-additional-import.scala", defaultOptions.and("-Yimports:scala,java.lang,scala.annotation")),
72+
compileFile("tests/pos-custom-args/multiple-additional-imports.scala", defaultOptions.and("-Yimports:scala,java.lang,scala.Predef,scala.annotation,scala.util.matching")),
7173
).checkCompile()
7274
}
7375

@@ -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)