Skip to content

Implement Block Label.#4535

Merged
P-E-P merged 1 commit into
Rust-GCC:masterfrom
Islam-Imad:implement/block-label
May 24, 2026
Merged

Implement Block Label.#4535
P-E-P merged 1 commit into
Rust-GCC:masterfrom
Islam-Imad:implement/block-label

Conversation

@Islam-Imad

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

Copy link
Copy Markdown
Contributor

implement block label.

  • add a label after the end of the block.
  • when we hit a break with a block label we assign a value to block temp var and goto the end of the block.

closes #4534

@Islam-Imad Islam-Imad marked this pull request as draft April 21, 2026 13:23
@dkm

dkm commented Apr 21, 2026

Copy link
Copy Markdown
Member

Hello!

Thanks for this new contribution :)
We're going to try and have more descriptive commit log from now on, when that makes sense. It would be nice in this case to add extra text in the commit log so that readers can have a better idea of the problem you're handling, how you do that, optional issue reference, etc.

Thanks again!

@Islam-Imad Islam-Imad force-pushed the implement/block-label branch 3 times, most recently from f5a2a7f to f379578 Compare April 21, 2026 20:02
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

Hello!

Thanks for this new contribution :) We're going to try and have more descriptive commit log from now on, when that makes sense. It would be nice in this case to add extra text in the commit log so that readers can have a better idea of the problem you're handling, how you do that, optional issue reference, etc.

Thanks again!

I have update the commit message and cleanup the remaining stuff :)

thanks Marc

@Islam-Imad Islam-Imad marked this pull request as ready for review April 21, 2026 20:12
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

ping ?

@CohenArthur CohenArthur left a comment

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.

thanks for the work @Islam-Imad :) a couple comments but it looks good to me. I'll leave @philberty to review it as this touches the backend

Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/testsuite/rust/execute/cf-labeled-block.rs Outdated
@Islam-Imad Islam-Imad force-pushed the implement/block-label branch 2 times, most recently from 2db5b18 to 5982607 Compare April 28, 2026 12:43

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

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 looks quite good given the amount of changes and their nature. I've put a few comments here and there but this is a great start!

Comment thread gcc/rust/backend/rust-compile-context.h Outdated
std::map<HirId, tree> compiled_fn_map;
std::map<HirId, tree> compiled_consts;
std::map<HirId, tree> compiled_labels;
std::map<HirId, Bvariable *> compiled_temp_vars;

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.

It just occurred to me that we're using map here but we should probably use unordered_map instead. I don't think we care about the HirdId order. It does not need to be fixed within that PR. We should probably open an issue instead.

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 i agree with you.
The access time will be O(1) instead of O(logn) which is away more faster.
i will open an issue for it and i think it should be good first.

Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
Comment thread gcc/rust/backend/rust-compile-context.h Outdated
Comment thread gcc/rust/backend/rust-compile-expr.cc Outdated
This change implements backend lowering support for Rust labeled blocks.

Previously, labeled blocks were rejected in "CompileExpr::visit(BlockExpr)"
as unsupported. With this patch, labeled blocks are lowered by introducing
the following :-

1. A backend "LABEL_DECL" used as the jump target for "break 'label".
2. A temporary "Bvariable" used to hold the block’s resulting value.

gcc/rust/ChangeLog:

	* backend/rust-compile-expr.cc (CompileExpr::visit): Lower labeled block.
	(CompileExpr::construct_block_label): Utility function to construct block label.
	(CompileExpr::lookup_label): Utility function to lookup label.
	(CompileExpr::lookup_temp_var): Utility function to lookup block temp variables.
	(CompileExpr::resolve_util): Utility to resolve NodeId to HirId.
	* backend/rust-compile-expr.h: Header functions.
	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Fix label resolution.

gcc/testsuite/ChangeLog:

	* rust/execute/cf-labeled-block.rs: New test.

Signed-off-by: Islam-Imad <islamimad404@gmail.com>
@Islam-Imad Islam-Imad force-pushed the implement/block-label branch from 5982607 to 50a94d3 Compare May 6, 2026 17:39
@Islam-Imad Islam-Imad requested a review from P-E-P May 7, 2026 11:46
@Islam-Imad

Copy link
Copy Markdown
Contributor Author

ping ?

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

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.

Looks great!

@P-E-P P-E-P added this pull request to the merge queue May 24, 2026
Merged via the queue into Rust-GCC:master with commit b2db938 May 24, 2026
12 checks passed
@P-E-P P-E-P mentioned this pull request May 29, 2026
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.

[backend] cannot compile labeled block.

4 participants