Rust: Model [T;n] -> [T] unsized coercions as implicit derefs - #22432
Conversation
fe1b663 to
93e96e8
Compare
93e96e8 to
1df2578
Compare
[T;n] -> [T] unsized coercion in type inference[T;n] -> [T] unsized coercions as implicit derefs
4fe48a8 to
eaef972
Compare
eaef972 to
1f8b466
Compare
There was a problem hiding this comment.
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
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
Derefimplementation 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.
paldepind
left a comment
There was a problem hiding this comment.
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"] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
We do not generate CFGs for functions not in source, so that will not work.

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 toDeref::deref.DCA looks good: a small improvement to number of resolved calls and, as a result, a few more alerts.