Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -experimental compiler flags #18571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ object Feature:

def checkExperimentalFeature(which: String, srcPos: SrcPos, note: => String = "")(using Context) =
if !isExperimentalEnabled then
report.error(em"Experimental $which may only be used with a nightly or snapshot version of the compiler$note", srcPos)
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
""", srcPos)

private def ccException(sym: Symbol)(using Context): Boolean =
ccEnabled && defn.ccExperimental.contains(sym)
Expand All @@ -159,7 +164,7 @@ object Feature:
do checkExperimentalFeature(s"feature $setting", NoSourcePosition)

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

/** Handle language import `import language.<prefix>.<imported>` if it is one
* of the global imports `pureFunctions` or `captureChecking`. In this case
Expand Down
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 @@ -119,6 +119,7 @@ trait CommonScalaSettings:
val explainTypes: Setting[Boolean] = BooleanSetting("-explain-types", "Explain type errors in more detail (deprecated, use -explain instead).", aliases = List("--explain-types", "-explaintypes"))
val unchecked: Setting[Boolean] = BooleanSetting("-unchecked", "Enable additional warnings where generated code depends on assumptions.", initialValue = true, aliases = List("--unchecked"))
val language: Setting[List[String]] = MultiStringSetting("-language", "feature", "Enable one or more language features.", aliases = List("--language"))
val experimental: Setting[Boolean] = BooleanSetting("-experimental", "Annotate all top-level definitions with @experimental. This enables the use of experimental features anywhere in the project.")

/* Coverage settings */
val coverageOutputDir = PathSetting("-coverage-out", "Destination for coverage classfiles and instrumentation data.", "", aliases = List("--coverage-out"))
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/plugins/Plugins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import config.{ PathResolver, Feature }
import dotty.tools.io._
import Phases._
import config.Printers.plugins.{ println => debug }
import config.Properties

/** Support for run-time loading of compiler plugins.
*
Expand Down Expand Up @@ -126,7 +127,7 @@ trait Plugins {
val updatedPlan = Plugins.schedule(plan, pluginPhases)

// add research plugins
if (Feature.isExperimentalEnabled)
if Properties.experimental && !ctx.settings.YnoExperimental.value then
plugins.collect { case p: ResearchPlugin => p }.foldRight(updatedPlan) {
(plug, plan) => plug.init(options(plug), plan)
}
Expand Down
11 changes: 9 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,15 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
)
}
case tree: ValDef =>
annotateExperimental(tree.symbol)
registerIfHasMacroAnnotations(tree)
checkErasedDef(tree)
val tree1 = cpy.ValDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
if tree1.removeAttachment(desugar.UntupledParam).isDefined then
checkStableSelection(tree.rhs)
processValOrDefDef(super.transform(tree1))
case tree: DefDef =>
annotateExperimental(tree.symbol)
registerIfHasMacroAnnotations(tree)
checkErasedDef(tree)
annotateContextResults(tree)
Expand Down Expand Up @@ -537,9 +539,14 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
report.error("`erased` definition cannot be implemented with en expression of type Null", tree.srcPos)

private def annotateExperimental(sym: Symbol)(using Context): Unit =
if sym.is(Module) && sym.companionClass.hasAnnotation(defn.ExperimentalAnnot) then
def isTopLevelDefinitionInSource(sym: Symbol) =
!sym.is(Package) && !sym.name.isPackageObjectName &&
(sym.owner.is(Package) || (sym.owner.isPackageObject && !sym.isConstructor))
if !sym.hasAnnotation(defn.ExperimentalAnnot)
&& (ctx.settings.experimental.value && isTopLevelDefinitionInSource(sym))
|| (sym.is(Module) && sym.companionClass.hasAnnotation(defn.ExperimentalAnnot))
then
sym.addAnnotation(Annotation(defn.ExperimentalAnnot, sym.span))
sym.companionModule.addAnnotation(Annotation(defn.ExperimentalAnnot, sym.span))

private def scala2LibPatch(tree: TypeDef)(using Context) =
val sym = tree.symbol
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/reference/changed-features/compiler-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ For experimentation and research, Scala 3 introduces _research plugin_. Research
are more powerful than Scala 2 analyzer plugins as they let plugin authors customize
the whole compiler pipeline. One can easily replace the standard typer by a custom one or
create a parser for a domain-specific language. However, research plugins are only
enabled for nightly or snaphot releases of Scala 3.
enabled with the `-experimental` compiler flag or in nightly/snapshot releases of Scala 3.

Common plugins that add new phases to the compiler pipeline are called
_standard plugins_ in Scala 3. In terms of features, they are similar to
Expand Down
6 changes: 6 additions & 0 deletions docs/_docs/reference/experimental/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ They are enabled by importing the feature or using the `-language` compiler flag
In general, experimental language features can be imported in an experimental scope (see [experimental definitions](../other-new-features/experimental-defs.md)).
They can be imported at the top-level if all top-level definitions are `@experimental`.

### `-experimental` compiler flag

This flag enables the use of any experimental language feature in the project.
It does this by adding an `@experimental` annotation to all top-level definitions.
Hence, dependent projects also have to be experimental.

## Experimental language features supported by special compiler options

Some experimental language features that are still in research and development can be enabled with special compiler options. These include
Expand Down
6 changes: 6 additions & 0 deletions docs/_docs/reference/other-new-features/experimental-defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,9 @@ class MyExperimentalTests {
```

</details>

## `-experimental` compiler flag

This flag enables the use of any experimental language feature in the project.
It does this by adding an `@experimental` annotation to all top-level definitions.
Hence, dependent projects also have to be experimental.
5 changes: 5 additions & 0 deletions tests/neg/expeimental-flag-with-lang-feature-1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using options -Yno-experimental

import scala.language.experimental.erasedDefinitions

erased def erasedFun(erased x: Int): Int = x // error // error
7 changes: 7 additions & 0 deletions tests/neg/expeimental-flag-with-lang-feature-2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//> using options -Yno-experimental

import scala.language.experimental.namedTypeArguments // error

def namedTypeArgumentsFun[T, U]: Int =
namedTypeArgumentsFun[T = Int, U = Int]
namedTypeArgumentsFun[U = Int, T = Int]
18 changes: 18 additions & 0 deletions tests/neg/expeimental-flag.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//> using options -Yno-experimental

import scala.annotation.experimental

class Foo:
def foo: Int = experimentalDef // error

class Bar:
def bar: Int = experimentalDef // error
object Bar:
def bar: Int = experimentalDef // error

object Baz:
def bar: Int = experimentalDef // error

def toplevelMethod: Int = experimentalDef // error

@experimental def experimentalDef: Int = 1
10 changes: 10 additions & 0 deletions tests/pos/expeimental-flag-with-lang-feature.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//> using options -experimental -Yno-experimental

import scala.language.experimental.erasedDefinitions
import scala.language.experimental.namedTypeArguments

erased def erasedFun(erased x: Int): Int = x

def namedTypeArgumentsFun[T, U]: Int =
namedTypeArgumentsFun[T = Int, U = Int]
namedTypeArgumentsFun[U = Int, T = Int]
18 changes: 18 additions & 0 deletions tests/pos/expeimental-flag.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//> using options -experimental -Yno-experimental
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Yno-experimental is set to disable experimental features on this snapshot. -experimental is set to add @experimental to the top-level definitions.


import scala.annotation.experimental

class Foo:
def foo: Int = experimentalDef

class Bar:
def bar: Int = experimentalDef
object Bar:
def bar: Int = experimentalDef

object Baz:
def bar: Int = experimentalDef

def toplevelMethod: Int = experimentalDef

@experimental def experimentalDef: Int = 1
Loading