From 9997dbc6ecfed8d02ae49401f785dcc4d4743e7f Mon Sep 17 00:00:00 2001 From: Srikanth Sankaran <131454720+srikanth-sankaran@users.noreply.github.com> Date: Mon, 6 Nov 2023 19:09:35 +0530 Subject: [PATCH] Roll back non-essential changes from the fix for #1506 (#1563) Roll back non-essential changes from the fix for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1506 as they trigger pre-existing bugs --- .../jdt/internal/compiler/ast/ASTNode.java | 6 +- .../regression/GenericsRegressionTest_9.java | 111 +++++++++++++++++- 2 files changed, 108 insertions(+), 9 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ASTNode.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ASTNode.java index 0ea6f007b7f..fd95ef4e6eb 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ASTNode.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ASTNode.java @@ -361,9 +361,9 @@ private static int checkInvocationArgument(BlockScope scope, Expression argument if (allocExp.typeExpected == null && !allocExp.expectedTypeWasInferred) { final boolean isDiamond = allocExp.type != null && (allocExp.type.bits & ASTNode.IsDiamond) != 0; allocExp.typeExpected = parameterType; - if (!isDiamond && allocExp.type.resolvedType.isParameterizedTypeWithActualArguments()) { - ParameterizedTypeBinding ptb = (ParameterizedTypeBinding) allocExp.type.resolvedType; - allocExp.reportTypeArgumentRedundancyProblem(ptb, scope); + if (!isDiamond && allocExp.resolvedType.isParameterizedTypeWithActualArguments()) { + ParameterizedTypeBinding pbinding = (ParameterizedTypeBinding) allocExp.resolvedType; + scope.problemReporter().redundantSpecificationOfTypeArguments(allocExp.type, pbinding.arguments); } } } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_9.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_9.java index 13d93dc78d1..15bab7385bc 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_9.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericsRegressionTest_9.java @@ -866,12 +866,7 @@ public void testGH1506_3() { + " ^^\n" + "X.AX is a raw type. References to generic type X.AX should be parameterized\n" + "----------\n" - + "2. ERROR in X.java (at line 6)\n" - + " X x5 = new X>(new AX() { private void foo() {} });\n" - + " ^^\n" - + "Redundant specification of type arguments \n" - + "----------\n" - + "3. WARNING in X.java (at line 6)\n" + + "2. WARNING in X.java (at line 6)\n" + " X x5 = new X>(new AX() { private void foo() {} });\n" + " ^^^^^\n" + "The method foo() from the type new X.AX(){} is never used locally\n" @@ -908,6 +903,110 @@ public void testGH1506_4() { + "----------\n", null, true, options); } +// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1560 +// ECJ recommends diamond when using it would result in non-denotable types. +public void testGH1560() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_ReportRedundantSpecificationOfTypeArguments, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + """ + import java.util.Collection; + import java.util.List; + + public class X { + + public interface MMenuElement {} + public interface IObservable {} + public static class ListDiffVisitor {} + public interface IObservablesListener {} + public interface IObservableCollection extends IObservable, Collection {} + public static class ObservableEvent {} + public interface IDiff {} + public static class ListDiff implements IDiff { + public void accept(ListDiffVisitor visitor) {} + } + public static class ListChangeEvent extends ObservableEvent { + public ListDiff diff; + } + public interface IListChangeListener extends IObservablesListener { + void handleListChange(ListChangeEvent event); + } + public interface IObservableList extends List, IObservableCollection { + void addListChangeListener(IListChangeListener listener); + } + + public void foo() { + + IObservableList l; + + l.addListChangeListener(event -> event.diff.accept(new ListDiffVisitor() {})); // <> should not be recommended here!!! + + } + } + """, + }, + "----------\n" + + "1. ERROR in X.java (at line 30)\n" + + " l.addListChangeListener(event -> event.diff.accept(new ListDiffVisitor() {})); // <> should not be recommended here!!!\n" + + " ^\n" + + "The local variable l may not have been initialized\n" + + "----------\n", + null, true, options); +} +// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1560 +// ECJ recommends diamond when using it would result in non-denotable types. +public void testGH1560_2() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTION_ReportRedundantSpecificationOfTypeArguments, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + """ + import java.util.Collection; + import java.util.List; + + public class X { + + public interface MMenuElement {} + public interface IObservable {} + public static class ListDiffVisitor {} + public interface IObservablesListener {} + public interface IObservableCollection extends IObservable, Collection {} + public static class ObservableEvent {} + public interface IDiff {} + public static class ListDiff implements IDiff { + public void accept(ListDiffVisitor visitor) {} + } + public static class ListChangeEvent extends ObservableEvent { + public ListDiff diff; + } + public interface IListChangeListener extends IObservablesListener { + void handleListChange(ListChangeEvent event); + } + public interface IObservableList extends List, IObservableCollection { + void addListChangeListener(IListChangeListener listener); + } + + public void foo() { + + IObservableList l; + + l.addListChangeListener(event -> event.diff.accept(new ListDiffVisitor<>() {})); // non-denotable type error + + } + } + """, + }, + "----------\n" + + "1. ERROR in X.java (at line 30)\n" + + " l.addListChangeListener(event -> event.diff.accept(new ListDiffVisitor<>() {})); // non-denotable type error\n" + + " ^^^^^^^^^^^^^^^\n" + + "Type X.ListDiffVisitor inferred for ListDiffVisitor<>, is not valid for an anonymous class with '<>'\n" + + "----------\n", + null, true, options); +} public static Class testClass() { return GenericsRegressionTest_9.class; }