[
https://issues.apache.org/jira/browse/GROOVY-12754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18143318#comment-18143318
]
ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------
codeconsole commented on PR #3285:
URL: https://github.com/apache/groovy/pull/3285#issuecomment-6812789846
@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 efa0808656. 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.
> 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)