Skip to content
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

All types are immutable for nullness. Field visibility shouldn't matter. #6709

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -126,7 +126,7 @@ public class NullnessAnnotatedTypeFactory
/** Aliases for {@code @Nonnull}. */
@SuppressWarnings({
"signature:argument", // Class names intentionally obfuscated
"signature:assignment.type.incompatible" // Class names intentionally obfuscated
"signature:assignment" // Class names intentionally obfuscated
})
private static final List<@FullyQualifiedName String> NONNULL_ALIASES =
Arrays.<@FullyQualifiedName String>asList(
Expand Down Expand Up @@ -221,7 +221,7 @@ public class NullnessAnnotatedTypeFactory
/** Aliases for {@code @Nullable}. */
@SuppressWarnings({
"signature:argument", // Class names intentionally obfuscated
"signature:assignment.type.incompatible" // Class names intentionally obfuscated
"signature:assignment" // Class names intentionally obfuscated
})
private static final List<@FullyQualifiedName String> NULLABLE_ALIASES =
Arrays.<@FullyQualifiedName String>asList(
Expand Down Expand Up @@ -356,7 +356,7 @@ public class NullnessAnnotatedTypeFactory
/** Aliases for {@code @PolyNull}. */
@SuppressWarnings({
"signature:argument", // Class names intentionally obfuscated
"signature:assignment.type.incompatible" // Class names intentionally obfuscated
"signature:assignment" // Class names intentionally obfuscated
})
private static final List<@FullyQualifiedName String> POLYNULL_ALIASES =
Arrays.<@FullyQualifiedName String>asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,11 @@ protected AnnotationMirror leastUpperBoundWithElements(
throw new TypeSystemError("Unexpected QualifierKinds: %s %s", qualifierKind1, qualifierKind2);
}

@SuppressWarnings("nullness:return")
@Override
@SuppressWarnings(
"nullness:return" // This class UnitsQualifierHierarchy is annotated for nullness,
// but the outer class UnitsAnnotatedTypeFactory is not, so the type of fields is @Nullable.
)
protected AnnotationMirror greatestLowerBoundWithElements(
AnnotationMirror a1,
QualifierKind qualifierKind1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public interface InterfaceTest {
default void requireAinferSibling1(@AinferSibling1 String x) {}

default void testX() {
// :: warning: (argument)
requireAinferSibling1(toaster);
}
}
19 changes: 19 additions & 0 deletions checker/tests/nullness/Issue4923.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Issue4923 {
interface Go {
void go();
}

final Go go =
new Go() {
@Override
public void go() {
synchronized (x) {
}
}
};
final Object x = new Object();

// Make sure that initializer type is compatible with declared type
// :: error: (assignment)
final Object y = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -471,7 +470,8 @@ private boolean isExpressionOrStatementPure(
/**
* Add field values to the initial store before {@code methodTree}.
*
* <p>The initializer value is inserted into {@code store} if the field is private and final.
* <p>The initializer value is inserted into {@code store} if the field is final and the field
* type is immutable, as defined by {@link AnnotatedTypeFactory#isImmutable(TypeMirror)}.
*
* <p>The declared value is inserted into {@code store} if:
*
Expand All @@ -492,9 +492,9 @@ private void addInitialFieldValues(S store, ClassTree classTree, MethodTree meth
TypeElement classEle = TreeUtils.elementFromDeclaration(classTree);
for (FieldInitialValue<V> fieldInitialValue : analysis.getFieldInitialValues()) {
VariableElement varEle = fieldInitialValue.fieldDecl.getField();
// Insert the value from the initializer of private final fields.
// TODO: should field visibility matter? An access from outside the class might observe
// the declared type instead of a refined type. Issue a warning to alert users?
if (fieldInitialValue.initializer != null
&& varEle.getModifiers().contains(Modifier.PRIVATE)
&& ElementUtils.isFinal(varEle)
&& analysis.atypeFactory.isImmutable(ElementUtils.getType(varEle))) {
store.insertValue(fieldInitialValue.fieldDecl, fieldInitialValue.initializer);
Expand Down