Skip to content

MalformedParameterizedTypeException in getGenericReturnType for T[X,Y] <: Iterable[(X, Y)] #15903

Open
@mprihoda

Description

@mprihoda

Compiler version

3.1.3

Minimized code

trait NotWorking:
  def example[K, A, T[X,Y] <: Iterable[(X, Y)]](ab: String): T[K,A] = ???

trait Working:
  type M[X,Y] = Iterable[(X, Y)]
  def example[K, A, T[X,Y] <: M[X,Y]](ab: String): T[K,A] = ???

// Throws java.lang.reflect.MalformedParameterizedTypeException: Mismatch of count of formal and actual type arguments in constructor of scala.collection.Iterable: 1 formal argument(s) 2 actual argument(s)
classOf[NotWorking].getMethods().map(_.getGenericReturnType())

// Works
classOf[Working].getMethods().map(_.getGenericReturnType())

Output

// No output or an exception thrown

Expectation

I'd expect to be able to obtain generic return type in both cases. I'd not expect observable difference between the two cases.

In the wild - stumbled upon this while trying to make GraalVM native image from an app using zio-json, where JSONEncoder has similar type parameters in keyValueIterator method:

https://github.com/zio/zio-json/blob/abb49d70ab307e983a9da479969f0256f0be0d30/zio-json/shared/src/main/scala/zio/json/JsonEncoder.scala#L431

Interestingly the exception is thrown when trying to get the return type of the trait's method, but not when accessing the completed JSONEncoder object's method.

Tested with OpenJDK 17 and OpenJDK 11.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions