Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Implement support for inference of type aliases #19146

Merged
merged 3 commits into from
Apr 7, 2025

Conversation

paldepind
Copy link
Contributor

This PR implements support for type aliases. I can think of two ways to implement type aliases:

  1. Use subtyping and getABaseTypeMention.
  2. Effectively "inline" the type alias by having type mentions that resolve to a type alias give the alias definition as their type in resolveTypeAt.

This PR does the latter, for a few reasons:

  • The approach handles nested type aliases with no extra work. Right now our support for subtyping doesn't consider subtypes within subtypes.
  • For something like if b { let x: Alias1 = ...; x } else { let y: Alias2 = ...; y } where Alias1 and Alias2 refer to the same type, immediately unfolding the alias means that the entire if expression only gets one inferred type, and not both (identical) type aliases.
  • When we get to handle constrained trait implementations we need to check if type are equal, and that might be easier when there are no aliases in the way and types are already "normalized".
  • We only apply subsumption at specific program points (arguments to functions, etc.) whereas this approach means that aliases are handled everywhere.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Mar 28, 2025
@paldepind paldepind marked this pull request as ready for review March 28, 2025 10:40
@paldepind paldepind requested review from Copilot and hvitved March 28, 2025 10:40
Copy link
Contributor

@Copilot 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 implements support for type alias inference in Rust by inlining alias definitions to normalize types. Key changes include the addition of a new helper method (unwrapSnd) for the PairOption type, updates to type alias definitions (including nested aliases), and adjustments to test cases to reflect these changes.

Files not reviewed (1)
  • rust/ql/lib/codeql/rust/internal/TypeMention.qll: Language not supported
Comments suppressed due to low confidence (1)

rust/ql/test/library-tests/type-inference/main.rs:541

  • [nitpick] The generic parameter 'A3' is not very descriptive. Consider using a more meaningful name to improve code clarity.
type AnotherPair<A3> = PairOption<S2, A3>;

Tip: Copilot code review supports C#, Go, Java, JavaScript, Markdown, Python, Ruby and TypeScript, with more languages coming soon. Learn more

// Alias to another alias
type AliasToAlias<A4> = AnotherPair<A4>;

// Alias that appears nested withing another alias
Copy link
Preview

Copilot AI Mar 28, 2025

Choose a reason for hiding this comment

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

Typo in comment: 'withing' should be corrected to 'within'.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

hvitved
hvitved previously approved these changes Apr 7, 2025
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.

Very nice, one suggestion.

@paldepind
Copy link
Contributor Author

Thanks for reviewing :)

@paldepind paldepind merged commit 3441af1 into github:main Apr 7, 2025
17 checks passed
@hvitved hvitved mentioned this pull request Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants