Skip to content

Commit

Permalink
Future migration warning for with type operator (#18818)
Browse files Browse the repository at this point in the history
* 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 = ???
```
  • Loading branch information
nicolasstucki authored Nov 2, 2023
2 parents 9724244 + de88b7c commit 80eb6e1
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 4 deletions.
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1756,8 +1756,12 @@ object Parsers {
if in.token == LBRACE || in.token == INDENT then
t
else
if sourceVersion.isAtLeast(future) then
deprecationWarning(DeprecatedWithOperator(), withOffset)
report.errorOrMigrationWarning(
DeprecatedWithOperator(rewriteNotice(`future-migration`)),
in.sourcePos(withOffset),
from = future)
if sourceVersion == `future-migration` then
patch(source, Span(withOffset, withOffset + 4), "&")
atSpan(startOffset(t)) { makeAndType(t, withType()) }
else t

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ extends EmptyCatchOrFinallyBlock(tryBody, EmptyCatchAndFinallyBlockID) {
|its body in a block; no exceptions are handled."""
}

class DeprecatedWithOperator()(using Context)
class DeprecatedWithOperator(rewrite: String)(using Context)
extends SyntaxMsg(DeprecatedWithOperatorID) {
def msg(using Context) =
i"""${hl("with")} as a type operator has been deprecated; use ${hl("&")} instead"""
i"""${hl("with")} as a type operator has been deprecated; use ${hl("&")} instead$rewrite"""
def explain(using Context) =
i"""|Dotty introduces intersection types - ${hl("&")} types. These replace the
|use of the ${hl("with")} keyword. There are a few differences in
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class CompilationTests {
compileFile("tests/rewrites/rewrites.scala", defaultOptions.and("-source", "3.0-migration").and("-rewrite", "-indent")),
compileFile("tests/rewrites/rewrites3x.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
compileFile("tests/rewrites/rewrites3x-fatal-warnings.scala", defaultOptions.and("-rewrite", "-source", "future-migration", "-Xfatal-warnings")),
compileFile("tests/rewrites/with-type-operator.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
compileFile("tests/rewrites/filtering-fors.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
compileFile("tests/rewrites/refutable-pattern-bindings.scala", defaultOptions.and("-rewrite", "-source", "3.2-migration")),
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")),
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/with-type-operator-future-migration.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E003] Syntax Error: tests/neg/with-type-operator-future-migration.scala:5:13 ---------------------------------------
5 |def foo: Int with String = ??? // error
| ^
| with as a type operator has been deprecated; use & instead
| This construct can be rewritten automatically under -rewrite -source future-migration.
|
| longer explanation available when compiling with `-explain`
5 changes: 5 additions & 0 deletions tests/neg/with-type-operator-future-migration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//> using options -Werror

import scala.language.`future-migration`

def foo: Int with String = ??? // error
3 changes: 3 additions & 0 deletions tests/neg/with-type-operator-future.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.language.`future`

def foo: Int with String = ??? // error
3 changes: 3 additions & 0 deletions tests/pos/with-type-operator-future-migration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.language.`future-migration`

def foo: Int with String = ??? // warn
3 changes: 3 additions & 0 deletions tests/pos/with-type-operator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//> using options -Werror

def foo: Int with String = ??? // warn
1 change: 1 addition & 0 deletions tests/rewrites/with-type-operator.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def foo: Int & String = ???
1 change: 1 addition & 0 deletions tests/rewrites/with-type-operator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def foo: Int with String = ???

0 comments on commit 80eb6e1

Please sign in to comment.