Repository navigation
Conversation
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
There was a problem hiding this comment.
🟡 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] |
There was a problem hiding this comment.
I am not too worried. But if a human echoes this we can make a change here.
There was a problem hiding this comment.
I'm fine with ignoring 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() { |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.

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).ParameterExsynthesizes parameters for implicit closure parameters.