@@ -3395,6 +3395,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3395
3395
/** Translate tuples of all arities */
3396
3396
def typedTuple (tree : untpd.Tuple , pt : Type )(using Context ): Tree =
3397
3397
val tree1 = desugar.tuple(tree, pt)
3398
+ checkAmbiguousNamedTupleAssignment(tree)
3398
3399
if tree1 ne tree then typed(tree1, pt)
3399
3400
else
3400
3401
val arity = tree.trees.length
@@ -3420,6 +3421,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3420
3421
val resTpe = TypeOps .nestedPairs(elemTpes)
3421
3422
app1.cast(resTpe)
3422
3423
3424
+ /** Checks if `tree` is a named tuple with one element that could be
3425
+ * interpreted as an assignment, such as `(x = 1)`. If so, issues a warning.
3426
+ */
3427
+ def checkAmbiguousNamedTupleAssignment (tree : untpd.Tuple )(using Context ): Unit =
3428
+ tree.trees match
3429
+ case List (NamedArg (name, value)) =>
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.
3435
+ report.migrationWarning(AmbiguousNamedTupleAssignment (name, value), tree.srcPos)
3436
+ case _ => ()
3437
+
3423
3438
/** Retrieve symbol attached to given tree */
3424
3439
protected def retrieveSym (tree : untpd.Tree )(using Context ): Symbol = tree.removeAttachment(SymOfTree ) match {
3425
3440
case Some (sym) =>
0 commit comments