Skip to content

Commit ce8cffc

Browse files
committed
Rust: Address comments
1 parent 9022f99 commit ce8cffc

File tree

4 files changed

+3704
-3701
lines changed

4 files changed

+3704
-3701
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ class NonAliasPathTypeMention extends PathTypeMention {
143143
)
144144
}
145145

146-
private TypeMention getPositionalTypeArgument0(int i) {
146+
/**
147+
* Gets the positional type argument at index `i` that occurs in this path, if
148+
* any.
149+
*/
150+
private TypeMention getPathPositionalTypeArgument(int i) {
147151
result = this.getSegment().getGenericArgList().getTypeArg(i)
148152
or
149153
// `Option::<i32>::Some` is valid in addition to `Option::Some::<i32>`
@@ -155,7 +159,7 @@ class NonAliasPathTypeMention extends PathTypeMention {
155159
* Gets the type mention that instantiates the implicit `Self` type parameter
156160
* for this path, if it occurs in the position of a trait bound.
157161
*/
158-
private TypeMention getSelfTypeParameter() {
162+
private TypeMention getSelfTraitBoundArg() {
159163
exists(ImplItemNode impl | this = impl.getTraitPath() and result = impl.(Impl).getSelfTy())
160164
or
161165
exists(Trait subTrait |
@@ -166,16 +170,10 @@ class NonAliasPathTypeMention extends PathTypeMention {
166170
exists(TypeParamItemNode tp | this = tp.getABoundPath() and result = tp)
167171
}
168172

169-
private Type getPositionalTypeArgument(int i, TypePath path) {
170-
result = this.getPositionalTypeArgument0(i).resolveTypeAt(path)
171-
or
172-
result = this.getDefaultPositionalTypeArgument(i, path)
173-
}
174-
175173
private Type getDefaultPositionalTypeArgument(int i, TypePath path) {
176174
// If a type argument is not given in the path, then we use the default for
177175
// the type parameter if one exists for the type.
178-
not exists(this.getPositionalTypeArgument0(i)) and
176+
not exists(this.getPathPositionalTypeArgument(i)) and
179177
// Defaults only apply to type mentions in type annotations
180178
this = any(PathTypeRepr ptp).getPath().getQualifier*() and
181179
exists(Type ty, TypePath prefix |
@@ -187,11 +185,17 @@ class NonAliasPathTypeMention extends PathTypeMention {
187185
// be substituted for the type that implements the trait.
188186
exists(TypePath suffix |
189187
path = prefix.append(suffix) and
190-
result = this.getSelfTypeParameter().resolveTypeAt(suffix)
188+
result = this.getSelfTraitBoundArg().resolveTypeAt(suffix)
191189
)
192190
)
193191
}
194192

193+
private Type getPositionalTypeArgument(int i, TypePath path) {
194+
result = this.getPathPositionalTypeArgument(i).resolveTypeAt(path)
195+
or
196+
result = this.getDefaultPositionalTypeArgument(i, path)
197+
}
198+
195199
/**
196200
* Gets the type for this path for the type parameter `tp` at `path`, when the
197201
* type parameter does not correspond directly to a type mention.
@@ -282,7 +286,7 @@ class NonAliasPathTypeMention extends PathTypeMention {
282286
// When the path refers to a trait, then the implicit `Self` type parameter
283287
// should be instantiated from the context.
284288
exists(TypePath suffix |
285-
result = this.getSelfTypeParameter().resolveTypeAt(suffix) and
289+
result = this.getSelfTraitBoundArg().resolveTypeAt(suffix) and
286290
typePath = TypePath::cons(TSelfTypeParameter(resolved), suffix)
287291
)
288292
}

rust/ql/test/library-tests/type-inference/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ multipleCallTargets
55
| dereference.rs:184:17:184:30 | ... .foo() |
66
| dereference.rs:186:17:186:25 | S.bar(...) |
77
| dereference.rs:187:17:187:29 | S.bar(...) |
8+
| main.rs:2481:13:2481:31 | ...::from(...) |
89
| main.rs:2482:13:2482:31 | ...::from(...) |
910
| main.rs:2483:13:2483:31 | ...::from(...) |
10-
| main.rs:2484:13:2484:31 | ...::from(...) |
11+
| main.rs:2489:13:2489:31 | ...::from(...) |
1112
| main.rs:2490:13:2490:31 | ...::from(...) |
1213
| main.rs:2491:13:2491:31 | ...::from(...) |
13-
| main.rs:2492:13:2492:31 | ...::from(...) |

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,6 @@ mod type_parameter_bounds {
654654

655655
mod trait_default_self_type_parameter {
656656
// A trait with a type parameter that defaults to `Self`.
657-
// trait TraitWithSelfTp<A = Self> {
658657
trait TraitWithSelfTp<A = Option<Self>> {
659658
// TraitWithSelfTp::get_a
660659
fn get_a(&self) -> A;

0 commit comments

Comments
 (0)