Skip to content

resolveSubtype() treats wildcard and raw type parameters as exact types instead of upper bounds #130

Closed

Description

@cowtowncoder

(follow-up to #127)

When resolving types, wildcards and raw types are erased to their upper bound (Wrapper<?> and raw Wrapper both become Wrapper<Object>; raw NumWrapper<N extends Number> becomes NumWrapper<Number>). After that, TypeResolver.resolveSubtype() cannot tell them apart from explicitly declared types, so it compares them for equality instead of treating them as upper bounds.

Fails, but should succeed

static class Wrapper<T> { }
static class ArrayWrapper<E> extends Wrapper<E[]> { }
static class IntWrapper extends Wrapper<Integer> { }
static class NumWrapper<N extends Number> { }
static class IntNumWrapper extends NumWrapper<Integer> { }
static class Pair<A,B> { }
static class SamePair<E> extends Pair<E,E> { }
static class RawExt extends Wrapper { } // raw

TypeResolver r = new TypeResolver();

// raw / unbounded wildcard into array type parameter:
// "Type parameter #1/1 differs; expected java.lang.Object, got <0>[]"
r.resolveSubtype(r.resolve(Wrapper.class), ArrayWrapper.class);
r.resolveSubtype(r.resolve(new GenericType<Wrapper<?>>() { }), ArrayWrapper.class);

// bounded wildcard / bounded raw type:
// "Type parameter #1/1 differs; expected java.lang.Number, got java.lang.Integer"
r.resolveSubtype(r.resolve(new GenericType<Wrapper<? extends Number>>() { }), IntWrapper.class);
r.resolveSubtype(r.resolve(NumWrapper.class), IntNumWrapper.class);

// nested wildcard in a repeated type variable binding:
// "Conflicting bindings for type variable `E` of SamePair: List<Object> vs List<String>"
r.resolveSubtype(r.resolve(new GenericType<Pair<List<?>,List<String>>>() { }), SamePair.class);

// subtype extending raw type (unchecked, but assignable):
// "Type parameter #1/1 differs; expected java.lang.String, got java.lang.Object"
r.resolveSubtype(r.resolve(Wrapper.class, String.class), RawExt.class);

Succeeds, but arguably should not

Since an explicit Object cannot be told apart from a wildcard, the special case added for #127 (a top-level Object binding of a repeated type variable is compatible with a more specific one) also accepts:

// returns SamePair<String>
r.resolveSubtype(r.resolve(Pair.class, String.class, Object.class), SamePair.class);

and the bounds check added for #127 skips all Object type parameters.

Suggested approach

Mark bindings that come from wildcards or raw types (in TypeBindings / ResolvedType), and verify them in resolveSubtype() as upper bounds (assignability) rather than by equality, binding any type placeholders within to their type variables' bounds. That would replace the Object special cases added for #127.

Activity

  1. cowtowncoder commented on Oct 9, 2026

    @cowtowncoder
    MemberAuthor

    Two related items deferred from #127:

    Bounds of type variables are not verified

    resolveSubtype() does not verify that type parameters of the subtype satisfy bounds of its type variables:

    static class NumArrayWrapper<E extends Number> extends Wrapper<E[]> { }
    
    // returns NumArrayWrapper<String>
    r.resolveSubtype(r.resolve(Wrapper.class, String[].class), NumArrayWrapper.class);

    A check was tried for #127, but it can not be made reliable as long as wildcards are erased to their upper bound: Wrapper<? extends Number> resolves to Wrapper<Number>, so refining it into IntOnlyWrapper<E extends Integer> extends Wrapper<E> binds E to Number, which would fail the bound check even though Java allows it. Once wildcard/raw-derived bindings are tracked (as suggested above), bounds can be checked for exact bindings only.

    Self-reference supertype resolves to the referenced type, not its own bindings

    static class SelfParam<T> extends Wrapper<SelfParam<String>> { }
    static class SelfParamSub<X> extends SelfParam<X> { }
    
    // self-reference `SelfParam<String>`, referencing type being resolved, `SelfParam<Integer>`
    ResolvedType selfRef = r.resolve(SelfParam.class, Integer.class)
            .getParentClass().getTypeParameters().get(0);
    // returns SelfParamSub<Integer>; should be SelfParamSub<String>
    r.resolveSubtype(selfRef, SelfParamSub.class);

    (same as in 1.7). Using bindings of the self-reference itself instead would fix this, but breaks raw self-bounded types: bindings of the self-reference within raw Enum are <Object> (to break the cycle), so it would become Enum<Object> instead of raw Enum. These two cases can not currently be told apart.

  2. added this to the 1.8.0 milestone on Oct 10, 2026
  3. added a commit that references this issue on Nov 26, 2026
    c4bf424
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions