Skip to content
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

Better error reporting for box adaptation failures #21675

Open
Linyxus opened this issue Sep 30, 2024 · 1 comment
Open

Better error reporting for box adaptation failures #21675

Linyxus opened this issue Sep 30, 2024 · 1 comment
Assignees
Labels
area:experimental:cc Capture checking related itype:bug

Comments

@Linyxus
Copy link
Contributor

Linyxus commented Sep 30, 2024

Compiler version

main

Minimized code

import language.experimental.captureChecking

import scala.collection.mutable.ListBuffer

class MyContainer:
  private val consumers = ListBuffer.empty[() => Unit]

  private def execute(): Unit =
    for consumer <- consumers do
      consumer()

  // ... which, is equivalent to ...

  private def executeAlt(): Unit =
    consumers.foreach: (f: () => Unit) =>
      f()

Output

-- [E007] Type Mismatch Error: errmsg.scala:9:8 --------------------------------
 9 |    for consumer <- consumers do
   |        ^
   |Found:    (consumer: () => Unit) ->? Unit
   |Required: (consumer: box () => Unit) -><fluid> Unit
   |
   |Note that () => Unit cannot be box-converted to box () => Unit
   |since at least one of their capture sets contains the root capability `cap`
10 |      consumer()
   |
   | longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: errmsg.scala:15:23 ------------------------------
15 |    consumers.foreach: (f: () => Unit) =>
   |                       ^
   |Found:    (f: () => Unit) ->? Unit
   |Required: (f: box () => Unit) -><fluid> Unit
   |
   |Note that () => Unit cannot be box-converted to box () => Unit
   |since at least one of their capture sets contains the root capability `cap`
16 |      f()
   |
   | longer explanation available when compiling with `-explain`
2 errors found

Expectation

The crux of this error is that ListBuffer[box () => Unit] is mutable and therefore the type argument box () => Unit is invariant. Reach refinement is impossible, and the attempt to unbox its elements results in a box conversion error.

@Linyxus
Copy link
Contributor Author

Linyxus commented Sep 30, 2024

I am assuming that we do want to hide boxes completely from the user. In other words, boxes are internal constructs, and only for those who really curious about what is under the hood are details of boxes relevant.

The problem then becomes how these boxing errors can be translated to human-readable messages. It feels that the only thing that these boxing errors are really talking about is simply "scope violation". And it seems to be at least two kinds of scope violation issues:

  1. Functions returning unnamable capabilities
  2. Mutable states containing unnamable capabilities

Note that the second category extends beyond var definitions. For instance, this issue falls in this second category as well.

It will be interesting to see whether this two scenarios can be detected, and errors be emitted in a more human-readable manner than the status quo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:experimental:cc Capture checking related itype:bug
Projects
None yet
Development

No branches or pull requests

5 participants