Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey;
import org.checkerframework.checker.formatter.qual.FormatMethod;
Expand All @@ -63,6 +64,7 @@
import org.checkerframework.javacutil.TreeUtils;
import org.checkerframework.javacutil.TreeUtilsAfterJava11.SwitchExpressionUtils;
import org.checkerframework.javacutil.TypesUtils;
import org.plumelib.util.ArraysPlume;

/** The visitor for the nullness type-system. */
public class NullnessVisitor
Expand Down Expand Up @@ -227,6 +229,46 @@ protected boolean commonAssignmentCheck(
Tree valueTree,
@CompilerMessageKey String errorKey,
Object... extraArgs) {
if (varType.getKind() == TypeKind.DECLARED && valueType.getKind() == TypeKind.DECLARED) {
AnnotatedTypeMirror.AnnotatedDeclaredType varD =
(AnnotatedTypeMirror.AnnotatedDeclaredType) varType;
AnnotatedTypeMirror.AnnotatedDeclaredType valD =
(AnnotatedTypeMirror.AnnotatedDeclaredType) valueType;
// Only check when the underlying declared types (erased) are the same or
// when the value can be cast to the variable's raw type (conservative check).
TypeMirror varUnderlying = varD.getUnderlyingType();
TypeMirror valUnderlying = valD.getUnderlyingType();
// Only apply this additional nullness invariant-type-argument check when the
// erased declared types are identical and neither side is a raw type. Using a
// looser subtype check caused false positives for assignments where the
// declared types differ (but are related by subtyping) or for raw types.
if (types.isSameType(types.erasure(varUnderlying), types.erasure(valUnderlying))
&& !varD.isUnderlyingTypeRaw()
&& !valD.isUnderlyingTypeRaw()) {
List<? extends AnnotatedTypeMirror> varArgs = varD.getTypeArguments();
List<? extends AnnotatedTypeMirror> valArgs = valD.getTypeArguments();
int n = Math.min(varArgs.size(), valArgs.size());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Subtype assignments bypass this new invariant-argument guard

At Line 245, the check only runs when erased types are exactly equal. Assignments like HashMap<@Nullable String, String> to Map<String, ?> can skip this block entirely, leaving the unsound path to super.commonAssignmentCheck(...).

Please normalize valueType to varType’s declared supertype before comparing type arguments.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@checker/src/main/java/org/checkerframework/checker/nullness/NullnessVisitor.java`
around lines 245 - 250, The current guard compares erasures only when
types.isSameType(types.erasure(varUnderlying), types.erasure(valUnderlying))
which misses cases where valueType is a subtype with a different raw type;
normalize the value to varType’s declared supertype before comparing type
arguments by calling types.asSuper (or equivalent) to get a version of
valUnderlying/valD projected to varUnderlying’s declared supertype and then use
that normalized valD when calling types.erasure(...) and getTypeArguments(); if
types.asSuper returns null or isn’t applicable, fall back to the existing path
that calls super.commonAssignmentCheck(...).

for (int i = 0; i < n; i++) {
AnnotatedTypeMirror va = varArgs.get(i);
AnnotatedTypeMirror aa = valArgs.get(i);
// If the variable's type argument is not a wildcard (i.e., invariant position)
// and the value's corresponding argument may be nullable while the variable's
// is not, report an assignment error.
if (va.getKind() != TypeKind.WILDCARD
&& !aa.hasEffectiveAnnotation(NONNULL)
&& aa.hasEffectiveAnnotation(NULLABLE)
&& va.hasEffectiveAnnotation(NONNULL)) {
String valueTypeString = valueType.toString();
String varTypeString = varType.toString();
checker.reportError(
valueTree,
errorKey,
ArraysPlume.concatenate(extraArgs, valueTypeString, varTypeString));
return false;
}
}
}
}
if (TypesUtils.isPrimitive(varType.getUnderlyingType())
&& !TypesUtils.isPrimitive(valueType.getUnderlyingType())) {
boolean succeed = checkForNullability(valueType, valueTree, UNBOXING_OF_NULLABLE);
Expand Down
29 changes: 29 additions & 0 deletions checker/tests/nullness/WildcardNullableKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;

class Holder<T> {
T f;

Holder(T f) {
this.f = f;
}

T getF() {
return f;
}
}

public class WildcardNullableKey {
public static void main(String[] args) {
Map<@Nullable String, String> map = new HashMap<>();
map.put(null, "");

Holder<Map<@Nullable String, String>> x = new Holder<>(map);

// :: error: (assignment)
Map<String, ?> y = x.getF();

y.keySet().iterator().next().toString();
}
}
Loading