Skip to content

Commit

Permalink
Do not enable experimental by default in nightly and snapshot versions
Browse files Browse the repository at this point in the history
This scheme was devised as a simple way to enable experimental on non-stable
released. This made it impossible to publish a library containing experimental
features for a non-experimental version of the compiler. We had `-Yno-experimental`
to test the expected behavior on stable releases. Later we added `-experimental`
which add `@experimental` to all top-level classes. On a stable release, this
has the same effect as enabling experimental features, but it also allows
publishing this library (users will need to also be experimental to use it).

We can use `-experimental` on nightly and snapshot releases to achieve the
same behavior as before. By doing this the `-Yno-experimental` flag is no
longer needed and can be removed.

We will need to use `-experimental` in all our tests to enable experimental
by default. We can add this compiler flag to the default settings. We
also need a few tests that do not use `-experimental` to test the behavior
of `@experimental`. These can be added in `tests/*-no-experimental/`. Not
all tests have `-experimental` enabled, this will be added on a need basis.

With this we detected one bug, the runtime staging compile crashes if the
code uses experimental features. To fix this we enable `-experimental` by
default in this compiler. Note that the experimental-ness checks will have
been done by the compiler that compiled the quotes.
  • Loading branch information
nicolasstucki committed Feb 16, 2024
1 parent 0800dec commit 01dfd08
Show file tree
Hide file tree
Showing 67 changed files with 41 additions and 53 deletions.
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ object Feature:
report.error(
em"""Experimental $which may only be used under experimental mode:
| 1. In a definition marked as @experimental
| 2. Compiling with the -experimental compiler flag
| 3. With a nightly or snapshot version of the compiler$note
| 2. Compiling with the -experimental compiler flag$note
""", srcPos)

private def ccException(sym: Symbol)(using Context): Boolean =
Expand All @@ -163,7 +162,7 @@ object Feature:
do checkExperimentalFeature(s"feature $setting", NoSourcePosition)

def isExperimentalEnabled(using Context): Boolean =
(Properties.experimental || ctx.settings.experimental.value) && !ctx.settings.YnoExperimental.value
ctx.settings.experimental.value

/** Handle language import `import language.<prefix>.<imported>` if it is one
* of the global imports `pureFunctions` or `captureChecking`. In this case
Expand Down
9 changes: 2 additions & 7 deletions compiler/src/dotty/tools/dotc/config/Properties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,8 @@ trait PropertiesTrait {
*/
val versionString: String = "version " + simpleVersionString

/** Whether the current version of compiler is experimental
*
* 1. Snapshot, nightly releases and non-bootstrapped compiler are experimental.
* 2. Features supported by experimental versions of the compiler:
* - research plugins
*/
val experimental: Boolean = versionString.contains("SNAPSHOT") || versionString.contains("NIGHTLY") || versionString.contains("nonbootstrapped")
/** Whether the current version of supports research plugins */
val researchPluginsEnabled: Boolean = versionString.contains("SNAPSHOT") || versionString.contains("NIGHTLY") || versionString.contains("nonbootstrapped")

