Skip to content

Conversation

@paldepind
Copy link
Contributor

@paldepind paldepind commented Sep 30, 2025

I don't think the getStructField and getTupleField predicates carries their weight enough to be on the Type class itself. I thus wanted to define them only on the relevant subclasses, but realized that removing them altogether also makes sense.

With the new asStruct and asUnion we don't need asItemNode either as using asStruct is clearer. For instance in

argType.(StructType).asItemNode() instanceof StringStruct

we're checking if the left hand side is a special subclass of Struct, so using a Struct directly makes more sense than using an ItemNode.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Sep 30, 2025
@paldepind paldepind marked this pull request as ready for review September 30, 2025 12:24
@paldepind paldepind requested a review from a team as a code owner September 30, 2025 12:24
Copilot AI review requested due to automatic review settings September 30, 2025 12:24
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 Rust type system by removing member predicates getStructField and getTupleField from the base Type class and replacing asItemNode() with more specific methods asStruct() and asUnion(). The changes improve code clarity by making type-specific operations explicit rather than abstract.

  • Removes abstract methods getStructField and getTupleField from the base Type class
  • Replaces asItemNode() calls with more specific asStruct() and asUnion() methods
  • Updates field resolution logic to use type-specific accessors directly

Reviewed Changes

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

File Description
rust/ql/lib/codeql/rust/internal/Type.qll Removes abstract predicates from Type class and adds asStruct()/asUnion() methods
rust/ql/lib/codeql/rust/internal/TypeInference.qll Updates field resolution and type checking to use new accessor methods
rust/ql/lib/codeql/rust/frameworks/rustcrypto/RustCrypto.qll Updates algorithm name extraction to use asStruct() instead of asItemNode()

@paldepind paldepind added the no-change-note-required This PR does not need a change note label Sep 30, 2025

override TupleField getTupleField(int i) { result = struct.getTupleField(i) }
/** Get the struct that this struct type represents. */
Struct asStruct() { result = struct }
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think getStruct is better; as is normally used when there is a possibility that the predicate does not hold.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, move the predicate all the way up into Type; then you can replace all of the (StructType).asStruct() occurrences with .asStruct().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I didn't know that. I thought as* was for when the predicate returned "everything" in this and thus was a kind of type cast. I prefer to keep it on StructType though, otherwise we need all the none() predicates again.

override StructField getStructField(string name) { result = struct.getStructField(name) }

override TupleField getTupleField(int i) { result = struct.getTupleField(i) }
/** Get the struct that this struct type represents. */
Copy link
Contributor

Choose a reason for hiding this comment

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

Gets


override TupleField getTupleField(int i) { none() }
/** Get the union that this union type represents. */
Union asUnion() { result = union }
Copy link
Contributor

Choose a reason for hiding this comment

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

Same (x 2)

@paldepind
Copy link
Contributor Author

Thanks for the review. Comments should be addressed now.

@paldepind paldepind merged commit 1408c24 into github:main Oct 1, 2025
18 checks passed
@paldepind paldepind deleted the rust/type-inference-delete-predicates branch October 1, 2025 14:49
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