Skip to content

Add goto support in libcc2rs-macros#161

Merged
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:goto_block
Jun 1, 2026
Merged

Add goto support in libcc2rs-macros#161
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:goto_block

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 31, 2026

This PR adds support for writing goto and goto_block in the following form:

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:

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:

{
    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");

lucic71 added 4 commits May 31, 2026 21:46
Make the statements inside goto_block printable, i.e. change form label
=> {} to label: {}
@nunoplopes nunoplopes merged commit 77f486e into Cpp2Rust:master Jun 1, 2026
9 checks passed
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.
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.

2 participants