Skip to content

transpile: Improve case translation - #1776

Open
Rua wants to merge 8 commits into
immunant:masterfrom
Rua:pattern-conversion
Open

transpile: Improve case translation#1776
Rua wants to merge 8 commits into
immunant:masterfrom
Rua:pattern-conversion

Conversation

@Rua

@Rua Rua commented May 4, 2026

Copy link
Copy Markdown
Contributor

Translating case statements is currently a very ad-hoc affair, so this is an attempt to make it a bit more generic and flexible. expr_to_pat now converts expressions to equivalent patterns, preserving the semantics and returning None if an expression with no equivalent is encountered.

Translation functions can now use is_pattern to detect if they are translating an expression that's meant to appear inside a Rust pattern, and adjust accordingly. That will help towards getting #1775 to work. Currently, all expressions that cannot (conservatively) be translated in pattern context return a translation error. That will then trigger fallback to directly using the ConstIntExpr value that's provided inside the Case value.

Additionally, it seems that it fixes a missing macro expansion in macrocase.c, so that's a bonus!

@Rua
Rua force-pushed the pattern-conversion branch 8 times, most recently from e63622e to 8eca20d Compare May 5, 2026 12:25
@Rua Rua changed the title transpile: Add is_pattern to ExprContext for customized case translation transpile: Improve case translation May 5, 2026
@Rua
Rua force-pushed the pattern-conversion branch 3 times, most recently from e437818 to a0d125a Compare May 5, 2026 13:59
@Rua
Rua force-pushed the pattern-conversion branch 8 times, most recently from 15a09e8 to 698955d Compare May 21, 2026 18:30
@Rua
Rua force-pushed the pattern-conversion branch 3 times, most recently from f0597ef to 8d67aeb Compare August 6, 2026 10:14
@Rua
Rua force-pushed the pattern-conversion branch from 8d67aeb to a688900 Compare August 6, 2026 10:16
@Rua
Rua marked this pull request as ready for review August 6, 2026 10:23
@Rua
Rua force-pushed the pattern-conversion branch from a688900 to 627d579 Compare August 6, 2026 10:32
Comment thread c2rust-transpile/src/cfg/mod.rs Outdated
}))
}

Expr::Lit(ExprLit { attrs, lit }) => Ok(Pat::Lit(PatLit { attrs, lit })),

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.

According to https://github.com/dtolnay/syn/blob/master/src/pat.rs#L12 some of these are just aliases, e.g., ExprLit as PatLit. You could simplify some of these arms.

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.

I've done that now, and added comments about the type aliases.

Comment thread c2rust-transpile/src/cfg/mod.rs Outdated
}
}

fn expr_to_pat(expr: Expr) -> Result<Pat, String> {

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.

These aren't exactly CFG-specific, maybe they should go in rust_ast or c2rust-ast-builder?

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.

I've moved it to rust_ast.

Comment thread c2rust-transpile/src/cfg/mod.rs Outdated
PatStruct, PatTuple, PatTupleStruct, UnOp,
};

match expr {

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.

There is some speculative generality here which should be fine for this patch, but generally is better avoided.

Comment thread c2rust-transpile/src/cfg/mod.rs Outdated
return Err("`ExprUnary::expr` is not an `ExprLit` with `lit: Lit::Int`".into());
};

let repr = format!("-{}{}", lit_int.base10_digits(), lit_int.suffix());

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.

If we ever pass a negative lit_int here, that will produce a double negative e.g. --1 which is invalid Rust. It might be worth adding a comment or something.

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.

I've added a check so that it catches that.

@Rua
Rua force-pushed the pattern-conversion branch from 627d579 to d369200 Compare August 11, 2026 08:34
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