From ec118412e0c1e84d8e4fce99c8ed2a7c922656d2 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Wed, 24 Apr 2024 18:47:00 +0200 Subject: [PATCH] Fix default args lookup for given classes --- .../src/dotty/tools/dotc/typer/Applications.scala | 15 ++++++++++++++- tests/pos/20088.scala | 6 ++++++ tests/pos/20088b.scala | 6 ++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/pos/20088.scala create mode 100644 tests/pos/20088b.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 184b250e94fb..4213c20f5a40 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -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 diff --git a/tests/pos/20088.scala b/tests/pos/20088.scala new file mode 100644 index 000000000000..308c5a0f0a91 --- /dev/null +++ b/tests/pos/20088.scala @@ -0,0 +1,6 @@ +trait Foo +trait Bar + +given (using foo: Foo = new {}): Bar with {} + +def Test = summon[Bar] diff --git a/tests/pos/20088b.scala b/tests/pos/20088b.scala new file mode 100644 index 000000000000..8cbf79d16959 --- /dev/null +++ b/tests/pos/20088b.scala @@ -0,0 +1,6 @@ +trait Foo +class Bar + +given (using foo: Foo = new {}): Bar() + +def Test = summon[Bar]