Skip to content

Conversation

@hvitved
Copy link
Contributor

@hvitved hvitved commented Dec 10, 2025

Consider the following example

trait MyTrait {
    fn f(self) -> Self;
}

impl MyTrait for i32 {
    // i32f
    fn f(self) -> Self {
        self
    }
}

impl MyTrait for usize {
    // usizef
    fn f(self) -> Self {
        self
    }
}

impl<T> MyTrait for &T {
    // Reff
    fn f(self) -> Self {
        self
    }
}

pub fn test() -> usize {
    let mut x = 0;
    x = x.f();
    x
}

Inside test, the variable x is assigned both types i32 (the default type for integer literals) and usize (because of the return type), because we do not currently support context-based typing of literals.

This means that the call x.f can resolve to both i32f and usizef. However, because of how we construct candidate receiver types, we also conclude that &usize and &i32 need to be considered, which means we also resolve x.f to Reff, and then because of the self-assignment, this will lead to an explosion in inferred types for x.

The solution, in the case above, is to tweak how we construct candidate receiver types, by only checking non-compatibility for non-root types.

Note that this does not resolve the issue with x having both types i32 and usize, and hence x.f having two targets.

DCA is rather uneventful.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Dec 10, 2025
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Dec 11, 2025
@hvitved hvitved marked this pull request as ready for review December 11, 2025 09:30
@hvitved hvitved requested review from a team as code owners December 11, 2025 09:30
@hvitved hvitved requested review from Copilot and paldepind December 11, 2025 09:30
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 strengthens the isNotInstantiationOf predicate to prevent type explosion when resolving method calls involving integer literals with ambiguous types. The fix adds a TypePath parameter to track where concrete types differ and filters incompatibilities to only consider paths within the root type's type parameters, preventing spurious reference type candidates from being added during method resolution.

Key changes:

  • Modified isNotInstantiationOf signature to include a TypePath parameter indicating where types differ
  • Added filtering logic to only consider incompatibilities within root type parameters using path.isCons(root.getATypeParameter(), _)
  • Updated all call sites to pass the additional path parameter and check the path constraint

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
shared/typeinference/codeql/typeinference/internal/TypeInference.qll Core logic change: adds TypePath parameter to isNotInstantiationOf predicate and implements wrapper that filters paths to root type parameters
rust/ql/lib/codeql/rust/internal/typeinference/FunctionType.qll Updates argIsNotInstantiationOf signature to accept and forward the new TypePath parameter
rust/ql/lib/codeql/rust/internal/TypeInference.qll Updates all call sites to use new signature with path filtering based on root type
rust/ql/test/library-tests/type-inference/main.rs Adds test case demonstrating the literal overlap scenario with trait implementations
rust/ql/test/library-tests/type-inference/type-inference.expected Updates expected test output with new type inferences for the test case
rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected Documents expected multiple targets for the new test case

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

LGTM!

@hvitved hvitved merged commit 3ab0ade into github:main Dec 11, 2025
51 checks passed
@hvitved hvitved deleted the rust/type-inference-fix-blowup branch December 11, 2025 11:04
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.

2 participants