Skip to content

Commit

Permalink
Try to type as an Assign
Browse files Browse the repository at this point in the history
[Cherry-picked d1e68f1]
  • Loading branch information
mbovel authored and WojciechMazur committed Nov 18, 2024
1 parent 2188612 commit 10c13da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3427,9 +3427,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
def checkAmbiguousNamedTupleAssignment(tree: untpd.Tuple)(using Context): Unit =
tree.trees match
case List(NamedArg(name, value)) =>
val typedName = typedIdent(untpd.Ident(name), WildcardType)
val sym = typedName.symbol
if sym.exists && (sym.is(Flags.Mutable) || sym.setter.exists) then
val tmpCtx = ctx.fresh.setNewTyperState()
typedAssign(untpd.Assign(untpd.Ident(name), value), WildcardType)(using tmpCtx)
if !tmpCtx.reporter.hasErrors then
// If there are no errors typing the above, then the named tuple is
// ambiguous and we issue a warning.
report.migrationWarning(AmbiguousNamedTupleAssignment(name, value), tree.srcPos)
case _ => ()

Expand Down
7 changes: 7 additions & 0 deletions tests/warn/21681c.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- [E203] Syntax Migration Warning: tests/warn/21681c.scala:5:2 --------------------------------------------------------
5 | (age = 29) // warn
| ^^^^^^^^^^
| Ambiguous syntax: this is interpreted as a named tuple with one element,
| not as an assignment.
|
| To assign a value, use curly braces: `{age = 29}`.
5 changes: 5 additions & 0 deletions tests/warn/21681c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test:
def age: Int = ???
def age_=(x: Int): Unit = ()
age = 29
(age = 29) // warn

0 comments on commit 10c13da

Please sign in to comment.