Skip to content

Unified: Add Guards instantiation. - #23350

Open
aschackmull wants to merge 3 commits into
github:mainfrom
aschackmull:unified/guards
Open

aschackmull wants to merge 3 commits into
github:mainfrom
aschackmull:unified/guards

Conversation

@aschackmull

@aschackmull aschackmull commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

This instantiates the Guards library for unified.

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

The guard analysis can infer false nil states from ordinary null identifiers and conditional casts, and incorrectly transfer checks between parameters with repeated labels.

3 open issues
What changed in this PR

Adds Unified’s integration with the shared guard-analysis library to reason about conditions and the control flow they govern.

Changes:

  • Instantiates guard analysis with Unified expressions, control flow, SSA, and wrapper calls.
  • Adds AST helpers for bitwise operators, compound assignments, and equality tests.
File Description
unified/​ql/​lib/​codeql/​unified/​internal/​controlflow/​Guards.qll Connects Unified to shared guard analysis.
unified/​ql/​lib/​codeql/​unified/​internal/​AstExtra.qll Adds operator classifications used by guard analysis.

Review effort: Balanced (adaptive)

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.

Comment on lines +100 to +105
class AndExpr extends BinaryExpr {
AndExpr() {
this instanceof BitwiseAndExpr or
this instanceof LogicalAndExpr or
this instanceof AssignBitwiseAndExpr or
this instanceof AssignLogicalAndExpr
Comment on lines +121 to +133
IdExpr() {
this instanceof AssignExpr
or
this instanceof TypeCastExpr and
// TODO: This is Swift-specific and needs to go through a plugin
not this.(TypeCastExpr).getOperator().getValue() = "as?"
}

Expr getEqualChildExpr() {
result = this.(AssignExpr).getValue()
or
result = this.(TypeCastExpr).getExpr()
}
Comment on lines +137 to +138
exists(U::EqualityTest eq | eq = eqtest |
eq.getLeft() = left and eq.getRight() = right and eq.getPolarity() = polarity
Comment on lines +136 to +139
predicate equalityTest(Expr eqtest, Expr left, Expr right, boolean polarity) {
exists(U::EqualityTest eq | eq = eqtest |
eq.getLeft() = left and eq.getRight() = right and eq.getPolarity() = polarity
)
Comment on lines +155 to +156
this instanceof ConstructorDeclaration or
this.(FunctionDeclaration).hasModifier("static") or
Comment on lines +166 to +167
result.getDeclaringCallable() = this and
ppos = getParameterPosition(result)
Comment on lines +64 to +67
private class StringConstant extends ConstantExpr {
StringConstant() { exists(this.getStringValue()) }

override ConstantValue asConstantValue() { result = TStringValue(this.getStringValue()) }
ppos = getParameterPosition(result)
}

Expr getAReturnExpr() { EP::isReturnedExpr(this, result) }
aschackmull and others added 2 commits December 4, 2026 13:45
Also, extend the test to cover more Swift cases.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
@aschackmull aschackmull added the no-change-note-required This PR does not need a change note label Dec 4, 2026
@aschackmull
aschackmull marked this pull request as ready for review December 4, 2026 13:00
@aschackmull
aschackmull requested a review from a team as a code owner December 4, 2026 13:00
@owen-mc
owen-mc requested a max review from Copilot December 5, 2026 07:27

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

The guard model treats optional-to-Any boxing as value-preserving, allowing a non-nil box to incorrectly prove its nil source non-null. Separately, repeated parameter labels transfer wrapper checks to unchecked arguments, variadic mapping can mark nil elements non-null, and failable initializer inference can misattribute delegated initialization failures to input nullness. Overloaded comparisons, logical operators, and expression patterns are also assigned built-in semantics they need not have. These paths need conservative modeling and regression coverage to prevent incorrect guard guarantees.

12 open issues

Review effort: Max

class Case extends AstNode instanceof U::SwitchCase {
Expr getSwitchExpr() { exists(U::SwitchExpr s | s.getValue() = result and s.getACase() = this) }

AstNode getAPattern() { result = super.getPattern() }
class OrExpr extends BinaryExpr {
OrExpr() {
this instanceof BitwiseOrExpr or
this instanceof LogicalOrExpr or
Comment on lines +183 to +184
arg.getValue() = result and
apos = getArgumentPosition(arg)
Comment on lines +60 to +62
private class IntegerConstant extends ConstantExpr instanceof U::IntLiteral {
override int asIntegerValue() { result = super.getValue().toInt() }
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants