Skip to content

Commit 10c13da

Browse files
mbovelWojciechMazur
authored andcommitted
Try to type as an Assign
[Cherry-picked d1e68f1]
1 parent 2188612 commit 10c13da

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -3427,9 +3427,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
34273427
def checkAmbiguousNamedTupleAssignment(tree: untpd.Tuple)(using Context): Unit =
34283428
tree.trees match
34293429
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
3430+
val tmpCtx = ctx.fresh.setNewTyperState()
3431+
typedAssign(untpd.Assign(untpd.Ident(name), value), WildcardType)(using tmpCtx)
3432+
if !tmpCtx.reporter.hasErrors then
3433+
// If there are no errors typing the above, then the named tuple is
3434+
// ambiguous and we issue a warning.
34333435
report.migrationWarning(AmbiguousNamedTupleAssignment(name, value), tree.srcPos)
34343436
case _ => ()
34353437

tests/warn/21681c.check

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E203] Syntax Migration Warning: tests/warn/21681c.scala:5:2 --------------------------------------------------------
2+
5 | (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/21681c.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test:
2+
def age: Int = ???
3+
def age_=(x: Int): Unit = ()
4+
age = 29
5+
(age = 29) // warn

0 commit comments

Comments
 (0)