Skip to content

Rust: Refine implSiblings#21703

Open
hvitved wants to merge 1 commit intogithub:mainfrom
hvitved:rust/type-inference-sibling
Open

Rust: Refine implSiblings#21703
hvitved wants to merge 1 commit intogithub:mainfrom
hvitved:rust/type-inference-sibling

Conversation

@hvitved
Copy link
Copy Markdown
Contributor

@hvitved hvitved commented Apr 13, 2026

#21206 follow-up.

Consider two implementations of the same trait to be siblings when the type being implemented by one is an instantiation of the type being implemented by the other.

For example, in

trait MyTrait<T> { ... }
impl MyTrait<i64> for i64 { ... }    // I1
impl MyTrait<u64> for i64 { ... }    // I2

impl MyTrait<i64> for S<i64> { ... } // I3
impl MyTrait<u64> for S<u64> { ... } // I4
impl MyTrait<bool> for S<T> { ... }  // I5

the pairs (I1, I2), (I3, I5), and (I4, I5) are siblings, but not (I3, I4).

Before this PR, only (I1, I2) were considered siblings.

DCA is fine; while we do loose some call targets, we also fix inconsistencies, most notably we reduce Nodes With Type At Length Limit.

@github-actions github-actions Bot added the Rust Pull requests that update Rust code label Apr 13, 2026
@hvitved hvitved force-pushed the rust/type-inference-sibling branch 4 times, most recently from 87447fb to d6ff5b1 Compare April 22, 2026 08:13
Consider two implementations of the same trait to be siblings when the
type being implemented by one is an instantiation of the type being
implemented by the other.
@hvitved hvitved force-pushed the rust/type-inference-sibling branch from d6ff5b1 to e60275c Compare April 22, 2026 11:34
@hvitved hvitved changed the title Rust: Refine implHasAmbiguousSiblingAt Rust: Refine implSiblings Apr 22, 2026
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Apr 23, 2026
@hvitved hvitved marked this pull request as ready for review April 23, 2026 13:19
@hvitved hvitved requested a review from a team as a code owner April 23, 2026 13:19
Copilot AI review requested due to automatic review settings April 23, 2026 13:19
@hvitved hvitved requested a review from a team as a code owner April 23, 2026 13:19
@hvitved hvitved requested a review from geoffw0 April 23, 2026 13:19
Copy link
Copy Markdown
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

Refines Rust trait-impl sibling detection in the type inference / overload resolution logic so that impls are treated as siblings when one Self type is an instantiation of the other, reducing inconsistent resolution outcomes.

Changes:

  • Extend shared type inference infrastructure with an isPseudoType hook to ignore pseudo types during non-instantiation checks.
  • Update Rust’s implSiblings logic to use instantiation checks rather than syntactic equality of Self type mentions.
  • Adjust Rust type-inference regression tests and expected output to match the refined sibling behavior.
Show a summary per file
File Description
shared/typeinference/codeql/typeinference/internal/TypeInference.qll Adds isPseudoType to the shared input signature and uses it to ignore pseudo types in isNotInstantiationOf.
rust/ql/lib/codeql/rust/internal/typeinference/TypeInference.qll Implements isPseudoType for Rust (treating UnknownType/NeverType as pseudo).
rust/ql/lib/codeql/rust/internal/typeinference/FunctionOverloading.qll Refines implSiblings to consider instantiation relationships between implemented Self types; updates related logic/docs.
rust/ql/test/library-tests/type-inference/regressions.rs Updates regression annotation to reflect the corrected inferred type after sibling refinement.
rust/ql/test/library-tests/type-inference/type-inference.expected Updates expected test output to match new inference/resolution results.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 1

Comment on lines 184 to 188
* // ^ `path` = "T"
* }
* impl MyAdd<i64> for i64 {
* fn method(&self, value: Foo<i64>) -> Self { ... }
* // ^^^ `type` = i64
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

In this documentation example, the trait is named MyTrait, but the subsequent impl line uses MyAdd<i64> for i64. This looks like a copy/paste typo and makes the example harder to follow; consider using the same trait name consistently throughout the snippet.

See below for a potential fix:

   * impl MyTrait<i64> for i64 {

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

Results LGTM. Where should I start to understand the implementation?

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.

4 participants