Skip to content

Commit f847cd8

Browse files
@deprecated(forRemoval=true) still propagates to members when consumed from binary (#4581)
Remove bogus propagation to members also in BinaryTypeBinding Bonus: add passing tests for deprecation on records Fixes #4580
1 parent 8486242 commit f847cd8

File tree

3 files changed

+163
-17
lines changed

3 files changed

+163
-17
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,6 @@ private void cachePartsFrom2(IBinaryType binaryType, boolean needFieldsAndMethod
656656
if (CharOperation.equals(elementValuePair.name, TypeConstants.FOR_REMOVAL)) {
657657
if (elementValuePair.value instanceof BooleanConstant && ((BooleanConstant) elementValuePair.value).booleanValue()) {
658658
this.tagBits |= TagBits.AnnotationTerminallyDeprecated;
659-
markImplicitTerminalDeprecation(this);
660659
}
661660
}
662661
}
@@ -679,22 +678,6 @@ private void cachePartsFrom2(IBinaryType binaryType, boolean needFieldsAndMethod
679678
}
680679
}
681680

682-
void markImplicitTerminalDeprecation(ReferenceBinding type) {
683-
for (ReferenceBinding member : type.memberTypes()) {
684-
member.tagBits |= TagBits.AnnotationTerminallyDeprecated;
685-
markImplicitTerminalDeprecation(member);
686-
}
687-
MethodBinding[] methodsOfType = type.unResolvedMethods();
688-
if (methodsOfType != null)
689-
for (MethodBinding methodBinding : methodsOfType)
690-
methodBinding.tagBits |= TagBits.AnnotationTerminallyDeprecated;
691-
692-
FieldBinding[] fieldsOfType = type.unResolvedFields();
693-
if (fieldsOfType != null)
694-
for (FieldBinding fieldBinding : fieldsOfType)
695-
fieldBinding.tagBits |= TagBits.AnnotationTerminallyDeprecated;
696-
}
697-
698681
/* When creating a method we need to pass in any default 'nullness' from a @NNBD immediately on this method. */
699682
private ITypeAnnotationWalker getTypeAnnotationWalker(IBinaryTypeAnnotation[] annotations, int nullness) {
700683
if (!isPrototype()) throw new IllegalStateException();

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,39 @@ The method nothing() from the type C1 is deprecated
10881088
""";
10891089
runner.runWarningTest();
10901090
}
1091+
public void testGH4580() {
1092+
Runner runner = new Runner();
1093+
runner.testFiles = new String[] {
1094+
"p/Dep.java",
1095+
"""
1096+
package p;
1097+
@Deprecated(forRemoval=true)
1098+
public class Dep {
1099+
@Deprecated public static final String S= "";
1100+
}
1101+
"""
1102+
};
1103+
runner.runConformTest();
1104+
runner.shouldFlushOutputDirectory = false;
1105+
runner.customOptions = getCompilerOptions();
1106+
runner.customOptions.put(JavaCore.COMPILER_PB_DEPRECATION, JavaCore.ERROR);
1107+
runner.customOptions.put(JavaCore.COMPILER_PB_TERMINAL_DEPRECATION, JavaCore.ERROR);
1108+
runner.customOptions.put(JavaCore.COMPILER_PB_UNUSED_WARNING_TOKEN, JavaCore.ERROR);
1109+
runner.customOptions.put(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, JavaCore.ENABLED);
1110+
runner.testFiles = new String[] {
1111+
"p/DepSub.java",
1112+
"""
1113+
package p;
1114+
1115+
@SuppressWarnings({"removal", "deprecation"})
1116+
public class DepSub extends Dep {
1117+
@SuppressWarnings("hiding")
1118+
public static final String S = Dep.S;
1119+
}
1120+
"""
1121+
};
1122+
runner.runConformTest();
1123+
}
10911124
public static Class<?> testClass() {
10921125
return Deprecated9Test.class;
10931126
}

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10980,4 +10980,134 @@ public static void main(String [] args) {
1098010980
"OK!");
1098110981

1098210982
}
10983+
public void testDeprecation_type() {
10984+
Runner runner = new Runner();
10985+
runner.testFiles = new String[] {
10986+
"X.java",
10987+
"""
10988+
@Deprecated record R(int i, boolean f) {}
10989+
public class X {
10990+
R test() {
10991+
return new R(1,false);
10992+
}
10993+
}
10994+
"""
10995+
};
10996+
runner.expectedCompilerLog =
10997+
"""
10998+
----------
10999+
1. WARNING in X.java (at line 3)
11000+
R test() {
11001+
^
11002+
The type R is deprecated
11003+
----------
11004+
2. WARNING in X.java (at line 4)
11005+
return new R(1,false);
11006+
^
11007+
The type R is deprecated
11008+
----------
11009+
""";
11010+
runner.runWarningTest();
11011+
}
11012+
public void testDeprecation_altCtor() {
11013+
Runner runner = new Runner();
11014+
runner.testFiles = new String[] {
11015+
"X.java",
11016+
"""
11017+
record R(int i, boolean f) {
11018+
@Deprecated R(int i) {
11019+
this(i, i>0);
11020+
}
11021+
R {
11022+
i = Math.abs(i);
11023+
}
11024+
}
11025+
public class X {
11026+
R test(int in) {
11027+
return switch(in) {
11028+
case 0 -> new R(0);
11029+
case 1 -> new R(1, false);
11030+
default -> null;
11031+
};
11032+
}
11033+
}
11034+
"""
11035+
};
11036+
runner.expectedCompilerLog =
11037+
"""
11038+
----------
11039+
1. WARNING in X.java (at line 12)
11040+
case 0 -> new R(0);
11041+
^
11042+
The constructor R(int) is deprecated
11043+
----------
11044+
""";
11045+
runner.runWarningTest();
11046+
}
11047+
public void testDeprecation_compactCtor() {
11048+
Runner runner = new Runner();
11049+
runner.testFiles = new String[] {
11050+
"X.java",
11051+
"""
11052+
record R(int i, boolean f) {
11053+
R(int i) {
11054+
this(i, i>0);
11055+
}
11056+
@Deprecated R {
11057+
i = Math.abs(i);
11058+
}
11059+
}
11060+
public class X {
11061+
R test(int in) {
11062+
return switch(in) {
11063+
case 0 -> new R(0);
11064+
case 1 -> new R(1, false);
11065+
default -> null;
11066+
};
11067+
}
11068+
}
11069+
"""
11070+
};
11071+
runner.expectedCompilerLog =
11072+
"""
11073+
----------
11074+
1. WARNING in X.java (at line 13)
11075+
case 1 -> new R(1, false);
11076+
^
11077+
The constructor R(int, boolean) is deprecated
11078+
----------
11079+
""";
11080+
runner.runWarningTest();
11081+
}
11082+
public void testDeprecation_accessor() {
11083+
Runner runner = new Runner();
11084+
runner.testFiles = new String[] {
11085+
"X.java",
11086+
"""
11087+
record R(int i, boolean f) {
11088+
@Deprecated public int i() {
11089+
return this.i;
11090+
}
11091+
}
11092+
public class X {
11093+
int test1() {
11094+
return new R(1,false).i();
11095+
}
11096+
boolean test2() {
11097+
return new R(1,false).f();
11098+
}
11099+
}
11100+
"""
11101+
};
11102+
runner.expectedCompilerLog =
11103+
"""
11104+
----------
11105+
1. WARNING in X.java (at line 8)
11106+
return new R(1,false).i();
11107+
^
11108+
The method i() from the type R is deprecated
11109+
----------
11110+
""";
11111+
runner.runWarningTest();
1098311112
}
11113+
}

0 commit comments

Comments
 (0)