Skip to content

Rust: Strengthen isNotInstantiationOf uses - #21010

Merged
hvitved merged 2 commits into
github:mainfrom
hvitved:rust/type-inference-fix-blowup
Dec 11, 2025
Merged

Rust: Strengthen isNotInstantiationOf uses#21010
hvitved merged 2 commits into
github:mainfrom
hvitved:rust/type-inference-fix-blowup

Conversation

@hvitved

@hvitvedhvitved commented Dec 10, 2025

Copy link
Copy Markdown
Contributor

Consider the following example

traitMyTrait{fnf(self) -> Self;}implMyTraitfori32{// i32ffnf(self) -> Self{self}}implMyTraitforusize{// usizeffnf(self) -> Self{self}}impl<T>MyTraitfor&T{// Refffnf(self) -> Self{self}}pubfntest() -> usize{letmut 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-actionsgithub-actionsBot added the Rust Pull requests that update Rust code label Dec 10, 2025
@hvitvedhvitved 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 ownersDecember 11, 2025 09:30

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

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
FileDescription
shared/typeinference/codeql/typeinference/internal/TypeInference.qllCore 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.qllUpdates argIsNotInstantiationOf signature to accept and forward the new TypePath parameter
rust/ql/lib/codeql/rust/internal/TypeInference.qllUpdates all call sites to use new signature with path filtering based on root type
rust/ql/test/library-tests/type-inference/main.rsAdds test case demonstrating the literal overlap scenario with trait implementations
rust/ql/test/library-tests/type-inference/type-inference.expectedUpdates expected test output with new type inferences for the test case
rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expectedDocuments expected multiple targets for the new test case

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

@paldepindpaldepind 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.

LGTM!

@hvitved
hvitved merged commit 3ab0ade into github:mainDec 11, 2025
51 checks passed
@hvitved
hvitved deleted the rust/type-inference-fix-blowup branch December 11, 2025 11:04
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-requiredThis PR does not need a change noteRustPull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@hvitved@paldepind