From c8a489ddffc992e62b2a09ae349d4940b8af66e8 Mon Sep 17 00:00:00 2001 From: aherlihy Date: Wed, 15 Oct 2025 12:31:59 +0200 Subject: [PATCH] Prohibit named tuple keys called 'apply' --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 4 ++++ compiler/src/dotty/tools/dotc/typer/Typer.scala | 13 ++++++++----- tests/neg/i24021.check | 8 ++++++++ tests/neg/i24021.scala | 4 ++++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tests/neg/i24021.check create mode 100644 tests/neg/i24021.scala diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index ba7b9132e88a..e5d10483d28d 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1749,6 +1749,10 @@ object desugar { report.error( em"$name cannot be used as the name of a tuple element because it is a regular tuple selector", arg.srcPos) + if name == nme.apply then + report.error( + em"$name cannot be used as the name of a tuple element", + arg.srcPos) elems match case elem :: elems1 => diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index e5841a3de768..12f185b34652 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -849,11 +849,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer val namedTupleElems = qual.tpe.widenDealias.namedTupleElementTypes(true) val nameIdx = namedTupleElems.indexWhere(_._1 == selName) if nameIdx >= 0 && sourceVersion.enablesNamedTuples then - typed( - untpd.Apply( - untpd.Select(untpd.TypedSplice(qual), nme.apply), - untpd.Literal(Constant(nameIdx))), - pt) + if selName == nme.apply then + EmptyTree + else + typed( + untpd.Apply( + untpd.Select(untpd.TypedSplice(qual), nme.apply), + untpd.Literal(Constant(nameIdx))), + pt) else EmptyTree // Otherwise, map combinations of A *: B *: .... EmptyTuple with nesting levels <= 22 diff --git a/tests/neg/i24021.check b/tests/neg/i24021.check new file mode 100644 index 000000000000..a34086e02265 --- /dev/null +++ b/tests/neg/i24021.check @@ -0,0 +1,8 @@ +-- Error: tests/neg/i24021.scala:2:3 ----------------------------------------------------------------------------------- +2 | (apply = () => 1, k2 = 2).k2 // error + | ^^^^^^^^^^^^^^^ + | apply cannot be used as the name of a tuple element +-- Error: tests/neg/i24021.scala:3:3 ----------------------------------------------------------------------------------- +3 | (apply = () => 1)() // error + | ^^^^^^^^^^^^^^^ + | apply cannot be used as the name of a tuple element diff --git a/tests/neg/i24021.scala b/tests/neg/i24021.scala new file mode 100644 index 000000000000..08780adbe560 --- /dev/null +++ b/tests/neg/i24021.scala @@ -0,0 +1,4 @@ +@main def Test = { + (apply = () => 1, k2 = 2).k2 // error + (apply = () => 1)() // error +}