From ea19290ae8ec0b6856b8066a0d5d15753ea8e933 Mon Sep 17 00:00:00 2001 From: Yichen Xu Date: Thu, 29 Aug 2024 14:45:53 +0200 Subject: [PATCH 1/2] No need to unbox if expected type is LhsProto --- compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala b/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala index ec134149eb49..b05ab8542137 100644 --- a/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala +++ b/compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala @@ -969,7 +969,7 @@ class CheckCaptures extends Recheck, SymTransformer: if tree.isTerm then if !ccConfig.useExistentials then checkReachCapsIsolated(res.widen, tree.srcPos) - if !pt.isBoxedCapturing then + if !pt.isBoxedCapturing && pt != LhsProto then markFree(res.boxedCaptureSet, tree.srcPos) res From a045b8c3bc287a9788c02d62fbf948d841f7d9db Mon Sep 17 00:00:00 2001 From: Yichen Xu Date: Fri, 30 Aug 2024 00:52:28 +0200 Subject: [PATCH 2/2] add test case for #21507 --- tests/pos-custom-args/captures/i21507.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/pos-custom-args/captures/i21507.scala diff --git a/tests/pos-custom-args/captures/i21507.scala b/tests/pos-custom-args/captures/i21507.scala new file mode 100644 index 000000000000..bb80dafb3b45 --- /dev/null +++ b/tests/pos-custom-args/captures/i21507.scala @@ -0,0 +1,10 @@ +import language.experimental.captureChecking + +trait Box[Cap^]: + def store(f: (() -> Unit)^{Cap^}): Unit + +def run[Cap^](f: Box[Cap]^{Cap^} => Unit): Box[Cap]^{Cap^} = + new Box[Cap]: + private var item: () ->{Cap^} Unit = () => () + def store(f: () ->{Cap^} Unit): Unit = + item = f // was error, now ok