Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6fa6093

Browse files
authored
Merge pull request#21067 from paldepind/rust/type-inference-use-type-item
Rust: Refactor type inference to use new `TypeItem` class
2 parentsb5d3d95 +e0e493a commit6fa6093

File tree

5 files changed

+91
-146
lines changed

5 files changed

+91
-146
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
3030
// extract the algorithm name from the type of `ce` or its receiver.
3131
exists(Typet,TypePathtp|
3232
t=inferType([call,call.(MethodCall).getReceiver()],tp)and
33-
rawAlgorithmName=t.(StructType).getStruct().(Addressable).getCanonicalPath().splitAt("::")
33+
rawAlgorithmName=
34+
t.(StructType).getTypeItem().(Addressable).getCanonicalPath().splitAt("::")
3435
)and
3536
algorithmName=simplifyAlgorithmName(rawAlgorithmName)and
3637
// only match a known cryptographic algorithm

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

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ private predicate dynTraitTypeParameter(Trait trait, AstNode n) {
3232

3333
cached
3434
newtypeTType=
35-
TStruct(Structs){ Stages::TypeInferenceStage::ref()}or
36-
TEnum(Enume)or
35+
TDataType(TypeItemti){ Stages::TypeInferenceStage::ref()}or
3736
TTrait(Traitt)or
38-
TUnion(Unionu)or
3937
TImplTraitType(ImplTraitTypeReprimpl)or
4038
TDynTraitType(Traitt){t=any(DynTraitTypeReprdt).getTrait()}or
4139
TNeverType()or
@@ -92,7 +90,7 @@ abstract class Type extends TType {
9290
classTupleTypeextendsStructType{
9391
privateintarity;
9492

95-
TupleType(){arity=this.getStruct().(Builtins::TupleType).getArity()}
93+
TupleType(){arity=this.getTypeItem().(Builtins::TupleType).getArity()}
9694

9795
/** Gets the arity of this tuple type. */
9896
intgetArity(){result=arity}
@@ -112,48 +110,55 @@ class UnitType extends TupleType {
112110
overridestringtoString(){result="()"}
113111
}
114112

115-
/** A struct type. */
116-
classStructTypeextendsType,TStruct{
117-
privateStructstruct;
113+
classDataTypeextendsType,TDataType{
114+
privateTypeItemtypeItem;
118115

119-
StructType(){this=TStruct(struct)}
116+
DataType(){this=TDataType(typeItem)}
120117

121-
/** Gets thestructthat thisstruct type represents. */
122-
StructgetStruct(){result=struct}
118+
/** Gets thetype itemthat thisdata type represents. */
119+
TypeItemgetTypeItem(){result=typeItem}
123120

124121
overrideTypeParametergetPositionalTypeParameter(inti){
125-
result=TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
122+
result=TTypeParamTypeParameter(typeItem.getGenericParamList().getTypeParam(i))
126123
}
127124

128125
overrideTypeMentiongetTypeParameterDefault(inti){
129-
result=struct.getGenericParamList().getTypeParam(i).getDefaultType()
126+
result=typeItem.getGenericParamList().getTypeParam(i).getDefaultType()
130127
}
131128

132-
overridestringtoString(){result=struct.getName().getText()}
129+
overridestringtoString(){result=typeItem.getName().getText()}
133130

134-
overrideLocationgetLocation(){result=struct.getLocation()}
131+
overrideLocationgetLocation(){result=typeItem.getLocation()}
132+
}
133+
134+
/** A struct type. */
135+
classStructTypeextendsDataType{
136+
privateStructstruct;
137+
138+
StructType(){struct=super.getTypeItem()}
139+
140+
/** Gets the struct that this struct type represents. */
141+
overrideStructgetTypeItem(){result=struct}
135142
}
136143

137144
/** An enum type. */
138-
classEnumTypeextendsType,TEnum{
145+
classEnumTypeextendsDataType{
139146
privateEnumenum;
140147

141-
EnumType(){this=TEnum(enum)}
148+
EnumType(){enum=super.getTypeItem()}
142149

143150
/** Gets the enum that this enum type represents. */
144-
EnumgetEnum(){result=enum}
145-
146-
overrideTypeParametergetPositionalTypeParameter(inti){
147-
result=TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
148-
}
151+
overrideEnumgetTypeItem(){result=enum}
152+
}
149153

150-
overrideTypeMentiongetTypeParameterDefault(inti){
151-
result=enum.getGenericParamList().getTypeParam(i).getDefaultType()
152-
}
154+
/** A union type. */
155+
classUnionTypeextendsDataType{
156+
privateUnionunion;
153157

154-
overridestringtoString(){result=enum.getName().getText()}
158+
UnionType(){union=super.getTypeItem()}
155159

156-
overrideLocationgetLocation(){result=enum.getLocation()}
160+
/** Gets the union that this union type represents. */
161+
overrideUniongetTypeItem(){result=union}
157162
}
158163

159164
/** A trait type. */
@@ -186,35 +191,13 @@ class TraitType extends Type, TTrait {
186191
overrideLocationgetLocation(){result=trait.getLocation()}
187192
}
188193

189-
/** A union type. */
190-
classUnionTypeextendsType,TUnion{
191-
privateUnionunion;
192-
193-
UnionType(){this=TUnion(union)}
194-
195-
/** Gets the union that this union type represents. */
196-
UniongetUnion(){result=union}
197-
198-
overrideTypeParametergetPositionalTypeParameter(inti){
199-
result=TTypeParamTypeParameter(union.getGenericParamList().getTypeParam(i))
200-
}
201-
202-
overrideTypeMentiongetTypeParameterDefault(inti){
203-
result=union.getGenericParamList().getTypeParam(i).getDefaultType()
204-
}
205-
206-
overridestringtoString(){result=union.getName().getText()}
207-
208-
overrideLocationgetLocation(){result=union.getLocation()}
209-
}
210-
211194
/**
212195
* An array type.
213196
*
214197
* Array types like `[i64; 5]` are modeled as normal generic types.
215198
*/
216199
classArrayTypeextendsStructType{
217-
ArrayType(){this.getStruct()instanceof Builtins::ArrayType}
200+
ArrayType(){this.getTypeItem()instanceof Builtins::ArrayType}
218201

219202
overridestringtoString(){result="[;]"}
220203
}
@@ -227,13 +210,13 @@ TypeParamTypeParameter getArrayTypeParameter() {
227210
abstractclassRefTypeextendsStructType{}
228211

229212
classRefMutTypeextendsRefType{
230-
RefMutType(){this.getStruct()instanceof Builtins::RefMutType}
213+
RefMutType(){this.getTypeItem()instanceof Builtins::RefMutType}
231214

232215
overridestringtoString(){result="&mut"}
233216
}
234217

235218
classRefSharedTypeextendsRefType{
236-
RefSharedType(){this.getStruct()instanceof Builtins::RefSharedType}
219+
RefSharedType(){this.getTypeItem()instanceof Builtins::RefSharedType}
237220

238221
overridestringtoString(){result="&"}
239222
}
@@ -330,7 +313,7 @@ class ImplTraitReturnType extends ImplTraitType {
330313
* with a single type argument.
331314
*/
332315
classSliceTypeextendsStructType{
333-
SliceType(){this.getStruct()instanceof Builtins::SliceType}
316+
SliceType(){this.getTypeItem()instanceof Builtins::SliceType}
334317

335318
overridestringtoString(){result="[]"}
336319
}
@@ -356,13 +339,13 @@ TypeParamTypeParameter getPtrTypeParameter() {
356339
}
357340

358341
classPtrMutTypeextendsPtrType{
359-
PtrMutType(){this.getStruct()instanceof Builtins::PtrMutType}
342+
PtrMutType(){this.getTypeItem()instanceof Builtins::PtrMutType}
360343

361344
overridestringtoString(){result="*mut"}
362345
}
363346

364347
classPtrConstTypeextendsPtrType{
365-
PtrConstType(){this.getStruct()instanceof Builtins::PtrConstType}
348+
PtrConstType(){this.getTypeItem()instanceof Builtins::PtrConstType}
366349

367350
overridestringtoString(){result="*const"}
368351
}
@@ -624,7 +607,7 @@ pragma[nomagic]
624607
predicatevalidSelfType(Typet){
625608
tinstanceofRefType
626609
or
627-
exists(Structs|t=TStruct(s)|
610+
exists(Structs|t=TDataType(s)|
628611
sinstanceofBoxStructor
629612
sinstanceofRcStructor
630613
sinstanceofArcStructor

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp