diff --git a/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala b/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala index c52e1aa6714e..fab0689b4df2 100644 --- a/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala +++ b/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala @@ -826,12 +826,16 @@ class CheckCaptures extends Recheck, SymTransformer: */ def adaptUniversal(actual: Type, expected: Type, tree: Tree)(using Context): Type = if expected.captureSet.disallowsUniversal && actual.captureSet.isUniversal then - val localRoot = impliedRoot(tree) - CapturingType( - actual.stripCapturing, - localRoot.termRef.singletonCaptureSet, - actual.isBoxedCapturing) - .showing(i"adapt universal $actual vs $expected = $result", capt) + if actual.isInstanceOf[SingletonType] then + // capture set is only exposed when widening + adaptUniversal(actual.widen, expected, tree) + else + val localRoot = impliedRoot(tree) + CapturingType( + actual.stripCapturing, + localRoot.termRef.singletonCaptureSet, + actual.isBoxedCapturing) + .showing(i"adapt universal $actual vs $expected = $result", capt) else actual private inline val debugSuccesses = false diff --git a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 4f63bfb38691..bc8e2e9bc88e 100644 --- a/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -640,7 +640,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { try changePrec(GlobalPrec)(toText(arg) ~ "^" ~ toTextCaptureSet(captureSet)) catch case ex: IllegalCaptureRef => toTextAnnot if annot.symbol.maybeOwner == defn.RetainsAnnot - && Feature.ccEnabled && Config.printCaptureSetsAsPrefix && !printDebug + && Feature.ccEnabled + && Config.printCaptureSetsAsPrefix && !printDebug + && Phases.checkCapturesPhase.exists // might be missing on -Ytest-pickler then toTextRetainsAnnot else toTextAnnot case EmptyTree => diff --git a/tests/pos/i18699.scala b/tests/pos/i18699.scala new file mode 100644 index 000000000000..61b21b0d3ad4 --- /dev/null +++ b/tests/pos/i18699.scala @@ -0,0 +1,7 @@ +import language.experimental.captureChecking +trait Cap: + def use: Int = 42 + +def test2(cs: List[Cap^]): Unit = + val t0: Cap^{cap[test2]} = cs.head // error + var t1: Cap^{cap[test2]} = cs.head // error \ No newline at end of file