val copyrightString: String = scalaPropOrElse("copyright.string", "(c) 2002-2017 LAMP/EPFL")

Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ private sealed trait YSettings:
val YretainTrees: Setting[Boolean] = BooleanSetting("-Yretain-trees", "Retain trees for top-level classes, accessible from ClassSymbol#tree")
val YshowTreeIds: Setting[Boolean] = BooleanSetting("-Yshow-tree-ids", "Uniquely tag all tree nodes in debugging output.")
val YfromTastyIgnoreList: Setting[List[String]] = MultiStringSetting("-Yfrom-tasty-ignore-list", "file", "List of `tasty` files in jar files that will not be loaded when using -from-tasty.")
val YnoExperimental: Setting[Boolean] = BooleanSetting("-Yno-experimental", "Disable experimental language features.")
val YlegacyLazyVals: Setting[Boolean] = BooleanSetting("-Ylegacy-lazy-vals", "Use legacy (pre 3.3.0) implementation of lazy vals.")
val YcompileScala2Library: Setting[Boolean] = BooleanSetting("-Ycompile-scala2-library", "Used when compiling the Scala 2 standard library.")
val YoutputOnlyTasty: Setting[Boolean] = BooleanSetting("-Youtput-only-tasty", "Used to only generate the TASTy file without the classfiles")
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/plugins/Plugins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ trait Plugins {
val updatedPlan = Plugins.schedule(plan, pluginPhases)

// add research plugins
if Properties.experimental && !ctx.settings.YnoExperimental.value then
if Properties.researchPluginsEnabled then
plugins.collect { case p: ResearchPlugin => p }.foldRight(updatedPlan) {
(plug, plan) => plug.init(options(plug), plan)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/DottyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ trait DottyTest extends ContextEscapeDetection {
protected def initializeCtx(fc: FreshContext): Unit = {
fc.setSetting(fc.settings.encoding, "UTF8")
fc.setSetting(fc.settings.classpath, TestConfiguration.basicClasspath)
fc.setSetting(fc.settings.experimental, true)
fc.setSetting(fc.settings.language, List("experimental.erasedDefinitions"))
fc.setProperty(ContextDoc, new ContextDocstrings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BootstrappedOnlyCompilationTests {
aggregateTests(
compileFilesInDir("tests/bench", defaultOptions.without("-Yno-deep-subtypes")),
compileFilesInDir("tests/pos-macros", defaultOptions.and("-Xcheck-macros")),
compileFilesInDir("tests/pos-no-experimental", defaultOptions.without("-experimental")),
).checkCompile()
}

Expand Down Expand Up @@ -104,8 +105,10 @@ class BootstrappedOnlyCompilationTests {

@Test def negMacros: Unit = {
implicit val testGroup: TestGroup = TestGroup("compileNegWithCompiler")
compileFilesInDir("tests/neg-macros", defaultOptions.and("-Xcheck-macros"))
.checkExpectedErrors()
aggregateTests(
compileFilesInDir("tests/neg-macros", defaultOptions.and("-Xcheck-macros")),
compileFilesInDir("tests/neg-no-experimental", defaultOptions.without("-experimental")),
).checkExpectedErrors()
}

@Test def negWithCompiler: Unit = {
Expand All @@ -120,8 +123,10 @@ class BootstrappedOnlyCompilationTests {

@Test def runMacros: Unit = {
implicit val testGroup: TestGroup = TestGroup("runMacros")
compileFilesInDir("tests/run-macros", defaultOptions.and("-Xcheck-macros"), FileFilter.exclude(TestSources.runMacrosScala2LibraryTastyBlacklisted))
.checkRuns()
aggregateTests(
compileFilesInDir("tests/run-macros", defaultOptions.and("-Xcheck-macros"), FileFilter.exclude(TestSources.runMacrosScala2LibraryTastyBlacklisted)),
compileFilesInDir("tests/run-no-experimental", defaultOptions.without("-experimental")),
).checkRuns()
}

@Test def runWithCompiler: Unit = {
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na
}

object ReplTest:
val commonOptions = Array("-color:never", "-language:experimental.erasedDefinitions", "-pagewidth", "80")
val commonOptions = Array("-color:never", "-pagewidth", "80")
val defaultOptions = commonOptions ++ Array("-classpath", TestConfiguration.basicClasspath)
lazy val withStagingOptions = commonOptions ++ Array("-classpath", TestConfiguration.withStagingClasspath)
3 changes: 2 additions & 1 deletion compiler/test/dotty/tools/repl/ScriptedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.junit.Test
import org.junit.experimental.categories.Category

/** Runs all tests contained in `compiler/test-resources/repl/` */
class ScriptedTests extends ReplTest {
class ScriptedTests extends ReplTest(ScriptedTests.options) {

@Test def replTests = scripts("/repl").foreach(testFile)

Expand All @@ -17,4 +17,5 @@ class ScriptedTests extends ReplTest {
}

object ScriptedTests {
val options = Array("-experimental", "-language:experimental.erasedDefinitions") ++ ReplTest.defaultOptions
}
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/vulpix/TestConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ object TestConfiguration {

val yCheckOptions = Array("-Ycheck:all")

val commonOptions = Array("-indent") ++ checkOptions ++ noCheckOptions ++ yCheckOptions
val commonOptions = Array("-indent", "-experimental") ++ checkOptions ++ noCheckOptions ++ yCheckOptions
val defaultOptions = TestFlags(basicClasspath, commonOptions)
val unindentOptions = TestFlags(basicClasspath, Array("-no-indent") ++ checkOptions ++ noCheckOptions ++ yCheckOptions)
val withCompilerOptions =
Expand Down
2 changes: 0 additions & 2 deletions docs/_docs/reference/other-new-features/experimental-defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ Experimental definitions can only be referenced in an experimental scope. Experi

</details>

6. Any code compiled using a [_Nightly_](https://search.maven.org/artifact/org.scala-lang/scala3-compiler_3) or _Snapshot_ version of the compiler is considered to be in an experimental scope.
Can use the `-Yno-experimental` compiler flag to disable it and run as a proper release.

In any other situation, a reference to an experimental definition will cause a compilation error.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Experimental definitions can only be referenced in an experimental scope. Experi

<details>
<summary>Example 1</summary>

```scala
import scala.annotation.experimental

Expand All @@ -242,7 +242,7 @@ Experimental definitions can only be referenced in an experimental scope. Experi
}
}
```

</details>

5. Annotations of an experimental definition are in experimental scopes. Examples:
Expand All @@ -265,9 +265,6 @@ Experimental definitions can only be referenced in an experimental scope. Experi

</details>

6. Any code compiled using a [_Nightly_](https://search.maven.org/artifact/org.scala-lang/scala3-compiler_3) or _Snapshot_ version of the compiler is considered to be in an experimental scope.
Can use the `-Yno-experimental` compiler flag to disable it and run as a proper release.

In any other situation, a reference to an experimental definition will cause a compilation error.

## Experimental inheritance
Expand Down
4 changes: 1 addition & 3 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,7 @@ object Build {
SourceLinksIntegrationTest / test:= ((SourceLinksIntegrationTest / test) dependsOn generateScalaDocumentation.toTask("")).value,
).
settings(
scalacOptions += "-experimental",
Compile / resourceGenerators ++= Seq(
generateStaticAssetsTask.taskValue,
bundleCSS.taskValue
Expand Down Expand Up @@ -2155,9 +2156,6 @@ object Build {
settings(
versionScheme := Some("semver-spec"),
libraryDependencies += "org.scala-lang" % "scala-library" % stdlibVersion,
// Make sure we do not refer to experimental features outside an experimental scope.
// In other words, disable NIGHTLY/SNAPSHOT experimental scope.
scalacOptions += "-Yno-experimental",
).
settings(dottyLibrarySettings)
if (mode == Bootstrapped) {
Expand Down
5 changes: 4 additions & 1 deletion staging/src/scala/quoted/staging/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ private class QuoteDriver(appClassloader: ClassLoader) extends Driver:
val ctx = {
val ctx0 = QuotesCache.init(initCtx.fresh)
val ctx1 = setup(settings.compilerArgs.toArray :+ "dummy.scala", ctx0).get._2
setCompilerSettings(ctx1.fresh.setSetting(ctx1.settings.outputDir, outDir), settings)
val ctx2 = ctx1.fresh
ctx2.setSetting(ctx1.settings.outputDir, outDir)
ctx2.setSetting(ctx1.settings.experimental, true)
setCompilerSettings(ctx2, settings)
}

new QuoteCompiler().newRun(ctx).compileExpr(exprBuilder) match
Expand Down
10 changes: 5 additions & 5 deletions tests/neg-macros/i18677-a.check
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

-- Error: tests/neg-macros/i18677-a/Test_2.scala:4:6 -------------------------------------------------------------------
3 |@extendFoo
4 |class AFoo // error
-- Error: tests/neg-macros/i18677-a/Test_2.scala:2:6 -------------------------------------------------------------------
1 |@extendFoo
2 |class AFoo // error
|^
|Malformed tree was found while expanding macro with -Xcheck-macros.
|The tree does not conform to the compiler's tree invariants.
|
|Macro was:
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-a/Test_2.scala") @extendFoo class AFoo()
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-a/Test_2.scala") @scala.annotation.experimental @extendFoo class AFoo()
|
|The macro returned:
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-a/Test_2.scala") @extendFoo class AFoo() extends Foo
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-a/Test_2.scala") @scala.annotation.experimental @extendFoo class AFoo() extends Foo
|
|Error:
|assertion failed: Parents of class symbol differs from the parents in the tree for class AFoo
Expand Down
4 changes: 1 addition & 3 deletions tests/neg-macros/i18677-a/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//> using -expermiental

import annotation.MacroAnnotation
import quoted.*

Expand All @@ -15,4 +13,4 @@ class extendFoo extends MacroAnnotation :
newTree :: Nil
case _ =>
report.error("@extendFoo can only annotate class definitions")
tree :: Nil
tree :: Nil
4 changes: 1 addition & 3 deletions tests/neg-macros/i18677-a/Test_2.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
//> using -expermiental

@extendFoo
class AFoo // error
class AFoo // error
10 changes: 5 additions & 5 deletions tests/neg-macros/i18677-b.check
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

-- Error: tests/neg-macros/i18677-b/Test_2.scala:4:6 -------------------------------------------------------------------
3 |@extendFoo
4 |class AFoo // error
-- Error: tests/neg-macros/i18677-b/Test_2.scala:2:6 -------------------------------------------------------------------
1 |@extendFoo
2 |class AFoo // error
|^
|Malformed tree was found while expanding macro with -Xcheck-macros.
|The tree does not conform to the compiler's tree invariants.
|
|Macro was:
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-b/Test_2.scala") @extendFoo class AFoo()
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-b/Test_2.scala") @scala.annotation.experimental @extendFoo class AFoo()
|
|The macro returned:
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-b/Test_2.scala") @extendFoo class AFoo() extends Foo
|@scala.annotation.internal.SourceFile("tests/neg-macros/i18677-b/Test_2.scala") @scala.annotation.experimental @extendFoo class AFoo() extends Foo
|
|Error:
|assertion failed: Parents of class symbol differs from the parents in the tree for class AFoo
Expand Down
4 changes: 1 addition & 3 deletions tests/neg-macros/i18677-b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//> using -expermiental

import annotation.MacroAnnotation
import quoted.*

Expand All @@ -15,4 +13,4 @@ class extendFoo extends MacroAnnotation :
newTree :: Nil
case _ =>
report.error("@extendFoo can only annotate class definitions")
tree :: Nil
tree :: Nil
4 changes: 1 addition & 3 deletions tests/neg-macros/i18677-b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
//> using -expermiental

@extendFoo
class AFoo // error
class AFoo // error
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/run-macros/term-show.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
()
}
@scala.annotation.internal.SourceFile("tests/run-macros/term-show/Test_2.scala") trait A() extends java.lang.Object {
@scala.annotation.internal.SourceFile("tests/run-macros/term-show/Test_2.scala") @scala.annotation.experimental trait A() extends java.lang.Object {
def imp(x: scala.Int)(implicit str: scala.Predef.String): scala.Int
def use(`x₂`: scala.Int)(using `str₂`: scala.Predef.String): scala.Int
def era(`x₃`: scala.Int)(erased `str₃`: scala.Predef.String): scala.Int
Expand Down

0 comments on commit 01dfd08

Please sign in to comment.