Skip to content

Support unnamed pattern variables #6996

@msridhar

Description

@msridhar

JDK 22 introduced Unnamed Variables & Patterns. Currently, unnamed pattern variables do not seem to be supported in CFG construction. Test case from uber/NullAway#1168:

package org.example;

public class NullAwayDiscardError {

	public sealed interface IntOrBool {}
	public record WrappedInt(int a) implements IntOrBool {}
	public record WrappedBoolean(boolean b) implements IntOrBool {}

	public static void unwrap(IntOrBool i) {
		switch(i) {
			case WrappedInt(_) -> {}
			case WrappedBoolean(_) -> {}
		}
	}

}

This causes NullAway to crash. I think the reason is that WrappedInt(_) is represented as a DeconstructedPatternNode, but the nested pattern ends up as null. Here is the relevant code:

public Node visitDeconstructionPattern21(Tree deconstructionPatternTree, Void p) {
List<? extends Tree> nestedPatternTrees =
DeconstructionPatternUtils.getNestedPatterns(deconstructionPatternTree);
List<Node> nestedPatterns = new ArrayList<>(nestedPatternTrees.size());
for (Tree pattern : nestedPatternTrees) {
nestedPatterns.add(scan(pattern, p));
}

Here when we scan the nested pattern tree, it's an AnyPatternTree, which is not handled in CFG construction, so scan returns null. Other code does not expect a null nested pattern in a DeconstructedPatternNode leading to the crash.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions