From 24c3e7f601c6ba74c541f1603251253bfde45ab3 Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Wed, 27 Nov 2024 23:54:11 +0100 Subject: [PATCH] Add migration rewrite deprecated assignment syntax --- .../dotty/tools/dotc/reporting/messages.scala | 1 + .../src/dotty/tools/dotc/typer/Typer.scala | 3 +++ .../dotty/tools/dotc/CompilationTests.scala | 1 + .../ambigious-named-tuple-assignment.check | 19 +++++++++++++++++++ .../ambigious-named-tuple-assignment.scala | 19 +++++++++++++++++++ tests/warn/21681.check | 1 + tests/warn/21681b.check | 1 + tests/warn/21681c.check | 1 + tests/warn/21770.check | 1 + 9 files changed, 47 insertions(+) create mode 100644 tests/rewrites/ambigious-named-tuple-assignment.check create mode 100644 tests/rewrites/ambigious-named-tuple-assignment.scala diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 2e74c5816c5e..b396aa62f599 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -3350,6 +3350,7 @@ final class DeprecatedAssignmentSyntax(key: Name, value: untpd.Tree)(using Conte |not as an assignment. | |To assign a value, use curly braces: `{${key} = ${value}}`.""" + + Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`) override protected def explain(using Context): String = "" diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index d74a69f2a114..f5318759bac2 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3442,6 +3442,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer // If there are no errors typing the above, then the named tuple is // ambiguous and we issue a warning. report.migrationWarning(DeprecatedAssignmentSyntax(name, value), tree.srcPos) + if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then + patch(tree.source, Span(tree.span.start, tree.span.start + 1), "{") + patch(tree.source, Span(tree.span.end - 1, tree.span.end), "}") case _ => () /** Retrieve symbol attached to given tree */ diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 3bd3b5138fad..9f72db6fc390 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -80,6 +80,7 @@ class CompilationTests { compileDir("tests/rewrites/annotation-named-pararamters", defaultOptions.and("-rewrite", "-source:3.6-migration")), compileFile("tests/rewrites/i21418.scala", unindentOptions.and("-rewrite", "-source:3.5-migration")), compileFile("tests/rewrites/infix-named-args.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")), + compileFile("tests/rewrites/ambigious-named-tuple-assignment.scala", defaultOptions.and("-rewrite", "-source:3.6-migration")), ).checkRewrites() } diff --git a/tests/rewrites/ambigious-named-tuple-assignment.check b/tests/rewrites/ambigious-named-tuple-assignment.check new file mode 100644 index 000000000000..00e6cc4112f1 --- /dev/null +++ b/tests/rewrites/ambigious-named-tuple-assignment.check @@ -0,0 +1,19 @@ +import scala.language.experimental.namedTuples + +object i21770: + def f(g: Int => Unit) = g(0) + var cache: Option[Int] = None + f(i => {cache = Some(i)}) + +object i21861: + var age: Int = 28 + { + age = 29 + } + + +object i21861c: + def age: Int = ??? + def age_=(x: Int): Unit = () + age = 29 + { age = 29 } diff --git a/tests/rewrites/ambigious-named-tuple-assignment.scala b/tests/rewrites/ambigious-named-tuple-assignment.scala new file mode 100644 index 000000000000..e9685b7b58cf --- /dev/null +++ b/tests/rewrites/ambigious-named-tuple-assignment.scala @@ -0,0 +1,19 @@ +import scala.language.experimental.namedTuples + +object i21770: + def f(g: Int => Unit) = g(0) + var cache: Option[Int] = None + f(i => (cache = Some(i))) + +object i21861: + var age: Int = 28 + ( + age = 29 + ) + + +object i21861c: + def age: Int = ??? + def age_=(x: Int): Unit = () + age = 29 + ( age = 29 ) diff --git a/tests/warn/21681.check b/tests/warn/21681.check index 5156a600d609..adf3586e6e0b 100644 --- a/tests/warn/21681.check +++ b/tests/warn/21681.check @@ -5,3 +5,4 @@ | not as an assignment. | | To assign a value, use curly braces: `{age = 29}`. + | This can be rewritten automatically under -rewrite -source 3.6-migration. diff --git a/tests/warn/21681b.check b/tests/warn/21681b.check index dd28df3168ed..09c007f351b4 100644 --- a/tests/warn/21681b.check +++ b/tests/warn/21681b.check @@ -5,3 +5,4 @@ | not as an assignment. | | To assign a value, use curly braces: `{age = 29}`. + | This can be rewritten automatically under -rewrite -source 3.6-migration. diff --git a/tests/warn/21681c.check b/tests/warn/21681c.check index bfb62618cbb9..20273f723384 100644 --- a/tests/warn/21681c.check +++ b/tests/warn/21681c.check @@ -5,3 +5,4 @@ | not as an assignment. | | To assign a value, use curly braces: `{age = 29}`. + | This can be rewritten automatically under -rewrite -source 3.6-migration. diff --git a/tests/warn/21770.check b/tests/warn/21770.check index 6c978a6078a2..7853d77a423c 100644 --- a/tests/warn/21770.check +++ b/tests/warn/21770.check @@ -5,3 +5,4 @@ | not as an assignment. | | To assign a value, use curly braces: `{cache = Some(i)}`. + | This can be rewritten automatically under -rewrite -source 3.6-migration.