# Capture leak when using varargs?

**URL:** <https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086>\
**Category:** Question\
**Created:** [October 26, 2025, 10:21am UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086 "2025-10-26T10:21:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrinaudo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/nrinaudo/32/2236_2.png) [@nrinaudo](https://users.scala-lang.org/u/nrinaudo)\
**Post date:** [October 26, 2025, 10:21am UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/1 "2025-10-26T10:21:30Z")

</div>

I’m writing the following code, where the interesting bit is in the capture set of `oneOf`.

```scala
//> using scala 3.nightly
//> using option -language:experimental.captureChecking -nowarn

import caps.*

trait Rand extends SharedCapability:
  def range(min: Int, max: Int): Int

def nextInt(max: Int): Rand ?-> Int =
  r ?=> r.range(0, max)

def oneOf[A](head: Rand ?=> A, tail: (Rand ?=> A)*): Rand ?->{head} A =
  val all: Seq[Rand ?->{head, tail*} A] = head +: tail

  all(nextInt(all.length))

```

Instinctively, I would expect `oneOf` to return `Rand ?→{head, tail*} A`, and am a little weirded out that the above code compiles. Isn’t this forgetting the fact that whatever is contained by `tail` is tracked?

I do need `all` to explicitly capture `{head, tail*}`_,_ so my expectations seem at list _a little_ reasonable…

---

<div class="post-metadata">

**Author:** ![Odersky](https://users.scala-lang.org/user_avatar/users.scala-lang.org/odersky/32/3160_2.png) [@Odersky](https://users.scala-lang.org/u/Odersky)\
**Post date:** [October 26, 2025, 7:07pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/2 "2025-10-26T19:07:42Z")

</div>

That’s indeed a bug in the implementation. In fact you could also have declared`oneOf` to return a pure function `Rand → A` and this would have typechecked also, which is even more wrong than what you showed. Fix is forthcoming.

---

<div class="post-metadata">

**Author:** ![Odersky](https://users.scala-lang.org/user_avatar/users.scala-lang.org/odersky/32/3160_2.png) [@Odersky](https://users.scala-lang.org/u/Odersky)\
**Post date:** [October 27, 2025, 2:37pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/3 "2025-10-27T14:37:10Z")

</div>

There’s now a PR that should fix this: [Fix apply rule by odersky · Pull Request #24273 · scala/scala3 · GitHub](https://github.com/scala/scala3/pull/24273)

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/nrinaudo/32/2236_2.png) [@nrinaudo](https://users.scala-lang.org/u/nrinaudo)\
**Post date:** [October 27, 2025, 2:54pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/4 "2025-10-27T14:54:19Z")

</div>

Thank you so much - and sorry I keep running in all these corner cases…

---

<div class="post-metadata">

**Author:** ![Odersky](https://users.scala-lang.org/user_avatar/users.scala-lang.org/odersky/32/3160_2.png) [@Odersky](https://users.scala-lang.org/u/Odersky)\
**Post date:** [October 27, 2025, 3:09pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/5 "2025-10-27T15:09:09Z")

</div>

You should not be sorry at all! It’s a big help for us that you find these corner cases.

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/nrinaudo/32/2236_2.png) [@nrinaudo](https://users.scala-lang.org/u/nrinaudo)\
**Post date:** [December 29, 2025, 7:29am UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/6 "2025-12-29T07:29:41Z")

</div>

I’m still running in the same issue in 3.8.0-RC3 and 3.nightly, even though the PR linked by Martin was merged quite a while ago.

Is this a regression, or have my expectations become incorrect in the face of more developments / discoveries of how capture sets work?

---

<div class="post-metadata">

**Author:** ![Odersky](https://users.scala-lang.org/user_avatar/users.scala-lang.org/odersky/32/3160_2.png) [@Odersky](https://users.scala-lang.org/u/Odersky)\
**Post date:** [December 29, 2025, 6:30pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/7 "2025-12-29T18:30:24Z")

</div>

I think you need to start the repl like this for this to work:

```auto
scala --scala-version 3.nightly -language:experimental.captureChecking

```

I don’t think the `using //>` work in this case. But they should report an error if they don’t work.

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/nrinaudo/32/2236_2.png) [@nrinaudo](https://users.scala-lang.org/u/nrinaudo)\
**Post date:** [December 29, 2025, 8:42pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/8 "2025-12-29T20:42:19Z")

</div>

After various tests, it doesn’t appear to make a difference whether I use the `//>…` bits or the command line you suggested. However, I think I made a mistake when testing the nightly this morning.

Trying again tonight:

- with 3.8.0-RC3: the problem is still the same.
- with 3.nightly, I get a different problem.

To remove potential ambiguities, here’s the exact code I run:

```scala
import caps.*

trait Rand extends SharedCapability:
  def range(min: Int, max: Int): Int

object Rand:

  def nextInt(max: Int): Rand ?-> Int =
    r ?=> r.range(0, max)

  def oneOf[A](head: Rand ?=> A, tail: (Rand ?=> A)*): Rand ?->{head, tail*} A =
    val all: Seq[Rand ?->{head, tail*} A] = head +: tail

    all(nextInt(all.length))

```

This is invoked with:

```auto
scala --scala-version 3.nightly -language:experimental.captureChecking rep.scala

```

The error I get is:

```auto
Compiling project (Scala 3.8.1-RC1-bin-20251229-e73ff2c-NIGHTLY, JVM (21))
[error] ./rep.scala:14:5
[error] Local reach capability tail* leaks into capture scope of method oneOf.
[error] You could try to abstract the capabilities referred to by tail* in a capset variable.
[error] all(nextInt(all.length))
[error] ^^^^^^^^^^^^^^^^^^^^^^^^

```

This is a little confusing, because if I remove the `tail*` bit of the capture set of `oneOf`, I get:

```auto
Compiling project (Scala 3.8.1-RC1-bin-20251229-e73ff2c-NIGHTLY, JVM (21))
[error] ./rep.scala:12:5
[error] Found: (contextual$1: Rand^'s1) ?->{head, tail*} A^'s2
[error] Required: (Rand^) ?->{head} A
[error] 
[error] Note that capability tail* is not included in capture set {head}.
[error] 
[error] where: ^ refers to the universal root capability

```

Which feels a little like the compiler is contracting itself - if I don’t include `tail*`, it complains it’s not there. If I do include it, it complains it’s leaking (which, I’m not entirely sure what that means here).

Is there anything I can do to help decide whether this is a bug or slightly confusing error messages? I’m sure that test case can be reduced a little more, for example.

---

<div class="post-metadata">

**Author:** ![Odersky](https://users.scala-lang.org/user_avatar/users.scala-lang.org/odersky/32/3160_2.png) [@Odersky](https://users.scala-lang.org/u/Odersky)\
**Post date:** [December 30, 2025, 3:02pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/9 "2025-12-30T15:02:22Z")

</div>

The error messages do give you a bit of a run-around. They are both correct, but not helpful since the obvious fix to the type mismatch causes the leakage error for the reach capability. I tried in [Warn if reach capability in result will likely cause leakage by odersky · Pull Request #24860 · scala/scala3 · GitHub](https://github.com/scala/scala3/pull/24860) to give you a warning at the point where you try toi add the reach capability to the result. That now produces the following for your example:

```auto
-- Warning: tests/neg-custom-args/captures/nicolas2.scala:9:53 ---------------------------------------------------------
9 |def oneOf[A](head: Rand ?=> A, tail: (Rand ?=> A)*): Rand ?->{head, tail*} A =
  | ^^^^^^^^^^^^^^^^^^^^^^^
  | Reach capability tail* in function result refers to parameter tail.
  | To avoid errors of the form "Local reach capability tail* leaks into capture scope ..."
  | you should replace the reach capability with a new capset variable in method oneOf.
-- Error: tests/neg-custom-args/captures/nicolas2.scala:11:5 -----------------------------------------------------------
11 | all(nextInt(all.length)) // error
   | ^^^^^^^^^^^^^^^^^^^^^^^^
   | Local reach capability tail* leaks into capture scope of method oneOf.
   | You could try to abstract the capabilities referred to by tail* in a capset variable.

```

The correct way to fix the example is:

```scala
import caps.*

trait Rand extends SharedCapability:
  def range(min: Int, max: Int): Int

def nextInt(max: Int): Rand ?-> Int =
  r ?=> r.range(0, max)

def oneOf[A, B^](head: Rand ?=> A, tail: (Rand ?->{B} A)*): Rand ?->{head, B} A =
  val all: Seq[Rand ?->{head, B} A] = head +: tail // error
  all(nextInt(all.length))

```

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/nrinaudo/32/2236_2.png) [@nrinaudo](https://users.scala-lang.org/u/nrinaudo)\
**Post date:** [December 30, 2025, 3:20pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/10 "2025-12-30T15:20:13Z")

</div>

Oh I see. I clearly misunderstood reach capabilities (and misunderstand them still!) because I honestly thought that, in this context, `tail*` was equivalent to your `B`.

Another thing I need to understand is how this actually ends up doing the right thing. I assumed it would get it wrong, but:

```scala
val i1: Rand ?->{a1} Int = ...
val i2: Rand ?->{a2} Int = ...
val i3: Rand ?->{a3} Int = ...

oneOf(i1, i2, i3) // correctly has type Rand ?->{i1, i2, i3} Int

```

Intuitively, `B` is inferred to be the union of the capture sets of all the elements of `tail`, but I’m not entirely sure how…

---

<div class="post-metadata">

**Author:** ![Odersky](https://users.scala-lang.org/user_avatar/users.scala-lang.org/odersky/32/3160_2.png) [@Odersky](https://users.scala-lang.org/u/Odersky)\
**Post date:** [December 30, 2025, 3:49pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/11 "2025-12-30T15:49:18Z")

</div>

> [@nrinaudo](#):
>
> Oh I see. I clearly misunderstood reach capabilities (and misunderstand them still!) because I honestly thought that, in this context, `tail*` was equivalent to your `B`.

Yes, that would be nice, but unfortunately there are some fine distinctions that we need to make for soundness. The issue arises when we would call `oneOf` in a lambda like this:

```scala
val xs: List[A^{b, c}] = List(b, c)
() => oneOf(a, xs*)

```

Do we charge the deep capture set of the type of `xs*` to the closure, which would be {b, c}, or just the capture set, which would be empty? I.e. Should this expression have type `() ->{a} A` or `() ->{a, b, c} A`? The answer is that we always charge the capture set, not the deep set, so it would be `() ->{a} A`. But that means we cannot access a reach capability of a parameter unless we immediately box it again.

If you replace the reach capability with a capset variable `B` it’s the other way round. We always charge the deep capture set of the type argument passed to `B`, so referring to `B` anywhere in the function result is sound.

This is admittedly very tricky, and the best thiung to do here is follow the compiler’s advice. Maybe we should simply drop reach capabilities and always rely on capset variables for these cases. Then these issues would not arise, but we’d get some expressiveness issues elsewhere.

> [@nrinaudo](#):
>
> Intuitively, `B` is inferred to be the union of the capture sets of all the elements of `tail`, but I’m not entirely sure how…

Yes, that’s correct. Inference is done with a constraint solver that effectively unions the argument sets passed into a capset variable.

---

<div class="post-metadata">

**Author:** ![nrinaudo](https://users.scala-lang.org/user_avatar/users.scala-lang.org/nrinaudo/32/2236_2.png) [@nrinaudo](https://users.scala-lang.org/u/nrinaudo)\
**Post date:** [December 30, 2025, 4:12pm UTC](https://users.scala-lang.org/t/capture-leak-when-using-varargs/12086/12 "2025-12-30T16:12:48Z")

</div>

Thanks for taking the time for this thorough explanation, it’s very much appreciated.

Yes, this is a little tricky, but that might be something that solves itself with conventions. If the convention becomes to use capset variables rather than reach capabilities, most people won’t even be aware of this difficulty.
