Repository navigation
Check an instanceof binding variable in the Nullness Checker
#8467
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
Open
smillst
wants to merge
74
commits into
typetools:master
Choose a base branch
from
smillst:pattern-variable-defaults
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+561
−40
Open
Changes from all commits
Commits
Show all changes
74 commits
Select commit
Hold shift + click to select a range
1ad145d
Default top-level pattern variables to top, like other local variables
smillst d91899f
Move the changelog entry to the 4.3.3 section
smillst 4eae124
Check the pattern of an instanceof in the Nullness Checker
smillst 86ca81b
Merge remote-tracking branch 'origin/master' into pattern-variable-de…
smillst 9c0cff7
Merge branch 'master' into pattern-variable-defaults
smillst dc4dfaf
Add failing pattern-variable tests; add instanceof-pattern hook; cach…
mernst c55db03
Merge branch 'master' into pattern-variable-defaults
smillst 149185a
Remove the cache for QualifierDefaults.isNestedBindingVariable
smillst f6789ee
Treat binding variables as local variables in ElementUtils.isLocalVar…
smillst 58eb0e0
Skip the test of an explicitly annotated case-label pattern
smillst fc7e9f0
Refine a binding variable as if the matched value were cast to its type
smillst a1908fd
Merge branch 'master' into pattern-variable-defaults
smillst 9004eea
Share the cast qualifier-bound logic in QualifierUpperBounds.limitToB…
smillst cc928fe
Merge remote-tracking branch 'smillst/pattern-variable-defaults' into…
smillst 441c036
Merge branch 'master' into pattern-variable-defaults
mernst 51f6c9b
Correct the description of pattern-variable refinement; list API changes
smillst 2f387fc
Test a @NonNull type pattern matched against a @Nullable expression
smillst 4cb169a
Say that a pattern variable is refined as for a cast, and that a type…
smillst 0dd16f2
Merge branch 'master' into pattern-variable-defaults
mernst 704e7d5
Merge branch 'master' into pattern-variable-defaults
mernst b5febe4
Merge remote-tracking branch 'upstream/master' into pattern-variable-…
mernst 24ba818
Merge branch 'master' into pattern-variable-defaults
mernst 994b14a
Rename `checkInstanceOfPattern` and `instanceOfPatternMatchedType`; i…
mernst e4ff1ce
Merge branch 'master' into pattern-variable-defaults
mernst 35e8315
Extract `QualifierUpperBounds.limitToBound` from `PropagationTreeAnno…
smillst 7cc6743
Merge branch 'limit-to-bound' into pattern-variable-defaults
smillst 54e763e
Remove the Nullness Checker check of an `instanceof` binding variable
smillst 3f1b499
Spell `instanceOf` as in `visitInstanceOf` in a test method name
smillst 1fe54e0
Spell `instanceOf` as in `visitInstanceOf`; improve the instanceof-pa…
smillst 5580737
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst 5d8ac06
Keep the type-based default of a nested binding variable
smillst b19f699
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst f34bc96
Do not refine a nested binding variable by the value that `instanceof…
smillst 2578ca9
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst ff92607
Address review findings: document the scope of the pattern-variable d…
smillst 1592a92
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst f25bbe5
Merge branch 'master' into pattern-variable-defaults
mernst 5365587
Merge branch 'master' into pattern-variable-defaults
mernst 988d346
Skip a test that uses switch patterns below Java 21
smillst 42fde04
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst 87bb45c
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst c24b1a4
Spell `instanceof` as one word in a test method name
smillst 2df2c61
Go back to spelling `instanceof` as one word in method names
smillst 5b8d7d2
Remove the skipped placeholder test for case-label pattern checking
smillst 4b65d6e
Write test expectations as `// :: error: [key]`, as checker/tests/REA…
smillst c39039a
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst e1f86fd
Write a test expectation as `// :: warning: [key]`
smillst 219f03c
Clarify three comments about `instanceof` binding variables
smillst 4ade2b1
Merge branch 'master' into pattern-variable-defaults
mernst 19a7bfe
Merge branch 'master' into pattern-variable-defaults
mernst 96f858d
Merge branch 'master' into pattern-variable-defaults-only
mernst 39a95c3
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst 4a51307
Brevity
mernst 7015407
Do not default a nested binding variable whose type is a type variable
mernst 82cca30
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst 6e51d74
Declare QualifierDefaults' default-application scope @Nullable
smillst 0388512
Merge branch 'master' into pattern-variable-defaults
mernst bb2bc2e
Merge branch 'master' into pattern-variable-defaults-only
mernst 48a5286
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst 20894ef
Add Javadoc for DefaultApplierElement's constructor and defaultableTy…
smillst a6db03c
Merge branch 'master' into pattern-variable-defaults
mernst 177e5cd
Merge branch 'master' into pattern-variable-defaults-only
mernst fd0f4c1
Merge branch 'master' into pattern-variable-defaults-only
mernst e8e18ce
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst fe6f83d
Merge branch 'master' into pattern-variable-defaults-only
mernst 5ba7706
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst e1e56fe
Merge branch 'master' into pattern-variable-defaults-only
mernst 7cdd97e
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst 6c7ed8d
Merge remote-tracking branch 'upstream/master' into pattern-variable-…
mernst 14012db
Merge branch 'master' into pattern-variable-defaults-only
mernst fac7f04
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst 3731301
Merge branch 'master' into pattern-variable-defaults-only
smillst dce06eb
Merge branch 'master' into pattern-variable-defaults-only
mernst 7a89103
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| import org.checkerframework.checker.interning.qual.Interned; | ||
|
|
||
| // @below-java21-jdk-skip-test | ||
|
|
||
| // Every Class is @Interned, so a binding variable of type Class is @Interned, as a cast to Class | ||
| // is. | ||
| public class BindingVariableClass { | ||
|
|
||
| void take(@Interned Class<?> c) {} | ||
|
|
||
| void cast(Object value) { | ||
| take((Class<?>) value); | ||
| } | ||
|
|
||
| void instanceofPattern(Object value) { | ||
| if (value instanceof Class<?> cls) { | ||
| take(cls); | ||
| } | ||
| } | ||
|
|
||
| void caseLabelPattern(Object value) { | ||
| switch (value) { | ||
| case Class<?> cls -> take(cls); | ||
| default -> {} | ||
| } | ||
| } | ||
|
|
||
| void notInterned(Object value) { | ||
| if (value instanceof String s) { | ||
| // :: error: [argument] | ||
| takeString(s); | ||
| } | ||
| } | ||
|
|
||
| void takeString(@Interned String s) {} | ||
| } |
This file contains hidden or 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,35 @@ | ||
| import org.checkerframework.checker.interning.qual.Interned; | ||
|
|
||
| // @below-java21-jdk-skip-test | ||
|
|
||
| // A binding variable nested in a record pattern keeps the type-based default of its type, such as | ||
| // @Interned for a class annotated @Interned. | ||
| public class NestedBindingVariableDefault { | ||
|
|
||
| @Interned static class Token {} | ||
|
|
||
| static class NotInterned {} | ||
|
|
||
| record Box(Token t, NotInterned n) {} | ||
|
|
||
| void take(@Interned Token t) {} | ||
|
|
||
| void takeNotInterned(@Interned NotInterned n) {} | ||
|
|
||
| void caseLabel(Object o) { | ||
| switch (o) { | ||
| case Box(Token t, NotInterned n) -> { | ||
| take(t); | ||
| // :: error: [argument] | ||
| takeNotInterned(n); | ||
| } | ||
| default -> {} | ||
| } | ||
| } | ||
|
|
||
| void instanceOf(Object o) { | ||
| if (o instanceof Box(Token t, NotInterned n)) { | ||
| take(t); | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
checker/tests/nullness/java17/InstanceOfPatternInitialization.java
This file contains hidden or 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,51 @@ | ||
| // @below-java17-jdk-skip-test | ||
|
|
||
| import org.checkerframework.checker.initialization.qual.Initialized; | ||
| import org.checkerframework.checker.initialization.qual.UnknownInitialization; | ||
| import org.checkerframework.checker.nullness.qual.NonNull; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| public class InstanceOfPatternInitialization { | ||
|
|
||
| // A type pattern in an instanceof does not match null, so the binding variable is non-null. | ||
| void nullable(@Nullable Object o) { | ||
| if (o instanceof String s) { | ||
| s.length(); | ||
| } | ||
| } | ||
|
|
||
| // An explicitly @NonNull binding variable is safe even when the expression is @Nullable. | ||
| void nonNullExplicit(@Nullable Object o) { | ||
| if (o instanceof @NonNull String s) { | ||
| s.length(); | ||
| } | ||
| } | ||
|
|
||
| // An unannotated binding variable has the qualifiers of the expression that instanceof tests. | ||
| void unknownInitialization(@UnknownInitialization Object o) { | ||
| if (o instanceof String s) { | ||
| @UnknownInitialization String t = s; | ||
| // :: error: [assignment] | ||
| @Initialized String u = s; | ||
| } | ||
| } | ||
|
|
||
| // instanceof does not check initialization, so writing @Initialized on the binding variable is | ||
| // unsafe. | ||
| void initializedExplicit(@UnknownInitialization Object o) { | ||
| // :: warning: [instanceof.pattern.unsafe] | ||
| if (o instanceof @Initialized String s) { | ||
| @Initialized String t = s; | ||
| } | ||
| } | ||
|
|
||
| static class Sub extends InstanceOfPatternInitialization {} | ||
|
|
||
| InstanceOfPatternInitialization() { | ||
| if (this instanceof Sub s) { | ||
| @UnknownInitialization Sub t = s; | ||
| // :: error: [method.invocation] | ||
| s.hashCode(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
checker/tests/nullness/java21/CaseLabelPatternInitialization.java
This file contains hidden or 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,43 @@ | ||
| import org.checkerframework.checker.initialization.qual.Initialized; | ||
| import org.checkerframework.checker.initialization.qual.UnknownInitialization; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| // @below-java21-jdk-skip-test | ||
|
|
||
| // None of the WPI formats supports the new Java 21 language features, so skip inference until they | ||
| // do. | ||
| // @infer-jaifs-skip-test | ||
| // @infer-ajava-skip-test | ||
| // @infer-stubs-skip-test | ||
| public class CaseLabelPatternInitialization { | ||
|
|
||
| record Box(@Nullable Object o) {} | ||
|
|
||
| // A type pattern that is a case label does not match null, so the binding variable is non-null. | ||
| void nullable(@Nullable Object o) { | ||
| switch (o) { | ||
| case String s -> s.length(); | ||
| case null, default -> {} | ||
| } | ||
| } | ||
|
|
||
| // An unannotated binding variable has the qualifiers of the selector. | ||
| void unknownInitialization(@UnknownInitialization Object o) { | ||
| switch (o) { | ||
| case String s -> { | ||
| @UnknownInitialization String t = s; | ||
| // :: error: [assignment] | ||
| @Initialized String u = s; | ||
| } | ||
| default -> {} | ||
| } | ||
| } | ||
|
|
||
| // A binding variable nested in a record pattern is not yet defaulted to top. See | ||
| // https://github.com/typetools/checker-framework/issues/7032 . | ||
| void nested(Box b) { | ||
| if (b instanceof Box(String s)) { | ||
| s.length(); | ||
| } | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
checker/tests/nullness/java21/NestedBindingVariableTypeVariable.java
This file contains hidden or 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,36 @@ | ||
| // @below-java21-jdk-skip-test | ||
|
|
||
| // None of the WPI formats supports the new Java 21 language features, so skip inference until they | ||
| // do. | ||
| // @infer-jaifs-skip-test | ||
| // @infer-ajava-skip-test | ||
| // @infer-stubs-skip-test | ||
|
|
||
| // A binding variable nested in a record pattern is not defaulted, so a binding variable whose type | ||
| // is a type variable has the type of that type variable, which might be nullable. The `accessor` | ||
| // method shows the expected behavior. | ||
| public class NestedBindingVariableTypeVariable { | ||
|
|
||
| record Box<T>(T t) {} | ||
|
|
||
| <T> void accessor(Box<T> b) { | ||
| // :: error: [dereference.of.nullable] | ||
| b.t().toString(); | ||
| } | ||
|
|
||
| <T> void instanceOf(Box<T> b) { | ||
| if (b instanceof Box<T>(T t)) { | ||
| // :: error: [dereference.of.nullable] | ||
| t.toString(); | ||
| } | ||
| } | ||
|
|
||
| <T> void caseLabel(Box<T> b) { | ||
| switch (b) { | ||
| case Box<T>(T t) -> { | ||
| // :: error: [dereference.of.nullable] | ||
| t.toString(); | ||
| } | ||
| } | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
checker/tests/signedness/java21/PatternVariableDefaults.java
This file contains hidden or 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,72 @@ | ||
| import org.checkerframework.checker.signedness.qual.Signed; | ||
| import org.checkerframework.checker.signedness.qual.Unsigned; | ||
|
|
||
| // @below-java21-jdk-skip-test | ||
|
|
||
| // None of the WPI formats supports the new Java 21 language features, so skip inference until they | ||
| // do. | ||
| // @infer-jaifs-skip-test | ||
| // @infer-ajava-skip-test | ||
| // @infer-stubs-skip-test | ||
|
|
||
| // An unannotated pattern variable gets the same default as a local variable, so it has the | ||
| // qualifiers of the matched value. The local variable in each `local*` method shows the expected | ||
| // behavior. | ||
| public class PatternVariableDefaults { | ||
|
|
||
| // Integer has a type-based default, @Signed, which does not apply to the top level of a local | ||
| // variable's type. | ||
|
|
||
| void localBoxed(@Unsigned Integer u) { | ||
| Object o = u; | ||
| Integer i = (Integer) o; | ||
| // :: error: [operation.unsignedlhs] | ||
| int q = i / 2; | ||
| } | ||
|
|
||
| void caseLabelBoxed(@Unsigned Integer u) { | ||
| Object o = u; | ||
| switch (o) { | ||
| case Integer i -> { | ||
| // :: error: [operation.unsignedlhs] | ||
| int q = i / 2; | ||
| } | ||
| default -> {} | ||
| } | ||
| } | ||
|
|
||
| void instanceofBoxed(@Unsigned Integer u) { | ||
| Object o = u; | ||
| if (o instanceof Integer i) { | ||
| // :: error: [operation.unsignedlhs] | ||
| int q = i / 2; | ||
| } | ||
| } | ||
|
|
||
| // A pattern variable whose type is a type variable. | ||
|
|
||
| <T extends Number> void localTypeVariable(@Unsigned T x) { | ||
| T t = x; | ||
| @Unsigned T u = t; | ||
| // :: error: [assignment] | ||
| @Signed T s = t; | ||
| } | ||
|
|
||
| <T extends Number> void caseLabelTypeVariable(@Unsigned T x) { | ||
| switch (x) { | ||
| case T t -> { | ||
| @Unsigned T u = t; | ||
| // :: error: [assignment] | ||
| @Signed T s = t; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| <T extends Number> void instanceofTypeVariable(@Unsigned T x) { | ||
| if (x instanceof T t) { | ||
| @Unsigned T u = t; | ||
| // :: error: [assignment] | ||
| @Signed T s = t; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.