Skip to content

transpile: Translate enums in switch as their own type instead of integers - #1817

Merged
ahomescu merged 5 commits into
immunant:masterfrom
Rua:match-enum
Aug 21, 2026
Merged

transpile: Translate enums in switch as their own type instead of integers#1817
ahomescu merged 5 commits into
immunant:masterfrom
Rua:match-enum

Conversation

@Rua

@Rua Rua commented May 21, 2026

Copy link
Copy Markdown
Contributor

Enum constant names are now preserved in match arms where present, which significantly improves code fidelity.

@ahomescu ahomescu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a rebase, but CI should pass now.

@Rua
Rua force-pushed the match-enum branch 4 times, most recently from 84cf432 to eb10415 Compare August 6, 2026 20:04
@ahomescu

ahomescu commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I think there might be one codegen bug here: for this C code

enum Bar { BarN1 = -1, Bar0, Bar1 };   // integral type: int
void f(int x) { enum Bar b = x; }

the transpiled Rust code fails to compile. Let me know if you reproduce it.

@Rua
Rua force-pushed the match-enum branch 2 times, most recently from a896bfd to fd1a4f7 Compare August 7, 2026 08:16
@Rua

Rua commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Yeah I could reproduce it. Fixed now, I hope.

@Rua
Rua force-pushed the match-enum branch 3 times, most recently from af4031b to 9c0e27d Compare August 19, 2026 17:52
@ahomescu

Copy link
Copy Markdown
Contributor

This needs another rebase.

@ahomescu ahomescu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs rebase.

@Rua
Rua marked this pull request as ready for review August 20, 2026 09:23

@fw-immunant fw-immunant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few small nits.

The first is commented inline on transpile: Add expected type for enums in `SwitchCases`­, and the other is that I'm not a big fan of transpile: Make `target_type_id` parameter of `convert_expr_with_cast` optional: it adds a branch to the function that makes the function not do what it says on the tin, which isn't what any of the existing callers want. Where transpile: Translate enums in `switch` as their own type instead of integers changes calls from convert_expr to the new convert_expr_with_cast, I'd prefer if it changed them to a new function (e.g. "convert_expr_possibly_casting" or such) that does that dispatch to either convert_expr or the old convert_expr_with_cast.

Otherwise, this LGTM and is a really clean idiomaticity win. Thanks for taking the numerous steps to make this improved translation happen!

This also seems to want a rebase now.

Comment thread c2rust-transpile/src/cfg/mod.rs
@Rua

Rua commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

I've now added convert_expr_with_optional_cast instead.

@fw-immunant fw-immunant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great now, thanks!

@ahomescu
ahomescu merged commit 8b3d221 into immunant:master Aug 21, 2026
11 checks passed
@Rua
Rua deleted the match-enum branch August 22, 2026 08:11
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.

case statements do not preserve enum constants

3 participants