Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Dec 18, 2025

Just a quick go at getting some mileage out of the new TypeItem class in the type inference implementation.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Dec 18, 2025
@paldepind paldepind added the no-change-note-required This PR does not need a change note label Dec 18, 2025
@paldepind paldepind force-pushed the rust/type-inference-use-type-item branch from bfc43d5 to fce7247 Compare December 18, 2025 14:52
@paldepind paldepind force-pushed the rust/type-inference-use-type-item branch from fce7247 to dde845e Compare December 18, 2025 15:08
@paldepind paldepind marked this pull request as ready for review December 18, 2025 16:14
@paldepind paldepind requested a review from a team as a code owner December 18, 2025 16:14
Copilot AI review requested due to automatic review settings December 18, 2025 16:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the type inference implementation in Rust QL to use a new unified TypeItem class, consolidating the handling of structs, enums, and unions under a common abstraction.

  • Introduces a new DataType class that replaces separate TStruct, TEnum, and TUnion type constructors with a single TDataType(TypeItem) constructor
  • Replaces specific getter methods (getStruct(), getEnum(), getUnion()) with a unified getTypeItem() method across the codebase
  • Refactors TupleLikeConstructor and Declaration classes to use the new abstraction, reducing code duplication

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
rust/ql/lib/codeql/rust/internal/Type.qll Introduces DataType class and makes StructType, EnumType, and UnionType subclasses; consolidates type constructors from TStruct, TEnum, TUnion to TDataType
rust/ql/lib/codeql/rust/internal/TypeInference.qll Updates all type references to use TDataType constructor; refactors TupleLikeConstructor and Declaration classes to use getTypeItem() method and eliminate duplicate logic
rust/ql/lib/codeql/rust/internal/TypeMention.qll Simplifies resolveRootType() by consolidating three separate type resolution cases into a single TDataType case
rust/ql/lib/codeql/rust/security/Barriers.qll Updates barrier classes to use getTypeItem() instead of getStruct() or getEnum()
rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll Updates algorithm name extraction to use getTypeItem() instead of getStruct()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

Nice refactor; some minor comments. I have also started a DCA run.

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 }

}
/** 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() }

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 }

result = this.getTypeParameter(_) and
path = TypePath::singleton(result)
or
// type of the struct itself
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs to be updated

}
override TypeItem getTypeItem() { result = this }

override TupleField getTupleField(int i) { result = this.(Struct).getTupleField(i) }
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 TupleField getTupleField(int i) { result = this.(Struct).getTupleField(i) }
override TupleField getTupleField(int i) { result = Struct.super.getTupleField(i) }

path = TypePath::singleton(result)
)
}
override TupleField getTupleField(int i) { result = this.(Variant).getTupleField(i) }
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 TupleField getTupleField(int i) { result = this.(Variant).getTupleField(i) }
override TupleField getTupleField(int i) { result = Variant.super.getTupleField(i) }

@paldepind paldepind requested a review from hvitved December 19, 2025 12:55
@paldepind paldepind merged commit 6fa6093 into github:main Dec 19, 2025
18 checks passed
@paldepind paldepind deleted the rust/type-inference-use-type-item branch December 19, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants