Skip to content

Commit 959f68c

Browse files
mbovelnicolasstuckibracevac
committed
Warn when named tuples resemble assignments
Co-Authored-By: Nicolas Stucki <[email protected]> Co-Authored-By: Oliver Bračevac <[email protected]>
1 parent 6e32627 commit 959f68c

File tree

7 files changed

+38
-3
lines changed

7 files changed

+38
-3
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+6-3
Original file line numberDiff line numberDiff line change
@@ -1605,9 +1605,10 @@ object desugar {
16051605

16061606
/** Translate tuple expressions
16071607
*
1608-
* () ==> ()
1609-
* (t) ==> t
1610-
* (t1, ..., tN) ==> TupleN(t1, ..., tN)
1608+
* () ==> ()
1609+
* (t) ==> t
1610+
* (t1, ..., tN) ==> TupleN(t1, ..., tN)
1611+
* (n1 = t1, ..., nN = tN) ==> NamedTuple.build[(n1, ..., nN)]()(TupleN(t1, ..., tN))
16111612
*/
16121613
def tuple(tree: Tuple, pt: Type)(using Context): Tree =
16131614
var elems = checkWellFormedTupleElems(tree.trees)
@@ -1638,6 +1639,8 @@ object desugar {
16381639
if ctx.mode.is(Mode.Type) then
16391640
AppliedTypeTree(ref(defn.NamedTupleTypeRef), namesTuple :: tup :: Nil)
16401641
else
1642+
if names.length == 1 && ctx.scope.lookup(names.head).is(Flags.Mutable) then
1643+
report.migrationWarning(AmbiguousNamedTupleAssignment(names.head, elemValues.head), tree.srcPos)
16411644
Apply(
16421645
Apply(
16431646
TypeApply(

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

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ 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
219220

220221
def errorNumber = ordinal - 1
221222

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

+9
Original file line numberDiff line numberDiff line change
@@ -3343,3 +3343,12 @@ final class QuotedTypeMissing(tpe: Type)(using Context) extends StagingMessage(Q
33433343
|"""
33443344

33453345
end QuotedTypeMissing
3346+
3347+
final class AmbiguousNamedTupleAssignment(key: Name, value: untpd.Tree)(using Context) extends SyntaxMsg(AmbiguousNamedTupleAssignmentID):
3348+
override protected def msg(using Context): String =
3349+
i"""Ambiguous syntax: this is interpreted as a named tuple with one element,
3350+
|not as an assignment.
3351+
|
3352+
|To assign a value, use curly braces: `{${key} = ${value}}`."""
3353+
3354+
override protected def explain(using Context): String = ""

tests/warn/21681.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E203] Syntax Migration Warning: tests/warn/21681.scala:3:2 ---------------------------------------------------------
2+
3 | (age = 29) // warn
3+
| ^^^^^^^^^^
4+
| Ambiguous syntax: this is interpreted as a named tuple with one element,
5+
| not as an assignment.
6+
|
7+
| To assign a value, use curly braces: `{age = 29}`.

tests/warn/21681.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def main() =
2+
var age: Int = 28
3+
(age = 29) // warn

tests/warn/21770.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E203] Syntax Migration Warning: tests/warn/21770.scala:5:9 ---------------------------------------------------------
2+
5 | f(i => (cache = Some(i))) // warn
3+
| ^^^^^^^^^^^^^^^^^
4+
| Ambiguous syntax: this is interpreted as a named tuple with one element,
5+
| not as an assignment.
6+
|
7+
| To assign a value, use curly braces: `{cache = Some(i)}`.

tests/warn/21770.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def f(g: Int => Unit) = g(0)
2+
3+
def test =
4+
var cache: Option[Int] = None
5+
f(i => (cache = Some(i))) // warn

0 commit comments

Comments
 (0)