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