File tree 5 files changed +39
-2
lines changed
compiler/src/dotty/tools/dotc
5 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -1639,8 +1639,6 @@ object desugar {
1639
1639
if ctx.mode.is(Mode .Type ) then
1640
1640
AppliedTypeTree (ref(defn.NamedTupleTypeRef ), namesTuple :: tup :: Nil )
1641
1641
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)
1644
1642
Apply (
1645
1643
Apply (
1646
1644
TypeApply (
Original file line number Diff line number Diff line change @@ -3398,6 +3398,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3398
3398
/** Translate tuples of all arities */
3399
3399
def typedTuple (tree : untpd.Tuple , pt : Type )(using Context ): Tree =
3400
3400
val tree1 = desugar.tuple(tree, pt)
3401
+ checkAmbiguousNamedTuple(tree)
3401
3402
if tree1 ne tree then typed(tree1, pt)
3402
3403
else
3403
3404
val arity = tree.trees.length
@@ -3423,6 +3424,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3423
3424
val resTpe = TypeOps .nestedPairs(elemTpes)
3424
3425
app1.cast(resTpe)
3425
3426
3427
+ def checkAmbiguousNamedTuple (tree : untpd.Tuple )(using Context ): Unit =
3428
+ tree.trees match
3429
+ case List (NamedArg (name, value)) =>
3430
+ val typedName = typedIdent(untpd.Ident (name), WildcardType )
3431
+ val sym = typedName.symbol
3432
+ if sym.exists && (sym.is(Flags .Mutable ) || sym.setter.exists) then
3433
+ report.migrationWarning(AmbiguousNamedTupleAssignment (name, value), tree.srcPos)
3434
+ case _ =>
3435
+
3426
3436
/** Retrieve symbol attached to given tree */
3427
3437
protected def retrieveSym (tree : untpd.Tree )(using Context ): Symbol = tree.removeAttachment(SymOfTree ) match {
3428
3438
case Some (sym) =>
Original file line number Diff line number Diff line change
1
+ def test1 () =
2
+ class Person :
3
+ def age : Int = ???
4
+ def age_= (x : Int ): Unit = ???
5
+
6
+ val person = Person ()
7
+
8
+ (person.age = 29 ) // no warn (interpreted as `person.age_=(29)`)
9
+
10
+ def test2 () =
11
+ class Person :
12
+ var age : Int = 28
13
+
14
+ val person = Person ()
15
+
16
+ (person.age = 29 ) // no warn (interpreted as `person.age_=(29)`)
17
+
Original file line number Diff line number Diff line change
1
+ -- [E203] Syntax Migration Warning: tests/warn/21681b.scala:4:2 --------------------------------------------------------
2
+ 4 | (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}`.
Original file line number Diff line number Diff line change
1
+
2
+ object Test :
3
+ var age : Int = 28
4
+ (age = 29 ) // warn
5
+
You can’t perform that action at this time.
0 commit comments