From c79b8e8f4d2eaf9d2095d82810d1b5b8f6383bbb Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 3 Nov 2023 14:20:13 +0100 Subject: [PATCH] Future migration warning for alphanumeric infix operator --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 12 ++++++------ .../test/dotty/tools/dotc/CompilationTests.scala | 1 + tests/neg/rewrite-messages.check | 2 +- tests/rewrites/alphanumeric-infix-operator.check | 3 +++ tests/rewrites/alphanumeric-infix-operator.scala | 3 +++ 5 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 tests/rewrites/alphanumeric-infix-operator.check create mode 100644 tests/rewrites/alphanumeric-infix-operator.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 5fd1e165cf20..21bf0bc81cd0 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -1081,8 +1081,7 @@ 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[...]") @@ -1090,12 +1089,13 @@ trait Checking { ("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), "`") } diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 0491660219b2..5fd3524e208e 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -62,6 +62,7 @@ class CompilationTests { 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/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")), diff --git a/tests/neg/rewrite-messages.check b/tests/neg/rewrite-messages.check index 3ee081edbed2..f368c2dc8997 100644 --- a/tests/neg/rewrite-messages.check +++ b/tests/neg/rewrite-messages.check @@ -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. diff --git a/tests/rewrites/alphanumeric-infix-operator.check b/tests/rewrites/alphanumeric-infix-operator.check new file mode 100644 index 000000000000..8ff077e856cf --- /dev/null +++ b/tests/rewrites/alphanumeric-infix-operator.check @@ -0,0 +1,3 @@ +extension (x: Int) def foo(y: Int): Int = x + y + +def f: Unit = 2 `foo` 4 diff --git a/tests/rewrites/alphanumeric-infix-operator.scala b/tests/rewrites/alphanumeric-infix-operator.scala new file mode 100644 index 000000000000..450f44834f05 --- /dev/null +++ b/tests/rewrites/alphanumeric-infix-operator.scala @@ -0,0 +1,3 @@ +extension (x: Int) def foo(y: Int): Int = x + y + +def f: Unit = 2 foo 4