Skip to content

Rust: Simple type inference for index expressions #19657

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

Merged
merged 2 commits into from
Jun 12, 2025

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jun 3, 2025

This PR adds simple type inference for index expressions like x[0]. Support is simple because we do not properly model the Index trait, but instead we only support Vec. Slice and array types are modeled correctly.

DCA confirms that we are now able to resolve more calls.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 3, 2025
@hvitved hvitved force-pushed the rust/type-inference-index-expr-simple branch 2 times, most recently from 214079f to 2652887 Compare June 4, 2025 08:16
* [1]: https://doc.rust-lang.org/reference/expressions/array-expr.html#r-expr.array.index
*/
pragma[nomagic]
private Type inferIndexExprType(IndexExpr ie, TypePath path) {

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning

The QLDoc has no documentation for ie, or path, but the QLDoc mentions usize
@hvitved hvitved force-pushed the rust/type-inference-index-expr-simple branch 3 times, most recently from 351a981 to d8161df Compare June 10, 2025 13:05
@hvitved hvitved marked this pull request as ready for review June 10, 2025 16:48
@hvitved hvitved requested a review from a team as a code owner June 10, 2025 16:48
@hvitved hvitved requested a review from paldepind June 10, 2025 16:48
@hvitved hvitved force-pushed the rust/type-inference-index-expr-simple branch from d8161df to 133aca0 Compare June 11, 2025 11:16
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

The DCA report is certainly an improvement.

Code LGTM, though it's my first time looking at the type inference library.

TRefTypeParameter() or
TSelfTypeParameter(Trait t)
TSelfTypeParameter(Trait t) or
TSliceTypeParameter()
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of interest, how are these different? i.e. why is just one TypeParameter() type insufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We model slice types as if there was a special struct Slice<T> = ... type, and TSliceTypeParameter corresponds to the T. So e.g. [i64] translates to Slice<i64>.

Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

I'm happy, up to you if you want to wait for a review from @paldepind as well or not.

@hvitved hvitved merged commit 649481e into github:main Jun 12, 2025
17 checks passed
@hvitved hvitved deleted the rust/type-inference-index-expr-simple branch June 12, 2025 08:27
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.

3 participants