Skip to content

Allow (...) around throws alternatives #13823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ object desugar {
* $throws[... $throws[A, E1] ... , En].
*/
def throws(tpt: Tree, op: Ident, excepts: Tree)(using Context): AppliedTypeTree = excepts match
case Parens(excepts1) =>
throws(tpt, op, excepts1)
case InfixOp(l, bar @ Ident(tpnme.raw.BAR), r) =>
throws(throws(tpt, op, l), bar, r)
case e =>
Expand Down
35 changes: 35 additions & 0 deletions tests/pos/i13816.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import language.experimental.saferExceptions

class Ex1 extends Exception("Ex1")
class Ex2 extends Exception("Ex2")

def foo1(i: Int): Unit throws Ex1 throws Ex2 =
if i > 0 then throw new Ex1 else throw new Ex2

def foo2(i: Int): Unit throws Ex1 | Ex2 =
if i > 0 then throw new Ex1 else throw new Ex2

def foo2a(i: Int): Unit throws (Ex1 | Ex2) =
if i > 0 then throw new Ex1 else throw new Ex2

def foo3(i: Int)(using CanThrow[Ex1], CanThrow[Ex2]) =
if i > 0 then throw new Ex1 else throw new Ex2

def foo4(i: Int)(using CanThrow[Ex1])(using CanThrow[Ex2]) =
if i > 0 then throw new Ex1 else throw new Ex2

//def foo5(i: Int)(using CanThrow[Ex1 & Ex2]) = // does not work: no capability aggregation is supported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally in the reported issue there was (using CanThrow[Ex1 | Ex2]) instead of (using CanThrow[Ex1 & Ex2]) and we need a separate negative test to check the error message in this case because it's wrong now. It still suggests adding (using CanThrow[Ex1 | Ex2]) clause to the method almost it's already there and it's not supposed to work.
In case of (using CanThrow[Ex1 & Ex2]) we could also try to make the compiler give a better message explaining why this might be wrong

Copy link
Contributor Author

@odersky odersky Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll take a look.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to have throws E1, E2 which will mirror the new extends Class1, Class2 syntax but probably off topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would lead to ambiguities.

// if i > 0 then throw new Ex1 else throw new Ex2

def test(): Unit =
try {
foo1(1)
foo2(1)
foo2a(1)
foo3(1)
foo4(1)
//foo5(1) // error
} catch {
case _: Ex1 =>
case _: Ex2 =>
}