Skip to content

Commit

Permalink
add borrowing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraks committed Oct 21, 2023
1 parent a05a7fc commit 68bec5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/qualfsub/TypeCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def checkSubtypeOverlap(T: QType, S: QType)(using Γ: TEnv): Unit =
val QType(t2, q2) = S
if (t1.isSubtype(t2)) {
val sq1 = q1.sat
val sq2 = q2.sat
val sq2 = q2.sat // FIXME: should not compute saturated sat here
if (sq1.isSubqual(sq2)) ()
else throw NonOverlap(sq2 - Fresh(), sq1 \ sq2)
} else throw NotSubtype(t1, t2)()
Expand Down
24 changes: 24 additions & 0 deletions src/test/scala/diamond/Diamond.scala
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,28 @@ class DiamondTest extends AnyFunSuite {
(TForall("𝐹#2","A","a",TTop^ ◆,
TFun("𝑓#3","x",TVar("A")^"a",TVar("A")^"x")^"a")^()))
}

test("borrowing") {
val p1 = """
def borrow[A^a <: Top^<>, B^b <: Top^<>](x: A^a, block: ((A^a) => B^b)^<>) = { block(x) };
val x = Ref 42;
borrow[Ref[Int]^x, Int](x, (y: Ref[Int]^x) => { !y }) // allowed
"""
assert(parseAndCheck(p1) == (TNum^()))

val p2 = """
def borrow[A^a <: Top^<>, B^b <: Top^<>](x: A^a, block: ((A^a) => B^b)^<>) = { block(x) };
val x = Ref 42;
borrow[Ref[Int]^x, Int](x, (y: Ref[Int]^x) => { !x }) // not allowed
"""
intercept[NonOverlap] { parseAndCheck(p2) }

val p3 = """
def borrow[A^a <: Top^<>, B^b <: Top^<>](x: A^a, block: ((A^a) => B^b)^<>) = { block(x) };
val x = Ref 42;
val z = x;
borrow[Ref[Int]^x, Int](x, (y: Ref[Int]^x) => { !z }) // not allowed
"""
intercept[NonOverlap] { parseAndCheck(p3) }
}
}

0 comments on commit 68bec5d

Please sign in to comment.