-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Model String
-> str
implicit conversion in type inference
#19737
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
Rust: Model String
-> str
implicit conversion in type inference
#19737
Conversation
63959d3
to
1ef5112
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
Adds hard-coded support for implicit String -> str
conversion in CodeQL’s Rust type inference, updates related tests, and defines the StringStruct
.
- Extends
TypeInference.qll
to special-caseString
deref tostr
when resolving method calls. - Introduces
StringStruct
inStdlib.qll
and updates expected outputs across multiple test suites to includeas_str
targets. - Adds a demonstration test in
main.rs
forString
->str
conversion viaparse
.
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
rust/ql/lib/codeql/rust/internal/TypeInference.qll | Adds special-case logic for String → str conversion in calls |
rust/ql/lib/codeql/rust/frameworks/stdlib/Stdlib.qll | Defines StringStruct mapping to alloc::string::String |
rust/ql/test/library-tests/type-inference/main.rs | Adds example test case showing implicit conversion with parse |
rust/ql/test/**/PathResolutionConsistency.expected (many) | Updates expected outputs to include new as_str and conversion targets |
Comments suppressed due to low confidence (1)
rust/ql/lib/codeql/rust/internal/TypeInference.qll:845
- [nitpick] Consider adding a brief comment above this condition to explain why the
StringStruct
→Builtins::Str
exclusion is needed, to aid future maintainers.
not (
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.
We will also want the same feature in data flow access paths at some point, but I guess the logic isn't shared between them.
// See also https://doc.rust-lang.org/reference/expressions/method-call-expr.html#r-expr.method.autoref-deref | ||
path.isEmpty() and | ||
t0.(StructType).asItemNode() instanceof StringStruct and | ||
result.(StructType).asItemNode() instanceof Builtins::Str |
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.
Is it OK that we also still get the result from result = t0
as well in this situation, i.e. that we get two results (of which only one applies depending on context)?
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.
Yes, that is deliberate.
1ef5112
to
d59bce5
Compare
d59bce5
to
538538d
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.
LGTM!
c3f6550
to
66c0ff6
Compare
Eventually we will want to generally support the
Deref
trait in type inference, but for now hard-code knowledge aboutString -> str
conversion.