Bug 511898

Summary: Unexpected type inference and resolution of method references with Collectors.groupingBy
Product: [Eclipse Project] JDT Reporter: Sebastian Millies <sebastian.millies>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: stephan.herrmann
Version: 4.6Keywords: needinfo
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=542489
Whiteboard: stalebug
Attachments:
Description Flags
Source code with JUnit test none

Description Sebastian Millies CLA 2017-02-08 05:27:08 EST
Created attachment 266722 [details]
Source code with JUnit test

The attached class (inspired by a Stackoverflow discussion, see class comment) compiles and runs fine with EJC. This is in contrast to javac, where I get the following errors with jdk1.8.0_121:

javac:

Error:(104, 26) java: no suitable method found for groupingBy(Something:[...]heOne,java.util.stream.Collector<T,capture#1 of ?,java.util.List<T>>)
    method java.util.stream.Collectors.<T,K,D,A,M>groupingBy(java.util.function.Function<? super T,? extends K>,java.util.function.Supplier<M>,java.util.stream.Collector<? super T,A,D>) is not applicable
      (cannot infer type-variable(s) T,K,D,A,M
        (actual and formal argument lists differ in length))
    method java.util.stream.Collectors.<T,K,A,D>groupingBy(java.util.function.Function<? super T,? extends K>,java.util.stream.Collector<? super T,A,D>) is not applicable
      (inferred type does not conform to upper bound(s)
        inferred: java8.streams.collectors.CollectToSortedList.Something
        upper bound(s): T,java.lang.Comparable<T>,java8.streams.collectors.CollectToSortedList.Something,java.lang.Object)
    method java.util.stream.Collectors.<T,K>groupingBy(java.util.function.Function<? super T,? extends K>) is not applicable
      (cannot infer type-variable(s) T,K
        (actual and formal argument lists differ in length))
Error:(104, 37) java: invalid method reference
  non-static method getTheOne() cannot be referenced from a static context
  
IntelliJ IDEA CE shows a message:

groupingBy(Function<? super SomeComparableThing, ? extends String>,java.util.stream.Collector<? super java8.streams.collectors.CollectToSortedList.SomeComparableThing,java.lang.Object,java.util.List<java8.streams.collectors.CollectToSortedList.SomeComparableThing>>)
in Collectors cannot be applied to
(<method reference>, java.util.stream.Collector<java8.streams.collectors.CollectToSortedList.SomeComparableThing,capture<?>,java.util.List<java8.streams.collectors.CollectToSortedList.SomeComparableThing>>)
reason: Type parameter T has incompatible upper bounds: T and Something  

In Eclipse Neon.2 the class compiles and the test is successful. This may be a case where Eclipse is actually better than Oracle javac. Or is it still a bug and EJC should also show a compile error?
Comment 1 Stephan Herrmann CLA 2017-02-09 09:05:40 EST
Here we have a case where both compilers are consistent over time:

- javac reject since Java 8 GA

- ecj accept since 4.5 (was StackOverflowError in 4.4)

Sometimes the javac team argues that if their implementation finds an inference solution, even if not supported by JLS, that's "better".

So, before starting in-depth analysis I'll wait if there's any indication that ecj's behaviour is "wrong". I'm happy with reasoning based on JLS as well as counter examples where ecj accepts code that unexpectedly fails at runtime (typically by ClassCastException in a location where the source code has not cast).
Comment 2 Eclipse Genie CLA 2020-11-27 09:08:08 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 3 Eclipse Genie CLA 2022-12-06 18:23:50 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.