From b68b34bd6a1d88cda845ad679fcad86615f5f2aa Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 18 Oct 2023 14:11:11 +0200 Subject: [PATCH] Use constructor's default getters in case class synthetic `apply` methods Fixes #18715 [Cherry-picked ef1458270dc5f84f5bf58dcd8582357a9c99b126] --- compiler/src/dotty/tools/dotc/typer/Namer.scala | 9 +++++++-- tests/pos/i18715.scala | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 tests/pos/i18715.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 9aad20113154..36ffbd2e64a4 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -1137,11 +1137,16 @@ class Namer { typer: Typer => def foreachDefaultGetterOf(sym: TermSymbol, op: TermSymbol => Unit): Unit = var n = 0 + val methodName = + if sym.name == nme.apply && sym.is(Synthetic) && sym.owner.companionClass.is(Case) then + // The synthesized `apply` methods of case classes use the constructor's default getters + nme.CONSTRUCTOR + else sym.name for params <- sym.paramSymss; param <- params do if param.isTerm then if param.is(HasDefault) then - val getterName = DefaultGetterName(sym.name, n) - val getter = pathType.member(DefaultGetterName(sym.name, n)).symbol + val getterName = DefaultGetterName(methodName, n) + val getter = pathType.member(getterName).symbol assert(getter.exists, i"$path does not have a default getter named $getterName") op(getter.asTerm) n += 1 diff --git a/tests/pos/i18715.scala b/tests/pos/i18715.scala new file mode 100644 index 000000000000..5023ef211fa3 --- /dev/null +++ b/tests/pos/i18715.scala @@ -0,0 +1,5 @@ +case class Foo(x: Int = 0) + +extension (x: Any) + private def foo = Foo + export foo.apply