From dad90f44b553ee4fb6cdb4f0712a2474f8627da7 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Tue, 5 Dec 2023 18:35:24 +0100 Subject: [PATCH] Fix getter memoized field signature Co-authored-by: Carlo Sana Co-authored-by: Matt Bovel --- .../src/dotty/tools/backend/jvm/BCodeHelpers.scala | 3 +++ tests/generic-java-signatures/17069.scala | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 tests/generic-java-signatures/17069.scala diff --git a/compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala b/compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala index 6331913049c2..2ad58fea4cd1 100644 --- a/compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala +++ b/compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala @@ -397,6 +397,9 @@ trait BCodeHelpers extends BCodeIdiomatic { atPhase(erasurePhase) { val memberTpe = if (sym.is(Method)) sym.denot.info + else if sym.denot.validFor.phaseId > erasurePhase.id && sym.isField && sym.getter.exists then + // Memoization field of getter entered after erasure, see run/i17069 for an example + sym.getter.denot.info.resultType else owner.denot.thisType.memberInfo(sym) getGenericSignatureHelper(sym, owner, memberTpe).orNull } diff --git a/tests/generic-java-signatures/17069.scala b/tests/generic-java-signatures/17069.scala new file mode 100644 index 000000000000..279a8c88e2db --- /dev/null +++ b/tests/generic-java-signatures/17069.scala @@ -0,0 +1,10 @@ + +class Foo: + val generic: List[String] = ??? + +@main def Test = + val tpe = classOf[Foo].getDeclaredField("generic").getGenericType() + assert(tpe.getTypeName == "scala.collection.immutable.List") + + val tpe2 = classOf[Foo].getDeclaredMethod("generic").getGenericReturnType() + assert(tpe2.getTypeName == "scala.collection.immutable.List")