Skip to content

Commit

Permalink
Add -Yadditional-imports compiler flag
Browse files Browse the repository at this point in the history
  • Loading branch information
adampauls committed Oct 23, 2022
1 parent d9301e0 commit e918e6c
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ private sealed trait YSettings:
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")

val YnoImports: Setting[Boolean] = BooleanSetting("-Yno-imports", "Compile without importing scala.*, java.lang.*, or Predef.")
val YadditionalImports: Setting[List[String]] = MultiStringSetting("-Yadditional-imports", helpArg="", "Custom root imports, appended to any existing default imports")
val YnoGenericSig: Setting[Boolean] = BooleanSetting("-Yno-generic-signatures", "Suppress generation of generic signatures for Java.")
val YnoPredef: Setting[Boolean] = BooleanSetting("-Yno-predef", "Compile without importing Predef.")
val Yskip: Setting[List[String]] = PhasesSetting("-Yskip", "Skip")
Expand Down
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1536,14 +1536,18 @@ class Definitions {
private val PredefImportFns: RootRef =
RootRef(() => ScalaPredefModule.termRef, isPredef=true)

@tu private lazy val YadditionalImportsImportFns: List[RootRef] = ctx.settings.YadditionalImports.value.map { imp =>
RootRef(() => requiredPackageRef(imp), isPredef = false)
}

@tu private lazy val JavaRootImportFns: List[RootRef] =
if ctx.settings.YnoImports.value then Nil
else JavaImportFns

@tu private lazy val ScalaRootImportFns: List[RootRef] =
if ctx.settings.YnoImports.value then Nil
(if ctx.settings.YnoImports.value then Nil
else if ctx.settings.YnoPredef.value then ScalaImportFns
else ScalaImportFns :+ PredefImportFns
else ScalaImportFns :+ PredefImportFns) ++ YadditionalImportsImportFns

@tu private lazy val JavaRootImportTypes: List[TermRef] = JavaRootImportFns.map(_.refFn())
@tu private lazy val ScalaRootImportTypes: List[TermRef] = ScalaRootImportFns.map(_.refFn())
Expand Down
4 changes: 4 additions & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class CompilationTests {
compileFile("tests/pos-custom-args/i10383.scala", defaultOptions.and("-source", "future", "-deprecation", "-Xfatal-warnings")),
compileFile("tests/pos-custom-args/i13044.scala", defaultOptions.and("-Xmax-inlines:33")),
compileFile("tests/pos-custom-args/jdk-8-app.scala", defaultOptions.and("-release:8")),
compileFile("tests/pos-custom-args/single-additional-import.scala", defaultOptions.and("-Yadditional-imports:scala.annotation")),
compileFile("tests/pos-custom-args/multiple-additional-imports.scala", defaultOptions.and("-Yadditional-imports:scala.annotation,scala.util.matching")),
).checkCompile()
}

Expand Down Expand Up @@ -154,6 +156,8 @@ class CompilationTests {
compileFile("tests/neg-custom-args/nopredef.scala", defaultOptions.and("-Yno-predef")),
compileFile("tests/neg-custom-args/noimports.scala", defaultOptions.and("-Yno-imports")),
compileFile("tests/neg-custom-args/noimports2.scala", defaultOptions.and("-Yno-imports")),
compileFile("tests/neg-custom-args/noimports-additional.scala", defaultOptions.and("-Yno-imports", "-Yadditional-imports:scala.annotation,scala.util.matching")),
compileFile("tests/neg-custom-args/nopredef-additional.scala", defaultOptions.and("-Yno-predef", "-Yadditional-imports:scala.annotation,scala.util.matching")),
compileFile("tests/neg-custom-args/i1650.scala", allowDeepSubtypes),
compileFile("tests/neg-custom-args/i3882.scala", allowDeepSubtypes),
compileFile("tests/neg-custom-args/i4372.scala", allowDeepSubtypes),
Expand Down
4 changes: 4 additions & 0 deletions tests/neg-custom-args/noimports-additional.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

class annotation extends Annotation
val s: String = "str" // error
val regex: Regex = new Regex("str")
3 changes: 3 additions & 0 deletions tests/neg-custom-args/nopredef-additional.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class annotation extends Annotation
val s: String = "str"
val regex: Regex = s.r // error
3 changes: 3 additions & 0 deletions tests/neg/missing-import.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class annotation extends Annotation // error
val s: String = "str"
val regex: Regex = s.r // error
4 changes: 4 additions & 0 deletions tests/pos-custom-args/multiple-additional-imports.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

class annotation extends Annotation
val s: String = "str"
val regex: Regex = s.r
1 change: 1 addition & 0 deletions tests/pos-custom-args/single-additional-import.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class annotation extends Annotation

0 comments on commit e918e6c

Please sign in to comment.