Skip to content

Commit 60dc2a2

Browse files
Regression test for eclipse-jdt#4900 (eclipse-jdt#4902)
1 parent 10c557d commit 60dc2a2

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7118,5 +7118,88 @@ public static void main(String [] args) {
71187118
};
71197119
runner.runConformTest();
71207120
}
7121+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4900
7122+
// Type interference with generics (and lambdas?) fails in Eclipse but compile in Javac
7123+
public void testIssue4900() {
7124+
7125+
Runner runner = new Runner();
7126+
runner.expectedOutputString =
7127+
"";
7128+
runner.expectedCompilerLog = "";
7129+
7130+
runner.testFiles = new String[] {
7131+
"de/ivu/example/typeinference/eclipse/EclipseTypeInferenceExample.java",
7132+
"""
7133+
package de.ivu.example.typeinference.eclipse;
7134+
7135+
import java.util.List;
7136+
import java.util.Map;
7137+
import java.util.Optional;
7138+
import java.util.function.Function;
7139+
import java.util.stream.Collectors;
7140+
7141+
/**
7142+
* Minimal examples to find where Eclipse type inference actually fails.
7143+
*/
7144+
public class EclipseTypeInferenceExample {
7145+
7146+
/**
7147+
* TEST 1: Generic method with type parameters where Eclipse type interference FAILS
7148+
*/
7149+
public static class Test_1 {
7150+
7151+
static <A, B, C, D> void process(
7152+
List<A> items,
7153+
Function<A, B> extract,
7154+
Function<List<B>, C> aggregate,
7155+
Function<C, Map<B, D>> transform) {
7156+
}
7157+
7158+
// FAILS in Eclipse: "Cannot infer type argument(s) for <A, B, C, D> process(...)"
7159+
public static void test_FAILS(List<String> items) {
7160+
process(
7161+
items,
7162+
String::toLowerCase,
7163+
list -> list.stream().distinct().toList(),
7164+
list -> list.stream().collect(Collectors.toMap(
7165+
Function.identity(),
7166+
String::length)));
7167+
}
7168+
7169+
// WORKS in Eclipse only with explicit type parameters
7170+
public static void test_WORKS(List<String> items) {
7171+
/* type information for eclipse */Test_1
7172+
.<String, String, List<String>, Integer> /* type information for eclipse */
7173+
process(
7174+
items,
7175+
String::toLowerCase,
7176+
list -> list.stream().distinct().toList(),
7177+
list -> list.stream().collect(Collectors.toMap(
7178+
Function.identity(),
7179+
String::length)));
7180+
}
7181+
7182+
/**
7183+
* TEST 2: Generic method with type parameters - simple case works fine in Eclipse
7184+
*/
7185+
public static class Test_2 {
7186+
7187+
static <A, B, C, D> void process(A a, Function<A, B> f1, Function<B, C> f2, Function<C, D> f3) {
7188+
}
7189+
7190+
public static void test_WORKS(String input) {
7191+
process(
7192+
input,
7193+
String::length,
7194+
len -> Optional.of(len),
7195+
opt -> List.of(opt));
7196+
}
7197+
}
7198+
}
7199+
}
7200+
"""
7201+
};
7202+
runner.runConformTest();
7203+
}
71217204
}
71227205

0 commit comments

Comments
 (0)