Skip to content

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

Open
Islam-Imad wants to merge 1 commit intoRust-GCC:masterfrom
Islam-Imad:fix/while-loop-label-break
Open

Improve resolution and compilation of control flow loops and labels#4522
Islam-Imad wants to merge 1 commit intoRust-GCC:masterfrom
Islam-Imad:fix/while-loop-label-break

Conversation

@Islam-Imad
Copy link
Copy Markdown
Contributor

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

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 ?

This patch 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: Push, Pop, Peek different labels.
	* backend/rust-compile-expr.cc (CompileExpr::visit): Fix WhileLoopExpr, LoopExpr.
	(CompileExpr::construct_loop_labels): Construct begin and end labels for loop.
	* backend/rust-compile-expr.h (RUST_COMPILE_EXPR): Construct loop header.
	* resolve/rust-default-resolver.cc (DefaultResolver::visit): Fix labels.
	* resolve/rust-default-resolver.h: Fix labels.
	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): 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 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
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

2 participants