Repository navigation
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.
joaotgouveia
pushed a commit
to joaotgouveia/cpp2rust
that referenced
this pull request
Oct 27, 2026
This PR adds support for writing goto and goto_block in the following
form:
```rs
let mut ret: i32 = 0;
goto_block!({
'__entry: {
if n < 0 {
ret = -1;
goto!('out);
}
ret = 100;
}
'out: {
return ret;
}
});
```
This is equivalent to the following C code:
```c
int f() {
int ret;
if (n < 0) {
ret = -1;
goto out;
}
ret = 100;
out:
return ret;
}
```
goto_block is already used by the switch-with-fallthrough macro
internally. I modified its syntax from a list of `label = { ... }` to
`label: { ... }` 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:
```rs
{
let mut __s: u32 = 0;
'__sm: loop {
match __s {
0u32 => {
if n < 0 {
ret = -1;
__s = 1; continue '__sm; // <- was goto!('out)
}
ret = 100;
__s = 1; continue '__sm; // fall-through to next arm
}
1u32 => {
return ret;
break '__sm; // last arm: exit
}
_ => break '__sm, // match exhaustiveness
}
}
}
```
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:
* local variables of a function that contains goto will be hoisted
outside the goto_block so that all arms of the goto_block macro can see
them. c2rust also does this. All hoisted variables will be default
initialized.
* the rust compiler cannot prove that all paths of the code inside the
loop of the state machine return. I reality the loop always returns
because the C behavior is preserved. To suppress the rustc error, I
added a panic at the end of the function: `panic!("ub: non-void function
does not return a value");`
joaotgouveia
pushed a commit
to joaotgouveia/cpp2rust
that referenced
this pull request
Oct 27, 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");