Skip to content

Commit 861beee

Browse files
committed
Add migration rewrite for deprecated assignment syntax
1 parent a4ea8bf commit 861beee

File tree

10 files changed

+28
-34
lines changed

10 files changed

+28
-34
lines changed

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

+1-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,8 @@ object Parsers {
11491149
if isType then infixOp
11501150
else infixOp.right match
11511151
case Tuple(args) if args.exists(_.isInstanceOf[NamedArg]) && !isNamedTupleOperator =>
1152-
report.errorOrMigrationWarning(AmbiguousNamedTupleInfixApply(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleInfixApply)
1153-
if MigrationVersion.AmbiguousNamedTupleInfixApply.needsPatch then
1152+
report.errorOrMigrationWarning(DeprecatedInfixNamedArgumentSyntax(), infixOp.right.srcPos, MigrationVersion.AmbiguousNamedTupleSyntax)
1153+
if MigrationVersion.AmbiguousNamedTupleSyntax.needsPatch then
11541154
val asApply = cpy.Apply(infixOp)(Select(opInfo.operand, opInfo.operator.name), args)
11551155
patch(source, infixOp.span, asApply.show(using ctx.withoutColors))
11561156
asApply // allow to use pre-3.6 syntax in migration mode

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

+2-2
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

+8-10
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -3404,7 +3404,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34043404
/** Translate tuples of all arities */
34053405
def typedTuple(tree: untpd.Tuple, pt: Type)(using Context): Tree =
34063406
val tree1 = desugar.tuple(tree, pt)
3407-
checkAmbiguousNamedTupleAssignment(tree)
3407+
checkDeprecatedAssignmentSyntax(tree)
34083408
if tree1 ne tree then typed(tree1, pt)
34093409
else
34103410
val arity = tree.trees.length
@@ -3433,15 +3433,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34333433
/** Checks if `tree` is a named tuple with one element that could be
34343434
* interpreted as an assignment, such as `(x = 1)`. If so, issues a warning.
34353435
*/
3436-
def checkAmbiguousNamedTupleAssignment(tree: untpd.Tuple)(using Context): Unit =
3436+
def checkDeprecatedAssignmentSyntax(tree: untpd.Tuple)(using Context): Unit =
34373437
tree.trees match
34383438
case List(NamedArg(name, value)) =>
34393439
val tmpCtx = ctx.fresh.setNewTyperState()
34403440
typedAssign(untpd.Assign(untpd.Ident(name), value), WildcardType)(using tmpCtx)
34413441
if !tmpCtx.reporter.hasErrors then
34423442
// If there are no errors typing the above, then the named tuple is
34433443
// ambiguous and we issue a warning.
3444-
report.migrationWarning(AmbiguousNamedTupleAssignment(name, value), tree.srcPos)
3444+
report.migrationWarning(DeprecatedAssignmentSyntax(name, value), tree.srcPos)
34453445
case _ => ()
34463446

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

tests/neg/infix-named-args.check

+8-12
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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)