Skip to content

Commit

Permalink
Use constructor's default getters in case class synthetic apply met…
Browse files Browse the repository at this point in the history
…hods (scala#18716)

Fixes scala#18715
  • Loading branch information
nicolasstucki authored Oct 18, 2023
2 parents e6242ca + ef14582 commit 5f5b517
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 @@ -1139,11 +1139,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 5f5b517

Please sign in to comment.