From fb365f66025c59eeaa9d3586a1fc49df0bcdf7fc Mon Sep 17 00:00:00 2001 From: Hamza Remmal Date: Tue, 28 Oct 2025 13:42:08 +0100 Subject: [PATCH] improve the generic signatures for singleton types --- .../dotty/tools/dotc/transform/GenericSignatures.scala | 6 ++++++ tests/run/i24272.check | 1 + tests/run/i24272.scala | 8 ++++++++ 3 files changed, 15 insertions(+) create mode 100644 tests/run/i24272.check create mode 100644 tests/run/i24272.scala diff --git a/compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala b/compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala index ed02587eb1f1..a882ed8f41c2 100644 --- a/compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala +++ b/compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala @@ -246,6 +246,12 @@ object GenericSignatures { jsig(erasedUnderlying, toplevel = toplevel, unboxedVCs = unboxedVCs) else typeParamSig(ref.paramName.lastPart) + case ref: SingletonType => + // Singleton types like `x.type` need to be widened to their underlying type + // For example, `def identity[A](x: A): x.type` should have signature + // with return type `A` (not `java.lang.Object`) + jsig(ref.underlying, toplevel = toplevel, unboxedVCs = unboxedVCs) + case defn.ArrayOf(elemtp) => if (isGenericArrayElement(elemtp, isScala2 = false)) jsig1(defn.ObjectType) diff --git a/tests/run/i24272.check b/tests/run/i24272.check new file mode 100644 index 000000000000..e58ee24a2424 --- /dev/null +++ b/tests/run/i24272.check @@ -0,0 +1 @@ +public B Foo.bar(B) diff --git a/tests/run/i24272.scala b/tests/run/i24272.scala new file mode 100644 index 000000000000..8c314195b626 --- /dev/null +++ b/tests/run/i24272.scala @@ -0,0 +1,8 @@ +// scalajs: --skip + +class Foo: + def bar[B](x: B): x.type = x + +@main def Test = + for mtd <- classOf[Foo].getDeclaredMethods.sortBy(_.getName) do + println(mtd.toGenericString)