Skip to content

Interaction between separation checking and pattern matching #22847

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

Open
Linyxus opened this issue Mar 20, 2025 · 2 comments
Open

Interaction between separation checking and pattern matching #22847

Linyxus opened this issue Mar 20, 2025 · 2 comments
Labels
area:experimental:cc Capture checking related itype:bug

Comments

@Linyxus
Copy link
Contributor

Linyxus commented Mar 20, 2025

Compiler version

main

Minimized code

import language.experimental.captureChecking
import caps.*
class Cell[T](init: T) extends Mutable:
  private var _data: T = init
  def get: T = _data
  mut def set(x: T) = _data = x
def swap[T](a: Cell[T]^, b: Cell[T]^): Unit =
  val t: T = a.get
  a.set(b.get)
  b.set(t)
def test1(a: Cell[Int]^): Unit =
  swap(a, a)  // error
  val b = a
  swap(a, b)  // error
  a match
    case a0: Cell[Int]^ =>
      swap(a, a0)  // should be error, but ok

Output

Two errors are emitted but the last line passed.

Expectation

The last line should be an error, since a and a0 are really aliases.

@Linyxus Linyxus added area:experimental:cc Capture checking related itype:bug labels Mar 20, 2025
@Linyxus
Copy link
Contributor Author

Linyxus commented Mar 20, 2025

I looked at the cc tree:

    def test1(a: Cell[Int]^): Unit =
      matchResult1[Unit]:
        {
          case val x1: (a : Cell[Int]^) = a
          if x1.$isInstanceOf[Cell[Int]^] then
            {
              case val a0: Cell[Int]^ = x1.$asInstanceOf[Cell[Int]^]
              return[matchResult1]
                {
                  swap[Int](a, a0)
                }
            }
           else ()
          throw new MatchError(x1)
        }

I think the cause is the $asInstanceOf inserted by pattern matching, which blindly widens x1 to Cell[Int]^ and bypasses the hidden set computation.

@noti0na1
Copy link
Member

We need #21828

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:experimental:cc Capture checking related itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants