Skip to content

Unified: Fix two bugs in parameter name scoping - #22960

Merged
asgerf merged 9 commits into
github:mainfrom
asgerf:unified/func-param-clash
Nov 12, 2026
Merged

asgerf merged 9 commits into
github:mainfrom
asgerf:unified/func-param-clash

Conversation

@asgerf

@asgerf asgerf commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes two bugs:

  • When a parameter had the same name as its enclosing function, they mapped to the same (name, scope) pair, and thus got the same LocalName entity. This entity had multiple declaring callables resulting in consistency errors.
  • Parameter defaults can't refer to the parameter list (in Swift) and were incorrectly resolved in this case.

Commit-by-commit review recommended.

@asgerf asgerf added the no-change-note-required This PR does not need a change note label Oct 30, 2026
predicate implicitDeclInScope(string name, AstNode scope) { implicitDeclInScope(name, scope, _) }

predicate accessCand(AstNode n, string name) { n.(PotentialLocalNameAccess).getName() = name }
predicate accessCand(AstNode n, string name) {
@asgerf
asgerf force-pushed the unified/func-param-clash branch from 959ff4f to 67db149 Compare October 30, 2026 14:00
@asgerf
asgerf force-pushed the unified/func-param-clash branch from 67db149 to 60a4adb Compare November 11, 2026 09:45
resolveId only accepted Identifier, but SuperExpr is also a PotentialLocalNameAccess.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf
asgerf marked this pull request as ready for review November 12, 2026 08:33
@asgerf
asgerf requested review from a team as code owners November 12, 2026 08:33
Copilot AI dynamic review requested due to automatic review settings November 12, 2026 08:33

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

Parameters share the body’s scope with local functions, making bindings and calls ambiguous when a local function shadows a parameter.

1 open issue
What changed in this PR

This PR updates unified name binding to separate parameters from enclosing function names and prevent Swift parameter defaults from resolving to parameters.

Changes:

  • Moves parameter bindings to callable-body scope and resolves binding identifiers through their declared scopes.
  • Exposes name-and-scope matching for all shared local entities.
  • Adds Swift regression cases for matching function/parameter names and default-value lookup, alongside fixture formatting.
File Description
unified/​ql/​test/​library-tests/​local-name-binding/​test.swift Adds two scoping regressions and reformats existing tests.
unified/​ql/​lib/​codeql/​unified/​internal/​LocalNameBinding.qll Updates parameter scopes, binding resolution, and scope-tree child handling.
shared/​namebinding/​codeql/​namebinding/​LocalNameBinding.qll Moves name-and-scope matching to the common local-entity class.

Review effort: Balanced (auto)

(
// Scope to the body so parameter defaults cannot see the parameter list
// TODO: Use a plugin for languages where parameter defaults can reference earlier parameters
scope = callable.getBody()

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.

Seems legit.

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.

Pushing a test case to document this, but I'd like to leave it as-is.

AFAICT data flow will actually do the right thing (albeit mostly by accident), but this seems like too much of niche compared to how hard it is to fix, and the fix is unlikely to generalize to other languages where quirks like this are likely to behave differently.

hvitved
hvitved previously approved these changes Nov 12, 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

@asgerf
asgerf merged commit a7925dc into github:main Nov 12, 2026
99 of 109 checks passed
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.

4 participants