Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Rust: Type inference for for loops and array expressions #19754

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 25 commits into from
Jun 24, 2025

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Jun 13, 2025

Adds type inference for for loops and array expressions. Currently for for loops this is limited to loops iterating through arrays.

@hvitved please advise.

  • I think for the more general cases we will need to decode the Iterator type and perhaps understand IntoIterator.
  • I'm not sure whether I should be attempting to model implicit dereferences, or perhaps a more general solution for that problem might be in the works?

@geoffw0 geoffw0 added the Rust Pull requests that update Rust code label Jun 13, 2025
@geoffw0
Copy link
Contributor Author

geoffw0 commented Jun 17, 2025

@hvitved do you think this is heading in the right direction?

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.

Great start!


// for loops with containers

let vals3 = vec![1, 2, 3]; // $ MISSING: type=vals3:Vec<i32>
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not how to make expected type annotations for constructed types, instead you should use the format type=<element>:<path>.<type> (see

else value = element + ":" + path.toString() + "." + t.toString()
). So in this case it should instead by type=vals3:T.i32.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I think I was relying on the test runner to start suggesting correct syntax, which it doesn't really do for optional results. I've updated the expectations now as best I can - there may still be add mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

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

This one still needs to be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point. 😆

Updated.

@geoffw0 geoffw0 marked this pull request as ready for review June 19, 2025 13:19
@geoffw0 geoffw0 requested a review from a team as a code owner June 19, 2025 13:19
@geoffw0
Copy link
Contributor Author

geoffw0 commented Jun 19, 2025

Ready for review: there are gaps (and apparently a conflict with main), but we get some useful results and I'd like to push towards merging this ASAP.

Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

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

Looks really great 👍. I've left a few minor comments.

map1.insert(1, Box::new("one")); // $ method=insert
map1.insert(2, Box::new("two")); // $ method=insert
for key in map1.keys() { } // $ method=keys MISSING: type=key:i32
for value in map1.values() { } // $ method=values MISSING: type=value:Box type=value:Box.T:&T.str
Copy link
Contributor

Choose a reason for hiding this comment

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

This type annotation has two :s. I think the later should be a . or tweaked in some other way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated - I'm not 100% confident I've got it right but it's certainly less wrong!

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.

One small last thing, otherwise LGTM.


// for loops with containers

let vals3 = vec![1, 2, 3]; // $ MISSING: type=vals3:Vec<i32>
Copy link
Contributor

Choose a reason for hiding this comment

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

This one still needs to be updated.

@geoffw0 geoffw0 dismissed paldepind’s stale review June 24, 2025 12:19

Concerns addressed, PR is approved, and he is away today so not able to add his own approval.

@geoffw0 geoffw0 merged commit e379795 into github:main Jun 24, 2025
19 checks passed
@geoffw0
Copy link
Contributor Author

geoffw0 commented Jun 24, 2025

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants