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

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jul 19, 2025

Without the added clause, i23570 looks like this after typer:

[[syntax trees at end of                     typer]] // i23570.scala
package <empty> {
  final lazy module val i23570$package: i23570$package = new i23570$package()
  final module class i23570$package() extends Object() {
    this: i23570$package.type =>
    def f[C >: scala.caps.CapSet <: scala.caps.CapSet^{cap}](
      xs: List[(() -> Unit)^{C}]): List[(() -> Unit)^{C}] = xs.reverse
    def test(io: Object^{cap}, async: Object^{cap}): Unit =
      {
        val ok: List[() ->{scala.caps.CapSet^{io}} Unit] =
          f[_root_.scala.caps.CapSet^{io}](Nil)
        val x:
          List[() ->{scala.caps.CapSet^{io}} Unit] ->
            List[() ->{scala.caps.CapSet^{io}} Unit]
         = (xs: List[() ->{scala.caps.CapSet^{io}} Unit]) =>
          f[_root_.scala.caps.CapSet^{io}](xs)
        val y:
          List[() ->{scala.caps.CapSet^{io, async}} Unit] ->
            List[() ->{scala.caps.CapSet^{io, async}} Unit]
         = (xs: List[() ->{scala.caps.CapSet^{io, async}} Unit]) =>
          f[_root_.scala.caps.CapSet^{io, async}](xs)
        ()
      }
  }
}

Note the nested capture sets in the types of x and y.

Fixes #23570

Without the added clause, i23570 looks like this after typer:

```scala
[[syntax trees at end of                     typer]] // i23570.scala
package <empty> {
  final lazy module val i23570$package: i23570$package = new i23570$package()
  final module class i23570$package() extends Object() {
    this: i23570$package.type =>
    def f[C >: scala.caps.CapSet <: scala.caps.CapSet^{cap}](
      xs: List[(() -> Unit)^{C}]): List[(() -> Unit)^{C}] = xs.reverse
    def test(io: Object^{cap}, async: Object^{cap}): Unit =
      {
        val ok: List[() ->{scala.caps.CapSet^{io}} Unit] =
          f[_root_.scala.caps.CapSet^{io}](Nil)
        val x:
          List[() ->{scala.caps.CapSet^{io}} Unit] ->
            List[() ->{scala.caps.CapSet^{io}} Unit]
         = (xs: List[() ->{scala.caps.CapSet^{io}} Unit]) =>
          f[_root_.scala.caps.CapSet^{io}](xs)
        val y:
          List[() ->{scala.caps.CapSet^{io, async}} Unit] ->
            List[() ->{scala.caps.CapSet^{io, async}} Unit]
         = (xs: List[() ->{scala.caps.CapSet^{io, async}} Unit]) =>
          f[_root_.scala.caps.CapSet^{io, async}](xs)
        ()
      }
  }
}
```
Note the nested capture sets in the types of `x` and `y`.

Fixes scala#23570
@@ -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.

Copy link
Member

@noti0na1 noti0na1 left a comment

Choose a reason for hiding this comment

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

LGTM

@odersky odersky merged commit e05e3a3 into scala:main Jul 21, 2025
32 checks passed
@odersky odersky deleted the fix-23570 branch July 21, 2025 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't pass an explicit capture set to a capset parameter in eta expansion
3 participants