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

Desugar for-loops in the AST #3392

Merged
merged 3 commits into from
Feb 4, 2025

Conversation

CohenArthur
Copy link
Member

I'm not a big fan of mutating the AST to do this, however this enables us to desugar for-loops easily. The alternative is desugaring during the lowering phase, which has the issue of not inserting any information about the generated nodes into our name resolver. This leads to failures during typechecking which is annoying.

The best thing would be to figure out a way to insert resolved nodes when we do the Desugar, but it's a bit tricky.

@philberty
Copy link
Member

philberty commented Jan 29, 2025

Although we wanted to do this desugar in the HIR, i see this as our first pass so that we show that our internals are 'working' and i bet down the line we will figure it out.

I bet there is a way that during HIR lowering we actually decouple the name resolution entirely by linking name resolution information in a different way to not require the ast_node_id but link directly the HIR node or something else. It would be a fairly big refactor which is basically not worth the effort right now but would be a good google summer of code project we could scope out.

@philberty
Copy link
Member

As always great work sir

@CohenArthur CohenArthur force-pushed the for-loop-desugar-ast branch 2 times, most recently from 6977a54 to 76d0b09 Compare January 29, 2025 13:55

fn next(&mut self) -> Option<A> {
if self.start < self.end {
// We check for overflow here, even though it can't actually
Copy link
Member

Choose a reason for hiding this comment

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

i'd love to know more about this bit in interators in rustc why this was introduced i wonder what the test case was

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

Lets gooooo

Copy link
Member

@P-E-P P-E-P left a comment

Choose a reason for hiding this comment

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

Looks great!

gcc/rust/ast/rust-desugar-for-loops.cc Outdated Show resolved Hide resolved
gcc/rust/ast/rust-desugar-for-loops.cc Show resolved Hide resolved
MatchCase
DesugarForLoops::DesugarCtx::make_continue_arm ()
{
auto val = builder.identifier_pattern ("val");
Copy link
Member

Choose a reason for hiding this comment

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

Should we use some underscore before val ?

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, right - I think we should use an even weirder character like a . or a # to really prevent it from leaking out

Copy link
Member

Choose a reason for hiding this comment

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

Tbh I don't really like the fact that we could name it, it should really be some kind of long unameable identifier that changes depending on the locus and a few other factor or something along this. Not sure how we should do this properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

well we can't name it if theidentifier starts with # because it won't parse. so a user cannot name it. the other option is to add extra hygiene checks (e.g. having a special-cased location for generated code like rustc does) or generating those identifiers in the HIR, after name resolution has gone through

Copy link
Member Author

Choose a reason for hiding this comment

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

we could also use $__next or .__next or any other kind of invalid identifier which won't parse in Rust - that's enough to prevent the user from coliding/using these identifiers

gcc/rust/ast/rust-desugar-for-loops.cc Outdated Show resolved Hide resolved
gcc/rust/ast/rust-desugar-for-loops.h Outdated Show resolved Hide resolved
gcc/rust/ast/rust-desugar-for-loops.h Outdated Show resolved Hide resolved
@CohenArthur CohenArthur requested a review from P-E-P January 29, 2025 16:03
@CohenArthur CohenArthur force-pushed the for-loop-desugar-ast branch 2 times, most recently from 462be8d to 759a3d4 Compare February 4, 2025 14:43
gcc/rust/ChangeLog:

	* ast/rust-ast-builder.h: Mark all arguments as &&.
	* ast/rust-ast-builder.cc (Builder::let): Likewise.
gcc/rust/ChangeLog:

	* ast/rust-desugar-for-loops.cc: New file.
	* ast/rust-desugar-for-loops.h: New file.
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Make lowering of for-loops an
	unreachable.
	* Make-lang.in: Compile it.

gcc/testsuite/ChangeLog:

	* rust/compile/for-loop1.rs: New test.
	* rust/compile/for-loop2.rs: New test.
	* rust/execute/torture/for-loop1.rs: New test.
	* rust/execute/torture/for-loop2.rs: New test.
	* rust/compile/nr2/exclude: Exclude for-loop1.rs
gcc/rust/ChangeLog:

	* rust-session-manager.cc (Session::compile_crate): Call the visitor.
@CohenArthur CohenArthur enabled auto-merge February 4, 2025 14:43
@CohenArthur CohenArthur added this pull request to the merge queue Feb 4, 2025
Merged via the queue into Rust-GCC:master with commit 5fb06c9 Feb 4, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants