Skip to content

Improve resolution and compilation of control flow loops and labels#4522

Merged
P-E-P merged 1 commit into
Rust-GCC:masterfrom
Islam-Imad:fix/while-loop-label-break
Jul 20, 2026
Merged

Improve resolution and compilation of control flow loops and labels#4522
P-E-P merged 1 commit into
Rust-GCC:masterfrom
Islam-Imad:fix/while-loop-label-break

Conversation

@Islam-Imad

@Islam-Imad Islam-Imad commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

This patch fixes and improves the handling of loop expressions, particularly
focusing on break and continue statements within labeled and nested loops.
closes #4521
closes #4530

@Islam-Imad

Copy link
Copy Markdown
Contributor Author

cc @powerboat9

res += i;
i += 1;
if res + i >= 99 {
break 'calculation;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does continue 'calculation work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i don't think because the label now after the loop.
i will check it then ping u for review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

its impossible to create a single label for both continue and break. we have to create two labels one at the start and one at the end. right now i am hacking gcc/go to see how this problem is solved and yeah there is separation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this will require more work to be done since we need to modify [loop,while,continue,break] and a lot of testing also. once its ready i will make it open again.

@Islam-Imad
Islam-Imad marked this pull request as draft April 13, 2026 14:46
@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch from dfc3c94 to d6e32c3 Compare April 19, 2026 20:22
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

@powerboat9 this is the core part of the patch, it still not ready.
i will add more tests, refactor again and remove the debugs.
i will continue explaining the changes in this document also link.
i hope tommorrow to submit the final patch.
also i am not sure about my solution to loop resolve problem.

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch 4 times, most recently from d3de692 to 75570ed Compare April 20, 2026 17:35
@Islam-Imad
Islam-Imad marked this pull request as ready for review April 20, 2026 18:28
@Islam-Imad
Islam-Imad requested a review from powerboat9 April 20, 2026 18:28
@Islam-Imad Islam-Imad changed the title Fix loop label placement in WhileLoopExpr lowering Improve resolution and compilation of control flow loops and labels Apr 20, 2026
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

ping ?

@powerboat9

Copy link
Copy Markdown
Collaborator

Could you squash the commits?

@Islam-Imad

Copy link
Copy Markdown
Contributor Author

ok i will but i am asking if there something i should i update or not ?

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch from 75570ed to 426f0bb Compare April 26, 2026 17:18
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

Could you squash the commits?

done :)

@Islam-Imad

Copy link
Copy Markdown
Contributor Author

ping ?

@P-E-P
P-E-P requested review from P-E-P and philberty April 30, 2026 14:03
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

is there any feedback on this ?

Comment thread gcc/rust/backend/rust-compile-context.h Outdated
compiled_break_labels[id] = label;
}

bool lookup_break_label (HirId id, tree *label)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Even if the old API uses an out pointer with a bool return value most of the new APIs try to use optional when available.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Even if the old API uses an out pointer with a bool return value most of the new APIs try to use optional when available.

i suggest to open a new PR to refactor the old APIs. what do u think ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes that would be a good idea :) do you want to work on that?

@Islam-Imad Islam-Imad Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes just mention this comment in an issue and assign it to me.

Comment thread gcc/rust/backend/rust-compile-context.h Outdated
compiled_continue_labels[id] = label;
}

bool lookup_continue_label (HirId id, tree *label)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Likewise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch from 426f0bb to 7383411 Compare July 6, 2026 06:43
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

CC @P-E-P @powerboat9

@dkm

dkm commented Jul 6, 2026

Copy link
Copy Markdown
Member

Drive-by comment on changelog... Looks like the first entry is not correct:

	* backend/rust-compile-context.h: Push, Pop, Peek different labels.

As you're adding a bunch of new functions, I think there should be entries like:

	* backend/rust-compile-context.h (bla): New function.

What do you think?

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch 2 times, most recently from 027469a to 35596f8 Compare July 6, 2026 11:38
@Islam-Imad

Islam-Imad commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Drive-by comment on changelog... Looks like the first entry is not correct:

	* backend/rust-compile-context.h: Push, Pop, Peek different labels.

As you're adding a bunch of new functions, I think there should be entries like:

	* backend/rust-compile-context.h (bla): New function.

What do you think?

I have used the mklog script :) , i have added entries for the new functions manually.
also updated the changes log to be more clear.

@Islam-Imad
Islam-Imad requested a review from P-E-P July 6, 2026 11:48

