Skip to content

Unified: Support implicit closure parameters - #23181

Open
paldepind wants to merge 2 commits into
mainfrom
unified/implicit-closure-parameter
Open

paldepind wants to merge 2 commits into
mainfrom
unified/implicit-closure-parameter

Conversation

@paldepind

@paldepind paldepind commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Add support for implicit closure parameters. In Swift these looks like $0, $1, etc. The implementation is generic and should be able to handle the same feature in other languages as well (we'd need to tweak things for Kotlin, which I've documented in a comment).

  • No changes in extraction. Use of implicit parameters is still normal identifiers.
  • A new predicate in the AST plugin makes it possible to specify when implicit closure parameters are used.
  • ParameterEx synthesizes parameters for implicit closure parameters.
  • The rest of the changes are to take the new parameters into account.

paldepind and others added 2 commits November 20, 2026 11:57
Cover local binding, type inference, and data flow for shorthand closure parameters, including nesting and skipped positions. Mark unsupported results as MISSING against the existing implementation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 59310c15-21d2-4bb3-86ff-0f2b06d57f1d

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

Unbounded synthetic parameter creation from large shorthand indices can cause excessive memory use or analysis time in both ParameterEx.qll and DataFlowNode.qll.

1 open issue
What changed in this PR

This PR adds implicit closure parameter support to Unified’s analysis libraries, starting with Swift’s $0, $1, and similar names, without changing extraction.

Changes:

  • Adds a language-plugin hook and synthesizes implicit closure parameters.
  • Integrates those parameters with name binding, type inference, and data flow.
  • Adds Swift tests for nested closures, skipped parameters, and contextual inference.
File Description
unified/​ql/​test/​library-tests/​type-inference/​type-inference.expected Updates expected closure type-inference results.
unified/​ql/​test/​library-tests/​type-inference/​closures.swift Adds shorthand parameter inference cases.
unified/​ql/​test/​library-tests/​local-name-binding/​test.ql Recognizes implicit parameter expectation tags.
unified/​ql/​test/​library-tests/​local-name-binding/​closures.swift Tests shorthand parameter binding and nesting.
unified/​ql/​test/​library-tests/​dataflow/​test.expected Updates expected shorthand parameter flow results.
unified/​ql/​test/​library-tests/​dataflow/​capture.swift Adds shorthand parameter and capture flow cases.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInference.qll Connects implicit parameters to inference.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​Type.qll Extends closure parameter pseudo-types to synthetic parameters.
unified/​ql/​lib/​codeql/​unified/​internal/​ParameterEx.qll Represents synthetic closure parameters.
unified/​ql/​lib/​codeql/​unified/​internal/​LocalNameBinding.qll Binds shorthand names to implicit local variables.
unified/​ql/​lib/​codeql/​unified/​internal/​FacadeAst.qll Includes implicit parameters in closure arity.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​DataFlowNode.qll Adds implicit parameter nodes and entry writes.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​DataFlowGraph.qll Links implicit parameters to variable writes.
unified/​ql/​lib/​codeql/​unified/​internal/​AstPluginSwift.qll Recognizes Swift shorthand parameter names.
unified/​ql/​lib/​codeql/​unified/​internal/​AstPlugin.qll Defines the generic implicit parameter hook.
shared/​typeinference/​codeql/​typeinference/​internal/​TypeInference.qll Uses implicit variable accesses as parameter inference nodes.

Review effort: Balanced (adaptive)

exists(LocalVariable v | v.isImplicitReceiverParameter(c))
} or
TImplicitFunctionExprParameter(FunctionExpr fe, int i) {
i = [0 .. fe.getNumberOfImplicitParameters() - 1]

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.

I am not too worried. But if a human echoes this we can make a change here.

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.

I'm fine with ignoring this

@paldepind
paldepind marked this pull request as ready for review November 20, 2026 13:43
@paldepind
paldepind requested review from a team as code owners November 20, 2026 13:43
@paldepind paldepind added the no-change-note-required This PR does not need a change note label Nov 20, 2026

@asgerf asgerf 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.

Looks great! Very nicely solved.

Perhaps @hvitved should take a look at the type inference changes as well.

I took the liberty of kicking off a DCA run for this.

/** Gets the number of parameters of this function. */
int getNumberOfParameters() { result = count(this.getAParameter()) }
/** Gets the number of implicit parameters, if any. */
int getNumberOfImplicitParameters() {

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.

Having no result is surprising behavior for a predicate with this name. Could we just make this return 0 when there are no implicit parameters? The existing use-cases should work fine because the range expressions become [0 ... -1].

Could you also expand the qldoc a bit to mention what "implicit parameters" are? I worry some might expect this number to include the receiver parameter as well, which it doesn't.

exists(LocalVariable v | v.isImplicitReceiverParameter(c))
} or
TImplicitFunctionExprParameter(FunctionExpr fe, int i) {
i = [0 .. fe.getNumberOfImplicitParameters() - 1]

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.

I'm fine with ignoring this

result = p.getPattern()
or
// Implicit parameters do not have a pattern, use an access instead.
result = any(Variable v | implicitParameterDecl(p, v)).getAnAccess()

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.

Could we add a type inference test case where the same implicit parameter is used multiple times? I'm not sure exactly what kind of bug might emerge from this, but it seems worth covering given that the associated "inference node" is not unique anymore.

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.

3 participants