Skip to content

Commit fb365f6

Browse files
committed
improve the generic signatures for singleton types
1 parent 2f39bfa commit fb365f6

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ object GenericSignatures {
246246
jsig(erasedUnderlying, toplevel = toplevel, unboxedVCs = unboxedVCs)
247247
else typeParamSig(ref.paramName.lastPart)
248248

249+
case ref: SingletonType =>
250+
// Singleton types like `x.type` need to be widened to their underlying type
251+
// For example, `def identity[A](x: A): x.type` should have signature
252+
// with return type `A` (not `java.lang.Object`)
253+
jsig(ref.underlying, toplevel = toplevel, unboxedVCs = unboxedVCs)
254+
249255
case defn.ArrayOf(elemtp) =>
250256
if (isGenericArrayElement(elemtp, isScala2 = false))
251257
jsig1(defn.ObjectType)

tests/run/i24272.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public <B> B Foo.bar(B)

tests/run/i24272.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// scalajs: --skip
2+
3+
class Foo:
4+
def bar[B](x: B): x.type = x
5+
6+
@main def Test =
7+
for mtd <- classOf[Foo].getDeclaredMethods.sortBy(_.getName) do
8+
println(mtd.toGenericString)

0 commit comments

Comments
 (0)