[
https://issues.apache.org/jira/browse/GROOVY-12754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18146067#comment-18146067
]
ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------
paulk-asert commented on PR #3293:
URL: https://github.com/apache/groovy/pull/3293#issuecomment-6940015941
Thanks, Daniel. Both programmes are now compile errors, and your reading of
where they were accepted was right in each case. The tip is c5627befab.
On the baseline again: neither was newly accepted. 5.1.3 and 6.0.0 compile
both and behave as the tip you reviewed did. Master rejects them, but it
rejects the legal forms beside them too.
โ
compiles and runs ยท โ compile error ยท ๐ฅ compiles, then fails or leaves a
`String` in a list of numbers
| Case | 5.1.3, 6.0.0 | master | reviewed tip | this tip | javac |
|---|---|---|---|---|---|
| `Supplier<List<String>> s = toSupplier(C::nil)` | ๐ฅ | โ | ๐ฅ | โ | โ |
| `Supplier<List<Integer>> s = toSupplier(C::nil)` | โ
| โ | โ
| โ
| โ
|
| `Consumer<String> c = toConsumer(identC(C::takeNumber))` | ๐ฅ | โ | ๐ฅ | โ |
โ |
| `Consumer<Integer> c = toConsumer(identC(C::takeNumber))` | โ
| โ | โ
| โ
| โ
|
**Your first programme.** A type parameter of the referenced method that the
parameters of the functional interface leave open is now taken from what it
returns, and checked against its bound, as you suggested. That also rejects
your `nilOf` and `emptyMap` forms, the method pointer form and
`Supplier<Collection<String>>`. `Supplier<List<? extends Number>>` and an
unbounded method such as `Collections::emptyList` still compile.
**Your second programme.** A nested generic call that its own arguments
leave open is now typed by the parameter in turn, so the target reaches the
reference. Both orders of nesting are rejected, and
`toFn(toFn(Arrays::asList))` compiles and runs.
**A grid, in place of one programme at a time.** Finding these one at a time
was not going to converge, so there is now a grid of programmes as a test,
`FunctionalTargetTypingSTCTest`, and the description has a section, "A grid of
programs", with its two tables: every combination of where the type parameter
is constrained, what carries it and where the target comes from, legal and
illegal, for the releases and for this tip. A programme is illegal if the
target gives the type parameter a type outside its bound, and each one goes on
to fail at run time, which the test checks, so none is only a rule of Java that
Groovy does not share. The programmes that do not yet behave as they should are
listed in the test by cause and are checked to behave as listed, so a change
that moves one either way fails.
What the tables show:
- Every difference from the releases is in the right direction but one, and
that one is explained there: the releases rejected it only together with its
legal form.
- The grid found a regression of this branch against the releases, which is
fixed: a method pointer to a method that returns its type parameter,
`Function<Integer, Integer> f = toFunction(C.&same)`, was rejected.
- What is left falls into a few whole rows and columns, each with one cause:
GROOVY-12777, GROOVY-12778, a bound that is not checked outside a generic call
with a target (the direct form you set aside, and the root of your first
programme), and a lambda parameter without a type. None is changed here, and
all are the same in 5.1.3 and 6.0.0.
**Conversions.** One group is deliberately not an error. Groovy converts a
value where it moves into a declared type, so `String s = C.zero()` is legal
for `<T extends Number> T zero()`: the type parameter is a `Number` and the
result is then converted, as if `toString()` were written. The rule the grid
follows is that such a programme is sound if it is sound with the conversion
written out. A type argument is not such a place, so a `GString` in a
`List<String>` is still an error. Where a lambda or reference returns to a
functional interface that declares a `String`, the conversion is accepted but
not always made; that is GROOVY-12783, which is about making it, not about
rejecting the programme.
Jochen's two comments on the inner class name are fixed in the tip. The root
test suite, `:checkstyleMain` and `:codenarcTest` pass locally on it.
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)