Skip to content
Open
Show file tree
Hide file tree
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 Oct 30, 2026
d91899f
Move the changelog entry to the 4.3.3 section
smillst Oct 30, 2026
4eae124
Check the pattern of an instanceof in the Nullness Checker
smillst Oct 30, 2026
86ca81b
Merge remote-tracking branch 'origin/master' into pattern-variable-de…
smillst Nov 2, 2026
9c0cff7
Merge branch 'master' into pattern-variable-defaults
smillst Nov 3, 2026
dc4dfaf
Add failing pattern-variable tests; add instanceof-pattern hook; cach…
mernst Nov 9, 2026
c55db03
Merge branch 'master' into pattern-variable-defaults
smillst Nov 9, 2026
149185a
Remove the cache for QualifierDefaults.isNestedBindingVariable
smillst Nov 9, 2026
f6789ee
Treat binding variables as local variables in ElementUtils.isLocalVar…
smillst Nov 9, 2026
58eb0e0
Skip the test of an explicitly annotated case-label pattern
smillst Nov 9, 2026
fc7e9f0
Refine a binding variable as if the matched value were cast to its type
smillst Nov 9, 2026
a1908fd
Merge branch 'master' into pattern-variable-defaults
smillst Nov 9, 2026
9004eea
Share the cast qualifier-bound logic in QualifierUpperBounds.limitToB…
smillst Nov 9, 2026
cc928fe
Merge remote-tracking branch 'smillst/pattern-variable-defaults' into…
smillst Nov 9, 2026
441c036
Merge branch 'master' into pattern-variable-defaults
mernst Nov 10, 2026
51f6c9b
Correct the description of pattern-variable refinement; list API changes
smillst Nov 10, 2026
2f387fc
Test a @NonNull type pattern matched against a @Nullable expression
smillst Nov 10, 2026
4cb169a
Say that a pattern variable is refined as for a cast, and that a type…
smillst Nov 10, 2026
0dd16f2
Merge branch 'master' into pattern-variable-defaults
mernst Nov 11, 2026
704e7d5
Merge branch 'master' into pattern-variable-defaults
mernst Nov 13, 2026
b5febe4
Merge remote-tracking branch 'upstream/master' into pattern-variable-…
mernst Nov 14, 2026
24ba818
Merge branch 'master' into pattern-variable-defaults
mernst Nov 14, 2026
994b14a
Rename `checkInstanceOfPattern` and `instanceOfPatternMatchedType`; i…
mernst Nov 14, 2026
e4ff1ce
Merge branch 'master' into pattern-variable-defaults
mernst Nov 16, 2026
35e8315
Extract `QualifierUpperBounds.limitToBound` from `PropagationTreeAnno…
smillst Nov 16, 2026
7cc6743
Merge branch 'limit-to-bound' into pattern-variable-defaults
smillst Nov 16, 2026
54e763e
Remove the Nullness Checker check of an `instanceof` binding variable
smillst Nov 16, 2026
3f1b499
Spell `instanceOf` as in `visitInstanceOf` in a test method name
smillst Nov 16, 2026
1fe54e0
Spell `instanceOf` as in `visitInstanceOf`; improve the instanceof-pa…
smillst Nov 16, 2026
5580737
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 16, 2026
5d8ac06
Keep the type-based default of a nested binding variable
smillst Nov 16, 2026
b19f699
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 16, 2026
f34bc96
Do not refine a nested binding variable by the value that `instanceof…
smillst Nov 16, 2026
2578ca9
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 16, 2026
ff92607
Address review findings: document the scope of the pattern-variable d…
smillst Nov 16, 2026
1592a92
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 16, 2026
f25bbe5
Merge branch 'master' into pattern-variable-defaults
mernst Nov 16, 2026
5365587
Merge branch 'master' into pattern-variable-defaults
mernst Nov 16, 2026
988d346
Skip a test that uses switch patterns below Java 21
smillst Nov 17, 2026
42fde04
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 17, 2026
87bb45c
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 17, 2026
c24b1a4
Spell `instanceof` as one word in a test method name
smillst Nov 17, 2026
2df2c61
Go back to spelling `instanceof` as one word in method names
smillst Nov 17, 2026
5b8d7d2
Remove the skipped placeholder test for case-label pattern checking
smillst Nov 17, 2026
4b65d6e
Write test expectations as `// :: error: [key]`, as checker/tests/REA…
smillst Nov 18, 2026
c39039a
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
smillst Nov 18, 2026
e1f86fd
Write a test expectation as `// :: warning: [key]`
smillst Nov 18, 2026
219f03c
Clarify three comments about `instanceof` binding variables
smillst Nov 18, 2026
4ade2b1
Merge branch 'master' into pattern-variable-defaults
mernst Nov 19, 2026
19a7bfe
Merge branch 'master' into pattern-variable-defaults
mernst Nov 21, 2026
96f858d
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 22, 2026
39a95c3
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 22, 2026
4a51307
Brevity
mernst Nov 22, 2026
7015407
Do not default a nested binding variable whose type is a type variable
mernst Nov 22, 2026
82cca30
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 22, 2026
6e51d74
Declare QualifierDefaults' default-application scope @Nullable
smillst Nov 23, 2026
0388512
Merge branch 'master' into pattern-variable-defaults
mernst Nov 24, 2026
bb2bc2e
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 24, 2026
48a5286
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 24, 2026
20894ef
Add Javadoc for DefaultApplierElement's constructor and defaultableTy…
smillst Nov 24, 2026
a6db03c
Merge branch 'master' into pattern-variable-defaults
mernst Nov 24, 2026
177e5cd
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 24, 2026
fd0f4c1
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 25, 2026
e8e18ce
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 25, 2026
fe6f83d
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 26, 2026
5ba7706
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 26, 2026
e1e56fe
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 28, 2026
7cdd97e
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 28, 2026
6c7ed8d
Merge remote-tracking branch 'upstream/master' into pattern-variable-…
mernst Nov 30, 2026
14012db
Merge branch 'master' into pattern-variable-defaults-only
mernst Nov 30, 2026
fac7f04
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Nov 30, 2026
3731301
Merge branch 'master' into pattern-variable-defaults-only
smillst Nov 30, 2026
dce06eb
Merge branch 'master' into pattern-variable-defaults-only
mernst Dec 1, 2026
7a89103
Merge branch 'pattern-variable-defaults-only' into pattern-variable-d…
mernst Dec 1, 2026
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 @@ -15,8 +15,8 @@ public enum TypeUseLocation {
FIELD,

/**
* Apply default annotations to all unannotated raw types of local variables, casts, and
* instanceof.
* Apply default annotations to all unannotated raw types of local variables, pattern variables
* that are not nested in a record pattern, casts, and instanceof.
*/
LOCAL_VARIABLE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
Expand All @@ -22,6 +23,7 @@
import org.checkerframework.dataflow.cfg.node.ArrayAccessNode;
import org.checkerframework.dataflow.cfg.node.FieldAccessNode;
import org.checkerframework.dataflow.cfg.node.InstanceOfNode;
import org.checkerframework.dataflow.cfg.node.LocalVariableNode;
import org.checkerframework.dataflow.cfg.node.MethodAccessNode;
import org.checkerframework.dataflow.cfg.node.MethodInvocationNode;
import org.checkerframework.dataflow.cfg.node.Node;
Expand Down Expand Up @@ -358,10 +360,24 @@ public TransferResult<NullnessValue, NullnessStore> visitInstanceOf(
TransferResult<NullnessValue, NullnessStore> result = super.visitInstanceOf(n, p);
NullnessStore thenStore = result.getThenStore();
NullnessStore elseStore = result.getElseStore();
// `n` evaluates to true only if its operand is non-null. So, in the then store, the operand
// is non-null, and so is the binding variable of `n`'s pattern, if that pattern is a type
// pattern.
makeNonNull(thenStore, n.getOperand());
if (n.getPatternNode() instanceof LocalVariableNode bindingVar) {
makeNonNull(thenStore, bindingVar);
}
Comment thread
smillst marked this conversation as resolved.
Resolved
return new ConditionalTransferResult<>(result.getResultValue(), thenStore, elseStore);
}

@Override
protected void refineCaseLabelBindingVariable(
NullnessStore store, VariableTree varTree, ExpressionTree selector) {
super.refineCaseLabelBindingVariable(store, varTree, selector);
// A type pattern that is a case label does not match null (JLS 14.11.1).
store.insertValue(JavaExpression.fromVariableTree(varTree), NONNULL);
}

@Override
public TransferResult<NullnessValue, NullnessStore> visitMethodAccess(
MethodAccessNode n, TransferInput<NullnessValue, NullnessStore> p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.AssertTree;
import com.sun.source.tree.BinaryTree;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.CatchTree;
import com.sun.source.tree.ClassTree;
Expand Down Expand Up @@ -445,6 +446,10 @@ public Void visitInstanceOf(InstanceOfTree tree, Void p) {
// Don't call super because it will issue an incorrect instanceof.unsafe warning.
// Instead, validate the reference type and scan the expression and the pattern or type.
Tree patternTree = tree.getPattern();
// Check the binding variable, as `super.visitInstanceOf` would.
if (patternTree instanceof BindingPatternTree) {
checkInstanceofPattern(tree);
}
if (patternTree == null) {
validateTypeOf(refTypeTree);
}
Expand All @@ -453,6 +458,25 @@ public Void visitInstanceOf(InstanceOfTree tree, Void p) {
return null;
}

@Override
protected AnnotatedTypeMirror instanceofPatternMatchedType(AnnotatedTypeMirror expType) {
// instanceof is false for null (JLS 15.20.2), so the nullness qualifier of the left operand
// does not restrict the binding variable.
return withNonNull(expType);
}

/**
* Returns a copy of {@code type} whose primary nullness qualifier is {@code @NonNull}.
*
* @param type a type
* @return a copy of {@code type} whose primary nullness qualifier is {@code @NonNull}
*/
private AnnotatedTypeMirror withNonNull(AnnotatedTypeMirror type) {
AnnotatedTypeMirror copy = type.shallowCopy();
copy.replaceAnnotation(NONNULL);
return copy;
}

/**
* Reports an error if a comparison of a @NonNull expression with the null literal is performed.
* Does nothing unless {@code -Alint=redundantNullComparison} is passed on the command line.
Expand Down
36 changes: 36 additions & 0 deletions checker/tests/interning/BindingVariableClass.java
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) {}
}
35 changes: 35 additions & 0 deletions checker/tests/interning/NestedBindingVariableDefault.java
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 checker/tests/nullness/java17/InstanceOfPatternInitialization.java
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ static class Box<T extends @NonNull Object> {}

static class Sub<T extends @NonNull Object> extends Box<T> {}

void instanceOfPattern(Box<String> b) {
void instanceofPattern(Box<String> b) {
// :: error: (type.argument)
if (b instanceof Sub<@Nullable String> s) {}
}

void instanceOfNoPattern(Box<String> b) {
void instanceofNoPattern(Box<String> b) {
// :: error: (type.argument)
if (b instanceof Sub<@Nullable String>) {}
}

void instanceOfValid(Box<String> b) {
void instanceofValid(Box<String> b) {
if (b instanceof Sub<String> s) {}
if (b instanceof Sub<String>) {}
}
Expand Down
43 changes: 43 additions & 0 deletions checker/tests/nullness/java21/CaseLabelPatternInitialization.java
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();
}
}
}
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 checker/tests/signedness/java21/PatternVariableDefaults.java
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;
}
}
}
Loading