Skip to content

Better error reporting for box adaptation failures #21675

Open
@Linyxus

Description

@Linyxus

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.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions