[ 
https://issues.apache.org/jira/browse/GROOVY-12754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18145478#comment-18145478
 ] 

ASF GitHub Bot commented on GROOVY-12754:
-----------------------------------------

paulk-asert commented on PR #3293:
URL: https://github.com/apache/groovy/pull/3293#issuecomment-6908587985

   Thanks, Daniel. Both programmes are fixed, and looking into them turned up 
more in the same area than the two you reported. The tip is 53a617239e.
   
   A note on the baseline first. Master includes GROOVY-12714, and where it 
rejects these programmes it rejects the legal forms beside them as well: 
`Consumer<Integer> c = identC(C::takeNumber)` and `Function<Object[], 
List<Object>> f = toFn(Arrays::asList)` are compile errors on master and 
compile with 5.1.3 and 6.0.0. So the table below compares the two releases, 
master, the tip you reviewed (9e0de418d3), this tip, and javac 25 for the Java 
equivalents. 5.1.3 and 6.0.0 agree on every row.
   
   โœ… compiles and runs ยท โŒ compile error ยท ๐Ÿ’ฅ compiles, then fails at run time ยท 
โš ๏ธ compiles and calls the wrong method ยท `a / b` is `@CompileStatic` / 
`@TypeChecked` where they differ
   
   | Case | 5.1.3, 6.0.0 | master | reviewed tip | this tip | javac |
   |---|---|---|---|---|---|
   | `Consumer<String> c = identC(C::takeNumber)` | ๐Ÿ’ฅ | โŒ | ๐Ÿ’ฅ | โŒ | โŒ |
   | `Consumer<Integer> c = identC(C::takeNumber)` | โœ… | โŒ | โœ… | โœ… | โœ… |
   | `Function<String, List<String>> f = toFn(Arrays::asList)` | ๐Ÿ’ฅ / โœ… | โŒ | ๐Ÿ’ฅ 
/ โœ… | โœ… | โœ… |
   | `Function<String, String> f = toFn(C::pick)`, with `pick(Object)` and 
`pick(String)` | โŒ | โŒ | โš ๏ธ / โœ… | โœ… | โœ… |
   | `Consumer<String> c = identC(System.out::print)` | โŒ | โŒ | ๐Ÿ’ฅ / โœ… | โœ… | โœ… |
   | `Function<Optional<String>, String> f = toFn(Optional::get)` | โŒ | โŒ | โŒ | 
โœ… | โœ… |
   | `BiFunction<List<String>, Integer, Boolean> f = toBi(List::add)` | โš ๏ธ | โŒ 
| โš ๏ธ | โŒ | โŒ |
   | `Function<List<String>, String> f = toFn(List::first)` | โœ… | โŒ | โŒ | โœ… | 
n/a |
   | `List<List<String>> l = ['x'].stream().map(Arrays::asList).toList()` | โœ… | 
โœ… | โŒ | โœ… | โœ… |
   | `Comparator<Map.Entry<String, Integer>> c = Map.Entry.comparingByKey()` | 
โœ… | โŒ | โŒ | โœ… | โœ… |
   
   **Your first programme.** A type parameter of the referenced method must now 
stay within its bounds, so `takeNumber` is rejected for a `Consumer<String>`, 
as is the nested case, `Consumer<List<String>> c = identC(C::takeNumberList)`. 
I didn't do it quite as you suggested. Instantiating the type parameters "when 
exactly one method fits" is circular, since whether a method fits is what is 
being decided, so they are inferred and checked for each candidate in 
`fittingSignatures`.
   
   **Your second programme.** You were right that the checker should accept it, 
and it wasn't specific to varargs or to generics. The reference is visited 
before the target gives its functional interface type, and the static compiler 
chose the overload and the variadic adapter from what was recorded then. So 
`toFn(C::pick)` quietly called `pick(Object)` under `@CompileStatic` and 
`pick(String)` under `@TypeChecked`, which worried me more than the exception. 
The resolved type is now recorded on the reference once the target is accepted, 
and the static compiler does what it does for a direct assignment.
   
   **The receiver of `Type::instanceMethod`.** It now takes the type arguments 
of its class from the first parameter of the functional interface, as in JLS 
15.13.1, and the self parameter of an extension method counts as the receiver. 
That accepts `Optional::get` and `List::first`, and rejects `List::add` with an 
`Integer`, which the releases let add an `Integer` to a `List<String>`.
   
   **Two programmes the releases accepted that this branch had been 
rejecting.** `stream().map(Arrays::asList)` was rejected by a stricter check in 
this PR, and `Map.Entry.comparingByKey()` assigned to a typed `Comparator` by 
GROOVY-12714. Both compile again.
   
   I also ran these as dynamic code: wherever `@TypeChecked` compiles a 
programme, in any of the versions, it gives the result dynamic code gives, and 
on this tip `@CompileStatic` gives it too.
   
   **Deferred.** These are the same in both releases, and each has a ticket; 
the first four have tests marked not yet implemented:
   - GROOVY-12777: `use(identC(C::takeNumber))`, your first programme as an 
argument, still compiles. It goes through the older `f(g())` rule, not target 
typing.
   - GROOVY-12778: `BinaryOperator<Object> c = identB(C::max)` for `<T extends 
Comparable<T>> T max(T, T)`.
   - GROOVY-12779: `toBi(List::join)`, an extension method with a further 
parameter.
   - GROOVY-12780: `Comparator.comparing(Map.Entry::getKey)` is rejected; javac 
accepts it.
   - GROOVY-12781: `Function<Object, String> f = C::pick` selects statically 
under `@CompileStatic`, as Java does, and by the argument otherwise. That one 
may be as designed.
   
   **Housekeeping.** I squashed my review commits, so the branch is 12 commits: 
Scott's eight are untouched, and mine are three, with the GROOVY-12757 tests at 
the end. It needed a force-push, so your inline comments will show as outdated; 
sorry for that. The guide has a paragraph on a method reference passed to a 
generic call, `COMPATIBILITY.md` has the new compile errors, and the 
description is up to date. The root test suite passes apart from five tests 
that failed to download a `@Grab` dependency and pass when rerun.
   
   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)

Reply via email to