[ 
https://issues.apache.org/jira/browse/GROOVY-12754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18145163#comment-18145163
 ] 

ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------

paulk-asert commented on PR #3293:
URL: https://github.com/apache/groovy/pull/3293#issuecomment-6894931205

   Thanks again, these were good finds. Three new commits are pushed on top of 
92a16f61:
   
   - a72eaff561 corrects the `getClass()` compatibility notes. They compared 
with master, where `getClass()` was typed `Class<?>`; they now compare with 
6.0.0, where it was typed `Class<? extends Object>` and accepted for any 
`Class<? extends X>`. My earlier reply had the same slip, where it said the 
`def` case compiled "when `getClass()` was typed `Class<?>`".
   - 67d737b831 closes the holes listed below.
   - 67f0591e7e fixes an inference bug I found along the way (see below).
   
   I compiled each of your programmes under `@CompileStatic` and `@TypeChecked` 
with 5.1.3, 6.0.0, master and this tip, and the Java equivalents with javac. 
The two annotations agree in every case. The table is the useful part, since 
some of these were new in this PR and some date back to the releases:
   
   | Case | 5.1.3 | 6.0.0 | master | this PR | javac |
   |---|---|---|---|---|---|
   | `List<List<? super Integer>> widened = same(intsOnly)` | โŒ | โŒ | โŒ | โŒ | โŒ 
|
   | `List<Integer> ints = ['a'].collect(String::toUpperCase)` | โŒ | โŒ | โŒ | โŒ 
| โŒ |
   | `toConsumer((String s) -> s.length())` as `Consumer<Object>` | โŒ | โŒ | โŒ | 
โŒ | โŒ |
   | `List<Long[]>` / `Optional<Long[]>` from an `Integer[]` | โŒ | โŒ | โŒ | โŒ | 
โŒ |
   | `items*.getClass()` as `List<Class<? extends T>>` | โœ… | โœ… | โŒ | โŒ | โŒ |
   | `rawLists*.getClass()` as `List<Class<? extends List<String>>>` | โœ… | โœ… | 
โŒ | โŒ | โŒ |
   | `rawClassList.collect { it }` (a parameter) | โœ… | โœ… | โœ… | โŒ | โŒ |
   | `ArrayList<Class<? extends List>>` assigned or returned as `List<Class<? 
extends List<String>>>` | โœ… | โœ… | โœ… | โŒ | โŒ |
   | `cast`: `List<T> r = s.collect { String x -> x }` | โœ… | โœ… | โœ… | โœ… (NYI 
test) | โŒ |
   | `bypassClassOf`: `singletonList(u.getClass())` | โœ… | โœ… | โŒ | โœ… (NYI test, 
GROOVY-12769) | โŒ |
   | `bypassBound`: `singletonList(c)` as `List<Class<? extends U>>` | โœ… | โœ… | 
โœ… | โœ… (NYI test) | โŒ |
   | `map.put('k', raw)` / `box.set(raw)` | โœ… | โœ… | โœ… | โœ… (NYI test) | โŒ |
   | `Class<? extends Number> c = s` after `s instanceof Class` | ๐Ÿ’ฅ | ๐Ÿ’ฅ | ๐Ÿ’ฅ | ๐Ÿ’ฅ 
(NYI test, GROOVY-12770) | โŒ |
   | `Inner` in a class nested in `Sub extends Outer<String>` | โŒ | โŒ | โŒ | โŒ 
(GROOVY-12771) | โœ… |
   
   โœ… compiles ยท โŒ compile error ยท ๐Ÿ’ฅ compiles, then fails at run time ยท NYI = 
`@NotYetImplemented` test
   
   **Closed in this PR.** `same(intsOnly)`, the method reference, the lambda 
parameters and the array type arguments were all rejected by the releases. 
92a16f61 had let them through, so these were regressions, now fixed along the 
lines you suggested:
   - `isApplicableForTargetType` checks the instantiated return type against 
the target.
   - The functional-argument loop returns false for an argument it cannot type. 
A method reference is checked by its return type, and a closure or lambda by 
its parameters against the SAM's.
   - `isSubtypeWithoutConversion` recurses into array components, and primitive 
components must be equal.
   
   Your `widened.add(['boom'])` doesn't compile in any version, so I used 
`add(new ArrayList<Object>(['boom']))` to show the pollution.
   
   **Also closed, although the releases accepted them.** These are the spread, 
the `collect` over a `Class<? extends List>` parameter, and the 
`ArrayList<Class<? extends List>>` assignment:
   - `isSpreadApplicableForTargetType` now uses `hasIncompatibleGenerics`.
   - `upperBoundEscapes` recurses into invariant type arguments.
   - `COMPATIBILITY.md` lists all of these as new compile errors.
   
   **Still open, all accepted by 5.1.3 and 6.0.0.** `cast`, `bypassClassOf`, 
