Skip to content

Commit 0b6d01e

Browse files
committed
Rust: Use defaults for type parameters
1 parent 373a0a4 commit 0b6d01e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ abstract class Type extends TType {
4242
/** Gets the `i`th type parameter of this type, if any. */
4343
abstract TypeParameter getTypeParameter(int i);
4444

45+
/** Gets the the default type for the `i`th type parameter, if any. */
46+
TypeMention getTypeParameterDefault(int i) { none() }
47+
4548
/** Gets a type parameter of this type. */
4649
final TypeParameter getATypeParameter() { result = this.getTypeParameter(_) }
4750

@@ -87,6 +90,10 @@ class StructType extends StructOrEnumType, TStruct {
8790
result = TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
8891
}
8992

93+
override TypeMention getTypeParameterDefault(int i) {
94+
result = struct.getGenericParamList().getTypeParam(i).getDefaultType()
95+
}
96+
9097
override string toString() { result = struct.getName().getText() }
9198

9299
override Location getLocation() { result = struct.getLocation() }
@@ -108,6 +115,10 @@ class EnumType extends StructOrEnumType, TEnum {
108115
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
109116
}
110117

118+
override TypeMention getTypeParameterDefault(int i) {
119+
result = enum.getGenericParamList().getTypeParam(i).getDefaultType()
120+
}
121+
111122
override string toString() { result = enum.getName().getText() }
112123

113124
override Location getLocation() { result = enum.getLocation() }
@@ -133,6 +144,10 @@ class TraitType extends Type, TTrait {
133144
any(AssociatedTypeTypeParameter param | param.getTrait() = trait and param.getIndex() = i)
134145
}
135146

147+
override TypeMention getTypeParameterDefault(int i) {
148+
result = trait.getGenericParamList().getTypeParam(i).getDefaultType()
149+
}
150+
136151
override string toString() { result = trait.toString() }
137152

138153
override Location getLocation() { result = trait.getLocation() }

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class PathTypeReprMention extends TypeMention instanceof PathTypeRepr {
8888
override TypeMention getTypeArgument(int i) {
8989
result = path.getSegment().getGenericArgList().getTypeArg(i)
9090
or
91+
// If a type argument is not given in the path, then we use the default for
92+
// the type parameter if one exists for the type.
93+
not exists(path.getSegment().getGenericArgList().getTypeArg(i)) and
94+
result = this.resolveType().getTypeParameterDefault(i)
95+
or
9196
// `Self` paths inside `impl` blocks have implicit type arguments that are
9297
// the type parameters of the `impl` block. For example, in
9398
//

0 commit comments

Comments
 (0)