|
| 1 | +import language.experimental.captureChecking |
| 2 | +import language.experimental.separationChecking |
| 3 | +import caps.cap |
| 4 | +import scala.caps.Mutable |
| 5 | + |
| 6 | +// Create a deeper nesting structure |
| 7 | +class D() |
| 8 | +class C(val d: D^) |
| 9 | +class B(val c: C^) |
| 10 | +class A(consume val b: B^) extends Mutable: |
| 11 | + update def use() = println("Using A") |
| 12 | + |
| 13 | +// Test 1: Accessing nested fields through a consumed path |
| 14 | +def testNestedFieldsAfterConsume = |
| 15 | + val d: D^ = D() |
| 16 | + val c: C^ = C(d) |
| 17 | + val b: B^ = B(c) |
| 18 | + val a = A(b) |
| 19 | + |
| 20 | + // After a consumed b, we should be able to access: |
| 21 | + println(a.b) // OK - a owns b |
| 22 | + println(a.b.c) // OK - accessing field of consumed b through a |
| 23 | + println(a.b.c.d) // OK - deeper nesting through consumed path |
| 24 | + |
| 25 | +// Test 2: Non-trivial prefix accessing a consumed field |
| 26 | +class Container(consume val a: A^) extends Mutable: |
| 27 | + val other: A^ = A(B(C(D()))) |
| 28 | + update def operate() = other.use() |
| 29 | + |
| 30 | +class Outer(consume val container: Container^) extends Mutable: |
| 31 | + update def execute() = container.operate() |
| 32 | + |
| 33 | +def testComplexPrefix = |
| 34 | + val d1: D^ = D() |
| 35 | + val c1: C^ = C(d1) |
| 36 | + val b1: B^ = B(c1) |
| 37 | + val a1 = A(b1) |
| 38 | + val container1 = Container(a1) |
| 39 | + val outer = Outer(container1) |
| 40 | + |
| 41 | + // Non-trivial prefix: outer.container.a (where 'a' was consumed by container) |
| 42 | + println(outer.container) // OK - outer consumed container |
| 43 | + println(outer.container.a) // OK - accessing consumed field through prefix |
| 44 | + println(outer.container.a.b) // OK - and then its nested fields |
| 45 | + println(outer.container.a.b.c) // OK - even deeper |
| 46 | + println(outer.container.a.b.c.d) // OK - deepest level |
| 47 | + |
| 48 | + println(container1) // error |
| 49 | + println(a1) // error |
| 50 | + |
| 51 | +// Test 3: Multiple consume parameters with nested access |
| 52 | +class Multi(consume val b1: B^, consume val b2: B^) extends Mutable: |
| 53 | + val b3: B^ = B(C(D())) |
| 54 | + update def combine() = () |
| 55 | + |
| 56 | +def testMultipleConsume = |
| 57 | + val b1: B^ = B(C(D())) |
| 58 | + val b2: B^ = B(C(D())) |
| 59 | + val multi = Multi(b1, b2) |
| 60 | + |
| 61 | + // All of these should work: |
| 62 | + println(multi.b1) // OK |
| 63 | + println(multi.b1.c) // OK - nested field of consumed b1 |
| 64 | + println(multi.b1.c.d) // OK - deeper nesting |
| 65 | + println(multi.b2) // OK |
| 66 | + println(multi.b2.c) // OK - nested field of consumed b2 |
| 67 | + println(multi.b2.c.d) // OK - deeper nesting |
| 68 | + println(multi.b3.c.d) // OK - non-consumed field |
| 69 | + |
| 70 | + println(b1) // error |
| 71 | + println(b2) // error |
| 72 | + |
| 73 | +// Test 4: Consume at multiple levels with complex paths |
| 74 | +class Top(consume val outer: Outer^) extends Mutable: |
| 75 | + update def topAction() = outer.execute() |
| 76 | + |
| 77 | +def testMultiLevelConsume = |
| 78 | + val d2: D^ = D() |
| 79 | + val c2: C^ = C(d2) |
| 80 | + val b2: B^ = B(c2) |
| 81 | + val a2 = A(b2) |
| 82 | + val container2 = Container(a2) |
| 83 | + val outer2 = Outer(container2) |
| 84 | + val top = Top(outer2) |
| 85 | + |
| 86 | + // Very deep path through multiple consume levels: |
| 87 | + println(top.outer) // OK - top consumed outer |
| 88 | + println(top.outer.container) // OK - continue through path |
| 89 | + println(top.outer.container.a) // OK - container consumed a |
| 90 | + println(top.outer.container.a.b) // OK - a consumed b |
| 91 | + println(top.outer.container.a.b.c) // OK - nested field |
| 92 | + println(top.outer.container.a.b.c.d) // OK - deepest field |
| 93 | + |
| 94 | + println(a2) // error |
| 95 | + println(container2) // error |
| 96 | + println(outer2) // error |
| 97 | + println(top) // ok |
0 commit comments