Skip to content

Rust: Fix type inference for library parameters #19658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jun 3, 2025

The old logic relied on parameters having a pattern, which is not the case for parameters extracted from library code.

The updated test output reveals that we do not handle operator calls correctly. For example, i32 implements both Add and Add<Rhs = &i32>, and currently we resolve 1 + 2 to both add methods, which means that 2 will have reverse-propagated type &i32 in addition to i32.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 3, 2025
@hvitved hvitved force-pushed the rust/type-inference-library-param-fix branch from bec542f to c4d46f2 Compare June 3, 2025 19:38
@hvitved hvitved force-pushed the rust/type-inference-library-param-fix branch from c4d46f2 to 07fe1bf Compare June 4, 2025 06:58
@hvitved hvitved marked this pull request as ready for review June 4, 2025 08:52
@Copilot Copilot AI review requested due to automatic review settings June 4, 2025 08:52
@hvitved hvitved requested a review from a team as a code owner June 4, 2025 08:52
@hvitved hvitved requested a review from paldepind June 4, 2025 08:52
Copy link
Contributor

@Copilot 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 fixes type inference for library parameters by switching from pattern-based inference to using explicit type representations, updates related test expectations for Pin methods, and renames a debug helper to match its new signature.

  • Replace inferAnnotatedType on parameter patterns with TypeMention.resolveTypeAt for library parameters
  • Adjust modeled dataflow tests and expected outputs to account for into_inner_unchecked
  • Rename debugResolveMethodCallExpr to accept MethodCall instead of MethodCallExpr

Reviewed Changes

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

File Description
rust/ql/test/library-tests/dataflow/modeled/main.rs Reorder imports and update test comment for Pin::into_inner_unchecked
rust/ql/test/library-tests/dataflow/modeled/inline-flow.expected Update expected model entries and edge labels
rust/ql/lib/codeql/rust/internal/TypeInference.qll Use TypeMention.resolveTypeAt for parameter types and rename debug function signature
Comments suppressed due to low confidence (1)

rust/ql/test/library-tests/dataflow/modeled/main.rs:98

  • [nitpick] These two imports from std::pin could be combined into a single statement: use std::pin::{pin, Pin}; for clarity and conciseness.
use std::pin::pin;

@@ -569,7 +569,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
exists(Param p, int i, boolean inMethod |
paramPos(this.getParamList(), p, i, inMethod) and
dpos = TPositionalDeclarationPosition(i, inMethod) and
result = inferAnnotatedType(p.getPat(), path)
result = p.getTypeRepr().(TypeMention).resolveTypeAt(path)
Copy link
Preview

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

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

The unchecked cast to TypeMention may fail if a parameter's TypeRepr isn't a TypeMention. Consider adding a guard (e.g., instanceof TypeMention) or using a safe resolution method to avoid runtime errors.

Copilot uses AI. Check for mistakes.

Function debugResolveMethodCallExpr(MethodCallExpr mce) {
mce = getRelevantLocatable() and
result = resolveMethodCallTarget(mce)
Function debugResolveMethodCallExpr(MethodCall mc) {
Copy link
Preview

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

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

[nitpick] The debug function signature and parameter name changed from MethodCallExpr/mce to MethodCall/mc. Consider updating any related comments or docs to prevent confusion when maintaining this helper.

Suggested change
Function debugResolveMethodCallExpr(MethodCall mc) {
Function debugResolveMethodCall(MethodCall mc) {

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant