Skip to content

Commit b68b34b

Browse files
nicolasstuckiWojciechMazur
authored andcommitted
Use constructor's default getters in case class synthetic apply methods
Fixes #18715 [Cherry-picked ef14582]
1 parent e7dd7b3 commit b68b34b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,11 +1137,16 @@ class Namer { typer: Typer =>
11371137

11381138
def foreachDefaultGetterOf(sym: TermSymbol, op: TermSymbol => Unit): Unit =
11391139
var n = 0
1140+
val methodName =
1141+
if sym.name == nme.apply && sym.is(Synthetic) && sym.owner.companionClass.is(Case) then
1142+
// The synthesized `apply` methods of case classes use the constructor's default getters
1143+
nme.CONSTRUCTOR
1144+
else sym.name
11401145
for params <- sym.paramSymss; param <- params do
11411146
if param.isTerm then
11421147
if param.is(HasDefault) then
1143-
val getterName = DefaultGetterName(sym.name, n)
1144-
val getter = pathType.member(DefaultGetterName(sym.name, n)).symbol
1148+
val getterName = DefaultGetterName(methodName, n)
1149+
val getter = pathType.member(getterName).symbol
11451150
assert(getter.exists, i"$path does not have a default getter named $getterName")
11461151
op(getter.asTerm)
11471152
n += 1

tests/pos/i18715.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
case class Foo(x: Int = 0)
2+
3+
extension (x: Any)
4+
private def foo = Foo
5+
export foo.apply

0 commit comments

Comments
 (0)