-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
f7021ab
to
9e0304b
Compare
9e0304b
to
a3c26b4
Compare
There was a problem hiding this 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 variousResult
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 handlesOption
-based?
expressions, but the tests only coverResult
. Consider adding a test function that uses?
on anOption
to validate inference forOption<T>
.
mod try_expressions {
rust/ql/lib/codeql/rust/internal/TypeInference.qll:9
- The import
codeql.rust.frameworks.stdlib.Stdlib
is not used byinferTryExprType
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 tox: Result<Result<S1, S1>, S1>
, but the function signature returnsResult<S1, S2>
. Without convertingS1
toS2
(e.g. viamap_err
), this will fail to compile. Align the error type or add an explicit conversion.
fn try_chained() -> Result<S1, S2> {
There was a problem hiding this 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 | |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)?; |
DCA looks great; we gain more call edges without affecting performance.