Skip to content

Fix #23223: Add handling for classes deriving from Capability during Setup #23248

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

Merged
merged 4 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/cc/Setup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,12 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
// Infer the self type for the rest, which is all classes without explicit
// self types (to which we also add nested module classes), provided they are
// neither pure, nor are publicily extensible with an unconstrained self type.
CapturingType(cinfo.selfType, CaptureSet.Var(cls, level = ccState.currentLevel))
val cs = CaptureSet.Var(cls, level = ccState.currentLevel)
if cls.derivesFrom(defn.Caps_Capability) then
// If cls is a capability class, we need to add a fresh readonly capability to
// ensure we cannot treat the class as pure.
CaptureSet.fresh(Origin.InDecl(cls)).readOnly.subCaptures(cs)
CapturingType(cinfo.selfType, cs)

// Compute new parent types
val ps1 = inContext(ctx.withOwner(cls)):
Expand Down
12 changes: 12 additions & 0 deletions tests/neg-custom-args/captures/i23223.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import language.experimental.captureChecking
import caps.*

class A:
def a: A = this

class B extends A, Capability // error

def leak(b: B): A = b.a

class C extends Capability:
def c: C^{} = this // error
4 changes: 4 additions & 0 deletions tests/pos-custom-args/captures/i20237.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import language.experimental.captureChecking
import caps.*

class Cap extends caps.Capability:
def use[T](body: Cap ?=> T) = body(using this)

class Cap2 extends caps.Capability:
def use[T](body: Cap2 => T) = body(this)

class Box[T](body: Cap ?=> T):
inline def open(using cap: Cap) = cap.use(body)

Expand Down
Loading