Skip to content

Commit

Permalink
Backport "Use constructor's default getters in case class synthetic `…
Browse files Browse the repository at this point in the history
…apply` methods" to LTS (#20725)

Backports #18716 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jun 22, 2024
2 parents c7f9073 + b68b34b commit ea2adba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i18715.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
case class Foo(x: Int = 0)

extension (x: Any)
private def foo = Foo
export foo.apply

0 comments on commit ea2adba

Please sign in to comment.