Skip to content

Commit 80eb6e1

Browse files
Future migration warning for with type operator (#18818)
* In `future-migration` we emit the deprecation warning and enable the patch with -rewrite. * In `future` we emit we make this syntax an error ```scala //> using options -source future def foo: Int with String = ??? // error ``` ```diff //> using options -rewrite -source future-migration - def foo: Int with String = ??? + def foo: Int & String = ??? ```
2 parents 9724244 + de88b7c commit 80eb6e1

10 files changed

+32
-4
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,8 +1756,12 @@ object Parsers {
17561756
if in.token == LBRACE || in.token == INDENT then
17571757
t
17581758
else
1759-
if sourceVersion.isAtLeast(future) then
1760-
deprecationWarning(DeprecatedWithOperator(), withOffset)
1759+
report.errorOrMigrationWarning(
1760+
DeprecatedWithOperator(rewriteNotice(`future-migration`)),
1761+
in.sourcePos(withOffset),
1762+
from = future)
1763+
if sourceVersion == `future-migration` then
1764+
patch(source, Span(withOffset, withOffset + 4), "&")
17611765
atSpan(startOffset(t)) { makeAndType(t, withType()) }
17621766
else t
17631767

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ extends EmptyCatchOrFinallyBlock(tryBody, EmptyCatchAndFinallyBlockID) {
138138
|its body in a block; no exceptions are handled."""
139139
}
140140

141-
class DeprecatedWithOperator()(using Context)
141+
class DeprecatedWithOperator(rewrite: String)(using Context)
142142
extends SyntaxMsg(DeprecatedWithOperatorID) {
143143
def msg(using Context) =
144-
i"""${hl("with")} as a type operator has been deprecated; use ${hl("&")} instead"""
144+
i"""${hl("with")} as a type operator has been deprecated; use ${hl("&")} instead$rewrite"""
145145
def explain(using Context) =
146146
i"""|Dotty introduces intersection types - ${hl("&")} types. These replace the
147147
|use of the ${hl("with")} keyword. There are a few differences in

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class CompilationTests {
6060
compileFile("tests/rewrites/rewrites.scala", defaultOptions.and("-source", "3.0-migration").and("-rewrite", "-indent")),
6161
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6262
compileFile("tests/rewrites/rewrites3x-fatal-warnings.scala", defaultOptions.and("-rewrite", "-source", "future-migration", "-Xfatal-warnings")),
63+
compileFile("tests/rewrites/with-type-operator.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
6364
compileFile("tests/rewrites/filtering-fors.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6465
compileFile("tests/rewrites/refutable-pattern-bindings.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
6566
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E003] Syntax Error: tests/neg/with-type-operator-future-migration.scala:5:13 ---------------------------------------
2+
5 |def foo: Int with String = ??? // error
3+
| ^
4+
| with as a type operator has been deprecated; use & instead
5+
| This construct can be rewritten automatically under -rewrite -source future-migration.
6+
|
7+
| longer explanation available when compiling with `-explain`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//> using options -Werror
2+
3+
import scala.language.`future-migration`
4+
5+
def foo: Int with String = ??? // error
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import scala.language.`future`
2+
3+
def foo: Int with String = ??? // error
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import scala.language.`future-migration`
2+
3+
def foo: Int with String = ??? // warn

tests/pos/with-type-operator.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//> using options -Werror
2+
3+
def foo: Int with String = ??? // warn
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def foo: Int & String = ???
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def foo: Int with String = ???

0 commit comments

Comments
 (0)