From e22744de00e65fa33622f3cf09b09b4a0f8dd174 Mon Sep 17 00:00:00 2001 From: odersky Date: Sat, 4 Nov 2023 10:54:57 +0100 Subject: [PATCH] Add sealed refs test and fix check files of other tests --- tests/neg-custom-args/captures/cc-this.check | 2 +- tests/neg-custom-args/captures/cc-this2.check | 14 +++++-- .../captures/cc-this2/D_2.scala | 2 +- .../captures/exception-definitions.check | 9 ++-- .../captures/exception-definitions.scala | 4 +- .../captures/sealed-refs.scala | 42 +++++++++++++++++++ 6 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 tests/neg-custom-args/captures/sealed-refs.scala diff --git a/tests/neg-custom-args/captures/cc-this.check b/tests/neg-custom-args/captures/cc-this.check index 335302c5c259..070e815d6d45 100644 --- a/tests/neg-custom-args/captures/cc-this.check +++ b/tests/neg-custom-args/captures/cc-this.check @@ -12,4 +12,4 @@ -- Error: tests/neg-custom-args/captures/cc-this.scala:17:8 ------------------------------------------------------------ 17 | class C4(val f: () => Int) extends C3 // error | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | reference (C4.this.f : () => Int) is not included in the allowed capture set {} of pure base class class C3 + |reference (C4.this.f : () => Int) captured by this self type is not included in the allowed capture set {} of pure base class class C3 diff --git a/tests/neg-custom-args/captures/cc-this2.check b/tests/neg-custom-args/captures/cc-this2.check index 5e43a45b67f5..bd9a1085d262 100644 --- a/tests/neg-custom-args/captures/cc-this2.check +++ b/tests/neg-custom-args/captures/cc-this2.check @@ -1,6 +1,12 @@ --- Error: tests/neg-custom-args/captures/cc-this2/D_2.scala:2:6 -------------------------------------------------------- +-- Error: tests/neg-custom-args/captures/cc-this2/D_2.scala:3:8 -------------------------------------------------------- +3 | this: D^ => // error + | ^^ + |reference (caps.cap : caps.Cap) captured by this self type is not included in the allowed capture set {} of pure base class class C +-- [E058] Type Mismatch Error: tests/neg-custom-args/captures/cc-this2/D_2.scala:2:6 ----------------------------------- 2 |class D extends C: // error - |^ - |reference (caps.cap : caps.Cap) is not included in the allowed capture set {} of pure base class class C -3 | this: D^ => + | ^ + | illegal inheritance: self type D^ of class D does not conform to self type C + | of parent class C + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg-custom-args/captures/cc-this2/D_2.scala b/tests/neg-custom-args/captures/cc-this2/D_2.scala index b22e5e456092..de1a722f73a9 100644 --- a/tests/neg-custom-args/captures/cc-this2/D_2.scala +++ b/tests/neg-custom-args/captures/cc-this2/D_2.scala @@ -1,3 +1,3 @@ class D extends C: // error - this: D^ => + this: D^ => // error diff --git a/tests/neg-custom-args/captures/exception-definitions.check b/tests/neg-custom-args/captures/exception-definitions.check index 4b1fe0273f52..72b88f252e59 100644 --- a/tests/neg-custom-args/captures/exception-definitions.check +++ b/tests/neg-custom-args/captures/exception-definitions.check @@ -1,8 +1,7 @@ --- Error: tests/neg-custom-args/captures/exception-definitions.scala:2:6 ----------------------------------------------- -2 |class Err extends Exception: // error - |^ - |reference (caps.cap : caps.Cap) is not included in the allowed capture set {} of pure base class class Throwable -3 | self: Err^ => +-- Error: tests/neg-custom-args/captures/exception-definitions.scala:3:8 ----------------------------------------------- +3 | self: Err^ => // error + | ^^^^ + |reference (caps.cap : caps.Cap) captured by this self type is not included in the allowed capture set {} of pure base class class Throwable -- Error: tests/neg-custom-args/captures/exception-definitions.scala:7:12 ---------------------------------------------- 7 | val x = c // error | ^ diff --git a/tests/neg-custom-args/captures/exception-definitions.scala b/tests/neg-custom-args/captures/exception-definitions.scala index a19b751825b8..fbc9f3fd1d33 100644 --- a/tests/neg-custom-args/captures/exception-definitions.scala +++ b/tests/neg-custom-args/captures/exception-definitions.scala @@ -1,6 +1,6 @@ -class Err extends Exception: // error - self: Err^ => +class Err extends Exception: + self: Err^ => // error def test(c: Any^) = class Err2 extends Exception: diff --git a/tests/neg-custom-args/captures/sealed-refs.scala b/tests/neg-custom-args/captures/sealed-refs.scala new file mode 100644 index 000000000000..05fa483acf28 --- /dev/null +++ b/tests/neg-custom-args/captures/sealed-refs.scala @@ -0,0 +1,42 @@ +class Ref[sealed A](init: A): + this: Ref[A]^ => + private var x: A = init + def get: A = x + def set(x: A): Unit = this.x = x + +class It[X]: + this: It[X]^ => + +def f1[B1](x: B1, next: B1 -> B1) = + var r = x // ok + r = next(x) + r + +def f2[B2](x: B2, next: B2 -> B2) = + val r = Ref[B2](x) // error + r.set(next(x)) + r.get + +def g[sealed B](x: B, next: B -> B) = + val r = Ref[B](x) // ok + r.set(next(x)) + r.get + +import annotation.unchecked.uncheckedCaptures + +def h[B](x: B, next: B -> B) = + val r = Ref[B @uncheckedCaptures](x) // ok + r.set(next(x)) + r.get + +def f3[B](x: B, next: B -> B) = + val r: Ref[B^{cap[f3]}] = Ref[B^{cap[f3]}](x) // error + r.set(next(x)) + val y = r.get + () + +def f4[B](x: B, next: B -> B) = + val r: Ref[B]^{cap[f4]} = Ref[B](x) // error + r.set(next(x)) + val y = r.get + () \ No newline at end of file