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.
--
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]