Skip to content

Commit d522766

Browse files
authored
Merge pull request #15303 from som-snytt/tweak/warn-nonlocal-return
Gradual warning for deprecated nonlocal return
2 parents f5f5105 + bb105ae commit d522766

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
262262
case tp: ClassInfo =>
263263
if tp.cls.derivesFrom(defn.PolyFunctionClass) then
264264
tp.member(nme.apply).info match
265-
case info: PolyType => return toTextMethodAsFunction(info)
266-
case _ =>
267-
toTextParents(tp.parents) ~~ "{...}"
265+
case info: PolyType => toTextMethodAsFunction(info)
266+
case _ => toTextParents(tp.parents) ~~ "{...}"
267+
else toTextParents(tp.parents) ~~ "{...}"
268268
case JavaArrayType(elemtp) =>
269269
toText(elemtp) ~ "[]"
270270
case tp: LazyRef if !printDebug =>

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import core._
55
import Contexts._, Symbols._, Types._, Flags._, StdNames._
66
import MegaPhase._
77
import NameKinds.NonLocalReturnKeyName
8-
import config.SourceVersion._
8+
import config.SourceVersion.*
99

1010
object NonLocalReturns {
1111
import ast.tpd._
@@ -95,10 +95,11 @@ class NonLocalReturns extends MiniPhase {
9595

9696
override def transformReturn(tree: Return)(using Context): Tree =
9797
if isNonLocalReturn(tree) then
98-
report.errorOrMigrationWarning(
98+
report.gradualErrorOrMigrationWarning(
9999
"Non local returns are no longer supported; use scala.util.control.NonLocalReturns instead",
100100
tree.srcPos,
101-
from = future)
101+
warnFrom = `3.2`,
102+
errorFrom = future)
102103
nonLocalReturnThrow(tree.expr, tree.from.symbol).withSpan(tree.span)
103104
else tree
104105
}

docs/_docs/reference/dropped-features/nonlocal-returns.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "Deprecated: Nonlocal Returns"
55
nightlyOf: https://docs.scala-lang.org/scala3/reference/dropped-features/nonlocal-returns.html
66
---
77

8-
Returning from nested anonymous functions has been deprecated.
8+
Returning from nested anonymous functions has been deprecated, and will produce a warning from version `3.2`.
99

1010
Nonlocal returns are implemented by throwing and catching `scala.runtime.NonLocalReturnException`-s. This is rarely what is intended by the programmer. It can be problematic because of the hidden performance cost of throwing and catching exceptions. Furthermore, it is a leaky implementation: a catch-all exception handler can intercept a `NonLocalReturnException`.
1111

docs/_docs/reference/language-versions/source-compatibility.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ The default Scala language syntax version currently supported by the Dotty compi
1717
- in conjunction with `-rewrite`, offer code rewrites from Scala 2.13 to 3.0.
1818

1919
- [`3.0`](https://scala-lang.org/api/3.x/scala/runtime/stdLibPatches/language$$3/0$.html), [`3.1`](https://scala-lang.org/api/3.x/scala/runtime/stdLibPatches/language$$3/1$.html): the default set of features included in scala versions `3.0.0` to `3.1.3`.
20-
- [`3.2`](https://scala-lang.org/api/3.x/scala/runtime/stdLibPatches/language$$3/2$.html): the same as `3.0` and `3.1`, but [stricter pattern bindings](https://docs.scala-lang.org/scala3/reference/changed-features/pattern-bindings.html) are now enabled (part of `future` in earlier `3.x` releases), producing warnings for refutable patterns. These warnings can be silenced to achieve the same runtime behavior, but in `future` they become errors and refutable patterns will not compile.
20+
- [`3.2`](https://scala-lang.org/api/3.x/scala/runtime/stdLibPatches/language$$3/2$.html): the same as `3.0` and `3.1`, but in addition:
21+
- [stricter pattern bindings](https://docs.scala-lang.org/scala3/reference/changed-features/pattern-bindings.html) are now enabled (part of `future` in earlier `3.x` releases), producing warnings for refutable patterns. These warnings can be silenced to achieve the same runtime behavior, but in `future` they become errors and refutable patterns will not compile.
22+
- [Nonlocal returns](https://docs.scala-lang.org/scala3/reference/dropped-features/nonlocal-returns.html) now produce a warning upon usage (they are still an error under `future`).
2123
- [`3.2-migration`](https://scala-lang.org/api/3.x/scala/runtime/stdLibPatches/language$$3/2-migration$.html): the same as `3.2`, but in conjunction with `-rewrite`, offer code rewrites from Scala `3.0/3.1` to `3.2`.
2224
- [`future`](https://scala-lang.org/api/3.x/scala/runtime/stdLibPatches/language$$future$.html): A preview of changes that will be introduced in `3.x` versions after `3.2`.
2325
Some Scala 2 specific idioms are dropped in this version. The feature set supported by this version may grow over time as features become stabilised for preview.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package scala.util.control
44
*
55
* Usage:
66
*
7-
* import scala.util.control.NonLocalReturns._
7+
* import scala.util.control.NonLocalReturns.*
88
*
99
* returning { ... throwReturn(x) ... }
1010
*/

0 commit comments

Comments
 (0)