Add goto support in libcc2rs-macros#161
Merged
Merged
Conversation
Make the statements inside goto_block printable, i.e. change form label
=> {} to label: {}
nunoplopes
pushed a commit
that referenced
this pull request
Jun 3, 2026
Depends on #161. See #161 for a more detailed description of how goto is implemented. On the codegen part, this PR emits the goto_block and goto macros when it hits functions that use labels and gotos internally. It also hoists local variables at the start of the function so that all goto_block arms can see and use the local variables.
lucic71
added a commit
to lucic71/cpp2rust
that referenced
this pull request
Jun 4, 2026
Depends on Cpp2Rust#161. See Cpp2Rust#161 for a more detailed description of how goto is implemented. On the codegen part, this PR emits the goto_block and goto macros when it hits functions that use labels and gotos internally. It also hoists local variables at the start of the function so that all goto_block arms can see and use the local variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds support for writing goto and goto_block in the following form:
This is equivalent to the following C code:
goto_block is already used by the switch-with-fallthrough macro internally. I modified its syntax from a list of
label = { ... }tolabel: { ... }so that rustfmt can format it properly.Then, I added GotoRewriter to rewrite each
goto!('label)into{ __s = <target index>; continue '__sm; }. Whenever GotoStateMachine hits a goto macro, it calls GotoRewriter to generate the following state machine:Finally, to support nested goto_blocks, for example a switch inside a goto_block, I added StateMachineNames which gives a unique name to each state machine so that each goto targets the correct state machine.
After this PR is merged, I will add the codegen counterpart in the
cpp2rust/dir.A few limitations about how goto will work:
panic!("ub: non-void function does not return a value");