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

CC unsoundness: TypeComparer.glb strips capture set #18246

Closed
Linyxus opened this issue Jul 19, 2023 · 1 comment · Fixed by #18254
Closed

CC unsoundness: TypeComparer.glb strips capture set #18246

Linyxus opened this issue Jul 19, 2023 · 1 comment · Fixed by #18254
Labels
cc-experiment Intended to be merged with cc-experiment branch on origin itype:bug
Milestone

Comments

@Linyxus
Copy link
Contributor

Linyxus commented Jul 19, 2023

Compiler version

main

Minimized code

import language.experimental.captureChecking
trait Cap
trait Foo[+T]

def magic[T](io: Cap^, x: Foo[T]^{io}): Foo[T]^{} =
  val x1: Foo[T]^{cap} & Foo[Any]^{io} = x
  val x2: Foo[T] = x1
  x2  // boom, an impure value becomes pure

Output

It compiles.

Expectation

It should be rejected.

Problematic glb trace:

==> glb(Foo[T]^, Foo[Any]^{io})?
  ==> glb(Foo[T], Foo[Any]^{io})?
    ==> glb(Foo[T], <notype>)?
    <== glb(Foo[T], <notype>) = Foo[T]
  <== glb(Foo[T], Foo[Any]^{io}) = Foo[T]
<== glb(Foo[T]^, Foo[Any]^{io}) = Foo[T]

Possible code location that causes this issue: https://github.com/lampepfl/dotty/blob/da16f434fa3aa74bcceacb04243f742a61f8b786/compiler/src/dotty/tools/dotc/core/TypeComparer.scala#L2603-L2609

@Linyxus Linyxus added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label cc-experiment Intended to be merged with cc-experiment branch on origin and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 19, 2023
Linyxus added a commit to dotty-staging/dotty that referenced this issue Jul 20, 2023
@Linyxus
Copy link
Contributor Author

Linyxus commented Jul 20, 2023

I attempted a fix in #18254. When working on the fix I was reasoning about the "glb" of two capture sets. Notably, unlike types (in DOT) which possess both glb (intersections) and lub (unions), capture sets have lubs (which are simply the set unions) but not always a glb. I believe this could lead to some interesting problems to be studied when developing the formalization combining DOT and CC. Besides, such a formalization enables us to have a more principled and solid understanding of these problems as well.

Linyxus added a commit that referenced this issue Jul 23, 2023
)

Intuitively, `glb(S1^C1, S2^C2)` should capture `C1 ⊓ C2`, where `⊓`
denotes the glb of capture sets. When computing `glb(tp1, tp2)`, if
`tp1` matches `CapturingType(parent1, refs1)`, it captures `C0 ++ refs1`
where `C0` is the nested capture sets inside `parent1`. Denote the
capture set of `tp2` with `C2`, the result should capture `(C0 ++ refs1)
⊓ C2`. Presumably, `⊓` distributes over the union (`++`); therefore `(C0
++ refs1) ⊓ C2` equals `C0 ⊓ C2 ++ refs1 ⊓ C2`.

Previously, if `C2` is a subcapture of `refs1`, it is simply dropped.
However, based on the above reasoning, we have `refs1 ⊓ C2` equals `C2`;
therefore `C0 ⊓ C2 ++ refs1 ⊓ C2` equals `C0 ⊓ C2 ++ C2`. `C2` should
not be dropped, but rather kept. This PR fixes the logic to behave
correctly.

Fixes #18246.
olhotak pushed a commit to dotty-staging/dotty that referenced this issue Jul 24, 2023
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cc-experiment Intended to be merged with cc-experiment branch on origin itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants