Skip to content

Commit 77b66cd

Browse files
mbovelWojciechMazur
authored andcommitted
Fix default args lookup for given classes
[Cherry-picked ec11841]
1 parent 900e74b commit 77b66cd

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Applications.scala

+14-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,20 @@ object Applications {
275275
if (getterDenot.exists) qual.select(TermRef(qual.tpe, getterName, getterDenot))
276276
else EmptyTree
277277
if !meth.isClassConstructor then
278-
selectGetter(receiver)
278+
val res = selectGetter(receiver)
279+
if res.isEmpty && meth.is(Given) then
280+
val classSym = meth.info.finalResultType.typeSymbol
281+
if classSym.isClass && classSym.isAllOf(Given | Synthetic) then
282+
// `meth` is an implicit wrapper: the `given def` desugared from a
283+
// `given C(...)` or `given C with ...` by `desugar#classDef`.
284+
// Therefore, we can try to look for the default getters of the
285+
// constructor of the `given class`. We find it via the `given
286+
// def`'s result type. See #20088 and associated test cases.
287+
val classRefTree = receiver.select(classSym)
288+
val constructorSym = classSym.primaryConstructor.asTerm
289+
findDefaultGetter(constructorSym, classRefTree, idx)
290+
else res
291+
else res
279292
else
280293
// default getters for class constructors are found in the companion object
281294
val cls = meth.owner

Diff for: tests/pos/20088.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Foo
2+
trait Bar
3+
4+
given (using foo: Foo = new {}): Bar with {}
5+
6+
def Test = summon[Bar]

Diff for: tests/pos/20088b.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Foo
2+
class Bar
3+
4+
given (using foo: Foo = new {}): Bar()
5+
6+
def Test = summon[Bar]

0 commit comments

Comments
 (0)