uber/NullAway

Enforce correct type-use annotation locations for nested types

Closed this issue · 1 comments

Type-use nullability annotations on a nested type need to be written on the inner-most type (so instead of writing @Nullable A.B, write A.@Nullable B; see here). Consider this example:

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class SampleQualified {
    class A {
        class B {}
    }
    @Nullable A.B f;
}

This example currently passes NullAway, but on the JSpecify reference checker yields an error:

SampleQualified.java:12: error: [nullness] illegal location for annotation: outer types are always non-null. To annotate the inner type, write `Foo.@Nullable Bar` instead of `@Nullable Foo.Bar`.
    @Nullable A.B f;
                  ^
1 error

As part of #708, we should update NullAway to also require that type-use annotations be written in the correct location for nested types, and issue an error otherwise. This new treatment should be applied independent of the JSpecifyMode flag. The previous treatment of type-use annotations should be preserved if the LegacyAnnotationLocations flag is set to true. And a declaration @Nullable annotation should continue to be treated as applying to the inner-most type (whether the JSpecifyMode flag is set or not).

Related: #706

And see also: #708 (comment)