Skip to content

Make separation checking controlled by language import #23560

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ class CheckCaptures extends Recheck, SymTransformer:
/** The set of symbols that were rechecked via a completer */
private val completed = new mutable.HashSet[Symbol]

var needAnotherRun = false
/** Set on recheckClassDef since there we see all language imports */
private var sepChecksEnabled = false

private var needAnotherRun = false

def resetIteration()(using Context): Unit =
needAnotherRun = false
Expand Down Expand Up @@ -511,7 +514,7 @@ class CheckCaptures extends Recheck, SymTransformer:
// The path-use.scala neg test contains an example.
val underlying = CaptureSet.ofTypeDeeply(c1.widen)
capt.println(i"Widen reach $c to $underlying in ${env.owner}")
if ccConfig.useSepChecks then
if sepChecksEnabled then
recur(underlying.filter(!_.isTerminalCapability), env, null)
// we don't want to disallow underlying Fresh instances, since these are typically locally created
// fresh capabilities. We don't need to also follow the hidden set since separation
Expand Down Expand Up @@ -1144,6 +1147,7 @@ class CheckCaptures extends Recheck, SymTransformer:
* is already done in the TypeApply.
*/
override def recheckClassDef(tree: TypeDef, impl: Template, cls: ClassSymbol)(using Context): Type =
if Feature.enabled(Feature.separationChecking) then sepChecksEnabled = true
val localSet = capturedVars(cls)
for parent <- impl.parents do // (1)
checkSubset(capturedVars(parent.tpe.classSymbol), localSet, parent.srcPos,
Expand Down Expand Up @@ -2014,7 +2018,7 @@ class CheckCaptures extends Recheck, SymTransformer:
end checker

checker.traverse(unit)(using ctx.withOwner(defn.RootClass))
if ccConfig.useSepChecks then SepCheck(this).traverse(unit)
if sepChecksEnabled then SepCheck(this).traverse(unit)
if !ctx.reporter.errorsReported then
// We dont report errors here if previous errors were reported, because other
// errors often result in bad applied types, but flagging these bad types gives
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object Feature:
val saferExceptions = experimental("saferExceptions")
val pureFunctions = experimental("pureFunctions")
val captureChecking = experimental("captureChecking")
val separationChecking = experimental("separationChecking")
val into = experimental("into")
val modularity = experimental("modularity")
val quotedPatternsWithPolymorphicFunctions = experimental("quotedPatternsWithPolymorphicFunctions")
Expand All @@ -40,7 +41,7 @@ object Feature:
def experimentalAutoEnableFeatures(using Context): List[TermName] =
defn.languageExperimentalFeatures
.map(sym => experimental(sym.name))
.filterNot(_ == captureChecking) // TODO is this correct?
.filterNot(sym => sym == captureChecking || sym == separationChecking) // TODO is this correct?

val values = List(
(nme.help, "Display all available features"),
Expand All @@ -60,6 +61,7 @@ object Feature:
(saferExceptions, "Enable safer exceptions"),
(pureFunctions, "Enable pure functions for capture checking"),
(captureChecking, "Enable experimental capture checking"),
(separationChecking, "Enable experimental separation checking (requires captureChecking)"),
(into, "Allow into modifier on parameter types"),
(modularity, "Enable experimental modularity features"),
(packageObjectValues, "Enable experimental package objects as values"),
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ object Checking {
val name = Feature.experimental(sel.name)
name == Feature.scala2macros
|| name == Feature.captureChecking
|| name == Feature.separationChecking
trees.filter {
case Import(qual, selectors) =>
languageImport(qual) match
Expand Down
6 changes: 3 additions & 3 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CompilationTests {
compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFile("tests/pos-special/sourcepath/outer/nested/Test4.scala", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
compileFilesInDir("tests/pos-scala2", defaultOptions.and("-source", "3.0-migration")),
compileFilesInDir("tests/pos-custom-args/captures", defaultOptions.and("-language:experimental.captureChecking", "-source", "3.8")),
compileFilesInDir("tests/pos-custom-args/captures", defaultOptions.and("-language:experimental.captureChecking", "-language:experimental.separationChecking")),
compileFile("tests/pos-special/utf8encoded.scala", defaultOptions.and("-encoding", "UTF8")),
compileFile("tests/pos-special/utf16encoded.scala", defaultOptions.and("-encoding", "UTF16")),
compileDir("tests/pos-special/i18589", defaultOptions.and("-Wsafe-init").without("-Ycheck:all")),
Expand Down Expand Up @@ -149,7 +149,7 @@ class CompilationTests {
aggregateTests(
compileFilesInDir("tests/neg", defaultOptions, FileFilter.exclude(TestSources.negScala2LibraryTastyExcludelisted)),
compileFilesInDir("tests/neg-deep-subtype", allowDeepSubtypes),
compileFilesInDir("tests/neg-custom-args/captures", defaultOptions.and("-language:experimental.captureChecking", "-source", "3.8")),
compileFilesInDir("tests/neg-custom-args/captures", defaultOptions.and("-language:experimental.captureChecking", "-language:experimental.separationChecking")),
compileFile("tests/neg-custom-args/sourcepath/outer/nested/Test1.scala", defaultOptions.and("-sourcepath", "tests/neg-custom-args/sourcepath")),
compileDir("tests/neg-custom-args/sourcepath2/hi", defaultOptions.and("-sourcepath", "tests/neg-custom-args/sourcepath2", "-Xfatal-warnings")),
compileList("duplicate source", List(
Expand All @@ -172,7 +172,7 @@ class CompilationTests {
aggregateTests(
compileFilesInDir("tests/run", defaultOptions.and("-Wsafe-init")),
compileFilesInDir("tests/run-deep-subtype", allowDeepSubtypes),
compileFilesInDir("tests/run-custom-args/captures", allowDeepSubtypes.and("-language:experimental.captureChecking", "-source", "3.8")),
compileFilesInDir("tests/run-custom-args/captures", allowDeepSubtypes.and("-language:experimental.captureChecking", "-language:experimental.separationChecking")),
// Run tests for legacy lazy vals.
compileFilesInDir("tests/run", defaultOptions.and("-Wsafe-init", "-Ylegacy-lazy-vals", "-Ycheck-constraint-deps"), FileFilter.include(TestSources.runLazyValsAllowlist)),
).checkRuns()
Expand Down
7 changes: 7 additions & 0 deletions library/src/scala/runtime/stdLibPatches/language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ object language:
@compileTimeOnly("`captureChecking` can only be used at compile time in import statements")
object captureChecking

/** Experimental support for separation checking; requires captureChecking also to be enabled.
*
* @see [[https://dotty.epfl.ch/docs/reference/experimental/cc]]
*/
@compileTimeOnly("`separationChecking` can only be used at compile time in import statements")
object separationChecking

Comment on lines +87 to +93
Copy link
Member

Choose a reason for hiding this comment

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

NOTE: I'll have to take this addition into account in #23562 after this PR is merged.

/** Experimental support for automatic conversions of arguments, without requiring
* a language import `import scala.language.implicitConversions`.
*
Expand Down
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ object Build {
settings(scala2LibraryBootstrappedSettings).
settings(
moduleName := "scala2-library-cc",
scalacOptions ++= Seq("-source", "3.8"), // for separation checking
scalacOptions += "-language:experimental.separationChecking" // for separation checking
)

lazy val scala2LibraryBootstrappedSettings = Seq(
Expand Down
2 changes: 2 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ object MiMaFilters {

ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.stdLibPatches.language.2.13"),
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$2$u002E13$"),
ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.stdLibPatches.language#experimental.separationChecking"),
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$separationChecking$"),

ProblemFilters.exclude[DirectMissingMethodProblem]("scala.Conversion.underlying"),
ProblemFilters.exclude[MissingClassProblem]("scala.Conversion$"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import language.experimental.captureChecking
import language.`3.7` // no separation checking, TODO enable
// no separation checking, TODO enable and move to neg-customargs
import caps.*

def test[C^] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using options -source 3.7
import language.experimental.captureChecking
// no separation checking
import caps.*
class IO
class Ref[X](init: X):
Expand Down
Loading