Skip to content

Commit cc2acd1

Browse files
author
Vicente Romero
committed
8343286: Missing unchecked cast warning in polymorphic method call
Reviewed-by: mcimadamore
1 parent b80ca49 commit cc2acd1

File tree

10 files changed

+40
-2
lines changed

10 files changed

+40
-2
lines changed

src/java.base/share/classes/java/util/concurrent/atomic/AtomicReference.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public String toString() {
279279
* @return the value
280280
* @since 9
281281
*/
282+
@SuppressWarnings("unchecked")
282283
public final V getPlain() {
283284
return (V)VALUE.get(this);
284285
}
@@ -302,6 +303,7 @@ public final void setPlain(V newValue) {
302303
* @return the value
303304
* @since 9
304305
*/
306+
@SuppressWarnings("unchecked")
305307
public final V getOpaque() {
306308
return (V)VALUE.getOpaque(this);
307309
}
@@ -324,6 +326,7 @@ public final void setOpaque(V newValue) {
324326
* @return the value
325327
* @since 9
326328
*/
329+
@SuppressWarnings("unchecked")
327330
public final V getAcquire() {
328331
return (V)VALUE.getAcquire(this);
329332
}
@@ -351,6 +354,7 @@ public final void setRelease(V newValue) {
351354
* expected value if successful
352355
* @since 9
353356
*/
357+
@SuppressWarnings("unchecked")
354358
public final V compareAndExchange(V expectedValue, V newValue) {
355359
return (V)VALUE.compareAndExchange(this, expectedValue, newValue);
356360
}
@@ -367,6 +371,7 @@ public final V compareAndExchange(V expectedValue, V newValue) {
367371
* expected value if successful
368372
* @since 9
369373
*/
374+
@SuppressWarnings("unchecked")
370375
public final V compareAndExchangeAcquire(V expectedValue, V newValue) {
371376
return (V)VALUE.compareAndExchangeAcquire(this, expectedValue, newValue);
372377
}
@@ -383,6 +388,7 @@ public final V compareAndExchangeAcquire(V expectedValue, V newValue) {
383388
* expected value if successful
384389
* @since 9
385390
*/
391+
@SuppressWarnings("unchecked")
386392
public final V compareAndExchangeRelease(V expectedValue, V newValue) {
387393
return (V)VALUE.compareAndExchangeRelease(this, expectedValue, newValue);
388394
}

src/java.base/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ private void readObject(java.io.ObjectInputStream s)
358358
* @return the value
359359
* @since 9
360360
*/
361+
@SuppressWarnings("unchecked")
361362
public final E getPlain(int i) {
362363
return (E)AA.get(array, i);
363364
}
@@ -383,6 +384,7 @@ public final void setPlain(int i, E newValue) {
383384
* @return the value
384385
* @since 9
385386
*/
387+
@SuppressWarnings("unchecked")
386388
public final E getOpaque(int i) {
387389
return (E)AA.getOpaque(array, i);
388390
}
@@ -407,6 +409,7 @@ public final void setOpaque(int i, E newValue) {
407409
* @return the value
408410
* @since 9
409411
*/
412+
@SuppressWarnings("unchecked")
410413
public final E getAcquire(int i) {
411414
return (E)AA.getAcquire(array, i);
412415
}
@@ -437,6 +440,7 @@ public final void setRelease(int i, E newValue) {
437440
* expected value if successful
438441
* @since 9
439442
*/
443+
@SuppressWarnings("unchecked")
440444
public final E compareAndExchange(int i, E expectedValue, E newValue) {
441445
return (E)AA.compareAndExchange(array, i, expectedValue, newValue);
442446
}
@@ -455,6 +459,7 @@ public final E compareAndExchange(int i, E expectedValue, E newValue) {
455459
* expected value if successful
456460
* @since 9
457461
*/
462+
@SuppressWarnings("unchecked")
458463
public final E compareAndExchangeAcquire(int i, E expectedValue, E newValue) {
459464
return (E)AA.compareAndExchangeAcquire(array, i, expectedValue, newValue);
460465
}
@@ -473,6 +478,7 @@ public final E compareAndExchangeAcquire(int i, E expectedValue, E newValue) {
473478
* expected value if successful
474479
* @since 9
475480
*/
481+
@SuppressWarnings("unchecked")
476482
public final E compareAndExchangeRelease(int i, E expectedValue, E newValue) {
477483
return (E)AA.compareAndExchangeRelease(array, i, expectedValue, newValue);
478484
}

src/java.base/share/classes/java/util/stream/ForEachOps.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ else if (spliterator != null) {
505505
// "happens-before" completion of the associated left-most leaf task
506506
// of right subtree (if any, which can be this task's right sibling)
507507
//
508+
@SuppressWarnings("unchecked")
508509
var leftDescendant = (ForEachOrderedTask<S, T>)NEXT.getAndSet(this, null);
509510
if (leftDescendant != null)
510511
leftDescendant.tryComplete();

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public enum Feature {
263263
FLEXIBLE_CONSTRUCTORS(JDK22, Fragments.FeatureFlexibleConstructors, DiagKind.NORMAL),
264264
MODULE_IMPORTS(JDK23, Fragments.FeatureModuleImports, DiagKind.PLURAL),
265265
PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19),
266+
ERASE_POLY_SIG_RETURN_TYPE(JDK24),
266267
;
267268

268269
enum DiagKind {

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public class Infer {
100100

101101
private final boolean dumpStacktraceOnError;
102102

103+
private final boolean erasePolySigReturnType;
104+
103105
public static Infer instance(Context context) {
104106
Infer instance = context.get(inferKey);
105107
if (instance == null)
@@ -123,6 +125,8 @@ protected Infer(Context context) {
123125

124126
emptyContext = new InferenceContext(this, List.nil());
125127
dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE);
128+
Source source = Source.instance(context);
129+
erasePolySigReturnType = Source.Feature.ERASE_POLY_SIG_RETURN_TYPE.allowedInSource(source);
126130
}
127131

128132
/** A value for prototypes that admit any type, including polymorphic ones. */
@@ -544,8 +548,8 @@ Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
544548
case TYPECAST:
545549
JCTypeCast castTree = (JCTypeCast)env.next.tree;
546550
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
547-
castTree.clazz.type :
548-
spType;
551+
(erasePolySigReturnType ? types.erasure(castTree.clazz.type) : castTree.clazz.type) :
552+
spType;
549553
break;
550554
case EXEC:
551555
JCTree.JCExpressionStatement execTree =

src/jdk.dynalink/share/classes/jdk/dynalink/BiClassValue.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ T getReverseValue(final Class<?> c) {
113113
}
114114

115115
private T compute(final VarHandle mapHandle, final Class<?> c, final Function<Class<?>, T> compute) {
116+
@SuppressWarnings("unchecked")
116117
Map<Class<?>, T> map = (Map<Class<?>, T>) mapHandle.getVolatile(this);
117118
T value;
118119
T newValue = null;
@@ -127,6 +128,7 @@ private T compute(final VarHandle mapHandle, final Class<?> c, final Function<Cl
127128
final Map.Entry<Class<?>, T>[] entries = map.entrySet().toArray(new Map.Entry[map.size() + 1]);
128129
entries[map.size()] = Map.entry(c, newValue);
129130
final var newMap = Map.ofEntries(entries);
131+
@SuppressWarnings("unchecked")
130132
final var witness = (Map<Class<?>, T>) mapHandle.compareAndExchange(this, map, newMap);
131133
if (witness == map) {
132134
value = newValue;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* /nodynamiccopyright/
3+
*/
4+
5+
import java.lang.invoke.VarHandle;
6+
7+
class PolymorphicMethodTest<V> {
8+
VarHandle vh;
9+
V method(Object obj) {
10+
return (V)vh.getAndSet(this, obj);
11+
}
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PolymorphicMethodTest.java:10:31: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Object, V
2+
1 warning
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- compiler.warn.source.no.system.modules.path: 23, (compiler.misc.source.no.system.modules.path: 23)
2+
1 warning

test/langtools/tools/javac/mandatoryWarnings/unchecked/Test.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@
4242
* @compile/ref=Test4b.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 1 A.java B.java
4343
* @compile/ref=Test4c.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 2 A.java B.java
4444
* @compile/ref=Test4d.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 3 A.java B.java
45+
* @compile/ref=PolymorphicMethodTest.out -XDrawDiagnostics -Xlint:unchecked PolymorphicMethodTest.java
46+
* @compile/ref=PolymorphicMethodTest_Source23.out -XDrawDiagnostics -Xlint:unchecked -source 23 PolymorphicMethodTest.java
4547
*/

0 commit comments

Comments
 (0)