Skip to content

Rust: Model [T;n] -> [T] unsized coercions as implicit derefs - #22432

Merged
hvitved merged 2 commits into
github:mainfrom
hvitved:rust/array-slice-unsized-coercion
Sep 22, 2026
Merged

hvitved merged 2 commits into
github:mainfrom
hvitved:rust/array-slice-unsized-coercion

Conversation

@hvitved

@hvitved hvitved commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

This PR models array to slice unsized coercions as if they were implemented using Deref. The reason for doing it like this is that the type inference library (and data flow library) is already able to handle implicit calls to Deref::deref.

DCA looks good: a small improvement to number of resolved calls and, as a result, a few more alerts.

@hvitved
hvitved force-pushed the rust/array-slice-unsized-coercion branch from fe1b663 to 93e96e8 Compare August 26, 2026 13:51
@github-actions github-actions Bot added the Rust Pull requests that update Rust code label Aug 26, 2026
@hvitved
hvitved force-pushed the rust/array-slice-unsized-coercion branch from 93e96e8 to 1df2578 Compare August 27, 2026 08:53
@hvitved hvitved changed the title Rust: Model [T;n] -> [T] unsized coercion in type inference Rust: Model [T;n] -> [T] unsized coercions as implicit derefs Aug 28, 2026
@hvitved
hvitved force-pushed the rust/array-slice-unsized-coercion branch 2 times, most recently from 4fe48a8 to eaef972 Compare September 16, 2026 18:40
@hvitved
hvitved force-pushed the rust/array-slice-unsized-coercion branch from eaef972 to 1f8b466 Compare September 18, 2026 13:10
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Sep 21, 2026
@hvitved
hvitved marked this pull request as ready for review September 21, 2026 11:23
@hvitved
hvitved requested a review from a team as a code owner September 21, 2026 11:23
@hvitved
hvitved requested review from paldepind and a balanced review from Copilot September 21, 2026 11:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Mutable slice iteration still lacks an element-flow summary, leaving array iter_mut flows unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Models Rust array-to-slice coercion through implicit dereferencing, improving call resolution and data flow.

Changes:

  • Adds a pseudo Deref implementation for arrays.
  • Adds array/slice flow summaries.
  • Updates type-inference, data-flow, and security tests.
File Description
rust/​tools/​builtins/​types.rs Defines the pseudo array dereference.
rust/​ql/​lib/​codeql/​rust/​frameworks/​stdlib/​core.model.yml Adds dereference and slice iteration summaries.
rust/​ql/​test/​library-tests/​type-inference/​main.rs Tests newly resolved slice methods.
rust/​ql/​test/​library-tests/​type-inference/​type-inference.expected Updates generated inference results.
rust/​ql/​test/​library-tests/​dataflow/​local/​main.rs Adds expected array iteration flows.
rust/​ql/​test/​library-tests/​dataflow/​local/​inline-flow.expected Updates generated flow results.
rust/​ql/​test/​library-tests/​dataflow/​local/​DataFlowStep.expected Updates generated flow steps.
rust/​ql/​test/​query-tests/​security/​CWE-078/​main.rs Removes resolved false-positive annotations.
rust/​ql/​test/​query-tests/​security/​CWE-078/​CommandInjection.expected Updates generated security-query results.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread rust/ql/lib/codeql/rust/frameworks/stdlib/core.model.yml

@paldepind paldepind left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One question + Copilot has a comment that seems reasonable.

- ["<&mut as core::ops::deref::Deref>::deref", "Argument[self].Reference", "ReturnValue", "value", "manual"]
- ["<core::ops::deref::Deref>::deref", "Argument[self].Reference", "ReturnValue.Reference", "taint", "manual"]
# for the pseudo implementation in `builtins/types.rs`
- ["<[;] as core::ops::deref::Deref>::deref", "Argument[self].Reference", "ReturnValue.Reference", "value", "manual"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might not work/be worth it, but could we have let the body of the pseudo implementation be &self and have gotten this for free?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We do not generate CFGs for functions not in source, so that will not work.

paldepind
paldepind previously approved these changes Sep 21, 2026
@hvitved
hvitved merged commit 693d930 into github:main Sep 22, 2026
15 checks passed
@hvitved
hvitved deleted the rust/array-slice-unsized-coercion branch September 22, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants