From e918e6cb7e04af5b19e6b0d9494a25e35d11158f Mon Sep 17 00:00:00 2001 From: adampauls Date: Wed, 19 Oct 2022 18:56:25 -0700 Subject: [PATCH] Add -Yadditional-imports compiler flag --- compiler/src/dotty/tools/dotc/config/ScalaSettings.scala | 1 + compiler/src/dotty/tools/dotc/core/Definitions.scala | 8 ++++++-- compiler/test/dotty/tools/dotc/CompilationTests.scala | 4 ++++ tests/neg-custom-args/noimports-additional.scala | 4 ++++ tests/neg-custom-args/nopredef-additional.scala | 3 +++ tests/neg/missing-import.scala | 3 +++ tests/pos-custom-args/multiple-additional-imports.scala | 4 ++++ tests/pos-custom-args/single-additional-import.scala | 1 + 8 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/neg-custom-args/noimports-additional.scala create mode 100644 tests/neg-custom-args/nopredef-additional.scala create mode 100644 tests/neg/missing-import.scala create mode 100644 tests/pos-custom-args/multiple-additional-imports.scala create mode 100644 tests/pos-custom-args/single-additional-import.scala diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index 81f1d4e2923c..2a0c9571bb9e 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -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") diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index afbcfe556c58..ae6d151bc29a 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -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()) diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 8d7a16dad8a4..097f7fbd0bed 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -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() } @@ -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), diff --git a/tests/neg-custom-args/noimports-additional.scala b/tests/neg-custom-args/noimports-additional.scala new file mode 100644 index 000000000000..5d14c5731615 --- /dev/null +++ b/tests/neg-custom-args/noimports-additional.scala @@ -0,0 +1,4 @@ + +class annotation extends Annotation +val s: String = "str" // error +val regex: Regex = new Regex("str") \ No newline at end of file diff --git a/tests/neg-custom-args/nopredef-additional.scala b/tests/neg-custom-args/nopredef-additional.scala new file mode 100644 index 000000000000..9f4531b17ec3 --- /dev/null +++ b/tests/neg-custom-args/nopredef-additional.scala @@ -0,0 +1,3 @@ +class annotation extends Annotation +val s: String = "str" +val regex: Regex = s.r // error \ No newline at end of file diff --git a/tests/neg/missing-import.scala b/tests/neg/missing-import.scala new file mode 100644 index 000000000000..486dd3bfe5a5 --- /dev/null +++ b/tests/neg/missing-import.scala @@ -0,0 +1,3 @@ +class annotation extends Annotation // error +val s: String = "str" +val regex: Regex = s.r // error \ No newline at end of file diff --git a/tests/pos-custom-args/multiple-additional-imports.scala b/tests/pos-custom-args/multiple-additional-imports.scala new file mode 100644 index 000000000000..474f3b9a1661 --- /dev/null +++ b/tests/pos-custom-args/multiple-additional-imports.scala @@ -0,0 +1,4 @@ + +class annotation extends Annotation +val s: String = "str" +val regex: Regex = s.r \ No newline at end of file diff --git a/tests/pos-custom-args/single-additional-import.scala b/tests/pos-custom-args/single-additional-import.scala new file mode 100644 index 000000000000..ef0780dc2302 --- /dev/null +++ b/tests/pos-custom-args/single-additional-import.scala @@ -0,0 +1 @@ +class annotation extends Annotation \ No newline at end of file