Skip to content

Commit 964bccf

Browse files
committed
Deprecate NonLocalReturns API
Change the recommendation in the warning about non-local returns accordingly. Still to do: A PR against scala/scala that deprecates scala.util.control.Breaks.
1 parent dcac92d commit 964bccf

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

compiler/src/dotty/tools/dotc/transform/NonLocalReturns.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class NonLocalReturns extends MiniPhase {
9797
override def transformReturn(tree: Return)(using Context): Tree =
9898
if isNonLocalReturn(tree) then
9999
report.gradualErrorOrMigrationWarning(
100-
em"Non local returns are no longer supported; use scala.util.control.NonLocalReturns instead",
100+
em"Non local returns are no longer supported; use `boundary` and `break` in `scala.util` instead",
101101
tree.srcPos,
102102
warnFrom = `3.2`,
103103
errorFrom = future)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ package scala.util.control
77
* import scala.util.control.NonLocalReturns.*
88
*
99
* returning { ... throwReturn(x) ... }
10+
*
11+
* This API has been deprecated. Its functionality is better served by
12+
*
13+
* - `scala.util.boundary` in place of `returning`
14+
* - `scala.util.break` in place of `throwReturn`
15+
*
16+
* The new abstractions work with plain `RuntimeExceptions` and are more
17+
* performant, since returns within the scope of the same method can be
18+
* rewritten by the compiler to jumps.
1019
*/
1120
object NonLocalReturns {
21+
@deprecated("Use scala.util.boundary.Break instead", "3.3")
1222
class ReturnThrowable[T] extends ControlThrowable {
1323
private var myResult: T = _
1424
def throwReturn(result: T): Nothing = {
@@ -19,10 +29,12 @@ object NonLocalReturns {
1929
}
2030

2131
/** Performs a nonlocal return by throwing an exception. */
32+
@deprecated("Use scala.util.break and scala.util.boundary instead", "3.3")
2233
def throwReturn[T](result: T)(using returner: ReturnThrowable[? >: T]): Nothing =
2334
returner.throwReturn(result)
2435

2536
/** Enable nonlocal returns in `op`. */
37+
@deprecated("Use scala.util.boundary and scala.util.break instead", "3.3")
2638
def returning[T](op: ReturnThrowable[T] ?=> T): T = {
2739
val returner = new ReturnThrowable[T]
2840
try op(using returner)

0 commit comments

Comments
 (0)