# Boundary / break and capture checking

**URL:** <https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036>\
**Category:** Question\
**Created:** [September 13, 2025, 10:25am UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036 "2025-09-13T10:25:45Z")\
**Posts on this page:** 6\
**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:** [September 13, 2025, 10:25am UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036/1 "2025-09-13T10:25:45Z")

</div>

I’ve been looking at the code of `boundary` and have a capture-checking related question.

`Label` extends `Capability`, which is perfectly reasonable: it cannot be allowed to escape, otherwise we might end up using it in a call to `break` outside of the `try / catch` block that knows to catch it.

This makes `Break` a little problematic, because of course `Break` captures the associated `Label`. Is this why the label field is defined as `Label[T]^{}`? I’m reading this rather naively and interpreting it as _even though `Label` always has `cap` in its capture set, this overwrites it to be empty_.

Which takes us to creating `Break` values: again, problematic, because how do you pass an untracked `Label` to `Break`, since `Label` is always tracked? and the code answers this by calling `unsafeAssumePure`, so is it correct to assume that this drops the capture set _even of types who are declared as always having a non-empty capture set_ like `Label`?

This unsafe calls has comment:

```scala
// SAFETY: labels cannot leak from [[Break]] [...]

```

Why? Neither `Label` nor `Break` are flagged as private, what stops me from manually catching `Break`, extracting the `Label`, and returning that?

---

<div class="post-metadata">

**Author:** ![sageserpent-open](https://users.scala-lang.org/user_avatar/users.scala-lang.org/sageserpent-open/32/2576_2.png) [@sageserpent-open](https://users.scala-lang.org/u/sageserpent-open)\
**Post date:** [September 15, 2025, 7:19am UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036/2 "2025-09-15T07:19:30Z")

</div>

Are you trying to implement `longjmp` here?

Why would a nice fellow want to do that? 😼

Sorry, way above my level, my advice is to leave it alone, maybe it’s an implementation wart?

---

<div class="post-metadata">

**Author:** ![natsukagami](https://users.scala-lang.org/user_avatar/users.scala-lang.org/natsukagami/32/3374_2.png) [@natsukagami](https://users.scala-lang.org/u/natsukagami)\
**Post date:** [September 15, 2025, 8:25pm UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036/3 "2025-09-15T20:25:13Z")

</div>

The reason the label field was left public was because of source compatibility: it was always public. We could swap it with another Break class, but due to inlining I don’t think we can leave the old class as it would always be vulnerable. The original proposal was to mark it private[boundary], and only allow comparison between labels be exposed (which is the purpose of the new sameLabelAs method).

Perhaps a comment should be added to note that extracting the label is unsafe.

Another piece of the puzzle here was from Break being an exception: we enforce Java types to be pure for interoperability, and that includes jl.Exception itself. Therefore it is a constraint on the library design itself to keep Break pure and enforce label-safety purely through scoping.

---

<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:** [September 15, 2025, 9:59pm UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036/4 "2025-09-15T21:59:23Z")

</div>

Thanks for that answer, it’s clear and very useful.

Out of curiosity, what was the alternative? Could you have made Break capture its label (assuming compatibility was not an issue)? Break can be made private and an implementation detail that no one could interact with, so arguably, making it pure is not terribly important (again, removing the compatibility constraint for the sake of argument).

I tried that but couldn’t work out make Break capture its label, the compiler error mentions self types and i didn’t really know what to do with that…

For example:

```scala
private case class Break[A](label: Label[A], value: A)
  extends ControlThrowable with NoStackTrace

```

Yields:

```auto
reference (Break.this.label : Label[A]) is not included in the allowed capture set {} of the self type of class Break

```

I tried fixing that with the obvious, naive interpretation of the error message:

```scala
private case class Break[A](label: Label[A], value: A) 
  extends ControlThrowable with NoStackTrace:
    self: Break[A]^{label} =>

```

But this then fails with:

```auto
Break[A] is a pure type, it makes no sense to add a capture set to it

```

And I have to admit I’m a little at a loss at what to do at this point.

---

<div class="post-metadata">

**Author:** ![natsukagami](https://users.scala-lang.org/user_avatar/users.scala-lang.org/natsukagami/32/3374_2.png) [@natsukagami](https://users.scala-lang.org/u/natsukagami)\
**Post date:** [September 16, 2025, 10:56am UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036/5 "2025-09-16T10:56:24Z")

</div>

> [@nrinaudo](#):
>
> ```auto
> private case class Break[A](label: Label[A], value: A)
> extends ControlThrowable
> 
> ```

Unfortunately the error lies already in the `extends ControlThrowable`, which has a lineage of `ControlThrowable <: java.lang.Throwable`. At the moment the compiler enforces every Java class (aside from Object itself) to have a pure self-type, i.e. cannot hold any tracked references. So for now that means you cannot throw tracked exceptions…

---

<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:** [September 16, 2025, 3:06pm UTC](https://users.scala-lang.org/t/boundary-break-and-capture-checking/12036/6 "2025-09-16T15:06:44Z")

</div>

Oh i see, i hadn’t realised this was enforced by the compiler! Thank you, that clarifies everything.

I’m a little curious though - shouldn’t everything Java be considered impure rather than pure by default? I would assume the decision was for backwards compatibility reasons, but there’s not any backwards compatibility to worry about on capture checking (yet).
