Skip to content

Rust: Type inference for ? expressions #19367

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

Merged
merged 3 commits into from
May 1, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Apr 24, 2025

DCA looks great; we gain more call edges without affecting performance.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Apr 24, 2025
@hvitved hvitved force-pushed the rust/type-inference-try-expr branch 3 times, most recently from f7021ab to 9e0304b Compare April 30, 2025 13:18
@hvitved hvitved force-pushed the rust/type-inference-try-expr branch from 9e0304b to a3c26b4 Compare April 30, 2025 18:35
@hvitved hvitved marked this pull request as ready for review May 1, 2025 06:38
@Copilot Copilot AI review requested due to automatic review settings May 1, 2025 06:38
@hvitved hvitved requested a review from a team as a code owner May 1, 2025 06:38
@hvitved hvitved requested a review from paldepind May 1, 2025 06:39
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 adds support for inferring types of Rust ? (try) expressions and includes test coverage for Result-based ? patterns.

  • Introduce inferTryExprType in the inference engine and include it in cached inference.
  • Add a new try_expressions test module exercising ? on various Result scenarios.
  • Update expected outputs for the new tests and extend the Postgres path‐resolution consistency expectations.

Reviewed Changes

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

File Description
rust/ql/lib/codeql/rust/internal/TypeInference.qll Added inferTryExprType, an import, and integrated it into cached inference
rust/ql/test/library-tests/type-inference/main.rs Added try_expressions module with ?-operator tests for Result
rust/ql/test/library-tests/type-inference/type-inference.expected Updated expected outputs to include ?-expression inference entries
rust/ql/test/library-tests/frameworks/postgres/CONSISTENCY/PathResolutionConsistency.expected Added duplicate method-call entries for consistency verification
Comments suppressed due to low confidence (3)

rust/ql/test/library-tests/type-inference/main.rs:922

  • The new inferTryExprType also handles Option-based ? expressions, but the tests only cover Result. Consider adding a test function that uses ? on an Option to validate inference for Option<T>.
mod try_expressions {

rust/ql/lib/codeql/rust/internal/TypeInference.qll:9

  • The import codeql.rust.frameworks.stdlib.Stdlib is not used by inferTryExprType or elsewhere in this file. Consider removing it to avoid a redundant dependency.
+private import codeql.rust.frameworks.stdlib.Stdlib

rust/ql/test/library-tests/type-inference/main.rs:945

  • In try_chained, the first ? is applied to x: Result<Result<S1, S1>, S1>, but the function signature returns Result<S1, S2>. Without converting S1 to S2 (e.g. via map_err), this will fail to compile. Align the error type or add an explicit conversion.
fn try_chained() -> Result<S1, S2> {

Copy link
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.

Code changes LGTM, and DCA LGTM as well.

| main.rs:30:5:30:39 | conn.query_opt(...) | file://:0:0:0:0 | fn query_opt |
| main.rs:30:5:30:39 | conn.query_opt(...) | file://:0:0:0:0 | fn query_opt |
| main.rs:35:17:35:67 | conn.query(...) | file://:0:0:0:0 | fn query |
| main.rs:35:17:35:67 | conn.query(...) | file://:0:0:0:0 | fn query |
Copy link
Contributor

Choose a reason for hiding this comment

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

How much of a concern are the new inconsistencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These consistencies are not really new, they are just surfaced because we can now infer a type for conn at

let mut conn = postgres::Client::connect("host=localhost user=postgres", postgres::NoTls)?;
.

@hvitved hvitved merged commit 1770f56 into github:main May 1, 2025
17 checks passed
@hvitved hvitved deleted the rust/type-inference-try-expr branch May 1, 2025 08:27
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.

2 participants