[
https://issues.apache.org/jira/browse/GROOVY-12754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18145356#comment-18145356
]
ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------
paulk-asert commented on PR #3293:
URL: https://github.com/apache/groovy/pull/3293#issuecomment-6901535330
Thanks, Daniel. You were right about the method references, and they were
worse than a missing check: most of them were regressions. The branch is
rebased on master, and the method-reference fix is 1570ed2e45.
The releases and master reject these through ordinary inference. Target
typing in this PR then accepted them: at 92a16f61 a method reference whose
return type wasn't known was skipped, and fc15242873 only added the return-type
check. I compiled your programmes and their siblings under `@CompileStatic` and
`@TypeChecked` with 5.1.3, 6.0.0, master, the previous tip (67f0591e7e, before
the rebase) and this tip, and the Java equivalents with javac. For the cases
that should be rejected, 92a16f61 gives the same results as the previous tip.
| Case | 5.1.3 | 6.0.0 | master | previous tip | this tip | javac |
|---|---|---|---|---|---|---|
| `toConsumer(String::length)` as `Consumer<Object>` | โ | โ | โ | ๐ฅ | โ | โ
|
| `toLength(String::length)` as `Function<Object, Integer>` | โ | โ | โ | ๐ฅ
| โ | โ |
| `toConsumer(String.&length)` | โ | โ | โ | ๐ฅ | โ | n/a |
| `Predicate`, `UnaryOperator`, `ToIntFunction` with a `String` method, for
`Object` | โ | โ | โ | ๐ฅ | โ | โ |
| `toCmp(String::compareTo)` as `Comparator<Object>` | ๐ฅ | ๐ฅ | โ | ๐ฅ | โ | โ
|
| `toFn(String::reverse)` (DGM) as `Function<Object, String>` | โ | โ | โ |
๐ฅ (TC) | โ | n/a |
| `toConsumer(String::length)` as `Consumer<String>` | โ
| โ
| โ
| โ
| โ
| โ
|
| `String::valueOf` as `Function<Object, String>` | โ
| โ
| โ
| โ
| โ
| โ
|
| `CharSequence::length` as `Function<String, Integer>` | โ | โ | โ | โ
| โ
| โ
|
| `Object::hashCode` as `Function<String, Integer>`, `ArrayList::new` as
`Supplier<List<String>>` | โ | โ | โ | โ
| โ
| โ
|
| `id(Arrays::asList)` as `Function<Object[], List<Object>>` | โ
| โ
| โ | โ
| โ
| โ
|
| `StringBuilder::new` as `Function<String, StringBuilder>` | โ
| โ
| โ
| โ
| โ
| โ
|
โ
compiles and runs ยท โ compile error ยท ๐ฅ compiles, then fails at run time
The check is the one you suggested, with one difference. Instead of
`collateMethodReferenceParameterTypes(candidates.get(0))`, it goes through
every method the reference names, as the arity check from GROOVY-11010 already
did for overload selection. That code now has one helper that lists each
method's parameter types, with the receiver of `Type::instanceMethod` first,
for both uses. The candidates stored on the reference aren't narrowed while the
SAM's parameter types are still unknown, so looking at the first one would have
rejected `String::valueOf` as a `Function<Object, String>`. The helper also
covers DGM methods and constructors. A reference is accepted if one of its
methods has the right arity and accepts the SAM's parameter types; a parameter
type that the target leaves unresolved isn't checked.
Your `id(Arrays::asList)` caution is fixed too. Its return type is `List<T>`
with `asList`'s own `T`, which the return-type check compared as it stood. When
exactly one method fits, its type parameters are now resolved from the SAM's
parameter types first, so it compiles and runs again.
`toFn(StringBuilder::new)` as a `Function<Object, Object>` still compiles
and fails at run time, in every version. That one doesn't come through target
typing: plain inference leaves `T` as `Object` without checking that a
constructor accepts it. It is GROOVY-12776, with a not-yet-implemented test.
**Tickets.** The deferred cases now each have one, and each test names it:
- `cast`: GROOVY-12772
- `bypassBound`: GROOVY-12773, related to GROOVY-12766
- `map.put` / `box.set`: GROOVY-12774. The ticket covers both, though the
test has only `map.put`.
- `items.collect { it.getClass() }` and `[IntList].collect { it }`:
GROOVY-12775
- the constructor reference: GROOVY-12776
On GROOVY-12769 and master, you're right. A build of the commit before
GROOVY-12714 accepts `bypassClassOf`, as 6.0.0 does, so master's rejection
comes from 12714's `Class<?>` typing of `getClass()`. Since 12714 is also on
`GROOVY_6_0_X`, 6.0.1 would reject it as well. The same holds for both
`collect` cases in GROOVY-12775.
**GROOVY-12757 / #3290.** edff64f2fd (the `asSubclass` fix from last round)
turned out to be a fix for the same bug as Scott's #3290, which you approved:
both change the two substitution steps in `inferReturnTypeGenerics`. #3290
reverses them, while this branch applies one map. Merging both would conflict,
so this branch now takes #3290's tests, with Scott's authorship (fbcb460b8c).
They pass on this branch without #3290's code change, including its `@ASTTest`
on the inferred type of `def result = value.map(...)` and the Java
compatibility test. The description lists #3290 as superseded.
The full root test suite passes on the rebased branch (20,241 tests), and
lint is clean.
> 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)