Repository navigation
Require every element of an intersection bound to be @Nullable #1836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,9 +304,7 @@ private boolean[] getTypeParamsWithNullableUpperBound(Type type) { | |
| for (int i = 0; i < baseTypeArgs.size(); i++) { | ||
| Type typeVariable = baseTypeArgs.get(i); | ||
| Type upperBound = typeVariable.getUpperBound(); | ||
| com.sun.tools.javac.util.List<Attribute.TypeCompound> annotationMirrors = | ||
| upperBound.getAnnotationMirrors(); | ||
| if (Nullness.hasNullableAnnotation(annotationMirrors.stream(), config) | ||
| if (GenericsUtils.isNullableAnnotated(upperBound, config) | ||
| || handler.onOverrideClassTypeVariableUpperBound(type.tsym.toString(), i)) { | ||
| result[i] = true; | ||
| } | ||
|
|
@@ -318,18 +316,69 @@ private boolean[] getTypeParamsWithNullableUpperBound(Type type) { | |
| com.sun.tools.javac.util.List<Attribute.TypeCompound> rawTypeAttributes = | ||
| tsym.getRawTypeAttributes(); | ||
| if (rawTypeAttributes != null) { | ||
| Map<Integer, Set<Integer>> nullableBoundsPerTypeParam = new LinkedHashMap<>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I correct that we should actually be removing all this code involving |
||
| for (Attribute.TypeCompound typeCompound : rawTypeAttributes) { | ||
| if (typeCompound.position.type.equals(TargetType.CLASS_TYPE_PARAMETER_BOUND) | ||
| && Nullness.isNullableAnnotation( | ||
| if (!typeCompound.position.type.equals(TargetType.CLASS_TYPE_PARAMETER_BOUND) | ||
| || !Nullness.isNullableAnnotation( | ||
| typeCompound.type.tsym.getQualifiedName().toString(), config)) { | ||
| int index = typeCompound.position.parameter_index; | ||
| result[index] = true; | ||
| continue; | ||
| } | ||
| int typeParamIndex = typeCompound.position.parameter_index; | ||
| Type bound = | ||
| boundAt( | ||
| baseTypeArgs.get(typeParamIndex).getUpperBound(), | ||
| typeCompound.position.bound_index); | ||
| // The annotation may sit on a type argument nested inside the bound, or on an enclosing | ||
| // type of a nested-class bound, rather than on the bound itself. | ||
| if (bound != null && NullabilityUtil.isDirectTypeUseAnnotation(typeCompound, bound)) { | ||
| nullableBoundsPerTypeParam | ||
| .computeIfAbsent(typeParamIndex, k -> new LinkedHashSet<>()) | ||
| .add(typeCompound.position.bound_index); | ||
| } | ||
| } | ||
| nullableBoundsPerTypeParam.forEach( | ||
| (index, nullableBounds) -> | ||
| result[index] |= | ||
| nullableBounds.size() == boundCount(baseTypeArgs.get(index).getUpperBound())); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the bound that {@code boundIndex} names in a {@code CLASS_TYPE_PARAMETER_BOUND} | ||
| * type-annotation position, or {@code null} if the index names no bound of an intersection. The | ||
| * type path of such an annotation is relative to that one bound, so it cannot be read against the | ||
| * intersection. A type variable with a single bound has only that bound, which is returned for | ||
| * any index. | ||
| * | ||
| * <p>javac numbers the bounds of an intersection in declaration order, giving index 0 to the | ||
| * class bound. Where the declaration has none, index 0 goes to the implicit {@code Object} | ||
| * supertype and the declared bounds start at 1. | ||
| */ | ||
| private static @Nullable Type boundAt(Type upperBound, int boundIndex) { | ||
| if (!(upperBound instanceof Type.IntersectionClassType intersectionType)) { | ||
| return upperBound; | ||
| } | ||
| com.sun.tools.javac.util.List<Type> bounds = intersectionType.getExplicitComponents(); | ||
| int position = boundIndex - (intersectionType.allInterfaces ? 1 : 0); | ||
| return position >= 0 && position < bounds.size() ? bounds.get(position) : null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the number of bounds declared for a type variable with the given upper bound. An | ||
| * intersection bound includes {@code null} only when every one of these bounds is annotated | ||
| * {@code @Nullable}. | ||
| * | ||
| * <p>An intersection whose bounds are all interfaces carries an implicit {@code Object} supertype | ||
| * that the declaration did not write, hence {@code getExplicitComponents()}. It occupies {@code | ||
| * bound_index} 0 but never carries an annotation; see {@link #boundAt}. | ||
| */ | ||
| private static int boundCount(Type upperBound) { | ||
| return upperBound instanceof Type.IntersectionClassType intersectionType | ||
| ? intersectionType.getExplicitComponents().size() | ||
| : 1; | ||
| } | ||
|
|
||
| /** | ||
| * Checks validity of type arguments at a generic method call. A {@code @Nullable} type argument | ||
| * can only be used for a type variable that has a {@code @Nullable} upper bound. | ||
|
|
@@ -373,10 +422,8 @@ public void checkGenericMethodCallTypeArguments(Tree tree, VisitorState state) { | |
| if (nullableTypeArguments.containsKey(i)) { | ||
| Type typeVariable = baseTypeVariables.get(i); | ||
| Type upperBound = typeVariable.getUpperBound(); | ||
| com.sun.tools.javac.util.List<Attribute.TypeCompound> annotationMirrors = | ||
| upperBound.getAnnotationMirrors(); | ||
| boolean hasNullableAnnotation = | ||
| Nullness.hasNullableAnnotation(annotationMirrors.stream(), config) | ||
| GenericsUtils.isNullableAnnotated(upperBound, config) | ||
| || handler.onOverrideClassTypeVariableUpperBound(baseType.tsym.toString(), i); | ||
| // if type variable's upper bound does not have @Nullable annotation then the instantiation | ||
| // is invalid | ||
|
|
@@ -1988,16 +2035,21 @@ private static boolean shouldRunDataflowForExpression(Type exprType, ExpressionT | |
|
|
||
| private Type updateTypeWithNullness( | ||
| VisitorState state, Type argumentType, Nullness refinedNullness) { | ||
| // Both branches act on the top-level annotation of argumentType, so they read the mirrors | ||
| // rather than going through GenericsUtils#isNullableAnnotated: removeNullableAnnotation | ||
| // requires a top-level annotation to be present, which an intersection never has. | ||
| boolean hasNullableAnnotation = | ||
| Nullness.hasNullableAnnotation(argumentType.getAnnotationMirrors().stream(), config); | ||
| if (NullabilityUtil.nullnessToBool(refinedNullness)) { | ||
| // refine to @Nullable | ||
| if (isNullableAnnotated(argumentType)) { | ||
| if (hasNullableAnnotation) { | ||
| return argumentType; | ||
| } | ||
| return TypeSubstitutionUtils.typeWithAnnot( | ||
| argumentType, getSyntheticNullableAnnotType(state)); | ||
| } else { | ||
| // refine to @NonNull, by removing the top-level @Nullable annotation if present. | ||
| if (!isNullableAnnotated(argumentType)) { | ||
| if (!hasNullableAnnotation) { | ||
| return argumentType; | ||
| } | ||
| return TypeSubstitutionUtils.removeNullableAnnotation(argumentType, config); | ||
|
|
@@ -2131,8 +2183,8 @@ boolean subtypeParameterNullability( | |
| Type.ArrayType rhsArrayType = (Type.ArrayType) rhsType; | ||
| Type lhsComponentType = lhsArrayType.getComponentType(); | ||
| Type rhsComponentType = rhsArrayType.getComponentType(); | ||
| boolean isLHSNullableAnnotated = isNullableAnnotated(lhsComponentType); | ||
| boolean isRHSNullableAnnotated = isNullableAnnotated(rhsComponentType); | ||
| boolean isLHSNullableAnnotated = GenericsUtils.isNullableAnnotated(lhsComponentType, config); | ||
| boolean isRHSNullableAnnotated = GenericsUtils.isNullableAnnotated(rhsComponentType, config); | ||
| // an array of @Nullable references is _not_ a subtype of an array of @NonNull references | ||
| if (isRHSNullableAnnotated && !isLHSNullableAnnotated) { | ||
| return false; | ||
|
|
@@ -2758,7 +2810,7 @@ private boolean substitutedMethodTypeVarUpperBoundIsNullable( | |
| return true; | ||
| } | ||
| Type upperBound = substitutedTypeVar.getUpperBound(); | ||
| if (Nullness.hasNullableAnnotation(upperBound.getAnnotationMirrors().stream(), config)) { | ||
| if (GenericsUtils.isNullableAnnotated(upperBound, config)) { | ||
| return true; | ||
| } | ||
| if (Nullness.hasNonNullAnnotation(upperBound.getAnnotationMirrors().stream(), config)) { | ||
|
|
@@ -2775,7 +2827,7 @@ private boolean substitutedMethodTypeVarUpperBoundIsNullable( | |
| List<Symbol.TypeVariableSymbol> originalTypeParams = overriddenMethod.getTypeParameters(); | ||
| Type originalBound = | ||
| (Type) ((TypeVariable) originalTypeParams.get(typeVarIndex).asType()).getUpperBound(); | ||
| if (Nullness.hasNullableAnnotation(originalBound.getAnnotationMirrors().stream(), config)) { | ||
| if (GenericsUtils.isNullableAnnotated(originalBound, config)) { | ||
| return true; | ||
| } | ||
| if (Nullness.hasNonNullAnnotation(originalBound.getAnnotationMirrors().stream(), config)) { | ||
|
|
@@ -3587,12 +3639,7 @@ private Nullness getParameterTypeNullness(Type type, boolean isVarargsParam, Vis | |
| * @return Returns the Nullness of the type based on the Nullability annotation. | ||
| */ | ||
| private Nullness getTypeNullness(Type type) { | ||
| boolean hasNullableAnnotation = | ||
| Nullness.hasNullableAnnotation(type.getAnnotationMirrors().stream(), config); | ||
| if (hasNullableAnnotation) { | ||
| return Nullness.NULLABLE; | ||
| } | ||
| return Nullness.NONNULL; | ||
| return GenericsUtils.isNullableAnnotated(type, config) ? Nullness.NULLABLE : Nullness.NONNULL; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -3726,10 +3773,6 @@ public void clearCache() { | |
| nestedNullabilityRepairInProgress.clear(); | ||
| } | ||
|
|
||
| public boolean isNullableAnnotated(Type type) { | ||
| return Nullness.hasNullableAnnotation(type.getAnnotationMirrors().stream(), config); | ||
| } | ||
|
|
||
| /** | ||
| * Store a poly expression's target type with explicit type-variable nullability from the | ||
| * assignment context, when the expression type has not already been cached. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,7 +94,7 @@ static Type wildcardUpperBound( | |
| // type variable in @NullUnmarked code | ||
| if (formalTypeVar != null | ||
| && upperBoundIsNullable(formalTypeVar.asElement(), config, handler, state) | ||
| && !Nullness.hasNullableAnnotation(upperBound.getAnnotationMirrors().stream(), config)) { | ||
| && !isNullableAnnotated(upperBound, config)) { | ||
| upperBound = | ||
| TypeSubstitutionUtils.typeWithAnnot( | ||
| upperBound, GenericsChecks.getSyntheticNullableAnnotType(state)); | ||
|
|
@@ -116,7 +116,8 @@ && upperBoundIsNullable(formalTypeVar.asElement(), config, handler, state) | |
| * library model overrides the bound nullability for the type variable, or when the declared upper | ||
| * bound has an explicit {@code @Nullable} annotation. An explicit {@code @NonNull} annotation on | ||
| * a type-variable bound takes precedence over nullability inherited from that type variable's | ||
| * upper bound. | ||
| * upper bound. An intersection bound is nullable only when every one of its elements is; see | ||
| * {@link #isNullableAnnotated}. | ||
| */ | ||
| static boolean upperBoundIsNullable( | ||
| Element typeVarElement, Config config, Handler handler, VisitorState state) { | ||
|
|
@@ -144,7 +145,7 @@ static boolean upperBoundIsNullable( | |
| } | ||
| } | ||
| Type upperBound = (Type) ((TypeVariable) typeVarElement.asType()).getUpperBound(); | ||
| if (Nullness.hasNullableAnnotation(upperBound.getAnnotationMirrors().stream(), config)) { | ||
| if (isNullableAnnotated(upperBound, config)) { | ||
| return true; | ||
| } | ||
| if (Nullness.hasNonNullAnnotation(upperBound.getAnnotationMirrors().stream(), config)) { | ||
|
|
@@ -156,6 +157,27 @@ static boolean upperBoundIsNullable( | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if {@code type} carries a {@code @Nullable} annotation, treating an intersection | ||
| * type as {@code @Nullable} only when every one of its elements is. | ||
| * | ||
| * <p>JSpecify assigns a nullness operator to each element of an intersection type separately, and | ||
| * fixes the operator of the intersection as a whole to {@code NO_CHANGE}; javac stores type | ||
| * annotations on the elements, and an {@code IntersectionClassType} cannot carry one at all. A | ||
| * type variable is null-exclusive as soon as one element of its bound is, since that element | ||
| * still establishes a nullness-subtyping edge. See the <a | ||
| * href="https://jspecify.dev/docs/spec/#intersection-types">JSpecify specification</a>. | ||
|
Comment on lines
+164
to
+169
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think this should be an inline comment under the |
||
| */ | ||
| static boolean isNullableAnnotated(Type type, Config config) { | ||
| if (type instanceof Type.IntersectionClassType intersectionType) { | ||
| return intersectionType.getExplicitComponents().stream() | ||
| .allMatch( | ||
| element -> | ||
| Nullness.hasNullableAnnotation(element.getAnnotationMirrors().stream(), config)); | ||
| } | ||
| return Nullness.hasNullableAnnotation(type.getAnnotationMirrors().stream(), config); | ||
| } | ||
|
|
||
| private static boolean fromUnannotatedMethodOrClass( | ||
| Element typeVarElement, Config config, Handler handler, VisitorState state) { | ||
| Element enclosingElement = typeVarElement.getEnclosingElement(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes give me more motivation to get rid of this fallback code entirely, so we don't need to keep maintaining it.