Skip to content

GROOVY-12754: STC: an upper-bounded wildcard argument is checked against a type parameter's bound the wrong way round - #3285

Open
codeconsole wants to merge 6 commits into
apache:masterfrom
codeconsole:stc-extends-wildcard-argument
Open

codeconsole wants to merge 6 commits into
apache:masterfrom
codeconsole:stc-extends-wildcard-argument

Conversation

@codeconsole

@codeconsole codeconsole commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/GROOVY-12754

compatibleConnection checked an upper-bounded wildcard argument against the bound of a method's type parameter the wrong way round, asking whether the bound fits within ? extends B. So static type checking rejected calls that javac accepts:

static <T extends Number> T first(List<T> list) { list.get(0) }
static <T extends CharSequence> int len(Class<T> type) { 0 }

static Object a(List<? extends Integer> ints) { first(ints) }
static int b(Class<? extends String> type) { len(type) }

And it accepted one that javac rejects, narrow(List<? extends Number>) for <T extends Integer> T narrow(List<T>), where the statically compiled call then returned 1 for 1.5.

A wildcard that is a type argument of the argument's type is captured (JLS 5.1.10), so the witness for the type parameter is a fresh type variable, bounded by the wildcard's bound and by the bound declared for that type argument. inferenceCheck now checks that capture against every bound of the type parameter, with type arguments and with the capture in place of the type parameter, and then checks the captured argument against the parameter (JLS 18.5.1):

  • List<? extends Integer> is accepted for <T extends Number>, <T extends Comparable<? super T>> and <T extends Number & Comparable<? super T>>. It is rejected for <T extends Comparable<T>>, because the capture is not comparable to itself, and List<? extends String> is rejected for <T extends CharSequence & Appendable>.
  • Class<? extends ArrayList<Integer>> is accepted for <T extends List<Integer>> and Class<? extends ArrayList<String>> is rejected. A raw Class<? extends ArrayList> stays an unchecked conversion.
  • List<? extends E> is checked through the bound of the type variable E.
  • Each wildcard has its own capture, so Map<? extends Integer, ? extends Integer> is rejected for <T extends Number> take(Map<T, T>).
  • A bound that names another type parameter is checked through that type parameter's bounds: List<? extends String> is rejected for <T extends Number, U extends T> U v(List<U>), and Class<? extends ArrayList<String>> for <T extends Number, U extends List<T>> v(Class<U>), where T is inferred from the capture's bound.

An unbounded wildcard, ? extends Object, a lower-bounded wildcard, a wildcard nested in a type argument (List<List<? extends Integer>>) and a type parameter that an earlier argument has resolved keep the existing path. The first three are checked in #3286, where x.class is no longer typed Class<?>: until then foo(l.class, l) in testMethodLevelGenericsForMethodCall depends on a Class<?> being accepted for <T extends List<Integer>>.

Master compared the wildcard with the erasure of the first bound only, so some calls that compiled before are now rejected, as javac rejects them: narrow above, List<? extends Serializable> for <T extends Number>, List<? extends E> for a type parameter whose bound E is not within, Class<? extends List<String>> for <T extends List<Integer>>, Enum.valueOf(type, name) with a Class<? extends Enum<?>> and Collections.sort(list) with a List<? extends Comparable<?>>. A raw bound, as in Class<? extends Enum>, is still accepted.

GenericsSTCTest, and so GenericsStaticCompileTest, has the Groovy forms, and GenericsJavaCompatibilityTest checks the Java forms against javac: the accepted calls above, and negative cases for a bound that names the type parameter (Comparable<T>, Number & Comparable<T>, Enum<T>), an intersection, a parameterized bound, a declared bound, two wildcards for one type parameter, a nested wildcard and a wildcard bounded by a type variable. The tests for the accepted calls, for narrow, for ? extends E and for Class<? extends List<String>> fail without the change. The other negative cases pass on master, which rejected every ? extends argument whose bound differs from the type parameter's; they hold the new check to the bounds.

GROOVY-12755 depends on this. For GROOVY_6_0_X, the comments describe an accept-only variant, which admits the calls that were wrongly rejected and rejects nothing that 6.0.0 accepts.

…nst a type parameter's bound the wrong way round
@codecov-commenter

codecov-commenter commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.11688% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.4659%. Comparing base (776a3d5) to head (85e5173).

Files with missing lines Patch % Lines
...roovy/transform/stc/StaticTypeCheckingSupport.java 83.1169% 1 Missing and 12 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3285        +/-   ##
==================================================
+ Coverage     74.4569%   74.4659%   +0.0090%     
- Complexity      44841      44876        +35     
==================================================
  Files            1702       1702                
  Lines          151994     152071        +77     
  Branches        29119      29152        +33     
==================================================
+ Hits           113170     113241        +71     
+ Misses          28415      28413         -2     
- Partials        10409      10417         +8     
Files with missing lines Coverage Δ
...roovy/transform/stc/StaticTypeCheckingSupport.java 79.5470% <83.1169%> (+0.2716%) ⬆️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reported bug is real, and the simple cases are fixed. On master, <T extends Integer> T narrow(List<T>) accepts List<? extends Number>, and both @TypeChecked and @CompileStatic return 1 for 1.5. This tip rejects that call. It accepts <T extends Number> T first(List<T>) on List<? extends Integer>, and the result is usable as Integer, Number, and int (the value is 4, not a truncated double). <T extends CharSequence> accepts Class<? extends String>. Those match javac. I compiled the same programs with javac 21.0.12.1, javac 25.0.5, and javac 25 --release 17; the three agree. The six new tests fail on 856b28a and pass on 1f5b3c1, under @TypeChecked (GenericsSTCTest), @CompileStatic (GenericsStaticCompileTest), and the javac harness.

The new check is not the JLS rule, and it opens calls javac rejects. extendsBoundOf rewrites ? extends B to ? super the raw type of B, then asks whether that raw type is a subtype of getCombinedBoundType(resolved).getPlainNodeReference(). That drops every type argument, and getCombinedBoundType keeps only the first bound. JLS 5.1.10 makes the capture of ? extends B a fresh type variable whose upper bound is glb(B, U), not B itself. Applicability (JLS 15.12.2, 18.5.1) then has to solve that capture against every bound of T, including bounds that mention T (JLS 18.3.1, 18.4). "The capture is a subtype of B, so check B" holds only when the bound is a type that does not mention T and has no type arguments that matter. It does not hold for an F-bound, an intersection, or List<Integer>.

These programs are rejected by javac and were rejected on master. This tip compiles them, with @TypeChecked and @CompileStatic in agreement:

static <T extends Comparable<T>> T id(List<T> list) { return list.get(0); }
static Object go(List<? extends Integer> list) { return id(list); }

static <T extends Number & Comparable<T>> T id(List<T> list) { return list.get(0); }
static Object go(List<? extends Integer> list) { return id(list); }

static <T extends Enum<T>> T id(List<T> list) { return list.get(0); }
static Object go(List<? extends Thread.State> list) { return id(list); }

static <T extends Comparable<T>> int cmp(List<T> list) {
    return list.get(0).compareTo(list.get(1));
}
static int go(List<? extends Integer> list) { return cmp(list); }

class Base implements Comparable<Base> {
    public int compareTo(Base o) { return 0; }
}
class Sub extends Base {}
static <T extends Comparable<T>> T id(List<T> list) { return list.get(0); }
static Object go(List<? extends Sub> list) { return id(list); }

static <T extends List<Integer>> T cast(Class<T> type) { return null; }
static void go(Class<? extends ArrayList<String>> type) { cast(type); }
static void go(Class<? extends List<String>> type) { cast(type); }

static <T extends List<Integer>> T id(List<T> lists) { return lists.get(0); }
static Object go(List<? extends ArrayList<String>> lists) { return id(lists); }

static <T extends List<? extends Integer>> T id(List<T> lists) { return lists.get(0); }
static Object go(List<? extends ArrayList<String>> lists) { return id(lists); }

class ListBox<E extends List<Integer>> {}
static <T extends List<Integer>> T id(ListBox<T> box) { return null; }
static Object go(ListBox<? extends ArrayList<String>> box) { return id(box); }
// javac: "type argument ? extends ArrayList<String> is not within bounds of type-variable E"

static <T extends CharSequence & Appendable> T id(List<T> list) { return list.get(0); }
static Object go(List<? extends String> list) { return id(list); }

static <T extends Number> void take(List<List<T>> lists) {}
static void go(List<List<? extends Integer>> lists) { take(lists); }

static <T extends Number> void take(Map<T, T> map) {}
static void go(Map<? extends Integer, ? extends Integer> map) { take(map); }

static <T extends Number> T first(List<T> list) { return list.get(0); }
static Object go(List<? extends Integer> ints) { return First.<Integer>first(ints); }

static Object go(List<? extends Integer> arg) {
    Double d = first(arg);
    return d;
}

