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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]