Skip to content

Guards: Update Guards-CFG integration to recognize MatchSuccessors. - #23182

Merged
aschackmull merged 2 commits into
github:mainfrom
aschackmull:guards/pattern-match
Dec 2, 2026
Merged

aschackmull merged 2 commits into
github:mainfrom
aschackmull:guards/pattern-match

Conversation

@aschackmull

@aschackmull aschackmull commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

This PR reimplements the way that switch-case guards work in the shared Guards library. Given the now much more uniform handling of switch statements and patterns in the shared CFG, we can depend on MatchingSuccessors to more precisely indicate matching patterns. When these patterns occur in a switch-case context then we can have them satisfy isEquality, which then provides the relevant implications. Support for expression-less switch statements is also added (they're considered to switch on the implicit constant true).

A legacy hook is kept in order to not drop support for C++ switch-case guards.

@aschackmull
aschackmull force-pushed the guards/pattern-match branch 4 times, most recently from f66a7ca to 03d59fb Compare December 1, 2026 10:24
@aschackmull
aschackmull marked this pull request as ready for review December 1, 2026 12:50
@aschackmull
aschackmull requested review from a team as code owners December 1, 2026 12:50
Copilot AI adaptive review requested due to automatic review settings December 1, 2026 12:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Java and Go guard consumers still require Boolean outcomes, dropping match-valued switch constraints and regressing numeric-refinement and equality reasoning.

2 open issues
What changed in this PR

This PR updates the shared Guards library to use control-flow matching outcomes for switch cases and patterns, improving precision across language integrations while preserving legacy C++ support.

Changes:

  • Adds match/no-match guard values, pattern-based equalities, and expression-less switch handling.
  • Updates Java, Go, and C# integrations, including Java type-flow and dispatch consumers.
  • Updates test queries, inline expectations, and expected results for pattern-based guards.
File Description
shared/​controlflow/​codeql/​controlflow/​Guards.qll Adds matching outcomes and pattern-based switch reasoning.
java/​ql/​test/​library-tests/​switch-default-impossible-dispatch/​Test.java Clarifies dispatch expectation comments.
java/​ql/​test/​library-tests/​switch-default-impossible-dispatch/​test.expected Updates impossible-dispatch results.
java/​ql/​test/​library-tests/​guards12/​guard.ql Excludes pattern expressions from equality output.
java/​ql/​test/​library-tests/​guards12/​guard.expected Records pattern-level matching outcomes.
java/​ql/​test/​library-tests/​guards/​guardslogic.ql Includes matching outcomes in test results.
java/​ql/​test/​library-tests/​guards/​guardslogic.expected Updates switch guard results.
java/​ql/​test/​library-tests/​guards/​GuardsInline.ql Removes case-specific Boolean formatting.
java/​ql/​test/​library-tests/​guards/​GuardsInline.expected Updates inline guard results.
java/​ql/​test/​library-tests/​guards/​Guards.java Updates inline expectation comments.
java/​ql/​lib/​semmle/​code/​java/​dispatch/​VirtualDispatch.qll Uses match-aware type-test outcomes.
java/​ql/​lib/​semmle/​code/​java/​dataflow/​TypeFlow.qll Carries guard values through type refinement.
java/​ql/​lib/​semmle/​code/​java/​controlflow/​Guards.qll Exposes case patterns and match-aware type tests.
go/​ql/​test/​library-tests/​semmle/​go/​controlflow/​Guards/​Guards.expected Updates switch and expression-less switch results.
go/​ql/​lib/​semmle/​go/​controlflow/​Guards.qll Replaces case-edge hooks with pattern access.
csharp/​ql/​test/​query-tests/​Nullness/​NullCheck.expected Adds pattern-based null-check results.
csharp/​ql/​test/​query-tests/​Nullness/​EqualityCheck.expected Records match-valued switch equalities.
csharp/​ql/​test/​library-tests/​controlflow/​guards/​GuardedExpr.expected Updates pattern-based expression guards.
csharp/​ql/​test/​library-tests/​controlflow/​guards/​GuardedControlFlowNode.expected Updates pattern-based control-flow guards.
csharp/​ql/​test/​library-tests/​controlflow/​guards/​BooleanGuardedExpr.expected Removes switch-arm Boolean guard results.
csharp/​ql/​lib/​semmle/​code/​csharp/​controlflow/​Guards.qll Adapts case patterns and nullness implications.
cpp/​ql/​lib/​semmle/​code/​cpp/​controlflow/​IRGuards.qll Implements the legacy case-match hook.

Review effort: Balanced (adaptive)

bb1.getASuccessor(successor) = bb2 and successor.getValue() = false
))
}
AstNode getAPattern() { result = this.(G::CaseClause).getAnExpr() }
Comment on lines +548 to +550
exprMatch(e1, this) and this = e2 and eqval.isMatch()
or
exprMatch(e2, this) and this = e1 and eqval.isMatch()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true (and has a minor impact on query results), but Tom and I decided to leave this as followup work for when we next deal with Range Analysis.

@aschackmull

aschackmull commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Dca shows minor alert changes for Java and C# (looks like the general trend is improvements with some back and forth). C++ and Go are completely uneventful.
The lack of match-awareness in range analysis shows a little, though.

@aschackmull aschackmull added the no-change-note-required This PR does not need a change note label Dec 2, 2026

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one question.

typeTest.appliesTypeTest(v.getARead(), t, false) and
guardControls_v1(typeTest, q.getBasicBlock(), false) and
typeTest.appliesTypeTest(v.getARead(), t, false, gv) and
guardControls_v1(typeTest, q.getBasicBlock(), gv.getDualValue()) and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this is gv.getDualValue() and not gv? Before both conjuncts had false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The two false values are unrelated. The appliesTypeTest predicate has gotten a new column with the relevant GuardValue, which was implicitly true before and the guardControls_v1 was implicitly negating that, which was then just written as false. The other false value that remains in the third column of appliesTypeTest is about whether the type test has additional nested patterns, which needs to be false in order for the test to be soundly negatable.

@aschackmull

Copy link
Copy Markdown
Contributor Author

The C# integration test failure looks unrelated. Merging.

@aschackmull
aschackmull merged commit 6489640 into github:main Dec 2, 2026
121 of 123 checks passed
@aschackmull
aschackmull deleted the guards/pattern-match branch December 2, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C# C++ Go Java no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants