Skip to content

Commit 2c87994

Browse files
authored
Merge pull request #15495 from lampepfl/nonlocalret
Fix `throwReturn` of `NonLocalReturns` to allow wider usage
2 parents 28faa0f + 8f4929a commit 2c87994

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

library/src/scala/util/control/NonLocalReturns.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object NonLocalReturns {
1919
}
2020

2121
/** Performs a nonlocal return by throwing an exception. */
22-
def throwReturn[T](result: T)(using returner: ReturnThrowable[T]): Nothing =
22+
def throwReturn[T](result: T)(using returner: ReturnThrowable[? >: T]): Nothing =
2323
returner.throwReturn(result)
2424

2525
/** Enable nonlocal returns in `op`. */

tests/run/nonlocal-return.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ object Test {
88
false
99
}
1010

11+
trait Animal
12+
object Dog extends Animal
13+
object Cat extends Animal
14+
15+
def animal(arg: Int): Animal = returning {
16+
if (arg < 0) throwReturn(Dog)
17+
Cat
18+
}
19+
1120
def main(arg: Array[String]): Unit = {
1221
assert(has(1 :: 2 :: Nil, 1))
1322
assert(has(1 :: 2 :: Nil, 2))
1423
assert(!has(1 :: 2 :: Nil, 3))
24+
assert(animal(1) == Cat)
25+
assert(animal(-1) == Dog)
1526
}
16-
}
27+
}

0 commit comments

Comments
 (0)