Skip to content

Commit

Permalink
"Inline local variable" produces broken code with JLS >= 1.8 (#2776)
Browse files Browse the repository at this point in the history
- Fix DefaultBindingResolver.isResolvedTypeInferredFromExpectedType()
- restore some tests that were changed when the move to min Java 1.8
  was merged

Fixes #2775

---------
Co-authored-by: Stephan Herrmann <[email protected]>
  • Loading branch information
jjohnstn authored Aug 1, 2024
1 parent 8676bca commit a08a449
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8199,7 +8199,7 @@ public void test0249() throws JavaModelException {
expression = fragment.getInitializer();
assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression.getNodeType());
methodInvocation = (MethodInvocation) expression;
assertFalse("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType());
assertTrue("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType());
}

//https://bugs.eclipse.org/bugs/show_bug.cgi?id=174436
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8196,7 +8196,7 @@ public void test0249() throws JavaModelException {
expression = fragment.getInitializer();
assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression.getNodeType());
methodInvocation = (MethodInvocation) expression;
assertFalse("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType());
assertTrue("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType());
}

//https://bugs.eclipse.org/bugs/show_bug.cgi?id=174436
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8235,7 +8235,7 @@ public void test0249() throws JavaModelException {
expression = fragment.getInitializer();
assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression.getNodeType());
methodInvocation = (MethodInvocation) expression;
assertFalse("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType());
assertTrue("Wrong value", methodInvocation.isResolvedTypeInferredFromExpectedType());
}

//https://bugs.eclipse.org/bugs/show_bug.cgi?id=174436
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2016 IBM Corporation and others.
* Copyright (c) 2011, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -21,7 +21,13 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFlattener;
import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore;

Expand Down Expand Up @@ -1113,4 +1119,49 @@ public void testGH1376() throws CoreException, IOException {
deleteProject("P");
}
}
public void testGH2275() throws CoreException {
try {
createJavaProject("P", new String[] { "" }, new String[] { "CONVERTER_JCL_LIB" }, "", "1.8", true);
createFolder("P/p");
createFile("P/p/A.java",
"""
package p;
import java.util.Collections;
import java.util.Map;
class A {
public A(Map<String, Integer> map) {
Map<String, Integer> emptyMap= Collections.emptyMap(); // return type inferred from the target type
Map<String, Integer> emptyMap2= foo(A.class); // inference used, but not influencing the return type
}
<T> Map<String, Integer> foo(Class<T> clazz) {
return Collections.emptyMap();
}
}
"""
);
ICompilationUnit cuA = getCompilationUnit("P/p/A.java");
ASTParser parser = createASTParser();
parser.setResolveBindings(true);
parser.setSource(cuA);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);

TypeDeclaration classA = (TypeDeclaration) cu.types().get(0);
MethodDeclaration constructor = classA.getMethods()[0];
assertTrue(constructor.isConstructor());
List statements = constructor.getBody().statements();

VariableDeclarationStatement local = (VariableDeclarationStatement) statements.get(0);
MethodInvocation invocation = (MethodInvocation) ((VariableDeclarationFragment) local.fragments().get(0)).getInitializer();
assertEquals("emptyMap", invocation.getName().getIdentifier());
assertTrue(invocation.isResolvedTypeInferredFromExpectedType());

VariableDeclarationStatement local2 = (VariableDeclarationStatement) statements.get(1);
MethodInvocation invocation2 = (MethodInvocation) ((VariableDeclarationFragment) local2.fragments().get(0)).getInitializer();
assertEquals("foo", invocation2.getName().getIdentifier());
assertFalse(invocation2.isResolvedTypeInferredFromExpectedType());
} finally {
deleteProject("P");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -574,7 +574,10 @@ boolean isResolvedTypeInferredFromExpectedType(MethodInvocation methodInvocation
org.eclipse.jdt.internal.compiler.lookup.MethodBinding methodBinding = messageSend.binding;
if (methodBinding instanceof ParameterizedGenericMethodBinding) {
ParameterizedGenericMethodBinding genericMethodBinding = (ParameterizedGenericMethodBinding) methodBinding;
return genericMethodBinding.inferredReturnType;
if (genericMethodBinding.wasInferred && messageSend.typeArguments == null) {
return org.eclipse.jdt.internal.compiler.lookup.TypeBinding.notEquals(
genericMethodBinding.original().returnType, genericMethodBinding.returnType);
}
}
}
return false;
Expand Down

0 comments on commit a08a449

Please sign in to comment.