Skip to content

Commit e3bd825

Browse files
committed
Allow (...) around throws alternatives
Fixes scala#13816
1 parent 29f0374 commit e3bd825

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,8 @@ object desugar {
12651265
* $throws[... $throws[A, E1] ... , En].
12661266
*/
12671267
def throws(tpt: Tree, op: Ident, excepts: Tree)(using Context): AppliedTypeTree = excepts match
1268+
case Parens(excepts1) =>
1269+
throws(tpt, op, excepts1)
12681270
case InfixOp(l, bar @ Ident(tpnme.raw.BAR), r) =>
12691271
throws(throws(tpt, op, l), bar, r)
12701272
case e =>

tests/pos/i13816.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import language.experimental.saferExceptions
2+
3+
class Ex1 extends Exception("Ex1")
4+
class Ex2 extends Exception("Ex2")
5+
6+
def foo1(i: Int): Unit throws Ex1 throws Ex2 =
7+
if i > 0 then throw new Ex1 else throw new Ex2
8+
9+
def foo2(i: Int): Unit throws Ex1 | Ex2 =
10+
if i > 0 then throw new Ex1 else throw new Ex2
11+
12+
def foo2a(i: Int): Unit throws (Ex1 | Ex2) =
13+
if i > 0 then throw new Ex1 else throw new Ex2
14+
15+
def foo3(i: Int)(using CanThrow[Ex1], CanThrow[Ex2]) =
16+
if i > 0 then throw new Ex1 else throw new Ex2
17+
18+
def foo4(i: Int)(using CanThrow[Ex1])(using CanThrow[Ex2]) =
19+
if i > 0 then throw new Ex1 else throw new Ex2
20+
21+
//def foo5(i: Int)(using CanThrow[Ex1 & Ex2]) = // does not work: no capability aggregation is supported
22+
// if i > 0 then throw new Ex1 else throw new Ex2
23+
24+
def test(): Unit =
25+
try {
26+
foo1(1)
27+
foo2(1)
28+
foo2a(1)
29+
foo3(1)
30+
foo4(1)
31+
//foo5(1) // error
32+
} catch {
33+
case _: Ex1 =>
34+
case _: Ex2 =>
35+
}

0 commit comments

Comments
 (0)