Skip to content

CC: Objects with tracked members appear pure #23850

@bracevac

Description

@bracevac

Compiler version

3.8 nightly

Minimized code

//> using scala 3.nightly

import scala.language.experimental.captureChecking
import scala.caps.*

class Console() extends SharedCapability:
  def println(s: String): Unit = System.out.println(s"console: $s")

def log(s: String)(using Console): Unit =
  summon[Console].println(s)

object test:
  val addPure: (Int, Int) -> Int = (a, b) => a + b // same as before
  
  given Console = Console() // provide capability locally
  
  // and this is now considered pure somehow 
  val addWritesToConsole: (Int, Int) -> Int = (a, b) =>
    log(s"adding a ($a) to b ($b)")
    a + b

Output

The capture checker happily accepts this.

Expectation

It should reject! If we turn test into a def, we'll get:

-- [E007] Type Mismatch Error: local/ccimplicit.scala:18:46 --------------------
18 |  val addWritesToConsole: (Int, Int) -> Int = (a, b) =>
   |                                              ^
   |   Found:    (a: Int, b: Int) ->{given_Console} Int
   |   Required: (Int, Int) -> Int
   |
   |   Note that capability given_Console is not included in capture set {}.
19 |    log(s"adding a ($a) to b ($b)")
20 |    a + b
   |
   | longer explanation available when compiling with `-explain`

We need to improve handling of objects/classes/self types.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions