Skip to content

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

Merged
merged 2 commits into from
Jun 13, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jun 11, 2025

Eventually we will want to generally support the Deref trait in type inference, but for now hard-code knowledge about String -> str conversion.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 11, 2025
@hvitved hvitved force-pushed the rust/type-inference-string-str-deref branch 3 times, most recently from 63959d3 to 1ef5112 Compare June 12, 2025 11:03
@hvitved hvitved marked this pull request as ready for review June 12, 2025 12:06
@Copilot Copilot AI review requested due to automatic review settings June 12, 2025 12:06
@hvitved hvitved requested a review from a team as a code owner June 12, 2025 12:06
@hvitved hvitved requested a review from paldepind June 12, 2025 12:06
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

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-case String deref to str when resolving method calls.
  • Introduces StringStruct in Stdlib.qll and updates expected outputs across multiple test suites to include as_str targets.
  • Adds a demonstration test in main.rs for String -> str conversion via parse.

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 Stringstr 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 StringStructBuiltins::Str exclusion is needed, to aid future maintainers.
not (

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.

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

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)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is deliberate.

@hvitved hvitved force-pushed the rust/type-inference-string-str-deref branch from 1ef5112 to d59bce5 Compare June 12, 2025 18:52
@hvitved hvitved force-pushed the rust/type-inference-string-str-deref branch from d59bce5 to 538538d Compare June 12, 2025 19:48
@hvitved hvitved closed this Jun 13, 2025
@hvitved hvitved reopened this Jun 13, 2025
redsun82
redsun82 previously approved these changes Jun 13, 2025
Copy link
Contributor

@redsun82 redsun82 left a comment

Choose a reason for hiding this comment

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

LGTM!

@hvitved hvitved force-pushed the rust/type-inference-string-str-deref branch from c3f6550 to 66c0ff6 Compare June 13, 2025 09:32
@hvitved hvitved merged commit ad64e04 into github:main Jun 13, 2025
17 checks passed
@hvitved hvitved deleted the rust/type-inference-string-str-deref branch June 13, 2025 11:09
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.

4 participants