File tree 3 files changed +35
-3
lines changed
compiler/src/dotty/tools/dotc/typer 3 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -1942,14 +1942,20 @@ class Typer extends Namer
1942
1942
}
1943
1943
var checkedArgs = preCheckKinds(args1, paramBounds)
1944
1944
// check that arguments conform to bounds is done in phase PostTyper
1945
- if (tpt1.symbol == defn.andType)
1945
+ val tycon = tpt1.symbol
1946
+ if (tycon == defn.andType)
1946
1947
checkedArgs = checkedArgs.mapconserve(arg =>
1947
1948
checkSimpleKinded(checkNoWildcard(arg)))
1948
- else if (tpt1.symbol == defn.orType)
1949
+ else if (tycon == defn.orType)
1949
1950
checkedArgs = checkedArgs.mapconserve(arg =>
1950
1951
checkSimpleKinded(checkNoWildcard(arg)))
1952
+ else if tycon == defn.throwsAlias
1953
+ && checkedArgs.length == 2
1954
+ && checkedArgs(1 ).tpe.derivesFrom(defn.RuntimeExceptionClass )
1955
+ then
1956
+ report.error(em " throws clause cannot be defined for RuntimeException " , checkedArgs(1 ).srcPos)
1951
1957
else if (ctx.isJava)
1952
- if (tpt1.symbol eq defn.ArrayClass ) then
1958
+ if tycon eq defn.ArrayClass then
1953
1959
checkedArgs match {
1954
1960
case List (arg) =>
1955
1961
val elemtp = arg.tpe.translateJavaArrayElementType
Original file line number Diff line number Diff line change
1
+ -- Error: tests/neg/i13846.scala:3:22 ----------------------------------------------------------------------------------
2
+ 3 |def foo(): Int throws ArithmeticException = 1 / 0 // error
3
+ | ^^^^^^^^^^^^^^^^^^^
4
+ | throws clause cannot be defined for RuntimeException
5
+ -- Error: tests/neg/i13846.scala:7:9 -----------------------------------------------------------------------------------
6
+ 7 | foo() // error
7
+ | ^
8
+ | The capability to throw exception ArithmeticException is missing.
9
+ | The capability can be provided by one of the following:
10
+ | - A using clause `(using CanThrow[ArithmeticException])`
11
+ | - A `throws` clause in a result type such as `X throws ArithmeticException`
12
+ | - an enclosing `try` that catches ArithmeticException
13
+ |
14
+ | The following import might fix the problem:
15
+ |
16
+ | import unsafeExceptions.canThrowAny
17
+ |
Original file line number Diff line number Diff line change
1
+ import language .experimental .saferExceptions
2
+
3
+ def foo (): Int throws ArithmeticException = 1 / 0 // error
4
+
5
+ def test (): Unit =
6
+ try
7
+ foo() // error
8
+ catch
9
+ case _ : ArithmeticException => println(" Caught" )
You can’t perform that action at this time.
0 commit comments