Skip to content

Warn when named tuples resemble assignments #21823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Try to type as an Assign
  • Loading branch information
mbovel committed Oct 22, 2024
commit d1e68f19c99d4171f0c4a6f17cef0ceb72a20bd8
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 @@ -3430,9 +3430,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
Loading