Skip to content

Commit

Permalink
Revert "Resolve regression for type annotations directly on inner typ…
Browse files Browse the repository at this point in the history
…es. (uber#706)"

This reverts commit c5e1a79.
  • Loading branch information
msridhar committed Jul 18, 2023
1 parent a6f7c11 commit 7265611
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 254 deletions.
34 changes: 1 addition & 33 deletions nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

package com.uber.nullaway;

import static com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind.ARRAY;
import static com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind.INNER_TYPE;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.VisitorState;
Expand All @@ -44,7 +41,6 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.TargetType;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic;
Expand Down Expand Up @@ -296,35 +292,7 @@ private static boolean isDirectTypeUseAnnotation(Attribute.TypeCompound t) {
// location is a list of TypePathEntry objects, indicating whether the annotation is
// on an array, inner type, wildcard, or type argument. If it's empty, then the
// annotation is directly on the type.
// We care about both annotations directly on the outer type and also those directly
// on an inner type or array dimension, but wish to discard annotations on wildcards,
// or type arguments.
// For arrays, we treat annotations on the outer type and on any dimension of the array
// as applying to the nullability of the array itself, not the elements.
// We don't allow mixing of inner types and array dimensions in the same location
// (i.e. `Foo.@Nullable Bar []` is meaningless).
// These aren't correct semantics for type use annotations, but a series of hacky
// compromises to keep some semblance of backwards compatibility until we can do a
// proper deprecation of the incorrect behaviors for type use annotations when their
// semantics don't match those of a declaration annotation in the same position.
// See https://github.com/uber/NullAway/issues/708
boolean locationHasInnerTypes = false;
boolean locationHasArray = false;
for (TypePathEntry entry : t.position.location) {
switch (entry.tag) {
case INNER_TYPE:
locationHasInnerTypes = true;
break;
case ARRAY:
locationHasArray = true;
break;
default:
// Wildcard or type argument!
return false;
}
}
// Make sure it's not a mix of inner types and arrays for this annotation's location
return !(locationHasInnerTypes && locationHasArray);
return t.position.location.isEmpty();
}

/**
Expand Down
30 changes: 30 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayCoreTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -960,4 +960,34 @@ public void primitiveCastsRememberNullChecks() {
"}")
.doTest();
}

@Test
public void annotationAppliedToTypeParameter() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.uber;",
"import java.util.List;",
"import java.util.ArrayList;",
"import org.checkerframework.checker.nullness.qual.Nullable;",
"class TypeArgumentAnnotation {",
" List<@Nullable String> fSafe = new ArrayList<>();",
" @Nullable List<String> fUnsafe = new ArrayList<>();",
" void useParamSafe(List<@Nullable String> list) {",
" list.hashCode();",
" }",
" void useParamUnsafe(@Nullable List<String> list) {",
" // BUG: Diagnostic contains: dereferenced",
" list.hashCode();",
" }",
" void useFieldSafe() {",
" fSafe.hashCode();",
" }",
" void useFieldUnsafe() {",
" // BUG: Diagnostic contains: dereferenced",
" fUnsafe.hashCode();",
" }",
"}")
.doTest();
}
}

This file was deleted.

0 comments on commit 7265611

Please sign in to comment.