@@ -7,7 +7,11 @@ private import codeql.rust.Concepts
77private import codeql.rust.controlflow.ControlFlowGraph as Cfg
88private import codeql.rust.controlflow.CfgNodes as CfgNodes
99private import codeql.rust.dataflow.DataFlow
10+ private import codeql.rust.dataflow.FlowSummary
1011private import codeql.rust.internal.PathResolution
12+ private import codeql.rust.internal.Type
13+ private import codeql.rust.internal.TypeInference
14+ private import codeql.rust.internal.TypeMention
1115
1216/**
1317 * A call to the `starts_with` method on a `Path`.
@@ -213,3 +217,111 @@ class StringStruct extends Struct {
213217 pragma [ nomagic]
214218 StringStruct ( ) { this .getCanonicalPath ( ) = "alloc::string::String" }
215219}
220+
221+ /**
222+ * The [`Deref` trait][1].
223+ *
224+ * [1]: https://doc.rust-lang.org/core/ops/trait.Deref.html
225+ */
226+ class DerefTrait extends Trait {
227+ pragma [ nomagic]
228+ DerefTrait ( ) { this .getCanonicalPath ( ) = "core::ops::deref::Deref" }
229+
230+ /** Gets the `Target` associated type. */
231+ pragma [ nomagic]
232+ TypeAlias getTargetType ( ) {
233+ result = this .getAssocItemList ( ) .getAnAssocItem ( ) and
234+ result .getName ( ) .getText ( ) = "Target"
235+ }
236+ }
237+
238+ /**
239+ * One of the two special
240+ *
241+ * ```rust
242+ * impl<T: ?Sized> const Deref for &T
243+ * impl<T: ?Sized> const Deref for &mut T
244+ * ```
245+ *
246+ * implementations.
247+ */
248+ private class CoreDerefImpl extends ImplItemNode {
249+ pragma [ nomagic]
250+ CoreDerefImpl ( ) {
251+ this .resolveTraitTy ( ) instanceof DerefTrait and
252+ this .( Impl )
253+ .getSelfTy ( )
254+ .( TypeMention )
255+ .resolveTypeAt ( TypePath:: singleton ( TRefTypeParameter ( ) ) )
256+ .( TypeParamTypeParameter )
257+ .getTypeParam ( ) = this .getTypeParam ( _)
258+ }
259+
260+ Function getDeref ( ) { result = this .getASuccessor ( "deref" ) }
261+ }
262+
263+ // TODO: Use MaD when `&(mut) T` is assigned an appropriate canonical path
264+ private class CoreDerefSummarizedCallable extends SummarizedCallable:: Range {
265+ CoreDerefSummarizedCallable ( ) { this = any ( CoreDerefImpl i ) .getDeref ( ) }
266+
267+ override predicate propagatesFlow ( string input , string output , boolean preservesValue ) {
268+ input = "Argument[self].Reference" and
269+ output = "ReturnValue" and
270+ preservesValue = true
271+ }
272+ }
273+
274+ /**
275+ * The [`Index` trait][1].
276+ *
277+ * [1]: https://doc.rust-lang.org/std/ops/trait.Index.html
278+ */
279+ class IndexTrait extends Trait {
280+ pragma [ nomagic]
281+ IndexTrait ( ) { this .getCanonicalPath ( ) = "core::ops::index::Index" }
282+
283+ /** Gets the `Output` associated type. */
284+ pragma [ nomagic]
285+ TypeAlias getOutputType ( ) {
286+ result = this .getAssocItemList ( ) .getAnAssocItem ( ) and
287+ result .getName ( ) .getText ( ) = "Output"
288+ }
289+ }
290+
291+ /**
292+ * One of the two special
293+ *
294+ * ```rust
295+ * impl<T, I, const N: usize> Index<I> for [T; N]
296+ * impl<T, I> ops::Index<I> for [T]
297+ * ```
298+ *
299+ * implementations.
300+ */
301+ private class CoreIndexImpl extends ImplItemNode {
302+ pragma [ nomagic]
303+ CoreIndexImpl ( ) {
304+ this .resolveTraitTy ( ) instanceof IndexTrait and
305+ exists ( TypeParameter tp | tp = TArrayTypeParameter ( ) or tp = TSliceTypeParameter ( ) |
306+ this .( Impl )
307+ .getSelfTy ( )
308+ .( TypeMention )
309+ .resolveTypeAt ( TypePath:: singleton ( tp ) )
310+ .( TypeParamTypeParameter )
311+ .getTypeParam ( ) = this .getTypeParam ( _)
312+ )
313+ }
314+
315+ Function getIndex ( ) { result = this .getASuccessor ( "index" ) }
316+ }
317+
318+ // TODO: Use MaD when `[T(; N)]` is assigned an appropriate canonical path
319+ private class CoreIndexSummarizedCallable extends SummarizedCallable:: Range {
320+ CoreIndexSummarizedCallable ( ) { this = any ( CoreIndexImpl i ) .getIndex ( ) }
321+
322+ override predicate propagatesFlow ( string input , string output , boolean preservesValue ) {
323+ input = "Argument[self].Reference.Element" and
324+ output = "ReturnValue" and
325+ preservesValue = true
326+ }
327+ }
0 commit comments