`bypassBound`, `map.put`/`box.set` and the `instanceof Class` case all compiled 
in every release. Rather than widen this PR further, each one has a 
`@NotYetImplemented` test expecting the javac result:
   - `bypassClassOf` is GROOVY-12769. It is a real hole, not just a formal one: 
`bypassClassOf(new ArrayList<String>())` returns a `Class<? extends 
List<String>>`, whose `cast` then lets an `ArrayList<Integer>` through as a 
`List<String>`, ending in a `ClassCastException`.
   - The `instanceof Class` case is GROOVY-12770. I'd rather the test 
establishes a `Class<?>`, which is what `s instanceof Class<?> c` gives in 
Java, than treat the intersection as a raw `Class`. With a raw `Class`, the 
assignment would only be an unchecked conversion.
   - The `map.put` and `box.set` cases need the 
`hasUnresolvedGenerics(resolvedType)` guard in `typeCheckMethodsWithGenerics` 
relaxed. That reaches every call on a parameterized receiver, so I'd like to do 
it separately with its own run of the probes.
   
   The inherited `Inner` is GROOVY-12771. It isn't specific to generics: an 
inherited member class, static or not, doesn't resolve by its simple name in 
any class nested in the subclass. Like you, I don't see it blocking this PR.
   
   **`items*.getClass()` for a `List<T>`.** While checking your spread case I 
tried it with `List<T> items` instead of `List<Object>`. It's a compile error 
with this PR, as in javac, although 6.0.0 accepted it. I think that's right. 
`T` may be `List<String>`, and a class has no type arguments; that's the same 
reason `bypassClassOf` is unsound. `COMPATIBILITY.md` gives two alternatives: 
the cast Java needs, or `items*.getClass()*.asSubclass(type)` with a `Class<T> 
type`.
   
   That last one turned up the bug fixed in 67f0591e7e. `c.asSubclass(type)` 
for a `Class<T> type` was inferred as `Class<? extends Object>` when the 
caller's type variable happened to be called `T`, the same name as `Class`'s 
own type parameter. `inferReturnTypeGenerics` filled in the method's type 
parameters from the call, then substituted the receiver's type parameters into 
the result, capturing the caller's `T`. Both are now substituted in one pass.
   
   The full root test suite passes, and lint is clean. I'd be glad of another 
look.
   




> STC: an upper-bounded wildcard argument is checked against a type parameter's 
> bound the wrong way round
> -------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12754
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12754
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> Static type checking rejects a call that passes an upper-bounded wildcard, 
> such as {{List<? extends Integer>}}, to a generic method whose type parameter 
> is bounded by a supertype of the wildcard's bound. It accepts the call when 
> the wildcard's bound is wider than the type parameter's. javac does the 
> opposite in both cases.
> {code:groovy}
> @groovy.transform.CompileStatic
> class C {
>     static <T extends Number> T first(List<T> list) { list.get(0) }
>     static <T extends CharSequence> int len(Class<T> type) { 0 }
>     static <T extends Integer> T narrow(List<T> list) { list.get(0) }
>     static Object a(List<? extends Integer> ints) { first(ints) }  // 
> rejected, javac accepts
>     static int b(Class<? extends String> type) { len(type) }       // 
> rejected, javac accepts
>     static Object c(List<? extends Number> nums) { narrow(nums) }  // 
> accepted, javac rejects
> }
> {code}
> {noformat}
> [Static type checking] - Cannot call <T extends java.lang.Number> 
> C#first(java.util.List<T>) with arguments [java.util.List<? extends 
> java.lang.Integer>]
> [Static type checking] - Cannot call <T extends java.lang.CharSequence> 
> C#len(java.lang.Class<T>) with arguments [java.lang.Class<? extends 
> java.lang.String>]
> {noformat}
> The capture of {{? extends B}} is a subtype of {{B}} (JLS 5.1.10), so it is 
> within the bound of {{T}} exactly when {{B}} is. javac reports "inference 
> variable T has incompatible bounds" for {{c}}. Groovy compiles {{c}}, and 
> {{c([1.5d])}} returns {{1}}.
> {{StaticTypeCheckingSupport#compatibleConnection}} tests a wildcard witness 
> with {{connection.isCompatibleWith(bound)}}, which asks whether the type 
> parameter's bound fits within {{? extends B}}. For a type argument {{B}}, it 
> tests {{? super B}} against the bound. An upper-bounded wildcard should take 
> the same test.
> PR: https://github.com/apache/groovy/pull/3285



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to