From 4e5b9e0f6574829e68a0ce96d73511eae6a459bd Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Sun, 7 Apr 2024 22:58:21 +0200 Subject: [PATCH] Erroneous "Cannot infer type arguments" error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #1475 Implementation of JLS 15.9.3. failed to substitute an enclosing type in - The return type of mj is θj applied to D. --- .../jdt/internal/compiler/lookup/Scope.java | 4 ++- .../GenericsRegressionTest_1_8.java | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java index 3cecc776fd8..de06d99f68a 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/Scope.java @@ -5382,7 +5382,9 @@ public TypeBinding substitute(TypeVariableBinding typeVariable) { } } } - staticFactory.returnType = environment.createParameterizedType(genericType, Scope.substitute(substitution, genericType.typeVariables()), originalEnclosingType); + staticFactory.returnType = environment.createParameterizedType(genericType, + Scope.substitute(substitution, genericType.typeVariables()), + (ReferenceBinding) Scope.substitute(substitution, originalEnclosingType)); staticFactory.parameters = Scope.substitute(substitution, method.parameters); staticFactory.thrownExceptions = Scope.substitute(substitution, method.thrownExceptions); if (staticFactory.thrownExceptions == null) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java index 4edc27eb36c..856b0c1d919 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_1_8.java @@ -10682,4 +10682,32 @@ public int getWeaveCount() { "----------\n", null, true, customOptions); } + public void testGH1475() { + runConformTest( + new String[] { + "CannotInferTypeArguments.java", + """ + public class CannotInferTypeArguments { + class Fish { + public V getFlavour() { + return null; + } + } + + class Shark { + } + + Shark fish() { + // This compiles fine with javac, but will only work in Eclipse with new Shark(); + return new Shark<>(); + } + + Shark fish2() { + Shark s = new Shark<>(); + return s; + } + } + """ + }); + } }