tree pop_loop_end_label ()
{
tree pop = loop_end_labels.back ();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is undefined behavior if the vector is empty. Add an assertion that it isn't like you did in peek_loop_end_label

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

std::vector<::std::vector<DropCandidate>> block_drop_candidates;
std::vector<::Bvariable *> loop_value_stack;
std::vector<tree> loop_begin_labels;
std::vector<tree> loop_end_labels;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it make sense to use something like the StackedContexts<T> class here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i think yes but i prefer to open a new refactor PR for compilation-context to make use of it and also update the old APIs.

Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
loop_label.get_lifetime ().get_mappings ().get_hirid (), label);
// Associate the loop's result temporary with the label so that a
// `break 'label value` can locate it (see visit (HIR::BreakExpr)).
loop_label = tl::optional<HIR::LoopLabel> (expr.get_loop_label ());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You don't need the constructor here

Suggested change
loop_label = tl::optional<HIR::LoopLabel> (expr.get_loop_label ());
loop_label = expr.get_loop_label ();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch 2 times, most recently from b73ece7 to 7a55fac Compare July 12, 2026 07:29
@Islam-Imad
Islam-Imad requested a review from CohenArthur July 12, 2026 07:30
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

ci failure ? but its ok on my machine

@powerboat9

Copy link
Copy Markdown
Collaborator

Looks like it's either a gcc5 specific issue, or some undefined behavior is semi-randomly causing problems. Maybe try running with valgrind?

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch from 7a55fac to c2a3c97 Compare July 13, 2026 05:32
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

@powerboat9 i just rerun the CI again and its now green.

'high: loop {
dump_number(3);
break 'mid;
dump_number(3);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would dump another number here to explicitly tell the user were something has gone wrong when the compiler breaks.

break 'mid;
dump_number(3);
}
dump_number(2);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Likewise

@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch from c2a3c97 to 886ef77 Compare July 20, 2026 07:42
This batch fixes and improves the handling of loop expressions, particularly
focusing on `break` and `continue` statements within labeled and nested loops.

gcc/rust/ChangeLog:

	* backend/rust-compile-context.h (RUST_COMPILE_CONTEXT): loop
	labels context functions.
	- insert_break_label
	- lookup_break_label
	- insert_continue_label
	- lookup_continue_label
	- push/pop/peek end_label (similar to begin_label).
	* backend/rust-compile-expr.cc (CompileExpr::visit): handle loop
	labels construction in WhileExpr, ForExpr, ContinueExpr and BreakExpr.
	(CompileExpr::construct_block_label): construct break label
	instead of normal label to unify handling break statement.
	(CompileExpr::construct_loop_labels): construct while/for loops
	labels (continue label before loop, break label after loop).
	* backend/rust-compile-expr.h (RUST_COMPILE_EXPR):
	construct_loop_labels function header.
	* resolve/rust-default-resolver.cc (DefaultResolver::visit): Fix
	labels.
	* resolve/rust-default-resolver.h: Fix labels.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::insert): Fix labels.
	(NameResolutionContext::insert_shadowable): Fix labels.
	(NameResolutionContext::insert_globbed): Fix labels.
	(NameResolutionContext::scoped): Fix labels.

gcc/testsuite/ChangeLog:

	* rust/execute/cf-break-continue.rs: New test.
	* rust/execute/cf-label-shadowing.rs: New test.
	* rust/execute/cf-labeled-break-nested.rs: New test.
	* rust/execute/cf-labeled-continue-nested.rs: New test.
	* rust/execute/cf-labeled-loops.rs: New test.
	* rust/execute/cf-loop-break-continue.rs: New test.
	* rust/execute/cf-mixed-labeled-unlabeled.rs: New test.
	* rust/execute/cf-nested-loops.rs: New test.

Signed-off-by: Islam-Imad <islamimad404@gmail.com>
@Islam-Imad
Islam-Imad force-pushed the fix/while-loop-label-break branch from 886ef77 to 7399903 Compare July 20, 2026 07:47
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

the test is updated @P-E-P

@Islam-Imad

Copy link
Copy Markdown
Contributor Author

cc @powerboat9 @P-E-P

@P-E-P
P-E-P added this pull request to the merge queue Jul 20, 2026
Merged via the queue into Rust-GCC:master with commit bf3ef5f Jul 20, 2026
13 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.

Failed to resolve loop label [backend] bad gimple output in HIR::WhileLoopExpr

5 participants