Repository navigation
GROOVY-12754: STC: an upper-bounded wildcard argument is checked against a type parameter's bound the wrong way round - #3285
Conversation
…nst a type parameter's bound the wrong way round
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
daniellansun
left a comment
There was a problem hiding this comment.
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<?>andList<? extends Object>are accepted here and rejected by javac.List<? super Integer>andList<? super Number>are accepted here and rejected by javac. AList<Object>that holds aString, viewed asList<? super Integer>, compiles and then throwsGroovyCastException: 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<?>andClass<? 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.
|
Thanks for the detailed review. I reproduced the compile results against javac 25.0.4 and pushed a6322b5. The bound check. The
Tests. Each of those programs is in
It also makes code that compiles today an error, as it is in javac: 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. 2. One more thing I noticed while testing: with an unbounded type parameter, 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. |
|
Thanks Scott, the capture-based revision is a big improvement. I ran a set of 1. A bound that names another type parameter is skipped and never checked. In if (bound.isGenericsPlaceHolder()) continue; // another type parameterBoth 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 2. Backport to 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:
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 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 One observation, not a request: with |
…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.
|
@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
The cases are in 2.
On that branch, the static type checking, static compilation and generics tests pass, except the negative tests for calls that On the overload observation: understood, and no change from me there. |
…d-argument # Conflicts: # COMPATIBILITY.md
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures🏷️ Commit: 85e5173 Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
|
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. |
https://issues.apache.org/jira/browse/GROOVY-12754
compatibleConnectionchecked 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: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 returned1for1.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.
inferenceChecknow 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, andList<? extends String>is rejected for<T extends CharSequence & Appendable>.Class<? extends ArrayList<Integer>>is accepted for<T extends List<Integer>>andClass<? extends ArrayList<String>>is rejected. A rawClass<? extends ArrayList>stays an unchecked conversion.List<? extends E>is checked through the bound of the type variableE.Map<? extends Integer, ? extends Integer>is rejected for<T extends Number> take(Map<T, T>).List<? extends String>is rejected for<T extends Number, U extends T> U v(List<U>), andClass<? extends ArrayList<String>>for<T extends Number, U extends List<T>> v(Class<U>), whereTis 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, wherex.classis no longer typedClass<?>: until thenfoo(l.class, l)intestMethodLevelGenericsForMethodCalldepends on aClass<?>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:
narrowabove,List<? extends Serializable>for<T extends Number>,List<? extends E>for a type parameter whose boundEis not within,Class<? extends List<String>>for<T extends List<Integer>>,Enum.valueOf(type, name)with aClass<? extends Enum<?>>andCollections.sort(list)with aList<? extends Comparable<?>>. A raw bound, as inClass<? extends Enum>, is still accepted.GenericsSTCTest, and soGenericsStaticCompileTest, has the Groovy forms, andGenericsJavaCompatibilityTestchecks 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, fornarrow, for? extends Eand forClass<? extends List<String>>fail without the change. The other negative cases pass on master, which rejected every? extendsargument 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.