The last one compiles here and evaluates to 4.0 (java.lang.Double). javac rejects it: the target type Double is part of inference (JLS 18.5.2). With append in the body, <T extends CharSequence & Appendable> T id(List<? extends String>) also compiles, and @CompileStatic then throws GroovyCastException: Cannot cast object 'hi' with class 'java.lang.String' to class 'java.lang.Appendable'. That is the same kind of hole as 1 for 1.5, on a call this patch newly accepts. Passing the raw ArrayList class literal to <T extends List<Integer>> Integer head(Class<? extends ArrayList<String>>, Object) with ['x'] compiles as well and returns 120.

The wildcards this patch deliberately leaves on the old path still disagree with javac. JLS 4.5.1 makes ? extends Object the same wildcard as ?. For <T extends Number> T first(List<T>):

  • List<?> and List<? extends Object> are accepted here and rejected by javac.
  • List<? super Integer> and List<? super Number> are accepted here and rejected by javac. A List<Object> that holds a String, viewed as List<? super Integer>, compiles and then throws GroovyCastException: Cannot cast object 'nope' ... to class 'java.lang.Number'.
  • <E extends Integer> first(List<? extends E>) is rejected here and accepted by javac.
  • <E extends Number> narrow(List<? extends E>) for <T extends Integer> T narrow(List<T>) is accepted here and rejected by javac.
  • <E> first(List<? extends E>) is accepted here and rejected by javac.
  • Class<?> and Class<? extends Object> for <T extends List<Integer>> T cast(Class<T>) are accepted here and rejected by javac.

? super is not an upper bound. The capture of ? super Integer is a supertype of Integer (JLS 5.1.10), so it is not a Number just because Integer is.

Please add failing tests for the programs above, in GenericsSTCTest (so GenericsStaticCompileTest runs them too) and, for the straight Java ones, in GenericsJavaCompatibilityTest. The harness compiles with JavaShell and no -Werror. A raw type that javac only notes as unchecked (Class<? extends ArrayList> for T extends List<Integer>, List for T extends Number) is a successful compile on both sides; that part of the new test is right. The harness never sees the note, so it cannot lock an error-versus-warning difference. The cases above are errors, not notes.

compatibleConnection is the general witness check used for calls, constructors, extension methods, overload disambiguation, and map putAt. It is not a getClass special case. Receiver and inner-class calls such as NumBox<? extends Integer>.get() and Outer<? extends Integer>.Inner.get() do match javac after this change. Please do not cherry-pick this revision onto GROOVY_6_0_X; it would ship the mismatches above. Once these programs match javac, that cherry-pick is the right follow-up, and GROOVY-12755 can stay stacked on the corrected change.

…ument against every bound of the type parameter

The capture of "? extends B" is a fresh type variable, so being a subtype
of B is enough only for a bound that does not mention the type parameter.
Check it against every bound, with type arguments and with the capture in
place of the type parameter, and check that the captured argument fits the
parameter. Only a wildcard that is a type argument of the argument's type
is captured. A wildcard bounded by a type variable is checked through the
bound of that type variable.
@codeconsole

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I reproduced the compile results against javac 25.0.4 and pushed a6322b5.

The bound check. The ? super B shortcut is gone. A wildcard that is a type argument of the argument's type is now treated as its capture: a fresh type variable bounded by the wildcard's bound and by the bound declared for that type argument. It is checked against every bound of the type parameter, with type arguments and with the capture in place of the type parameter, and then the captured argument is checked against the parameter. With that, these are rejected again, under @TypeChecked and @CompileStatic:

  • <T extends Comparable<T>>, <T extends Number & Comparable<T>> and cmp on List<? extends Integer>, <T extends Enum<T>> on List<? extends Thread.State>, and the Base/Sub case
  • <T extends List<Integer>> on Class<? extends ArrayList<String>>, Class<? extends List<String>> and List<? extends ArrayList<String>>, <T extends List<? extends Integer>>, and ListBox<? extends ArrayList<String>>
  • <T extends CharSequence & Appendable> on List<? extends String>, so the append cast failure and the head call that returned 120 no longer compile
  • take(List<List<T>>) on List<List<? extends Integer>> and take(Map<T, T>) on Map<? extends Integer, ? extends Integer>

Class<? extends List<String>> for <T extends List<Integer>> compiled on master as well; it is rejected now. So are a few calls that master accepted because it compared only the erasure of the first bound, and that javac rejects: Enum.valueOf(type, name) with a Class<? extends Enum<?>>, Collections.sort(list) with a List<? extends Comparable<?>>, and List<? extends Serializable> for <T extends Number>. <T extends Comparable<? super T>>, <T extends Number & Comparable<? super T>> and <T extends List<? extends Number>> are accepted for the matching wildcards, as javac accepts them.

? extends E. A wildcard bounded by a type variable is checked through the bound of that type variable, so the three you listed match javac: <E extends Integer> is accepted for <T extends Number>, and <E extends Number> for narrow and <E> for first are rejected.

Tests. Each of those programs is in GenericsSTCTest (so GenericsStaticCompileTest runs it too), and the Java forms are in GenericsJavaCompatibilityTest. The new negative tests fail on the previous revision, and the new positive ones fail on master.

?, ? extends Object and ? super. These are done as well, in a separate commit on #3286 (2b99f21) rather than here. With it List<?>, List<? extends Object>, List<? super Integer> and List<? super Number> are rejected for <T extends Number>, and Class<?> and Class<? extends Object> for <T extends List<Integer>>, while NumBox<?> and NumBox<? super Integer> for a NumBox<N extends Number> and EnumSet<?> for <T extends Enum<T>> stay accepted, as in javac. The tests are in the same three classes. It is on #3286 because of what it does before GROOVY-12755:

  • x.getClass() and x.class are typed Class<?> until GROOVY-12755, so foo(l.class, l) in testMethodLevelGenericsForMethodCall (GROOVY-5891) fails, and <T extends CharSequence> len(Class<T>) rejects len(s.getClass()), which javac accepts.

It also makes code that compiles today an error, as it is in javac: Enum.valueOf(type, name) and method.getAnnotation(type) with a Class<?>, Collections.sort(list) with a List<?>. If you prefer it in this PR, I can move it here and change the GROOVY-5891 test.

Two parts of the review are not changed. For each I've described what the change involves; I'm happy to make either, here or as a follow-up, whichever you prefer.

1. First.<Integer>first(ints). Explicit type arguments are not compared with the arguments on master: C.<String>id(listOfInteger) and C.<Integer>first(listOfNumber) compile there. Master rejected the listed call only because it rejected every ? extends argument for a bounded type parameter. Rejecting it needs a check of the arguments against the explicit type arguments, which would also reject those two calls.

2. Double d = first(arg). The call is typed Integer, and the assignment then follows Groovy's assignment rules, which let an Integer be assigned to a Double. Double d = first(listOfInteger) compiles on master in the same way and also gives 4.0, while Date d = first(arg) is rejected. Rejecting it would mean changing that rule, or using the target type in inference for this case.

One more thing I noticed while testing: with an unbounded type parameter, <T> void take(Map<T, T>) on Map<? extends Integer, ? extends Integer> and <T> void take(List<List<T>>) on List<List<? extends Integer>> compile on master, and javac rejects both. This PR does not change those.

Understood on GROOVY_6_0_X. Both commits of this PR cherry-pick onto it without conflicts, for when the open points above are settled.

@paulk-asert

Copy link
Copy Markdown
Contributor

Thanks Scott, the capture-based revision is a big improvement. I ran a set of @CompileStatic probes against master and against this branch (and the same patch applied to GROOVY_6_0_X; results were identical on both). The headline cases behave as described, and subtype arguments, multi-parameter methods, constructors, class-level bounds and DGM calls on wildcard receivers all came out fine. Two things before this goes further.

1. A bound that names another type parameter is skipped and never checked.

In compatibleCapture:

if (bound.isGenericsPlaceHolder()) continue; // another type parameter

Both of these are rejected on master and compile on this branch:

static <T extends Number, U extends T> U v(List<U> b) { b.get(0) }
static g(List<? extends String> l) { Number n = v(l); n }
// g(['x']) -> GroovyCastException: Cannot cast object 'x' with class 'java.lang.String' to class 'java.lang.Number'

static <T extends Number, U extends List<T>> int v(Class<U> c) { 1 }
static g(Class<? extends ArrayList<String>> c) { v(c) }

javac 25.0.5 rejects both ("inference variable T has incompatible bounds"), and accepts the same calls with List<? extends Integer> and Class<? extends ArrayList<Integer>>. Could you add them to GenericsSTCTest and GenericsJavaCompatibilityTest and resolve the bound through the other type parameter's own bound rather than skipping it? This is the same kind of hole as narrow, on calls the PR newly accepts, so I'd like it closed before merging to master.

2. Backport to GROOVY_6_0_X.

