Skip to content

Commit

Permalink
Add migration rewrite deprecated assignment syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMazur committed Nov 27, 2024
1 parent 861beee commit 24c3e7f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
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 @@ -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()
}

Expand Down
19 changes: 19 additions & 0 deletions tests/rewrites/ambigious-named-tuple-assignment.check
Original file line number Diff line number Diff line change
@@ -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 }
19 changes: 19 additions & 0 deletions tests/rewrites/ambigious-named-tuple-assignment.scala
Original file line number Diff line number Diff line change
@@ -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 )
1 change: 1 addition & 0 deletions tests/warn/21681.check
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions tests/warn/21681b.check
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions tests/warn/21681c.check
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions tests/warn/21770.check
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 24c3e7f

Please sign in to comment.