Skip to content

Commit

Permalink
Future migration warning for alphanumeric infix operator (#18908)
Browse files Browse the repository at this point in the history
Copy of #18835 with a clean branch name.
  • Loading branch information
nicolasstucki authored Nov 13, 2023
2 parents 41279ac + c79b8e8 commit 9333536
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1081,21 +1081,21 @@ trait Checking {
!name.isOperatorName &&
!meth.isDeclaredInfix &&
!meth.maybeOwner.is(Scala2x) &&
!infixOKSinceFollowedBy(tree.right) &&
sourceVersion.isAtLeast(future) =>
!infixOKSinceFollowedBy(tree.right) =>
val (kind, alternative) =
if (ctx.mode.is(Mode.Type))
("type", (n: Name) => s"prefix syntax $n[...]")
else if (ctx.mode.is(Mode.Pattern))
("extractor", (n: Name) => s"prefix syntax $n(...)")
else
("method", (n: Name) => s"method syntax .$n(...)")
def rewriteMsg = Message.rewriteNotice("The latter", options = "-deprecation")
report.deprecationWarning(
def rewriteMsg = Message.rewriteNotice("The latter", version = `future-migration`)
report.errorOrMigrationWarning(
em"""Alphanumeric $kind $name is not declared ${hlAsKeyword("infix")}; it should not be used as infix operator.
|Instead, use ${alternative(name)} or backticked identifier `$name`.$rewriteMsg""",
tree.op.srcPos)
if (ctx.settings.deprecation.value) {
tree.op.srcPos,
from = future)
if sourceVersion == `future-migration` then {
patch(Span(tree.op.span.start, tree.op.span.start), "`")
patch(Span(tree.op.span.end, tree.op.span.end), "`")
}
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 @@ -63,6 +63,7 @@ class CompilationTests {
compileFile("tests/rewrites/uninitialized-var.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
compileFile("tests/rewrites/with-type-operator.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
compileFile("tests/rewrites/private-this.scala", defaultOptions.and("-rewrite", "-source", "future-migration")),
compileFile("tests/rewrites/alphanumeric-infix-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
2 changes: 1 addition & 1 deletion tests/neg/rewrite-messages.check
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
| ^^^
| Alphanumeric method foo is not declared infix; it should not be used as infix operator.
| Instead, use method syntax .foo(...) or backticked identifier `foo`.
| The latter can be rewritten automatically under -rewrite -deprecation.
| The latter can be rewritten automatically under -rewrite -source future-migration.
3 changes: 3 additions & 0 deletions tests/rewrites/alphanumeric-infix-operator.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extension (x: Int) def foo(y: Int): Int = x + y

def f: Unit = 2 `foo` 4
3 changes: 3 additions & 0 deletions tests/rewrites/alphanumeric-infix-operator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extension (x: Int) def foo(y: Int): Int = x + y

def f: Unit = 2 foo 4

0 comments on commit 9333536

Please sign in to comment.