Skip to content

Commit 0cf60c4

Browse files
committed
Rust: Address comments on documentation
1 parent 06cfa9a commit 0cf60c4

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

rust/ql/lib/codeql/rust/elements/internal/MethodCallExprImpl.qll

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ module Impl {
5656
}
5757

5858
override Function getStaticTarget() {
59+
// Functions in source code also gets extracted as library code, due to
60+
// this duplication we prioritize functions from source code.
5961
result = this.getStaticTargetFrom(true)
6062
or
6163
not exists(this.getStaticTargetFrom(true)) and

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

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ private module Input1 implements InputSig1<Location> {
1919

2020
class TypeParameter = T::TypeParameter;
2121

22+
/**
23+
* A type abstraction. I.e., a place in the program where type variables are
24+
* introduced.
25+
*
26+
* Example:
27+
* ```rust
28+
* impl<A, B> Foo<A, B> { }
29+
* // ^^^^^^ a type abstraction
30+
* ```
31+
*/
2232
class TypeAbstraction = T::TypeAbstraction;
2333

2434
private newtype TTypeArgumentPosition =
@@ -118,6 +128,13 @@ private module Input2 implements InputSig2 {
118128
result = tp.(SelfTypeParameter).getTrait()
119129
}
120130

131+
/**
132+
* Use the constraint mechanism in the shared type inference library to
133+
* support traits. In Rust `constraint` is always a trait.
134+
*
135+
* See the documentation of `conditionSatisfiesConstraint` in the shared type
136+
* inference module for more information.
137+
*/
121138
predicate conditionSatisfiesConstraint(
122139
TypeAbstraction abs, TypeMention condition, TypeMention constraint
123140
) {

shared/typeinference/codeql/typeinference/internal/TypeInference.qll

+33-13
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,25 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
327327
* // ^^^^^^^^^^^^^ `constraint`
328328
* ```
329329
*
330-
* Note that the type parameters in `abs` significantly change the meaning
331-
* of type parameters that occur in `condition`. For instance, in the Rust
332-
* example
330+
* To see how `abs` change the meaning of the type parameters that occur in
331+
* `condition`, consider the following examples in Rust:
333332
* ```rust
334-
* fn foo<T: Trait>() { }
333+
* impl<T> Trait for T { }
334+
* // ^^^ `abs` ^ `condition`
335+
* // ^^^^^ `constraint`
335336
* ```
336-
* we have that the type parameter `T` satisfies the constraint `Trait`. But,
337-
* only that specific `T` satisfy the constraint. Hence we would not have
338-
* `T` in `abs`. On the other hand, in the Rust example
337+
* Here the meaning is "for all type parameters `T` it is the case that `T`
338+
* implements `Trait`". On the other hand, in
339339
* ```rust
340-
* impl<T> Trait for T { }
340+
* fn foo<T: Trait>() { }
341+
* // ^ `condition`
342+
* // ^^^^^ `constraint`
341343
* ```
342-
* the constraint `Trait` is in fact satisfied for all types, and we would
343-
* have `T` in `abs` to make it free in the condition.
344+
* the meaning is "`T` implements `Trait`" where the constraint is only
345+
* valid for the specific `T`. Note that `condition` and `condition` are
346+
* identical in the two examples. To encode the difference, `abs` in the
347+
* first example should contain `T` whereas in the seconds example `abs`
348+
* should be empty.
344349
*/
345350
predicate conditionSatisfiesConstraint(
346351
TypeAbstraction abs, TypeMention condition, TypeMention constraint
@@ -359,9 +364,24 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
359364

360365
signature module IsInstantiationOfInputSig<TypeTreeSig App> {
361366
/**
362-
* Holds if `abs` is a type abstraction under which `tm` occurs and if
363-
* `app` is potentially the result of applying the abstraction to type
364-
* some type argument.
367+
* Holds if `abs` is a type abstraction, `tm` occurs under `abs`, and
368+
* `app` is potentially an application/instantiation of `abs`.
369+
*
370+
* For example:
371+
* ```rust
372+
* impl<A> Foo<A, A> {
373+
* // ^^^ `abs`
374+
* // ^^^^^^^^^ `tm`
375+
* fn bar(self) { ... }
376+
* }
377+
* // ...
378+
* foo.bar();
379+
* // ^^^ `app`
380+
* ```
381+
* Here `abs` introduces the type parameter `A` and `tm` occurs under
382+
* `abs` (i.e., `A` is bound in `tm` by `abs`). On the last line,
383+
* accessing the `bar` method of `foo` potentially instantiates the `impl`
384+
* block with a type argument for `A`.
365385
*/
366386
predicate potentialInstantiationOf(App app, TypeAbstraction abs, TypeMention tm);
367387

0 commit comments

Comments
 (0)