Skip to content

Commit 8668473

Browse files
authored
Merge pull request #20683 from hvitved/rust/type-inference-arg-target-typed
Rust: Restrict type propagation into arguments
2 parents 4005a6e + 4c7f9c0 commit 8668473

File tree

14 files changed

+4611
-4605
lines changed

14 files changed

+4611
-4605
lines changed

rust/ql/lib/codeql/rust/internal/Type.qll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ newtype TType =
5151
TSliceType() or
5252
TNeverType() or
5353
TPtrType() or
54+
TUnknownType() or
5455
TTupleTypeParameter(int arity, int i) { exists(TTuple(arity)) and i in [0 .. arity - 1] } or
5556
TTypeParamTypeParameter(TypeParam t) or
5657
TAssociatedTypeTypeParameter(TypeAlias t) { any(TraitItemNode trait).getAnAssocItem() = t } or
@@ -371,6 +372,36 @@ class PtrType extends Type, TPtrType {
371372
override Location getLocation() { result instanceof EmptyLocation }
372373
}
373374

375+
/**
376+
* A special pseudo type used to indicate that the actual type may have to be
377+
* inferred by propagating type information back into call arguments.
378+
*
379+
* For example, in
380+
*
381+
* ```rust
382+
* let x = Default::default();
383+
* foo(x);
384+
* ```
385+
*
386+
* `Default::default()` is assigned this type, which allows us to infer the actual
387+
* type from the type of `foo`'s first parameter.
388+
*
389+
* Unknown types are not restricted to root types, for example in a call like
390+
* `Vec::new()` we assign this type at the type path corresponding to the type
391+
* parameter of `Vec`.
392+
*
393+
* Unknown types are used to restrict when type information is allowed to flow
394+
* into call arguments (including method call receivers), in order to avoid
395+
* combinatorial explosions.
396+
*/
397+
class UnknownType extends Type, TUnknownType {
398+
override TypeParameter getPositionalTypeParameter(int i) { none() }
399+
400+
override string toString() { result = "(context typed)" }
401+
402+
override Location getLocation() { result instanceof EmptyLocation }
403+
}
404+
374405
/** A type parameter. */
375406
abstract class TypeParameter extends Type {
376407
override TypeParameter getPositionalTypeParameter(int i) { none() }

0 commit comments

Comments
 (0)