Skip to content

Commit 18a1c20

Browse files
authored
Merge pull request #1334 from dotty-jvican/implement-no-predef-no-import
Implement -Yno-predef and -Yno-imports
2 parents 409c6c3 + 6684706 commit 18a1c20

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class ScalaSettings extends Settings.SettingGroup {
127127
val Ylogcp = BooleanSetting("-Ylog-classpath", "Output information about what classpath is being applied.")
128128
val Ynogenericsig = BooleanSetting("-Yno-generic-signatures", "Suppress generation of generic signatures for Java.")
129129
val YnoImports = BooleanSetting("-Yno-imports", "Compile without importing scala.*, java.lang.*, or Predef.")
130-
val nopredef = BooleanSetting("-Yno-predef", "Compile without importing Predef.")
130+
val YnoPredef = BooleanSetting("-Yno-predef", "Compile without importing Predef.")
131131
val noAdaptedArgs = BooleanSetting("-Yno-adapted-args", "Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver.")
132132
val selfInAnnots = BooleanSetting("-Yself-in-annots", "Include a \"self\" identifier inside of annotations.")
133133
val Yshowtrees = BooleanSetting("-Yshow-trees", "(Requires -Xprint:) Print detailed ASTs in formatted form.")

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,15 +627,24 @@ class Definitions {
627627
def isTupleClass(cls: Symbol) = isVarArityClass(cls, tpnme.Tuple)
628628
def isProductClass(cls: Symbol) = isVarArityClass(cls, tpnme.Product)
629629

630-
val RootImportFns = List[() => TermRef](
631-
() => JavaLangPackageVal.termRef,
632-
() => ScalaPackageVal.termRef,
630+
val StaticRootImportFns = List[() => TermRef](
631+
() => JavaLangPackageVal.termRef,
632+
() => ScalaPackageVal.termRef
633+
)
634+
635+
val PredefImportFns = List[() => TermRef](
633636
() => ScalaPredefModuleRef,
634-
() => DottyPredefModuleRef)
637+
() => DottyPredefModuleRef
638+
)
639+
640+
lazy val RootImportFns =
641+
if (ctx.settings.YnoImports.value) List.empty[() => TermRef]
642+
else if (ctx.settings.YnoPredef.value) StaticRootImportFns
643+
else StaticRootImportFns ++ PredefImportFns
635644

636645
lazy val RootImportTypes = RootImportFns.map(_())
637646

638-
/** `Modules whose members are in the default namespace and their module classes */
647+
/** Modules whose members are in the default namespace and their module classes */
639648
lazy val UnqualifiedOwnerTypes: Set[NamedType] =
640649
RootImportTypes.toSet[NamedType] ++ RootImportTypes.map(_.symbol.moduleClass.typeRef)
641650

test/dotc/tests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ class tests extends CompilerTest {
135135
@Test def neg_tailcall2 = compileFile(negTailcallDir, "tailrec-2")
136136
@Test def neg_tailcall3 = compileFile(negTailcallDir, "tailrec-3")
137137

138+
@Test def neg_nopredef = compileFile(negCustomArgs, "nopredef", List("-Yno-predef"))
139+
@Test def neg_noimports = compileFile(negCustomArgs, "noimports", List("-Yno-imports"))
140+
@Test def neg_noimpots2 = compileFile(negCustomArgs, "noimports2", List("-Yno-imports"))
141+
138142
@Test def run_all = runFiles(runDir)
139143

140144
val stdlibFiles = Source.fromFile("./test/dotc/scala-collections.whitelist", "UTF8").getLines()

tests/neg/customArgs/noimports.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
val t: Int = 1 // error: not found Int
3+
}

tests/neg/customArgs/noimports2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
assert("asdf" == "asdf") // error: not found assert
3+
}

tests/neg/customArgs/nopredef.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
assert("asdf" == "asdf") // error: not found assert
3+
}

0 commit comments

Comments
 (0)