Skip to content

Rust: Unify logic in MethodResolution; remove TypeQualifierIsInstantiationOfImplSelf logic#21366

Merged
hvitved merged 2 commits intogithub:mainfrom
hvitved:rust/type-inference-unify-method-resolution
Feb 26, 2026
Merged

Rust: Unify logic in MethodResolution; remove TypeQualifierIsInstantiationOfImplSelf logic#21366
hvitved merged 2 commits intogithub:mainfrom
hvitved:rust/type-inference-unify-method-resolution

Conversation

@hvitved
Copy link
Contributor

@hvitved hvitved commented Feb 25, 2026

This PR gets rid of the special TypeQualifierIsInstantiationOfImplSelf logic for resolving method calls using type qualifiers such as Foo::m(x). The solution is to introduce another kind of ArgumentPosition for representing type qualifiers, and then generalize existing logic that was hard-coded to self positions to also cover type qualifier positions, which allows us to handle Foo::m(x) and x.m() uniformly.

DCA is great; we gain quite a lot of new call edges without significant performance cost.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Feb 25, 2026
@hvitved hvitved force-pushed the rust/type-inference-unify-method-resolution branch from 93d8651 to de9b1ad Compare February 25, 2026 08:18
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Feb 25, 2026
@hvitved hvitved marked this pull request as ready for review February 25, 2026 10:33
@hvitved hvitved requested a review from a team as a code owner February 25, 2026 10:33
@hvitved hvitved requested review from Copilot and paldepind February 25, 2026 10:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors Rust type inference/method resolution to treat type-qualified calls (for example Foo::m(x)) uniformly with receiver-style calls (x.m()), removing the bespoke TypeQualifierIsInstantiationOfImplSelf path and introducing an explicit “type qualifier” argument position.

Changes:

  • Adds a new ArgumentPosition variant to represent type qualifiers and threads it through method-call candidate/receiver resolution.
  • Removes the special-case TypeQualifierIsInstantiationOfImplSelf logic and generalizes existing “self”-based matching to “self or type qualifier”.
  • Updates type inference expected outputs to reflect additional inferred types/call edges.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
rust/ql/test/library-tests/type-inference/type-inference.expected Updates expected results for type inference tests.
rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll Refactors method resolution to unify receiver and type-qualifier handling; removes special-case qualifier logic.
rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll Introduces FunctionPosition.isTypeQualifier / isSelfOrTypeQualifier and uses qualifier position for assoc function typing.
rust/ql/lib/codeql/rust/internal/typeinference/FunctionOverloading.qll Adjusts overload-resolution dependence logic to exclude both self and type qualifier positions.
rust/ql/lib/codeql/rust/elements/internal/InvocationExprImpl.qll Adds TTypeQualifierArgumentPosition and exposes ArgumentPosition.isTypeQualifier().

Comment on lines 89 to 91
// We always include the type qualifer position, even for non-methods, where it is used
// to match type qualifiers against the `impl` or trait type, such as in `Vec::new`.
(exists(pos.getTypeMention(f)) or pos.isSelf())
(exists(pos.getTypeMention(f)) or pos.isTypeQualifier())
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: "type qualifer" should be "type qualifier".

Copilot uses AI. Check for mistakes.
/**
* Holds if this call position is a type qualifier, that is, not an actual
* argument, but rather an annotation that is needed to resolve the call target,
* just like actual arguments may be need to resolve the call target.
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar in comment: "may be need" should be "may be needed".

Suggested change
* just like actual arguments may be need to resolve the call target.
* just like actual arguments may be needed to resolve the call target.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice simplification!

/**
* Holds if this call position is a type qualifier, that is, not an actual
* argument, but rather an annotation that is needed to resolve the call target,
* just like actual arguments may be need to resolve the call target.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding an example would be useful here?

    Vec<i64>::new()
 // ^^^^^^^^ type qualifier

@hvitved hvitved requested a review from paldepind February 26, 2026 13:24
@hvitved hvitved merged commit 4280d35 into github:main Feb 26, 2026
20 of 22 checks passed
@hvitved hvitved deleted the rust/type-inference-unify-method-resolution branch February 26, 2026 13:38
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants