transpile: Improve case translation - #1776
Conversation
e63622e to
8eca20d
Compare
is_pattern to ExprContext for customized case translationcase translation
e437818 to
a0d125a
Compare
15a09e8 to
698955d
Compare
f0597ef to
8d67aeb
Compare
| })) | ||
| } | ||
|
|
||
| Expr::Lit(ExprLit { attrs, lit }) => Ok(Pat::Lit(PatLit { attrs, lit })), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I've done that now, and added comments about the type aliases.
| } | ||
| } | ||
|
|
||
| fn expr_to_pat(expr: Expr) -> Result<Pat, String> { |
There was a problem hiding this comment.
These aren't exactly CFG-specific, maybe they should go in rust_ast or c2rust-ast-builder?
There was a problem hiding this comment.
I've moved it to rust_ast.
| PatStruct, PatTuple, PatTupleStruct, UnOp, | ||
| }; | ||
|
|
||
| match expr { |
There was a problem hiding this comment.
There is some speculative generality here which should be fine for this patch, but generally is better avoided.
| return Err("`ExprUnary::expr` is not an `ExprLit` with `lit: Lit::Int`".into()); | ||
| }; | ||
|
|
||
| let repr = format!("-{}{}", lit_int.base10_digits(), lit_int.suffix()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I've added a check so that it catches that.
627d579 to
d369200
Compare
can_propagate_castfunction, elide some more casts #1770i32#1774Translating
casestatements is currently a very ad-hoc affair, so this is an attempt to make it a bit more generic and flexible.expr_to_patnow converts expressions to equivalent patterns, preserving the semantics and returningNoneif an expression with no equivalent is encountered.Translation functions can now use
is_patternto 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 theConstIntExprvalue that's provided inside theCasevalue.Additionally, it seems that it fixes a missing macro expansion in
macrocase.c, so that's a bonus!