Skip to content

Type inference: Add support for named arguments - #22973

Merged
hvitved merged 5 commits into
github:mainfrom
hvitved:type-inference-named-args
Nov 20, 2026
Merged

hvitved merged 5 commits into
github:mainfrom
hvitved:type-inference-named-args

Conversation

@hvitved

@hvitved hvitved commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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:

func f(a first: (Int) -> Void, a second: (String) -> Void) {}

f { x in
} a: { y in
}

Since the first trailing closure is not labeled, it gets matched (correctly) by position, but the second trailing closure is considered the first a argument, so it is also matched against the first parameter instead of the second.

This PR also changes the inferInvocationArgumentTypeContextualDefault interface in order to handle contextual propagation into synthetic arguments (documented with a test case).

@github-actions github-actions Bot added Rust Pull requests that update Rust code Unified labels Nov 2, 2026
@hvitved
hvitved force-pushed the type-inference-named-args branch from baaa8f7 to e6d8f96 Compare November 11, 2026 10:15
@hvitved
hvitved requested a max review from Copilot November 11, 2026 10:18

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

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

@hvitved
hvitved force-pushed the type-inference-named-args branch from e6d8f96 to d6d329e Compare November 11, 2026 21:01

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

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

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

Trailing-closure matching regressions reuse already-consumed parameters for repeated labels and mishandle _ labels, producing incorrect or missing inferred types.

2 open issues
3 resolved since last review

Review effort: Max

@hvitved
hvitved force-pushed the type-inference-named-args branch 6 times, most recently from 48791ee to dc74749 Compare November 16, 2026 13:36
@hvitved
hvitved force-pushed the type-inference-named-args branch from dc74749 to 6dff9e0 Compare November 16, 2026 13:44
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Nov 16, 2026
@hvitved
hvitved marked this pull request as ready for review November 16, 2026 20:11
@hvitved
hvitved requested a review from a team as a code owner November 16, 2026 20:11
@hvitved
hvitved requested review from a team as code owners November 16, 2026 20:11
@paldepind

Copy link
Copy Markdown
Contributor

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?

@hvitved

hvitved commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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?

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?

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?

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.

@paldepind

Copy link
Copy Markdown
Contributor

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?

I was thinking that it would fix the edge case you mentioned and be a simple change where we change getExternalName to not provide labels for parameters that are best distinguished by their position (which includes those in the f function in our example since no variadic or default parameters precede them).

@asgerf

asgerf commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

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 foo cannot be associated with the correct numeric position prior to type-based overload resolution, which is not available at the time we need to do this for type inference:

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

@hvitved

hvitved commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

I was thinking that it would fix the edge case you mentioned and be a simple change where we change getExternalName to not provide labels for parameters that are best distinguished by their position (which includes those in the f function in our example since no variadic or default parameters precede them).

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).

@paldepind paldepind 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 good to me.

@hvitved
hvitved requested a review from paldepind November 19, 2026 15:41
@hvitved
hvitved merged commit 1e29be7 into github:main Nov 20, 2026
112 checks passed
@hvitved
hvitved deleted the type-inference-named-args branch November 20, 2026 08:22
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 Rust Pull requests that update Rust code Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants