diff --git a/compiler/src/dotty/tools/dotc/config/Feature.scala b/compiler/src/dotty/tools/dotc/config/Feature.scala index 8fda99be6896..3b96eb83731d 100644 --- a/compiler/src/dotty/tools/dotc/config/Feature.scala +++ b/compiler/src/dotty/tools/dotc/config/Feature.scala @@ -133,7 +133,7 @@ object Feature: /** Is captureChecking enabled for this compilation unit? */ def ccEnabled(using Context) = enabledBySetting(captureChecking) - || ctx.compilationUnit.needsCaptureChecking + || ctx.originalCompilationUnit.needsCaptureChecking /** Is pureFunctions enabled for any of the currently compiled compilation units? */ def pureFunsEnabledSomewhere(using Context) = diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala index a867f90b237a..85edadd40c80 100644 --- a/compiler/src/dotty/tools/dotc/core/Contexts.scala +++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala @@ -42,6 +42,7 @@ import plugins.* import java.util.concurrent.atomic.AtomicInteger import java.nio.file.InvalidPathException import dotty.tools.dotc.coverage.Coverage +import scala.annotation.tailrec object Contexts { @@ -776,6 +777,14 @@ object Contexts { c end FreshContext + extension (ctx: Context) + /** Get the original compilation unit, ignoring any highlighting wrappers. */ + @tailrec + def originalCompilationUnit: CompilationUnit = + val cu = ctx.compilationUnit + if cu.source.name == SyntaxHighlighting.VirtualSourceName then ctx.outer.originalCompilationUnit + else cu + extension (c: Context) def addNotNullInfo(info: NotNullInfo) = if c.explicitNulls then c.withNotNullInfos(c.notNullInfos.extendWith(info)) else c @@ -787,18 +796,16 @@ object Contexts { if !c.explicitNulls || (c.notNullInfos eq infos) then c else c.fresh.setNotNullInfos(infos) // TODO: Fix issue when converting ModeChanges and FreshModeChanges to extension givens - extension (c: Context) { + extension (c: Context) final def withModeBits(mode: Mode): Context = if (mode != c.mode) c.fresh.setMode(mode) else c final def addMode(mode: Mode): Context = withModeBits(c.mode | mode) final def retractMode(mode: Mode): Context = withModeBits(c.mode &~ mode) - } - extension (c: FreshContext) { + extension (c: FreshContext) final def addMode(mode: Mode): c.type = c.setMode(c.mode | mode) final def retractMode(mode: Mode): c.type = c.setMode(c.mode &~ mode) - } /** Run `op` with a pool-allocated context that has an ExporeTyperState. */ inline def explore[T](inline op: Context ?=> T)(using Context): T = diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index ea58cff357c1..41ab7412d748 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -16,6 +16,9 @@ import java.util.Arrays /** This object provides functions for syntax highlighting in the REPL */ object SyntaxHighlighting { + /** The name of the virtual source file used for highlighting */ + val VirtualSourceName = "" + /** if true, log erroneous positions being highlighted */ private inline val debug = true @@ -33,7 +36,7 @@ object SyntaxHighlighting { def freshCtx = ctx.fresh.setReporter(Reporter.NoReporter) if (in.isEmpty || ctx.settings.color.value == "never") in else { - val source = SourceFile.virtual("", in) + val source = SourceFile.virtual(VirtualSourceName, in) given Context = freshCtx .setCompilationUnit(CompilationUnit(source, mustExist = false)(using freshCtx)) diff --git a/tests/neg-custom-args/captures/i23389-1.scala b/tests/neg-custom-args/captures/i23389-1.scala new file mode 100644 index 000000000000..568c9da52aa7 --- /dev/null +++ b/tests/neg-custom-args/captures/i23389-1.scala @@ -0,0 +1,8 @@ +import language.experimental.captureChecking +import caps.* + +trait Collection[+T] extends Mutable: + update def add(elem: T): Unit // error + update def remove(elem: T): Unit // error + def get(index: Int): Option[T] + diff --git a/tests/neg-custom-args/captures/i23389-2.scala b/tests/neg-custom-args/captures/i23389-2.scala new file mode 100644 index 000000000000..a7612891d3a9 --- /dev/null +++ b/tests/neg-custom-args/captures/i23389-2.scala @@ -0,0 +1,8 @@ +import language.experimental.captureChecking +import caps.* + +trait Collection[T] extends Mutable // <- note the forgotten : + update def add(elem: T): Unit // error // error + update def remove(elem: T): Unit // error // error + def get(index: Int): Option[T] // error // error +