Skip to content

Commit

Permalink
Fix default args lookup for given classes (#20256)
Browse files Browse the repository at this point in the history
Fixes #20088.
  • Loading branch information
odersky authored Apr 26, 2024
2 parents f026720 + ec11841 commit 82afe52
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,20 @@ object Applications {
if (getterDenot.exists) qual.select(TermRef(qual.tpe, getterName, getterDenot))
else EmptyTree
if !meth.isClassConstructor then
selectGetter(receiver)
val res = selectGetter(receiver)
if res.isEmpty && meth.is(Given) then
val classSym = meth.info.finalResultType.typeSymbol
if classSym.isClass && classSym.isAllOf(Given | Synthetic) then
// `meth` is an implicit wrapper: the `given def` desugared from a
// `given C(...)` or `given C with ...` by `desugar#classDef`.
// Therefore, we can try to look for the default getters of the
// constructor of the `given class`. We find it via the `given
// def`'s result type. See #20088 and associated test cases.
val classRefTree = receiver.select(classSym)
val constructorSym = classSym.primaryConstructor.asTerm
findDefaultGetter(constructorSym, classRefTree, idx)
else res
else res
else
// default getters for class constructors are found in the companion object
val cls = meth.owner
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/20088.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Foo
trait Bar

given (using foo: Foo = new {}): Bar with {}

def Test = summon[Bar]
6 changes: 6 additions & 0 deletions tests/pos/20088b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Foo
class Bar

given (using foo: Foo = new {}): Bar()

def Test = summon[Bar]

0 comments on commit 82afe52

Please sign in to comment.