Skip to content

Flatten nested capture sets in retainedElementsRaw #23571

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

Merged
merged 2 commits into from
Jul 21, 2025
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/cc/CaptureOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ extension (tp: Type)
def retainedElementsRaw(using Context): List[Type] = tp match
case OrType(tp1, tp2) =>
tp1.retainedElementsRaw ++ tp2.retainedElementsRaw
case AnnotatedType(tp1, ann) if tp1.derivesFrom(defn.Caps_CapSet) && ann.symbol.isRetains =>
Copy link
Member

Choose a reason for hiding this comment

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

Do we need isRetainsLike here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ni, I don't think a call-by-name set can end up here.

ann.tree.retainedSet.retainedElementsRaw
case tp =>
// Nothing is a special type to represent the empty set
if tp.isNothingType then Nil
Expand Down
7 changes: 7 additions & 0 deletions tests/pos-custom-args/captures/i23570.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def f[C^](xs: List[() ->{C} Unit]): List[() ->{C} Unit] =
xs.reverse

def test(io: Object^, async: Object^): Unit =
val ok = f[{io}](Nil)
val x = f[{io}] // was error
val y = f[{io, async}] // was error
24 changes: 24 additions & 0 deletions tests/pos/cc-use-alternatives.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import language.experimental.captureChecking
// no separation checking
import caps.{cap, use}

def foo1(@use xs: List[() => Unit]): Unit =
var x: () ->{xs*} Unit = xs.head
var ys = xs
while ys.nonEmpty do
ys = ys.tail
x = ys.head

def foo2(@use xs: List[() => Unit]): Unit =
var x: () => Unit = xs.head // note: this would fail separation checking
var ys = xs
while ys.nonEmpty do
ys = ys.tail
x = ys.head

def foo3[@use C^](xs: List[() ->{C} Unit]): Unit =
var x: () ->{C} Unit = xs.head
var ys = xs
while ys.nonEmpty do
ys = ys.tail
x = ys.head
Loading