[
https://issues.apache.org/jira/browse/GROOVY-12727?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149374#comment-18149374
]
ASF GitHub Bot commented on GROOVY-12727:
-----------------------------------------
paulk-asert commented on PR #3265:
URL: https://github.com/apache/groovy/pull/3265#issuecomment-7084748052
Thanks Daniel, I've pushed an updated commit that takes your points:
- **Same-class and `import static` calls** now get the target type too. They
are converted to `StaticMethodCallExpression`, so I added the same
enclosing-call tracking there, with the pop in a `finally`.
- **`? super X` and `? extends X`** targets give `X` as the witness, and a
witness is only taken if it satisfies the bounds of `T`. An unbounded `?` still
determines nothing.
- **Conditional expressions:** the target now reaches both ternary branches,
including elvis and nested ones.
- **Method references** are excluded, since GROOVY-12755 already checks
them. With the target applied to them as well, an invalid bounded case was
accepted by STC and then failed in static compilation.
- **Cast hook:** I added a comment saying it is intentional. javac rejects
`(Beh<String>) setup(...)`, but the result here is sound.
- **Tightening:** the lambda parameter is now `Ctx<Integer>` instead of
`Ctx<?>` when the target is concrete. So `Integer s = conv(c -> { c.value =
'x'; 1 })` now fails, as it does in javac. It compiled before, but it polluted
the heap.
I've left these for follow-up issues: a target type for a generic call
passed as a method argument, the target in `inferReturnTypeGenerics` and
constructors, a better diagnostic when `Map<T,T>` gets conflicting targets, and
GROOVY-11446. The same goes for the existing leniency around unbounded type
parameters, which is identical on 5.1.3 and 6.0.0.
The stc and sc suites, groovy-macro, groovy-contracts and groovy-ginq pass.
> STC: a generic method's type parameter isn't inferred from the return-type
> target when the argument is a lambda
> ---------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12727
> URL: https://issues.apache.org/jira/browse/GROOVY-12727
> Project: Groovy
> Issue Type: Bug
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> Under {{@TypeChecked}} (and {{@CompileStatic}}), when a generic method's only
> argument is an implicitly typed lambda, the type parameter is not inferred
> from the expected return type. The lambda's parameter is typed with a
> wildcard ({{Ctx<? extends Object>}}), and a call inside the lambda that needs
> the concrete type is rejected. javac infers {{T = String}} from the
> return-type target and compiles the equivalent Java.
> This is the standard Apache Pekko (and Akka) typed-actor factory,
> {{Behaviors.setup(context -> new MyActor(context))}}, so statically compiled
> Pekko code needs a type witness or an explicitly typed lambda parameter.
> h3. Repro (no dependencies)
> {code:groovy}
> import java.util.function.Function
> import groovy.transform.TypeChecked
> class Ctx<T> {}
> class Beh<T> {}
> class Behaviors {
> static <T> Beh<T> setup(Function<Ctx<T>, Beh<T>> factory) {
> factory.apply(new Ctx<T>()) }
> }
> class Bot extends Beh<String> {
> Bot(Ctx<String> c, int max) {}
> }
> @TypeChecked
> class M {
> static Beh<String> lambda(int max) { Behaviors.setup(c -> new Bot(c,
> max)) }
> }
> {code}
> h3. Actual
> {noformat}
> [Static type checking] - Cannot call Bot#<init>(Ctx<java.lang.String>, int)
> with arguments [Ctx<? extends java.lang.Object>, int]
> {noformat}
> h3. Expected
> Compiles, as the Java equivalent does with javac ({{T}} inferred as
> {{String}} from the {{Beh<String>}} return type).
> h3. Workarounds (both compile on 6.0.0)
> {code:groovy}
> Behaviors.<String>setup(c -> new Bot(c, max)) // explicit
> type witness
> Behaviors.setup((Ctx<String> c) -> new Bot(c, max)) // explicitly
> typed lambda parameter
> {code}
> h3. Notes
> * The closure form, Behaviors.setup \{ c -> new Bot(c, max) }, fails with the
> same message.
> * An assignment target fails the same way as a return target: {{Beh<String> b
> = Behaviors.setup(c -> new Bot(c, max))}}.
> * Possibly related, not duplicates:
> ** GROOVY-11446: target-type handling for a generic method with wildcards, in
> the opposite direction (accepted when it should fail).
> ** GROOVY-11077: lambda parameter types from a wildcard SAM; no generic
> method or inference involved.
> ** GROOVY-12714 / PR #3246: javac-compatibility changes to wildcard
> containment in the same visitor; they don't appear to supply this inference
> (not verified against the branch).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)