Skip to content

Commit fa525fa

Browse files
committed
Add -Yadditional-imports compiler flag
1 parent 66c36e2 commit fa525fa

File tree

6 files changed

+17
-2
lines changed

6 files changed

+17
-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 YadditionalImports: Setting[List[String]] = MultiStringSetting("-Yadditional-imports", helpArg="", "Custom root imports, appended to any existing default imports")
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,14 +1536,18 @@ class Definitions {
15361536
private val PredefImportFns: RootRef =
15371537
RootRef(() => ScalaPredefModule.termRef, isPredef=true)
15381538

1539+
@tu private lazy val YadditionalImportsImportFns: List[RootRef] = ctx.settings.YadditionalImports.value.map { imp =>
1540+
RootRef(() => requiredPackageRef(imp), isPredef = false)
1541+
}
1542+
15391543
@tu private lazy val JavaRootImportFns: List[RootRef] =
15401544
if ctx.settings.YnoImports.value then Nil
15411545
else JavaImportFns
15421546

15431547
@tu private lazy val ScalaRootImportFns: List[RootRef] =
1544-
if ctx.settings.YnoImports.value then Nil
1548+
(if ctx.settings.YnoImports.value then Nil
15451549
else if ctx.settings.YnoPredef.value then ScalaImportFns
1546-
else ScalaImportFns :+ PredefImportFns
1550+
else ScalaImportFns :+ PredefImportFns) ++ YadditionalImportsImportFns
15471551

15481552
@tu private lazy val JavaRootImportTypes: List[TermRef] = JavaRootImportFns.map(_.refFn())
15491553
@tu private lazy val ScalaRootImportTypes: List[TermRef] = ScalaRootImportFns.map(_.refFn())

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

Lines changed: 2 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("-Yadditional-imports:scala.annotation")),
69+
compileFile("tests/pos-custom-args/multiple-additional-imports.scala", defaultOptions.and("-Yadditional-imports:scala.annotation,scala.util.matching")),
6870
).checkCompile()
6971
}
7072

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)