Skip to content
Open
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
17 changes: 12 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,18 @@ 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 namedTupleElems.forall(_._1 != nme.apply) then
typed(
untpd.Apply(
untpd.Select(untpd.TypedSplice(qual), nme.apply),
untpd.Literal(Constant(nameIdx))),
pt)
else
report.error(
em"""Named tuples that define an `apply` field do not allow field selection.
|The `apply` field should be renamed to something else.""",
tree0.srcPos)
EmptyTree
else EmptyTree

// Otherwise, map combinations of A *: B *: .... EmptyTuple with nesting levels <= 22
Expand Down
28 changes: 28 additions & 0 deletions tests/neg/named-tuple-apply.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Error: tests/neg/named-tuple-apply.scala:5:2 ------------------------------------------------------------------------
5 | (apply = () => 1)(()) // error // error
| ^^^^^^^^^^^^^^^^^
| Named tuples that define an `apply` field do not allow field selection.
| The `apply` field should be renamed to something else.
-- [E007] Type Mismatch Error: tests/neg/named-tuple-apply.scala:5:20 --------------------------------------------------
5 | (apply = () => 1)(()) // error // error
| ^^
| Found: Unit
| Required: Int
|
| longer explanation available when compiling with `-explain`
-- Error: tests/neg/named-tuple-apply.scala:6:29 -----------------------------------------------------------------------
6 | (apply = () => 1, foo = 2).foo // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Named tuples that define an `apply` field do not allow field selection.
| The `apply` field should be renamed to something else.
-- Error: tests/neg/named-tuple-apply.scala:7:29 -----------------------------------------------------------------------
7 | (apply = () => 1, foo = 2).apply(1) // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Named tuples that define an `apply` field do not allow field selection.
| The `apply` field should be renamed to something else.
-- [E050] Type Error: tests/neg/named-tuple-apply.scala:10:2 -----------------------------------------------------------
10 | foo() // error (error message could be better)
| ^^^
| method apply in class Foo does not take parameters
|
| longer explanation available when compiling with `-explain`
10 changes: 10 additions & 0 deletions tests/neg/named-tuple-apply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Foo:
def apply: () => Int = () => 2

def test =
(apply = () => 1)(()) // error // error
(apply = () => 1, foo = 2).foo // error
(apply = () => 1, foo = 2).apply(1) // error

val foo = Foo()
foo() // error (error message could be better)
Loading