Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
13 changes: 8 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

@road21 road21 Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the behavior of the following code now:

val boo: NamedTuple.NamedTuple[("_2", "apply"), (Int, Int)] = (1, 2)
println(boo.apply)

I mean, that's too easy to bypass restrictions on named tuple field names. What error will be in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation doesn't apply to non-standard construction of named tuples (#23360). The compiler team decided that validation should only be applied to named tuples created with the spec (k:V) syntax. Either way of course it should not crash the compiler.

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
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i24021.check
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/neg/i24021.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@main def Test = {
(apply = () => 1, k2 = 2).k2 // error
(apply = () => 1)() // error
}
Loading