Repository navigation
Type inference: Add support for named arguments - #22973
Conversation
baaa8f7 to
e6d8f96
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Argument matching mishandles repeated Swift labels and labelled enum-case parameters, producing spurious return types and breaking generic type propagation.
2 open issues
What changed in this PR
Adds named-argument matching to shared type inference, enabling Swift to infer argument types when earlier defaulted parameters are omitted.
Changes:
- Exposes argument names and parameter labels through the inference interfaces.
- Adds label-based matching while retaining positional arguments for Rust.
- Updates Swift test annotations and generated inference expectations.
| File | Description |
|---|---|
unified/ql/test/library-tests/type-inference/type-inference.expected |
Records inferred argument-node types and improved closure typing. |
unified/ql/test/library-tests/type-inference/constructors.swift |
Updates an inline expectation comment for closure parameter inference. |
unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll |
Introduces named argument wrappers and receiver handling. |
unified/ql/lib/codeql/unified/internal/ParameterEx.qll |
Exposes external names for source and synthesized parameters. |
shared/typeinference/codeql/typeinference/internal/TypeInference.qll |
Matches arguments to parameters by name or position. |
rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll |
Adapts positional-only Rust arguments to the shared interface. |
Review effort: Max
e6d8f96 to
d6d329e
Compare
d6d329e to
416224b
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved type-inference defects cause label/rank key collisions, misalign repeated-label trailing closures, and block expected-type propagation to function-value call arguments.
3 open issues
2 resolved since last review
Review effort: Max
416224b to
3ae6159
Compare
48791ee to
dc74749
Compare
dc74749 to
6dff9e0
Compare
|
A high level question (I've only skimmed the implementation thus far): Given that Swift requires named arguments to be passed in declaration order and that names are not unique, aren't normal parameters in fact best considered distinguished by position? Could we use positions for "normal" parameters and then only use labels for those where the label can matter (which I think is parameters succeeding parameters with defaults or variadic parameters)? I'd think C# would require a similar treatment given that parameters can be both positional or named? |
That would mean special casing in the Swift logic (the shared type inference logic should not change), which I don't think buys us anything?
Again, this would be Swift specific; C# allows for named arguments to be supplied in different order than the declaration order, which the shared type inference logic should be able to handle. |
I was thinking that it would fix the edge case you mentioned and be a simple change where we change |
|
I think we should go ahead with this PR's notion of named parameters. It is needed for many other languages, so we'll need it eventually, and given that we have it, it is currently the best approximation of Swift semantics. We can refine Swift-specific quirks in later PRs. Associating all arguments with a numeric position comes with its own set of edges cases. In the example below, the second argument to func foo(_ x: String, named: Int = 0, _ y: Int) {}
func foo(_ x: String, _ y: String) {}
foo("x", 45) // targets first too
foo("x", "y") // targets second foo |
If we stop providing labels for parameters with external names, then they will not be able to be matched against labeled arguments, unless we change those as well, which means we would either not support optional arguments or it would have to rely on knowing the call target which I would like to avoid (even though, ideally, there should always be only one call target, I would like to be robust against cases where there are multiple targets). |


This PR aligns the interface for parameters and arguments of the type inference library with that of Unified, in order to be able to handle arguments passed in by name. While Swift requires named arguments to respect the parameter declaration order, this is not assumed in the type inference library, which means that the implementation should work for e.g. C# as well.
Swift allows for multiple parameters with the same external name, so we account for that by tagging named arguments/parameters with their rank. However, the following corner-case is not handled:
Since the first trailing closure is not labeled, it gets matched (correctly) by position, but the second trailing closure is considered the first
aargument, so it is also matched against the first parameter instead of the second.This PR also changes the
inferInvocationArgumentTypeContextualDefaultinterface in order to handle contextual propagation into synthetic arguments (documented with a test case).