I'd prefer not to cherry-pick this as it stands. Matching javac here means that code which compiles and runs correctly on 6.0.0 becomes a compile error in a patch release:

  • Enum.valueOf(type, name) and EnumSet.allOf(type) with a Class<? extends Enum<?>>, which is a natural way to type an "any enum class" parameter or field
  • Collections.sort(list) with a List<? extends Comparable<?>>
  • any wildcard declared looser than the method needs, e.g. a Class<? extends Runnable> holding Thread passed to <T extends Thread> m(Class<T>)

The workarounds are easy and the COMPATIBILITY entry describes them well, but that entry is rightly headed "Groovy 7", and #3286 extends the same tightening to Class<?> and List<?>.

If GROOVY-12755 needs the accepting half on 6.0.x, one option is an accept-only variant for that branch: when the capture check fails, fall through to the existing compatibleConnection path instead of returning incompatible. That admits the calls that were wrongly rejected without rejecting anything 6.0.0 accepts. It leaves the narrow miscompile in place on 6, which is existing behaviour. I haven't built that variant, so treat it as a suggestion; happy to hear other views, @daniellansun included, since this differs from the earlier "cherry-pick once it matches javac".

One observation, not a request: with m(List<T extends Integer>) and m(Collection<?>), passing a List<? extends Number> is now an error rather than selecting the second overload as javac would. That's how the checker already treats a failed generics check, and it does mean there is no silent re-dispatch, so I'm fine leaving it.

…er type parameter

A bound such as T in <T extends Number, U extends T>, or List<T> in
<T extends Number, U extends List<T>>, was skipped. Check the capture
against the bounds of that type parameter, inferring it from the
capture's bound where the bound gives it type arguments.
@codeconsole

Copy link
Copy Markdown
Contributor Author

@paulk-asert thanks. I compiled your programs and some neighbours against javac 25.0.4 and this branch.

1. A bound that names another type parameter. Fixed in efa0808. The capture is now checked against the bounds of the type parameter that the bound names, and when the bound gives that type parameter type arguments, as List<T> does, T is inferred from the capture's bound first. Both of your programs are rejected, and the List<? extends Integer> and Class<? extends ArrayList<Integer>> forms compile. So do these, as in javac:

  • rejected: <T extends Integer, U extends T> for a List<? extends Number>, and <K extends Number, V, U extends Map<K, V>> for a Class<? extends HashMap<String, Integer>>
  • accepted: <K extends Number, V, U extends Map<K, V>> for a Class<? extends HashMap<Integer, String>>, <T extends Comparable<T>, U extends T> for a List<? extends Integer>, and <T, U extends T> for a List<? extends String>

The cases are in GenericsSTCTest (so GenericsStaticCompileTest too) and GenericsJavaCompatibilityTest. The negative ones fail on the previous revision. One related difference is left, and it predates this PR: <T extends Number, U extends T> v(T t, List<U> l) with a Double and a List<? extends Integer> is rejected, where javac widens T and accepts it. Master rejects it too.

2. GROOVY_6_0_X. I built the accept-only variant you describe, to see whether it holds. On GROOVY_6_0_X, a capture the check does not admit falls through to the existing compatibleConnection path instead of being rejected. I compiled the 115 programs I have been using for this PR against the GROOVY_6_0_X tip and against the variant:

  • Nothing that the tip accepts is rejected by the variant.
  • 34 programs that the tip rejects compile. 31 of them javac accepts, including first(List<? extends Integer>), len(Class<? extends String>), Collections.sort on a List<? extends Integer>, and the U extends T cases above.
  • The other three are C.<Integer>first(ints), Double d = first(ints) and String s = first(ints) for a List<? extends Integer>. javac rejects them, and master with this PR accepts them too; these are the explicit-type-argument and assignment points from my earlier reply. The tip rejected them only because it rejected every ? extends argument for a bounded type parameter.

On that branch, the static type checking, static compilation and generics tests pass, except the negative tests for calls that GROOVY_6_0_X accepts (narrow and the others listed in the COMPATIBILITY entry), which would be left out of a port. The variant is a local branch for now; I'm happy to open it as a PR against GROOVY_6_0_X with those tests adjusted, or to leave the port to whatever you and @daniellansun decide.

On the overload observation: understood, and no change from me there.

@testlens-app

testlens-app Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Lint gate / lint

🏷️ Commit: 85e5173
▶️ Tests: 127780 executed
⚪️ Checks: 26/26 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@paulk-asert

Copy link
Copy Markdown
Contributor

Thanks Scott, I am going to put together a bundle of related PRs affecting Grails with our generics rules spelt out a bit more clearly. I'll notify when done.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants