8343286: Missing unchecked cast warning in polymorphic method call

Reviewed-by: mcimadamore
This commit is contained in:
Vicente Romero 2024-11-13 15:31:02 +00:00
parent b80ca4902a
commit cc2acd14b1
10 changed files with 40 additions and 2 deletions

View File

@ -279,6 +279,7 @@ public class AtomicReference<V> implements java.io.Serializable {
* @return the value * @return the value
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final V getPlain() { public final V getPlain() {
return (V)VALUE.get(this); return (V)VALUE.get(this);
} }
@ -302,6 +303,7 @@ public class AtomicReference<V> implements java.io.Serializable {
* @return the value * @return the value
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final V getOpaque() { public final V getOpaque() {
return (V)VALUE.getOpaque(this); return (V)VALUE.getOpaque(this);
} }
@ -324,6 +326,7 @@ public class AtomicReference<V> implements java.io.Serializable {
* @return the value * @return the value
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final V getAcquire() { public final V getAcquire() {
return (V)VALUE.getAcquire(this); return (V)VALUE.getAcquire(this);
} }
@ -351,6 +354,7 @@ public class AtomicReference<V> implements java.io.Serializable {
* expected value if successful * expected value if successful
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final V compareAndExchange(V expectedValue, V newValue) { public final V compareAndExchange(V expectedValue, V newValue) {
return (V)VALUE.compareAndExchange(this, expectedValue, newValue); return (V)VALUE.compareAndExchange(this, expectedValue, newValue);
} }
@ -367,6 +371,7 @@ public class AtomicReference<V> implements java.io.Serializable {
* expected value if successful * expected value if successful
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final V compareAndExchangeAcquire(V expectedValue, V newValue) { public final V compareAndExchangeAcquire(V expectedValue, V newValue) {
return (V)VALUE.compareAndExchangeAcquire(this, expectedValue, newValue); return (V)VALUE.compareAndExchangeAcquire(this, expectedValue, newValue);
} }
@ -383,6 +388,7 @@ public class AtomicReference<V> implements java.io.Serializable {
* expected value if successful * expected value if successful
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final V compareAndExchangeRelease(V expectedValue, V newValue) { public final V compareAndExchangeRelease(V expectedValue, V newValue) {
return (V)VALUE.compareAndExchangeRelease(this, expectedValue, newValue); return (V)VALUE.compareAndExchangeRelease(this, expectedValue, newValue);
} }

View File

@ -358,6 +358,7 @@ public class AtomicReferenceArray<E> implements java.io.Serializable {
* @return the value * @return the value
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final E getPlain(int i) { public final E getPlain(int i) {
return (E)AA.get(array, i); return (E)AA.get(array, i);
} }
@ -383,6 +384,7 @@ public class AtomicReferenceArray<E> implements java.io.Serializable {
* @return the value * @return the value
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final E getOpaque(int i) { public final E getOpaque(int i) {
return (E)AA.getOpaque(array, i); return (E)AA.getOpaque(array, i);
} }
@ -407,6 +409,7 @@ public class AtomicReferenceArray<E> implements java.io.Serializable {
* @return the value * @return the value
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final E getAcquire(int i) { public final E getAcquire(int i) {
return (E)AA.getAcquire(array, i); return (E)AA.getAcquire(array, i);
} }
@ -437,6 +440,7 @@ public class AtomicReferenceArray<E> implements java.io.Serializable {
* expected value if successful * expected value if successful
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final E compareAndExchange(int i, E expectedValue, E newValue) { public final E compareAndExchange(int i, E expectedValue, E newValue) {
return (E)AA.compareAndExchange(array, i, expectedValue, newValue); return (E)AA.compareAndExchange(array, i, expectedValue, newValue);
} }
@ -455,6 +459,7 @@ public class AtomicReferenceArray<E> implements java.io.Serializable {
* expected value if successful * expected value if successful
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final E compareAndExchangeAcquire(int i, E expectedValue, E newValue) { public final E compareAndExchangeAcquire(int i, E expectedValue, E newValue) {
return (E)AA.compareAndExchangeAcquire(array, i, expectedValue, newValue); return (E)AA.compareAndExchangeAcquire(array, i, expectedValue, newValue);
} }
@ -473,6 +478,7 @@ public class AtomicReferenceArray<E> implements java.io.Serializable {
* expected value if successful * expected value if successful
* @since 9 * @since 9
*/ */
@SuppressWarnings("unchecked")
public final E compareAndExchangeRelease(int i, E expectedValue, E newValue) { public final E compareAndExchangeRelease(int i, E expectedValue, E newValue) {
return (E)AA.compareAndExchangeRelease(array, i, expectedValue, newValue); return (E)AA.compareAndExchangeRelease(array, i, expectedValue, newValue);
} }

View File

@ -505,6 +505,7 @@ final class ForEachOps {
// "happens-before" completion of the associated left-most leaf task // "happens-before" completion of the associated left-most leaf task
// of right subtree (if any, which can be this task's right sibling) // of right subtree (if any, which can be this task's right sibling)
// //
@SuppressWarnings("unchecked")
var leftDescendant = (ForEachOrderedTask<S, T>)NEXT.getAndSet(this, null); var leftDescendant = (ForEachOrderedTask<S, T>)NEXT.getAndSet(this, null);
if (leftDescendant != null) if (leftDescendant != null)
leftDescendant.tryComplete(); leftDescendant.tryComplete();

View File

@ -263,6 +263,7 @@ public enum Source {
FLEXIBLE_CONSTRUCTORS(JDK22, Fragments.FeatureFlexibleConstructors, DiagKind.NORMAL), FLEXIBLE_CONSTRUCTORS(JDK22, Fragments.FeatureFlexibleConstructors, DiagKind.NORMAL),
MODULE_IMPORTS(JDK23, Fragments.FeatureModuleImports, DiagKind.PLURAL), MODULE_IMPORTS(JDK23, Fragments.FeatureModuleImports, DiagKind.PLURAL),
PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19), PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19),
ERASE_POLY_SIG_RETURN_TYPE(JDK24),
; ;
enum DiagKind { enum DiagKind {

View File

@ -100,6 +100,8 @@ public class Infer {
private final boolean dumpStacktraceOnError; private final boolean dumpStacktraceOnError;
private final boolean erasePolySigReturnType;
public static Infer instance(Context context) { public static Infer instance(Context context) {
Infer instance = context.get(inferKey); Infer instance = context.get(inferKey);
if (instance == null) if (instance == null)
@ -123,6 +125,8 @@ public class Infer {
emptyContext = new InferenceContext(this, List.nil()); emptyContext = new InferenceContext(this, List.nil());
dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE); dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE);
Source source = Source.instance(context);
erasePolySigReturnType = Source.Feature.ERASE_POLY_SIG_RETURN_TYPE.allowedInSource(source);
} }
/** A value for prototypes that admit any type, including polymorphic ones. */ /** A value for prototypes that admit any type, including polymorphic ones. */
@ -544,8 +548,8 @@ public class Infer {
case TYPECAST: case TYPECAST:
JCTypeCast castTree = (JCTypeCast)env.next.tree; JCTypeCast castTree = (JCTypeCast)env.next.tree;
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
castTree.clazz.type : (erasePolySigReturnType ? types.erasure(castTree.clazz.type) : castTree.clazz.type) :
spType; spType;
break; break;
case EXEC: case EXEC:
JCTree.JCExpressionStatement execTree = JCTree.JCExpressionStatement execTree =

View File

@ -113,6 +113,7 @@ final class BiClassValue<T> {
} }
private T compute(final VarHandle mapHandle, final Class<?> c, final Function<Class<?>, T> compute) { private T compute(final VarHandle mapHandle, final Class<?> c, final Function<Class<?>, T> compute) {
@SuppressWarnings("unchecked")
Map<Class<?>, T> map = (Map<Class<?>, T>) mapHandle.getVolatile(this); Map<Class<?>, T> map = (Map<Class<?>, T>) mapHandle.getVolatile(this);
T value; T value;
T newValue = null; T newValue = null;
@ -127,6 +128,7 @@ final class BiClassValue<T> {
final Map.Entry<Class<?>, T>[] entries = map.entrySet().toArray(new Map.Entry[map.size() + 1]); final Map.Entry<Class<?>, T>[] entries = map.entrySet().toArray(new Map.Entry[map.size() + 1]);
entries[map.size()] = Map.entry(c, newValue); entries[map.size()] = Map.entry(c, newValue);
final var newMap = Map.ofEntries(entries); final var newMap = Map.ofEntries(entries);
@SuppressWarnings("unchecked")
final var witness = (Map<Class<?>, T>) mapHandle.compareAndExchange(this, map, newMap); final var witness = (Map<Class<?>, T>) mapHandle.compareAndExchange(this, map, newMap);
if (witness == map) { if (witness == map) {
value = newValue; value = newValue;

View File

@ -0,0 +1,12 @@
/*
* /nodynamiccopyright/
*/
import java.lang.invoke.VarHandle;
class PolymorphicMethodTest<V> {
VarHandle vh;
V method(Object obj) {
return (V)vh.getAndSet(this, obj);
}
}

View File

@ -0,0 +1,2 @@
PolymorphicMethodTest.java:10:31: compiler.warn.prob.found.req: (compiler.misc.unchecked.cast.to.type), java.lang.Object, V
1 warning

View File

@ -0,0 +1,2 @@
- compiler.warn.source.no.system.modules.path: 23, (compiler.misc.source.no.system.modules.path: 23)
1 warning

View File

@ -42,4 +42,6 @@
* @compile/ref=Test4b.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 1 A.java B.java * @compile/ref=Test4b.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 1 A.java B.java
* @compile/ref=Test4c.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 2 A.java B.java * @compile/ref=Test4c.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 2 A.java B.java
* @compile/ref=Test4d.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 3 A.java B.java * @compile/ref=Test4d.out -XDrawDiagnostics -nowarn -Xlint:unchecked -Xmaxwarns 3 A.java B.java
* @compile/ref=PolymorphicMethodTest.out -XDrawDiagnostics -Xlint:unchecked PolymorphicMethodTest.java
* @compile/ref=PolymorphicMethodTest_Source23.out -XDrawDiagnostics -Xlint:unchecked -source 23 PolymorphicMethodTest.java
*/ */