paulk-asert commented on PR #3293:
URL: https://github.com/apache/groovy/pull/3293#issuecomment-6885390375
Thanks for the thorough review. The branch is now rebased on master, and
Scott's commits are squashed by topic with his authorship kept, so it is 17
commits. The description is rewritten. Point by point:
**1. `Closure#call`.** Fixed in d21c0e79e8. The closure's return value now
takes the declared return type when the target types it, so `c.call(x)` and
`c(x)` are a `List<Class<?>>`.
**Argument position.** I've gone the other way from locking the rejections
in. `takes(Collections.singletonList(x.getClass()))`, `takes(list*.getClass())`
and `takes(list.collect { it.getClass() })` compile on master and on every
release, and javac accepts the first, so rejecting them would have been a
regression in this PR. An argument now takes its type from the parameter of the
selected method, as an assignment or return target does. A generic call nested
in another, such as `same(Collections.singletonList(x.getClass()))`, is typed
the same way. The method is selected before this happens, so the choice of
overload doesn't change. Tests lock the accepted forms and the ones that must
stay rejected: a variable that already has a type, `same(l)` for a `List<?
extends Number>`, and a wrong element type.
The one form that stays rejected is a variable declared with `def` or `var`,
which has the type of its value: `def v =
Collections.singletonList(x.getClass()); takes(v)`. That is what javac does
with `var`. The error now suggests the fix, `... Declare variable v as
java.util.List<java.lang.Class<?>> to pass it`. It only does so when that
declaration would compile.
**2. Description, diamond and overloads.** The description is rewritten. It
includes the raw-bound erasure case, and the truncated sentences and the typo
are fixed. The guide and `COMPATIBILITY.md` describe diamond inference from the
constructor arguments, including the `List<?>` case and the ways to keep that
compiling.
I compiled `Holder<Number> h = new Holder<>(list)`. It is accepted, and it
is unsound as you suspected. A `Double` can then be added to the
`ArrayList<Integer>`. However, it doesn't come from this PR. 4.0.33, 5.1.3,
6.0.0 and master all accept it, for a `List<? extends Integer>` and for a plain
`List<Integer>`, while `new Holder<Number>(list)` is rejected. It's filed as
GROOVY-12765, and `testDiamondTypeArgumentsAreCheckedAgainstTarget` records it
with `@NotYetImplemented`.
The overload point is now in `COMPATIBILITY.md`. It isn't new behaviour
either: with `f(Object)` and `f(Class<String>)`, `f(sb.getClass())` was
rejected in the same way when `getClass()` was typed `Class<?>`; only the type
in the message changed. The note gives the workaround of casting the argument
to `Object`.
**3. Explicit type arguments.** I compiled your program on this branch,
master, 6.0.0 and 5.1.3, and got something different from what you describe:
- Under `@CompileStatic` it doesn't pollute the list. It fails at run time
with `GroovyCastException: Cannot cast object 'hello' with class
'java.lang.String' to class 'java.lang.StringBuilder'`. Under `@TypeChecked` it
runs, and the list holds the `String`.
- The explicit type argument isn't the cause. `add(list, "hello")` without
`<CharSequence>` behaves identically. Flow typing gives the local `list` the
type of its initializer, `ArrayList<StringBuilder>`. When `list` is a parameter
typed `List<? extends CharSequence>`, both the explicit and the inferred forms
are rejected, as they have been since 5.x. A test now locks the rejection of
the explicit form.
The underlying gap is real, and broader than wildcards. For `<T>
add(List<T>, T)`, `add(builders, 'hello')` for a `List<StringBuilder>`, and
`add(chars, 42)` for a `List<CharSequence>`, compile in every version from 4.0
on. So do `this.<String>first(chars)` and `this.<CharSequence>add(builders,
…)`. javac rejects all of them. That's GROOVY-12766, recorded by two
`@NotYetImplemented` tests. The rule you suggest, checking an explicit type
argument against the parameters as usual, is the fix I'd expect there.
`this.<Integer>first(ints)` for a `List<? extends Integer>` and `Double d =
first(ints)` are locked by a test. A comment says javac rejects the first and
that it's safe only because `Integer` is final. I haven't added a
`COMPATIBILITY.md` entry, since this PR doesn't change that behaviour. Instead,
the description's "Not changed" section lists it, along with the narrowed-local
difference between the two annotations.
**Other tests you mentioned.** `getAnnotation(Class<?>)`, and
`Collections.sort` on a `List<?>` or a `List<? super Integer>`, now have tests.
They are compile errors, and the workarounds the guide gives compile.
**Lint.** It's green after the rebase.
**One more thing found along the way.** Target typing had checked a type
argument with the rules for an assigned value, which allow conversions. So
`List<Long> l = Collections.singletonList(1)`, `Optional<Long> o =
Optional.of(1)`, `List.of(4)` for a `List<Double>` and
`Collections.singletonList("a$x")` for a `List<String>` compiled, and stored an
`Integer` or a `GString`. 635bea377e makes these compile errors again: the
elements of a list are stored as they are, so the type argument isn't a
conversion. Subtypes are still accepted: an `Integer` for a `List<Number>`, a
`GString` for a `List<CharSequence>`.
To make sure the PR doesn't break working code, I compiled and ran about 80
programs under both annotations with 5.1.3, 6.0.0, master and this branch.
- Among those programs, the PR newly rejects only two kinds of code that
compiled with 6.0.0: a `List<?>` passed to a diamond whose class has a bounded
type parameter, and the `def` variable case above. Both are in
`COMPATIBILITY.md` with the change that keeps them compiling.
- A few more forms are rejected on master already, by GROOVY-12714.
- Separately, the GROOVY-12754 wildcard checks reject calls that javac
rejects, such as `getAnnotation(Class<?>)`, `Collections.sort(List<?>)` and
`Enum.valueOf` with a `Class<? extends Enum<?>>`. `COMPATIBILITY.md` lists them
with their workarounds.
Root `:test` passes: 20,168 tests, no failures.
--
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]