Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class StreamCipherInit extends Cryptography::CryptographicOperation::Range {
// extract the algorithm name from the type of `ce` or its receiver.
exists(Type t, TypePath tp |
t = inferType([call, call.(MethodCall).getReceiver()], tp) and
rawAlgorithmName = t.(StructType).getStruct().(Addressable).getCanonicalPath().splitAt("::")
rawAlgorithmName =
t.(StructType).getTypeItem().(Addressable).getCanonicalPath().splitAt("::")
) and
algorithmName = simplifyAlgorithmName(rawAlgorithmName) and
// only match a known cryptographic algorithm
Expand Down
93 changes: 35 additions & 58 deletions rust/ql/lib/codeql/rust/internal/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ private predicate dynTraitTypeParameter(Trait trait, AstNode n) {

cached
newtype TType =
TStruct(Struct s) { Stages::TypeInferenceStage::ref() } or
TEnum(Enum e) or
TDataType(TypeItem ti) { Stages::TypeInferenceStage::ref() } or
TTrait(Trait t) or
TUnion(Union u) or
TImplTraitType(ImplTraitTypeRepr impl) or
TDynTraitType(Trait t) { t = any(DynTraitTypeRepr dt).getTrait() } or
TNeverType() or
Expand Down Expand Up @@ -92,7 +90,7 @@ abstract class Type extends TType {
class TupleType extends StructType {
private int arity;

TupleType() { arity = this.getStruct().(Builtins::TupleType).getArity() }
TupleType() { arity = this.getTypeItem().(Builtins::TupleType).getArity() }

/** Gets the arity of this tuple type. */
int getArity() { result = arity }
Expand All @@ -112,48 +110,49 @@ class UnitType extends TupleType {
override string toString() { result = "()" }
}

/** A struct type. */
class StructType extends Type, TStruct {
private Struct struct;
class DataType extends Type, TDataType {
private TypeItem typeItem;

StructType() { this = TStruct(struct) }
DataType() { this = TDataType(typeItem) }

/** Gets the struct that this struct type represents. */
Struct getStruct() { result = struct }
/** Gets the type item that this data type represents. */
TypeItem getTypeItem() { result = typeItem }

override TypeParameter getPositionalTypeParameter(int i) {
result = TTypeParamTypeParameter(struct.getGenericParamList().getTypeParam(i))
result = TTypeParamTypeParameter(typeItem.getGenericParamList().getTypeParam(i))
}

override TypeMention getTypeParameterDefault(int i) {
result = struct.getGenericParamList().getTypeParam(i).getDefaultType()
result = typeItem.getGenericParamList().getTypeParam(i).getDefaultType()
}

override string toString() { result = struct.getName().getText() }
override string toString() { result = typeItem.getName().getText() }

override Location getLocation() { result = struct.getLocation() }
override Location getLocation() { result = typeItem.getLocation() }
}

/** An enum type. */
class EnumType extends Type, TEnum {
private Enum enum;

EnumType() { this = TEnum(enum) }
/** A struct type. */
class StructType extends DataType {
StructType() { super.getTypeItem() instanceof Struct }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
StructType() { super.getTypeItem() instanceof Struct }
private Struct struct;
StructType() { struct = super.getTypeItem() }


/** Gets the enum that this enum type represents. */
Enum getEnum() { result = enum }
/** Gets the struct that this struct type represents. */
override Struct getTypeItem() { result = super.getTypeItem() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override Struct getTypeItem() { result = super.getTypeItem() }
override Struct getTypeItem() { result = struct }

}

override TypeParameter getPositionalTypeParameter(int i) {
result = TTypeParamTypeParameter(enum.getGenericParamList().getTypeParam(i))
}
/** An enum type. */
class EnumType extends DataType {
EnumType() { super.getTypeItem() instanceof Enum }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EnumType() { super.getTypeItem() instanceof Enum }
private Enum enum;
EnumType() { enum = super.getTypeItem() }


override TypeMention getTypeParameterDefault(int i) {
result = enum.getGenericParamList().getTypeParam(i).getDefaultType()
}
/** Gets the enum that this enum type represents. */
override Enum getTypeItem() { result = super.getTypeItem() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override Enum getTypeItem() { result = super.getTypeItem() }
override Enum getTypeItem() { result = enum }

}

override string toString() { result = enum.getName().getText() }
/** A union type. */
class UnionType extends DataType {
UnionType() { super.getTypeItem() instanceof Union }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UnionType() { super.getTypeItem() instanceof Union }
private Union union;
UnionType() { union = super.getTypeItem() }


override Location getLocation() { result = enum.getLocation() }
/** Gets the union that this union type represents. */
override Union getTypeItem() { result = super.getTypeItem() }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override Union getTypeItem() { result = super.getTypeItem() }
override Union getTypeItem() { result = union }

}

/** A trait type. */
Expand Down Expand Up @@ -186,35 +185,13 @@ class TraitType extends Type, TTrait {
override Location getLocation() { result = trait.getLocation() }
}

/** A union type. */
class UnionType extends Type, TUnion {
private Union union;

UnionType() { this = TUnion(union) }

/** Gets the union that this union type represents. */
Union getUnion() { result = union }

override TypeParameter getPositionalTypeParameter(int i) {
result = TTypeParamTypeParameter(union.getGenericParamList().getTypeParam(i))
}

override TypeMention getTypeParameterDefault(int i) {
result = union.getGenericParamList().getTypeParam(i).getDefaultType()
}

override string toString() { result = union.getName().getText() }

override Location getLocation() { result = union.getLocation() }
}

/**
* An array type.
*
* Array types like `[i64; 5]` are modeled as normal generic types.
*/
class ArrayType extends StructType {
ArrayType() { this.getStruct() instanceof Builtins::ArrayType }
ArrayType() { this.getTypeItem() instanceof Builtins::ArrayType }

override string toString() { result = "[;]" }
}
Expand All @@ -227,13 +204,13 @@ TypeParamTypeParameter getArrayTypeParameter() {
abstract class RefType extends StructType { }

class RefMutType extends RefType {
RefMutType() { this.getStruct() instanceof Builtins::RefMutType }
RefMutType() { this.getTypeItem() instanceof Builtins::RefMutType }

override string toString() { result = "&mut" }
}

class RefSharedType extends RefType {
RefSharedType() { this.getStruct() instanceof Builtins::RefSharedType }
RefSharedType() { this.getTypeItem() instanceof Builtins::RefSharedType }

override string toString() { result = "&" }
}
Expand Down Expand Up @@ -330,7 +307,7 @@ class ImplTraitReturnType extends ImplTraitType {
* with a single type argument.
*/
class SliceType extends StructType {
SliceType() { this.getStruct() instanceof Builtins::SliceType }
SliceType() { this.getTypeItem() instanceof Builtins::SliceType }

override string toString() { result = "[]" }
}
Expand All @@ -356,13 +333,13 @@ TypeParamTypeParameter getPtrTypeParameter() {
}

class PtrMutType extends PtrType {
PtrMutType() { this.getStruct() instanceof Builtins::PtrMutType }
PtrMutType() { this.getTypeItem() instanceof Builtins::PtrMutType }

override string toString() { result = "*mut" }
}

class PtrConstType extends PtrType {
PtrConstType() { this.getStruct() instanceof Builtins::PtrConstType }
PtrConstType() { this.getTypeItem() instanceof Builtins::PtrConstType }

override string toString() { result = "*const" }
}
Expand Down Expand Up @@ -624,7 +601,7 @@ pragma[nomagic]
predicate validSelfType(Type t) {
t instanceof RefType
or
exists(Struct s | t = TStruct(s) |
exists(Struct s | t = TDataType(s) |
s instanceof BoxStruct or
s instanceof RcStruct or
s instanceof ArcStruct or
Expand Down
Loading