From 7576fbe34e7b8a26774bdea46cb604f8c0cea1e9 Mon Sep 17 00:00:00 2001 From: odersky Date: Sat, 22 Apr 2023 13:21:57 +0200 Subject: [PATCH] Some additions and changes in tests --- tests/neg-custom-args/captures/filevar.scala | 18 +++++++++ tests/pending/pos/i16826.scala | 10 +++++ tests/pos-custom-args/captures/filevar.scala | 37 +++++++++++++++++++ .../captures/nested-classes.scala | 21 +++++++++++ tests/run/errorhandling/Result.scala | 4 +- 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 tests/neg-custom-args/captures/filevar.scala create mode 100644 tests/pending/pos/i16826.scala create mode 100644 tests/pos-custom-args/captures/filevar.scala create mode 100644 tests/pos-custom-args/captures/nested-classes.scala diff --git a/tests/neg-custom-args/captures/filevar.scala b/tests/neg-custom-args/captures/filevar.scala new file mode 100644 index 000000000000..9251be8f1592 --- /dev/null +++ b/tests/neg-custom-args/captures/filevar.scala @@ -0,0 +1,18 @@ +import language.experimental.captureChecking +import compiletime.uninitialized + +class File: + def write(x: String): Unit = ??? + +class Service: + var file: {*} File = uninitialized + def log = file.write("log") // error + +def withFile[T](op: (f: {*} File) => T): T = + op(new File) + +def test = + withFile: f => + val o = Service() + o.file = f + o.log diff --git a/tests/pending/pos/i16826.scala b/tests/pending/pos/i16826.scala new file mode 100644 index 000000000000..a938ab42dac3 --- /dev/null +++ b/tests/pending/pos/i16826.scala @@ -0,0 +1,10 @@ +import language.experimental.captureChecking +class A +class B(a: {*} A) +class C(a: {*} A): + def setB(b: {a} B): Unit = ??? + + +def test(a1: {*} A)(b1: {a1} B) = + val c = new C(a1) + c.setB(b1) diff --git a/tests/pos-custom-args/captures/filevar.scala b/tests/pos-custom-args/captures/filevar.scala new file mode 100644 index 000000000000..17a8281c4d29 --- /dev/null +++ b/tests/pos-custom-args/captures/filevar.scala @@ -0,0 +1,37 @@ +import language.experimental.captureChecking +import annotation.capability +import compiletime.uninitialized + +object test1: + class File: + def write(x: String): Unit = ??? + + class Service(f: {*} File): + def log = f.write("log") + + def withFile[T](op: (f: {*} File) => T): T = + op(new File) + + def test = + withFile: f => + val o = Service(f) + o.log + +object test2: + @capability class IO + + class File: + def write(x: String): Unit = ??? + + class Service(io: IO): + var file: {io} File = uninitialized + def log = file.write("log") + + def withFile[T](io: IO)(op: (f: {io} File) => T): T = + op(new File) + + def test(io: IO) = + withFile(io): f => + val o = Service(io) + o.file = f + o.log diff --git a/tests/pos-custom-args/captures/nested-classes.scala b/tests/pos-custom-args/captures/nested-classes.scala new file mode 100644 index 000000000000..2e893b7413a5 --- /dev/null +++ b/tests/pos-custom-args/captures/nested-classes.scala @@ -0,0 +1,21 @@ +import language.experimental.captureChecking +import annotation.{capability, constructorOnly} + +@capability class IO +class Blah +class Pkg(using @constructorOnly io: IO): + class Foo: + def m(foo: {io} Blah) = ??? +class Pkg2(using io: IO): + class Foo: + def m(foo: {io} Blah): Any = io; ??? + +def main(using io: IO) = + val pkg = Pkg() + val f = pkg.Foo() + f.m(???) + val pkg2 = Pkg2() + val f2 = pkg2.Foo() + f2.m(???) + + diff --git a/tests/run/errorhandling/Result.scala b/tests/run/errorhandling/Result.scala index 027c07c86769..07d7a9f90c8a 100644 --- a/tests/run/errorhandling/Result.scala +++ b/tests/run/errorhandling/Result.scala @@ -29,14 +29,14 @@ object Result: case err: Err[_] => err /** Validate both `r` and `other`; return a pair of successes or a list of failures. */ - def * [U](other: Result[U, E]): Result[(T, U), List[E]] = (r, other) match + def zip[U](other: Result[U, E]): Result[(T, U), List[E]] = (r, other) match case (Ok(x), Ok(y)) => Ok((x, y)) case (Ok(_), Err(e)) => Err(e :: Nil) case (Err(e), Ok(_)) => Err(e :: Nil) case (Err(e1), Err(e2)) => Err(e1 :: e2 :: Nil) /** Validate both `r` and `other`; return a tuple of successes or a list of failures. - * Unlike with `*`, the right hand side `other` must be a `Result` returning a `Tuple`, + * Unlike with `zip`, the right hand side `other` must be a `Result` returning a `Tuple`, * and the left hand side is added to it. See `Result.empty` for a convenient * right unit of chains of `*:`s. */