File tree Expand file tree Collapse file tree 3 files changed +50
-5
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +50
-5
lines changed Original file line number Diff line number Diff line change @@ -850,11 +850,18 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
850850 val namedTupleElems = qual.tpe.widenDealias.namedTupleElementTypes(true )
851851 val nameIdx = namedTupleElems.indexWhere(_._1 == selName)
852852 if nameIdx >= 0 && sourceVersion.enablesNamedTuples then
853- typed(
854- untpd.Apply (
855- untpd.Select (untpd.TypedSplice (qual), nme.apply),
856- untpd.Literal (Constant (nameIdx))),
857- pt)
853+ if namedTupleElems.forall(_._1 != nme.apply) then
854+ typed(
855+ untpd.Apply (
856+ untpd.Select (untpd.TypedSplice (qual), nme.apply),
857+ untpd.Literal (Constant (nameIdx))),
858+ pt)
859+ else
860+ report.error(
861+ em """ Named tuples that define an `apply` field do not allow field selection.
862+ |The `apply` field should be renamed to something else. """ ,
863+ tree0.srcPos)
864+ EmptyTree
858865 else EmptyTree
859866
860867 // Otherwise, map combinations of A *: B *: .... EmptyTuple with nesting levels <= 22
Original file line number Diff line number Diff line change 1+ -- Error: tests/neg/named-tuple-apply.scala:5:2 ------------------------------------------------------------------------
2+ 5 | (apply = () => 1)(()) // error // error
3+ | ^^^^^^^^^^^^^^^^^
4+ | Named tuples that define an `apply` field do not allow field selection.
5+ | The `apply` field should be renamed to something else.
6+ -- [E007] Type Mismatch Error: tests/neg/named-tuple-apply.scala:5:20 --------------------------------------------------
7+ 5 | (apply = () => 1)(()) // error // error
8+ | ^^
9+ | Found: Unit
10+ | Required: Int
11+ |
12+ | longer explanation available when compiling with `-explain`
13+ -- Error: tests/neg/named-tuple-apply.scala:6:29 -----------------------------------------------------------------------
14+ 6 | (apply = () => 1, foo = 2).foo // error
15+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+ | Named tuples that define an `apply` field do not allow field selection.
17+ | The `apply` field should be renamed to something else.
18+ -- Error: tests/neg/named-tuple-apply.scala:7:29 -----------------------------------------------------------------------
19+ 7 | (apply = () => 1, foo = 2).apply(1) // error
20+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+ | Named tuples that define an `apply` field do not allow field selection.
22+ | The `apply` field should be renamed to something else.
23+ -- [E050] Type Error: tests/neg/named-tuple-apply.scala:10:2 -----------------------------------------------------------
24+ 10 | foo() // error (error message could be better)
25+ | ^^^
26+ | method apply in class Foo does not take parameters
27+ |
28+ | longer explanation available when compiling with `-explain`
Original file line number Diff line number Diff line change 1+ class Foo :
2+ def apply : () => Int = () => 2
3+
4+ def test =
5+ (apply = () => 1 )(()) // error // error
6+ (apply = () => 1 , foo = 2 ).foo // error
7+ (apply = () => 1 , foo = 2 ).apply(1 ) // error
8+
9+ val foo = Foo ()
10+ foo() // error (error message could be better)
You can’t perform that action at this time.
0 commit comments