Skip to content

Commit 7c4e3a9

Browse files
committed
Make tests variance aware
1 parent 1223ea2 commit 7c4e3a9

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

tests/neg/safeThrowsStrawman.check

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
| The capability to throw exception scalax.Fail is missing.
55
| The capability can be provided by one of the following:
66
| - A using clause `(using CanThrow[scalax.Fail])`
7-
| - A throws clause in a result type `X throws scalax.Fail`
7+
| - A throws clause in a result type such as `X throws scalax.Fail`
88
| - an enclosing `try` that catches scalax.Fail
9+
-- Error: tests/neg/safeThrowsStrawman.scala:27:15 ---------------------------------------------------------------------
10+
27 | println(bar) // error
11+
| ^
12+
| The capability to throw exception Exception is missing.
13+
| The capability can be provided by one of the following:
14+
| - A using clause `(using CanThrow[Exception])`
15+
| - A throws clause in a result type such as `X throws Exception`
16+
| - an enclosing `try` that catches Exception

tests/neg/safeThrowsStrawman.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import language.experimental.erasedTerms
22
import annotation.implicitNotFound
33

44
object scalax:
5-
@implicitNotFound("The capability to throw exception ${E} is missing.\nThe capability can be provided by one of the following:\n - A using clause `(using CanThrow[${E}])`\n - A throws clause in a result type `X throws ${E}`\n - an enclosing `try` that catches ${E}")
6-
erased class CanThrow[E <: Exception]
5+
@implicitNotFound("The capability to throw exception ${E} is missing.\nThe capability can be provided by one of the following:\n - A using clause `(using CanThrow[${E}])`\n - A throws clause in a result type such as `X throws ${E}`\n - an enclosing `try` that catches ${E}")
6+
erased class CanThrow[-E <: Exception]
77

8-
infix type throws[R, E <: Exception] = CanThrow[E] ?=> R
8+
infix type throws[R, +E <: Exception] = CanThrow[E] ?=> R
99

1010
class Fail extends Exception
1111

@@ -16,10 +16,14 @@ import scalax._
1616
def foo(x: Boolean): Int =
1717
if x then 1 else raise(Fail()) // error
1818

19+
def bar: Int throws Exception =
20+
raise(Fail())
21+
1922
@main def Test =
2023
try
2124
erased given CanThrow[Fail] = ???
2225
println(foo(true))
2326
println(foo(false))
27+
println(bar) // error
2428
catch case ex: Fail =>
2529
println("failed")

tests/run/safeThrowsStrawman.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import language.experimental.erasedTerms
22

33
object scalax:
4-
erased class CanThrow[E <: Exception]
4+
erased class CanThrow[-E <: Exception]
55

6-
infix type throws[R, E <: Exception] = CanThrow[E] ?=> R
6+
infix type throws[R, +E <: Exception] = CanThrow[E] ?=> R
77

88
class Fail extends Exception
99

@@ -14,8 +14,8 @@ import scalax._
1414
def foo(x: Boolean): Int throws Fail =
1515
if x then 1 else raise(Fail())
1616

17-
def bar(x: Boolean)(using CanThrow[Fail]): Int =
18-
if x then 1 else raise(Fail())
17+
def bar(x: Boolean)(using CanThrow[Fail]): Int = foo(x)
18+
def baz: Int throws Exception = foo(false)
1919

2020
@main def Test =
2121
try
@@ -24,3 +24,8 @@ def bar(x: Boolean)(using CanThrow[Fail]): Int =
2424
println(foo(false))
2525
catch case ex: Fail =>
2626
println("failed")
27+
try
28+
given CanThrow[Exception] = ???
29+
println(baz)
30+
catch case ex: Fail =>
31+
println("failed")

0 commit comments

Comments
 (0)