Skip to content

Tighten the screws a bit more to seal the soundness hole for reach capabilities #20056

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 2 commits into from
Apr 5, 2024
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/cc/CaptureOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ extension (tp: Type)
var ok = true
def traverse(t: Type): Unit =
if ok then
t match
t.dealias match
case CapturingType(_, cs) if cs.isUniversal && variance <= 0 =>
ok = false
case _ =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class CheckCaptures extends Recheck, SymTransformer:
var refVariances: Map[Boolean, Int] = Map.empty
var seenReach: CaptureRef | Null = null
def traverse(tp: Type) =
tp match
tp.dealias match
case CapturingType(parent, refs) =>
traverse(parent)
for ref <- refs.elems do
Expand Down
5 changes: 5 additions & 0 deletions tests/neg/unsound-reach-4.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Error: tests/neg/unsound-reach-4.scala:20:19 ------------------------------------------------------------------------
20 | escaped = boom.use(f) // error
| ^^^^^^^^
| Reach capability backdoor* and universal capability cap cannot both
| appear in the type (x: F): box File^{backdoor*} of this expression
20 changes: 20 additions & 0 deletions tests/neg/unsound-reach-4.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import language.experimental.captureChecking
trait File:
def close(): Unit

def withFile[R](path: String)(op: File^ => R): R = ???

type F = File^

trait Foo[+X]:
def use(x: F): X
class Bar extends Foo[File^]:
def use(x: F): File^ = x

def bad(): Unit =
val backdoor: Foo[File^] = new Bar
val boom: Foo[File^{backdoor*}] = backdoor

var escaped: File^{backdoor*} = null
withFile("hello.txt"): f =>
escaped = boom.use(f) // error