-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce Strict Interpretation Of Type Use Annotation Locations Outsid…
…e of JSpecify mode (#1010) Currently, outside of JSpecify, both top-level and element annotations are treated the same, i.e., considered as top-level annotations. This changes the default behavior by ignoring annotations on elements and adds a flag to revert back to legacy behavior. **Example:** ``` @nullable Object[] foo1 = null; Object @nullable[] foo2 = null; ``` **Current Behavior:** Both assignments are fine **New Behavior:** The first assignment raises an error since annotations on elements are ignored altogether and **_NOT_** treated as top-level annotations. Fixes #1007 --------- Co-authored-by: Manu Sridharan <[email protected]>
- Loading branch information
1 parent
a863221
commit 0d500cc
Showing
9 changed files
with
334 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
nullaway/src/test/java/com/uber/nullaway/ArrayTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Copyright (c) 2024 Uber Technologies, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.nullaway; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class ArrayTests extends NullAwayTestsBase { | ||
@Test | ||
public void arrayDeclarationAnnotation() { | ||
defaultCompilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import javax.annotation.Nullable;", | ||
"class Test {", | ||
" static @Nullable String [] fizz = {\"1\"};", | ||
" static Object o1 = new Object();", | ||
" static void foo() {", | ||
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field", | ||
" o1 = fizz;", | ||
" // BUG: Diagnostic contains: dereferenced expression fizz is @Nullable", | ||
" o1 = fizz.length;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void arrayLegacyDeclarationAnnotation() { | ||
makeLegacyModeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import javax.annotation.Nullable;", | ||
"class Test {", | ||
" static @Nullable String [] fizz = {\"1\"};", | ||
" static Object o1 = new Object();", | ||
" static void foo() {", | ||
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field", | ||
" o1 = fizz;", | ||
" // BUG: Diagnostic contains: dereferenced expression fizz is @Nullable", | ||
" o1 = fizz.length;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void typeUseLegacyAnnotationOnArray() { | ||
makeLegacyModeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"class Test {", | ||
" // ok only for backwards compat", | ||
" @Nullable Object[] foo1 = null;", | ||
" // ok according to spec", | ||
" Object @Nullable[] foo2 = null;", | ||
" // ok, but elements are not treated as @Nullable outside of JSpecify mode", | ||
" @Nullable Object @Nullable[] foo3 = null;", | ||
" // ok only for backwards compat", | ||
" @Nullable Object [][] foo4 = null;", | ||
" // ok according to spec", | ||
" Object @Nullable [][] foo5 = null;", | ||
" // ok, but @Nullable applies to first array dimension not the elements or the array ref", | ||
" Object [] @Nullable [] foo6 = null;", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void typeUseAnnotationOnArray() { | ||
defaultCompilationHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"class Test {", | ||
" // @Nullable is not applied on top-level of array", | ||
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field", | ||
" @Nullable Object[] foo1 = null;", | ||
" // ok according to spec", | ||
" Object @Nullable[] foo2 = null;", | ||
" // ok according to spec", | ||
" @Nullable Object @Nullable [] foo3 = null;", | ||
" // @Nullable is not applied on top-level of array", | ||
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field", | ||
" @Nullable Object [][] foo4 = null;", | ||
" // ok according to spec", | ||
" Object @Nullable [][] foo5 = null;", | ||
" // @Nullable is not applied on top-level of array", | ||
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field", | ||
" Object [] @Nullable [] foo6 = null;", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void typeUseAndDeclarationAnnotationOnArray() { | ||
defaultCompilationHelper | ||
.addSourceLines( | ||
"Nullable.java", | ||
"package com.uber;", | ||
"import java.lang.annotation.ElementType;", | ||
"import java.lang.annotation.Target;", | ||
"@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})", | ||
"public @interface Nullable {}") | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"class Test {", | ||
" @Nullable Object[] foo1 = null;", | ||
" Object @Nullable[] foo2 = null;", | ||
" @Nullable Object @Nullable [] foo3 = null;", | ||
" @Nullable Object [][] foo4 = null;", | ||
" Object @Nullable [][] foo5 = null;", | ||
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field", | ||
" Object [] @Nullable [] foo6 = null;", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void typeUseAndDeclarationLegacyAnnotationOnArray() { | ||
makeLegacyModeHelper() | ||
.addSourceLines( | ||
"Nullable.java", | ||
"package com.uber;", | ||
"import java.lang.annotation.ElementType;", | ||
"import java.lang.annotation.Target;", | ||
"@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})", | ||
"public @interface Nullable {}") | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"class Test {", | ||
" @Nullable Object[] foo1 = null;", | ||
" Object @Nullable[] foo2 = null;", | ||
" @Nullable Object @Nullable [] foo3 = null;", | ||
" @Nullable Object [][] foo4 = null;", | ||
" Object @Nullable [][] foo5 = null;", | ||
" Object [] @Nullable [] foo6 = null;", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
private CompilationTestHelper makeLegacyModeHelper() { | ||
return makeTestHelperWithArgs( | ||
Arrays.asList( | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
"-XepOpt:NullAway:LegacyAnnotationLocations=true")); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
nullaway/src/test/java/com/uber/nullaway/LegacyAndJspecifyModeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.uber.nullaway; | ||
|
||
import static com.uber.nullaway.ErrorProneCLIFlagsConfig.FL_JSPECIFY_MODE; | ||
import static com.uber.nullaway.ErrorProneCLIFlagsConfig.FL_LEGACY_ANNOTATION_LOCATION; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.google.errorprone.ErrorProneFlags; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import org.junit.Test; | ||
|
||
public class LegacyAndJspecifyModeTest { | ||
|
||
private static final String ERROR = | ||
"-XepOpt:" | ||
+ FL_LEGACY_ANNOTATION_LOCATION | ||
+ " cannot be used when " | ||
+ FL_JSPECIFY_MODE | ||
+ " is set "; | ||
|
||
@Test | ||
public void testIllegalStateExceptionUsingReflection() throws Exception { | ||
ErrorProneFlags flags = | ||
ErrorProneFlags.builder() | ||
.putFlag("NullAway:AnnotatedPackages", "com.uber") | ||
.putFlag("NullAway:JSpecifyMode", "true") | ||
.putFlag("NullAway:LegacyAnnotationLocations", "true") | ||
.build(); | ||
|
||
Constructor<?> constructor = | ||
ErrorProneCLIFlagsConfig.class.getDeclaredConstructor(ErrorProneFlags.class); | ||
constructor.setAccessible(true); | ||
|
||
InvocationTargetException exception = | ||
assertThrows( | ||
InvocationTargetException.class, | ||
() -> constructor.newInstance(flags), | ||
"Expected IllegalStateException when both jspecifyMode and legacyAnnotationLocation are true."); | ||
|
||
Throwable cause = exception.getCause(); | ||
assertThat(cause, instanceOf(IllegalStateException.class)); | ||
assertEquals(cause.getMessage(), ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.