Skip to content

Commit 4d0dee7

Browse files
committed
Add migration rewrite for deprecated assignment syntax
[Cherry-picked 861beee]
1 parent 3e7955e commit 4d0dee7

File tree

10 files changed

+28
-34
lines changed

10 files changed

+28
-34
lines changed

compiler/src/dotty/tools/dotc/config/MigrationVersion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum MigrationVersion(val warnFrom: SourceVersion, val errorFrom: SourceVersion)
2626
case WithOperator extends MigrationVersion(`3.4`, future)
2727
case FunctionUnderscore extends MigrationVersion(`3.4`, future)
2828
case NonNamedArgumentInJavaAnnotation extends MigrationVersion(`3.6`, `3.6`)
29-
case AmbiguousNamedTupleInfixApply extends MigrationVersion(`3.6`, never)
29+
case AmbiguousNamedTupleSyntax extends MigrationVersion(`3.6`, future)
3030
case ImportWildcard extends MigrationVersion(future, future)
3131
case ImportRename extends MigrationVersion(future, future)
3232
case ParameterEnclosedByParenthesis extends MigrationVersion(future, future)

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,8 @@ object Parsers {
11331133
if isType then infixOp
11341134
else infixOp.right match
11351135
case Tuple(args) if args.exists(_.isInstanceOf[NamedArg]) && !isNamedTupleOperator =>
1136-
report.errorOrMigrationWarning(AmbiguousNamedTupleInfixApply(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleInfixApply)
1137-
if MigrationVersion.AmbiguousNamedTupleInfixApply.needsPatch then
1136+
report.errorOrMigrationWarning(DeprecatedInfixNamedArgumentSyntax(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleSyntax)
1137+
if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then
11381138
val asApply = cpy.Apply(infixOp)(Select(opInfo.operand, opInfo.operator.name), args)
11391139
patch(source, infixOp.span, asApply.show(using ctx.withoutColors))
11401140
asApply // allow to use pre-3.6 syntax in migration mode

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
216216
case FinalLocalDefID // errorNumber: 200
217217
case NonNamedArgumentInJavaAnnotationID // errorNumber: 201
218218
case QuotedTypeMissingID // errorNumber: 202
219-
case AmbiguousNamedTupleAssignmentID // errorNumber: 203
220-
case AmbiguousNamedTupleInfixApplyID // errorNumber: 204
219+
case DeprecatedAssignmentSyntaxID // errorNumber: 203
220+
case DeprecatedInfixNamedArgumentSyntaxID // errorNumber: 204
221221

222222
def errorNumber = ordinal - 1
223223

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,21 +3344,19 @@ final class QuotedTypeMissing(tpe: Type)(using Context) extends StagingMessage(Q
33443344

33453345
end QuotedTypeMissing
33463346

3347-
final class AmbiguousNamedTupleAssignment(key: Name, value: untpd.Tree)(using Context) extends SyntaxMsg(AmbiguousNamedTupleAssignmentID):
3347+
final class DeprecatedAssignmentSyntax(key: Name, value: untpd.Tree)(using Context) extends SyntaxMsg(DeprecatedAssignmentSyntaxID):
33483348
override protected def msg(using Context): String =
3349-
i"""Ambiguous syntax: this is interpreted as a named tuple with one element,
3349+
i"""Deprecated syntax: in the future it would be interpreted as a named tuple with one element,
33503350
|not as an assignment.
33513351
|
33523352
|To assign a value, use curly braces: `{${key} = ${value}}`."""
3353-
3353+
33543354
override protected def explain(using Context): String = ""
33553355

3356-
class AmbiguousNamedTupleInfixApply()(using Context) extends SyntaxMsg(AmbiguousNamedTupleInfixApplyID):
3356+
class DeprecatedInfixNamedArgumentSyntax()(using Context) extends SyntaxMsg(DeprecatedInfixNamedArgumentSyntaxID):
33573357
def msg(using Context) =
3358-
"Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list."
3359-
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
3358+
i"""Deprecated syntax: infix named arguments lists are deprecated; in the future it would be interpreted as a single name tuple argument.
3359+
|To avoid this warning, either remove the argument names or use dotted selection."""
3360+
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
33603361

3361-
def explain(using Context) =
3362-
i"""Starting with Scala 3.6 infix named arguments are interpretted as Named Tuple.
3363-
|
3364-
|To avoid this warning, either remove the argument names or use dotted selection."""
3362+
def explain(using Context) = ""

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,7 +3394,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
33943394
/** Translate tuples of all arities */
33953395
def typedTuple(tree: untpd.Tuple, pt: Type)(using Context): Tree =
33963396
val tree1 = desugar.tuple(tree, pt)
3397-
checkAmbiguousNamedTupleAssignment(tree)
3397+
checkDeprecatedAssignmentSyntax(tree)
33983398
if tree1 ne tree then typed(tree1, pt)
33993399
else
34003400
val arity = tree.trees.length
@@ -3423,15 +3423,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34233423
/** Checks if `tree` is a named tuple with one element that could be
34243424
* interpreted as an assignment, such as `(x = 1)`. If so, issues a warning.
34253425
*/
3426-
def checkAmbiguousNamedTupleAssignment(tree: untpd.Tuple)(using Context): Unit =
3426+
def checkDeprecatedAssignmentSyntax(tree: untpd.Tuple)(using Context): Unit =
34273427
tree.trees match
34283428
case List(NamedArg(name, value)) =>
34293429
val tmpCtx = ctx.fresh.setNewTyperState()
34303430
typedAssign(untpd.Assign(untpd.Ident(name), value), WildcardType)(using tmpCtx)
34313431
if !tmpCtx.reporter.hasErrors then
34323432
// If there are no errors typing the above, then the named tuple is
34333433
// ambiguous and we issue a warning.
3434-
report.migrationWarning(AmbiguousNamedTupleAssignment(name, value), tree.srcPos)
3434+
report.migrationWarning(DeprecatedAssignmentSyntax(name, value), tree.srcPos)
34353435
case _ => ()
34363436

34373437
/** Retrieve symbol attached to given tree */

tests/neg/infix-named-args.check

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,24 @@
1414
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:4:15 --------------------------------------------------------
1515
4 | def f = 42 + (x = 1) // error // werror
1616
| ^^^^^^^
17-
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
17+
|Deprecated syntax: infix named arguments lists are deprecated; in the future it would be interpreted as a single name tuple argument.
18+
|To avoid this warning, either remove the argument names or use dotted selection.
1819
|This can be rewritten automatically under -rewrite -source 3.6-migration.
19-
|
20-
| longer explanation available when compiling with `-explain`
2120
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:7:26 --------------------------------------------------------
2221
7 | def g = new C() `multi` (x = 42, y = 27) // werror
2322
| ^^^^^^^^^^^^^^^^
24-
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
23+
|Deprecated syntax: infix named arguments lists are deprecated; in the future it would be interpreted as a single name tuple argument.
24+
|To avoid this warning, either remove the argument names or use dotted selection.
2525
|This can be rewritten automatically under -rewrite -source 3.6-migration.
26-
|
27-
| longer explanation available when compiling with `-explain`
2826
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:8:21 --------------------------------------------------------
2927
8 | def h = new C() ** (x = 42, y = 27) // werror
3028
| ^^^^^^^^^^^^^^^^
31-
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
29+
|Deprecated syntax: infix named arguments lists are deprecated; in the future it would be interpreted as a single name tuple argument.
30+
|To avoid this warning, either remove the argument names or use dotted selection.
3231
|This can be rewritten automatically under -rewrite -source 3.6-migration.
33-
|
34-
| longer explanation available when compiling with `-explain`
3532
-- [E204] Syntax Warning: tests/neg/infix-named-args.scala:15:18 -------------------------------------------------------
3633
15 | def f = this ** (x = 2) // werror
3734
| ^^^^^^^
38-
|Ambigious syntax: this infix call argument list is interpreted as single named tuple argument, not as an named arguments list.
35+
|Deprecated syntax: infix named arguments lists are deprecated; in the future it would be interpreted as a single name tuple argument.
36+
|To avoid this warning, either remove the argument names or use dotted selection.
3937
|This can be rewritten automatically under -rewrite -source 3.6-migration.
40-
|
41-
| longer explanation available when compiling with `-explain`

tests/warn/21681.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E203] Syntax Migration Warning: tests/warn/21681.scala:5:2 ---------------------------------------------------------
22
5 | (age = 29) // warn
33
| ^^^^^^^^^^
4-
| Ambiguous syntax: this is interpreted as a named tuple with one element,
4+
| Deprecated syntax: in the future it would be interpreted as a named tuple with one element,
55
| not as an assignment.
66
|
77
| To assign a value, use curly braces: `{age = 29}`.

tests/warn/21681b.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E203] Syntax Migration Warning: tests/warn/21681b.scala:5:2 --------------------------------------------------------
22
5 | (age = 29) // warn
33
| ^^^^^^^^^^
4-
| Ambiguous syntax: this is interpreted as a named tuple with one element,
4+
| Deprecated syntax: in the future it would be interpreted as a named tuple with one element,
55
| not as an assignment.
66
|
77
| To assign a value, use curly braces: `{age = 29}`.

tests/warn/21681c.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E203] Syntax Migration Warning: tests/warn/21681c.scala:7:2 --------------------------------------------------------
22
7 | (age = 29) // warn
33
| ^^^^^^^^^^
4-
| Ambiguous syntax: this is interpreted as a named tuple with one element,
4+
| Deprecated syntax: in the future it would be interpreted as a named tuple with one element,
55
| not as an assignment.
66
|
77
| To assign a value, use curly braces: `{age = 29}`.

tests/warn/21770.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- [E203] Syntax Migration Warning: tests/warn/21770.scala:7:9 ---------------------------------------------------------
22
7 | f(i => (cache = Some(i))) // warn
33
| ^^^^^^^^^^^^^^^^^
4-
| Ambiguous syntax: this is interpreted as a named tuple with one element,
4+
| Deprecated syntax: in the future it would be interpreted as a named tuple with one element,
55
| not as an assignment.
66
|
77
| To assign a value, use curly braces: `{cache = Some(i)}`.

0 commit comments

Comments
 (0)