Skip to content

Commit 1408c24

Browse files
authored
Merge pull request #20557 from paldepind/rust/type-inference-delete-predicates
Rust: Remove member predicates on `Type`
2 parents ff3d795 + daf0cf1 commit 1408c24

File tree

3 files changed

+19
-83
lines changed

3 files changed

+19
-83
lines changed

rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
3131
// extract the algorithm name from the type of `ce` or its receiver.
3232
exists(Type t, TypePath tp |
3333
t = inferType([ce, ce.(MethodCallExpr).getReceiver()], tp) and
34-
rawAlgorithmName =
35-
t.(StructType).asItemNode().(Addressable).getCanonicalPath().splitAt("::")
34+
rawAlgorithmName = t.(StructType).getStruct().(Addressable).getCanonicalPath().splitAt("::")
3635
) and
3736
algorithmName = simplifyAlgorithmName(rawAlgorithmName) and
3837
// only match a known cryptographic algorithm

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

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ private predicate implTraitTypeParam(ImplTraitTypeRepr implTrait, int i, TypePar
7878
* types, such as traits and implementation blocks.
7979
*/
8080
abstract class Type extends TType {
81-
/** Gets the struct field `name` belonging to this type, if any. */
82-
pragma[nomagic]
83-
abstract StructField getStructField(string name);
84-
85-
/** Gets the `i`th tuple field belonging to this type, if any. */
86-
pragma[nomagic]
87-
abstract TupleField getTupleField(int i);
88-
8981
/**
9082
* Gets the `i`th positional type parameter of this type, if any.
9183
*
@@ -117,10 +109,6 @@ class TupleType extends Type, TTuple {
117109

118110
TupleType() { this = TTuple(arity) }
119111

120-
override StructField getStructField(string name) { none() }
121-
122-
override TupleField getTupleField(int i) { none() }
123-
124112
override TypeParameter getPositionalTypeParameter(int i) {
125113
result = TTupleTypeParameter(arity, i)
126114
}
@@ -140,21 +128,14 @@ class UnitType extends TupleType {
140128
override string toString() { result = "()" }
141129
}
142130

143-
abstract private class StructOrEnumType extends Type {
144-
abstract ItemNode asItemNode();
145-
}
146-
147131
/** A struct type. */
148-
class StructType extends StructOrEnumType, TStruct {
132+
class StructType extends Type, TStruct {
149133
private Struct struct;
150134

151135
StructType() { this = TStruct(struct) }
152136

153-
override ItemNode asItemNode() { result = struct }
154-
155-
override StructField getStructField(string name) { result = struct.getStructField(name) }
156-
157-
override TupleField getTupleField(int i) { result = struct.getTupleField(i) }
137+
/** Gets the struct that this struct type represents. */
138+
Struct getStruct() { result = struct }
158139

159140
override TypeParameter getPositionalTypeParameter(int i) {
160141
result = TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
@@ -170,17 +151,11 @@ class StructType extends StructOrEnumType, TStruct {
170151
}
171152

172153
/** An enum type. */
173-
class EnumType extends StructOrEnumType, TEnum {
154+
class EnumType extends Type, TEnum {
174155
private Enum enum;
175156

176157
EnumType() { this = TEnum(enum) }
177158

178-
override ItemNode asItemNode() { result = enum }
179-
180-
override StructField getStructField(string name) { none() }
181-
182-
override TupleField getTupleField(int i) { none() }
183-
184159
override TypeParameter getPositionalTypeParameter(int i) {
185160
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
186161
}
@@ -203,10 +178,6 @@ class TraitType extends Type, TTrait {
203178
/** Gets the underlying trait. */
204179
Trait getTrait() { result = trait }
205180

206-
override StructField getStructField(string name) { none() }
207-
208-
override TupleField getTupleField(int i) { none() }
209-
210181
override TypeParameter getPositionalTypeParameter(int i) {
211182
result = TTypeParamTypeParameter(trait.getGenericParamList().getTypeParam(i))
212183
}
@@ -229,16 +200,13 @@ class TraitType extends Type, TTrait {
229200
}
230201

231202
/** A union type. */
232-
class UnionType extends StructOrEnumType, TUnion {
203+
class UnionType extends Type, TUnion {
233204
private Union union;
234205

235206
UnionType() { this = TUnion(union) }
236207

237-
override ItemNode asItemNode() { result = union }
238-
239-
override StructField getStructField(string name) { result = union.getStructField(name) }
240-
241-
override TupleField getTupleField(int i) { none() }
208+
/** Gets the union that this union type represents. */
209+
Union getUnion() { result = union }
242210

243211
override TypeParameter getPositionalTypeParameter(int i) {
244212
result = TTypeParamTypeParameter(union.getGenericParamList().getTypeParam(i))
@@ -262,10 +230,6 @@ class UnionType extends StructOrEnumType, TUnion {
262230
class ArrayType extends Type, TArrayType {
263231
ArrayType() { this = TArrayType() }
264232

265-
override StructField getStructField(string name) { none() }
266-
267-
override TupleField getTupleField(int i) { none() }
268-
269233
override TypeParameter getPositionalTypeParameter(int i) {
270234
result = TArrayTypeParameter() and
271235
i = 0
@@ -285,10 +249,6 @@ class ArrayType extends Type, TArrayType {
285249
class RefType extends Type, TRefType {
286250
RefType() { this = TRefType() }
287251

288-
override StructField getStructField(string name) { none() }
289-
290-
override TupleField getTupleField(int i) { none() }
291-
292252
override TypeParameter getPositionalTypeParameter(int i) {
293253
result = TRefTypeParameter() and
294254
i = 0
@@ -318,10 +278,6 @@ class ImplTraitType extends Type, TImplTraitType {
318278
/** Gets the function that this `impl Trait` belongs to. */
319279
abstract Function getFunction();
320280

321-
override StructField getStructField(string name) { none() }
322-
323-
override TupleField getTupleField(int i) { none() }
324-
325281
override TypeParameter getPositionalTypeParameter(int i) {
326282
exists(TypeParam tp |
327283
implTraitTypeParam(impl, i, tp) and
@@ -339,10 +295,6 @@ class DynTraitType extends Type, TDynTraitType {
339295

340296
DynTraitType() { this = TDynTraitType(trait) }
341297

342-
override StructField getStructField(string name) { none() }
343-
344-
override TupleField getTupleField(int i) { none() }
345-
346298
override DynTraitTypeParameter getPositionalTypeParameter(int i) {
347299
result = TDynTraitTypeParameter(trait.getGenericParamList().getTypeParam(i))
348300
}
@@ -389,10 +341,6 @@ class ImplTraitReturnType extends ImplTraitType {
389341
class SliceType extends Type, TSliceType {
390342
SliceType() { this = TSliceType() }
391343

392-
override StructField getStructField(string name) { none() }
393-
394-
override TupleField getTupleField(int i) { none() }
395-
396344
override TypeParameter getPositionalTypeParameter(int i) {
397345
result = TSliceTypeParameter() and
398346
i = 0
@@ -404,10 +352,6 @@ class SliceType extends Type, TSliceType {
404352
}
405353

406354
class NeverType extends Type, TNeverType {
407-
override StructField getStructField(string name) { none() }
408-
409-
override TupleField getTupleField(int i) { none() }
410-
411355
override TypeParameter getPositionalTypeParameter(int i) { none() }
412356

413357
override string toString() { result = "!" }
@@ -416,10 +360,6 @@ class NeverType extends Type, TNeverType {
416360
}
417361

418362
class PtrType extends Type, TPtrType {
419-
override StructField getStructField(string name) { none() }
420-
421-
override TupleField getTupleField(int i) { none() }
422-
423363
override TypeParameter getPositionalTypeParameter(int i) {
424364
i = 0 and
425365
result = TPtrTypeParameter()
@@ -432,10 +372,6 @@ class PtrType extends Type, TPtrType {
432372

433373
/** A type parameter. */
434374
abstract class TypeParameter extends Type {
435-
override StructField getStructField(string name) { none() }
436-
437-
override TupleField getTupleField(int i) { none() }
438-
439375
override TypeParameter getPositionalTypeParameter(int i) { none() }
440376
}
441377

@@ -634,10 +570,6 @@ class ImplTraitTypeTypeParameter extends ImplTraitType, TypeParameter {
634570

635571
override Function getFunction() { result = function }
636572

637-
override StructField getStructField(string name) { none() }
638-
639-
override TupleField getTupleField(int i) { none() }
640-
641573
override TypeParameter getPositionalTypeParameter(int i) { none() }
642574
}
643575

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,8 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
11731173
path = TypePath::cons(TRefTypeParameter(), path0)
11741174
else (
11751175
not (
1176-
argType.(StructType).asItemNode() instanceof StringStruct and
1177-
result.(StructType).asItemNode() instanceof Builtins::Str
1176+
argType.(StructType).getStruct() instanceof StringStruct and
1177+
result.(StructType).getStruct() instanceof Builtins::Str
11781178
) and
11791179
(
11801180
not path0.isCons(TRefTypeParameter(), _) and
@@ -1889,8 +1889,8 @@ final class MethodCall extends Call {
18891889
//
18901890
// See also https://doc.rust-lang.org/reference/expressions/method-call-expr.html#r-expr.method.autoref-deref
18911891
path.isEmpty() and
1892-
t0.(StructType).asItemNode() instanceof StringStruct and
1893-
result.(StructType).asItemNode() instanceof Builtins::Str
1892+
t0.(StructType).getStruct() instanceof StringStruct and
1893+
result.(StructType).getStruct() instanceof Builtins::Str
18941894
)
18951895
else result = this.getReceiverTypeAt(path)
18961896
}
@@ -2518,15 +2518,20 @@ private module Cached {
25182518
*/
25192519
cached
25202520
StructField resolveStructFieldExpr(FieldExpr fe) {
2521-
exists(string name | result = getFieldExprLookupType(fe, name).getStructField(name))
2521+
exists(string name, Type ty | ty = getFieldExprLookupType(fe, name) |
2522+
result = ty.(StructType).getStruct().getStructField(name) or
2523+
result = ty.(UnionType).getUnion().getStructField(name)
2524+
)
25222525
}
25232526

25242527
/**
25252528
* Gets the tuple field that the field expression `fe` resolves to, if any.
25262529
*/
25272530
cached
25282531
TupleField resolveTupleFieldExpr(FieldExpr fe) {
2529-
exists(int i | result = getTupleFieldExprLookupType(fe, i).getTupleField(i))
2532+
exists(int i |
2533+
result = getTupleFieldExprLookupType(fe, i).(StructType).getStruct().getTupleField(i)
2534+
)
25302535
}
25312536

25322537
/**

0 commit comments

Comments
 (0)