Skip to content

Commit a6b6694

Browse files
committed
upgrade to syn 2 in c2rust-transpile
1 parent d1cf202 commit a6b6694

File tree

14 files changed

+214
-230
lines changed

14 files changed

+214
-230
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c2rust-transpile/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ serde_json = "1.0"
3636
smallvec = "1.0"
3737
strum = "0.24"
3838
strum_macros = "0.24"
39-
syn = { version = "1.0", features = ["full", "extra-traits", "parsing", "printing"]}
39+
syn = { version = "2.0", features = ["full", "extra-traits", "parsing", "printing"]}
4040
tempfile = "3.5.0"
4141

4242
[features]

c2rust-transpile/src/cfg/inc_cleanup.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl IncCleanup {
3333

3434
let mut removed_tail_expr = false;
3535

36-
if let Stmt::Expr(expr) = &mut stmt {
36+
if let Stmt::Expr(expr, _) = &mut stmt {
3737
match expr {
3838
Expr::If(ExprIf {
3939
cond: _,
@@ -74,7 +74,7 @@ impl IncCleanup {
7474
}
7575

7676
fn is_idempotent_tail_expr(&self, stmt: &Stmt) -> bool {
77-
let tail_expr = if let Stmt::Semi(expr, _token) = stmt {
77+
let tail_expr = if let Stmt::Expr(expr, _token) = stmt {
7878
expr
7979
} else {
8080
return false;
@@ -124,35 +124,44 @@ impl IncCleanup {
124124
/// Remove empty else clauses from if expressions that can arise from
125125
/// removing idempotent statements.
126126
fn cleanup_if(stmt: Stmt) -> Stmt {
127-
if let Stmt::Expr(Expr::If(ExprIf {
128-
cond,
129-
then_branch,
130-
else_branch: Some((token, else_)),
131-
..
132-
})) = &stmt
127+
if let Stmt::Expr(
128+
Expr::If(ExprIf {
129+
cond,
130+
then_branch,
131+
else_branch: Some((token, else_)),
132+
..
133+
}),
134+
_semi,
135+
) = &stmt
133136
{
134137
if let Expr::Block(ExprBlock {
135138
block, label: None, ..
136139
}) = &**else_
137140
{
138141
if block.stmts.is_empty() {
139-
return Stmt::Expr(Expr::If(ExprIf {
140-
cond: cond.clone(),
141-
then_branch: then_branch.clone(),
142-
else_branch: Default::default(),
143-
attrs: Default::default(),
144-
if_token: Default::default(),
145-
}));
146-
} else if block.stmts.len() == 1 {
147-
// flatten nested if expression to else if chain
148-
if let Stmt::Expr(Expr::If(nested_if_expr)) = &block.stmts[0] {
149-
return Stmt::Expr(Expr::If(ExprIf {
142+
return Stmt::Expr(
143+
Expr::If(ExprIf {
150144
cond: cond.clone(),
151145
then_branch: then_branch.clone(),
152-
else_branch: Some((*token, Box::new(Expr::If(nested_if_expr.clone())))),
146+
else_branch: Default::default(),
153147
attrs: Default::default(),
154148
if_token: Default::default(),
155-
}));
149+
}),
150+
None,
151+
);
152+
} else if block.stmts.len() == 1 {
153+
// flatten nested if expression to else if chain
154+
if let Stmt::Expr(Expr::If(nested_if_expr), _semi) = &block.stmts[0] {
155+
return Stmt::Expr(
156+
Expr::If(ExprIf {
157+
cond: cond.clone(),
158+
then_branch: then_branch.clone(),
159+
else_branch: Some((*token, Box::new(Expr::If(nested_if_expr.clone())))),
160+
attrs: Default::default(),
161+
if_token: Default::default(),
162+
}),
163+
None,
164+
);
156165
}
157166
}
158167
}

c2rust-transpile/src/cfg/mod.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::io;
3030
use std::io::Write;
3131
use std::ops::Deref;
3232
use std::ops::Index;
33+
use syn::Lit;
3334
use syn::{spanned::Spanned, Arm, Expr, Pat, Stmt};
3435

3536
use failure::format_err;
@@ -92,6 +93,17 @@ impl Label {
9293
fn to_string_expr(&self) -> Box<Expr> {
9394
mk().lit_expr(self.debug_print())
9495
}
96+
97+
fn to_int_lit(&self) -> Lit {
98+
let mut s = DefaultHasher::new();
99+
self.hash(&mut s);
100+
let as_num = s.finish();
101+
mk().int_lit(as_num as u128, "")
102+
}
103+
104+
fn to_string_lit(&self) -> Lit {
105+
mk().str_lit(&self.debug_print())
106+
}
95107
}
96108

97109
impl Serialize for Label {
@@ -1835,18 +1847,24 @@ impl CfgBuilder {
18351847
.to_pure_expr()
18361848
{
18371849
Some(expr) => match *expr {
1838-
Expr::Lit(..) | Expr::Path(..) => Some(expr),
1850+
Expr::Lit(lit) => Some(mk().lit_pat(lit.lit)),
1851+
Expr::Path(path) => Some(mk().path_pat(path.path, path.qself)),
18391852
_ => None,
18401853
},
18411854
_ => None,
18421855
}
18431856
}
18441857
_ => None,
18451858
};
1846-
let branch = match branch {
1847-
Some(expr) => expr,
1848-
None => translator.convert_constant(cie)?,
1859+
1860+
let pat = match branch {
1861+
Some(pat) => pat,
1862+
None => match cie {
1863+
ConstIntExpr::U(n) => mk().lit_pat(mk().int_unsuffixed_lit(n as u128)),
1864+
ConstIntExpr::I(n) => mk().lit_pat(mk().int_unsuffixed_lit(n as u128)),
1865+
},
18491866
};
1867+
18501868
self.switch_expr_cases
18511869
.last_mut()
18521870
.ok_or_else(|| {
@@ -1856,7 +1874,7 @@ impl CfgBuilder {
18561874
)
18571875
})?
18581876
.cases
1859-
.push((mk().lit_pat(branch), this_label.clone()));
1877+
.push((pat, this_label.clone()));
18601878

18611879
// Sub stmt
18621880
let sub_stmt_next =

c2rust-transpile/src/cfg/structures.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn structured_cfg(
2626
// If the very last statement in the vector is a `return`, we can either cut it out or replace
2727
// it with the returned value.
2828
if cut_out_trailing_ret {
29-
if let Some(Stmt::Expr(ret) | Stmt::Semi(ret, _)) = stmts.last() {
29+
if let Some(Stmt::Expr(ret, _)) = stmts.last() {
3030
if let Expr::Return(ExprReturn { expr: None, .. }) = ret {
3131
stmts.pop();
3232
}
@@ -530,7 +530,7 @@ impl StructureState {
530530
}
531531
(false, false) => {
532532
fn is_expr(kind: &Stmt) -> bool {
533-
matches!(kind, Stmt::Expr(Expr::If(..) | Expr::Block(..)))
533+
matches!(kind, Stmt::Expr(Expr::If(..) | Expr::Block(..), _semi))
534534
}
535535

536536
// Do the else statements contain a single If, IfLet or
@@ -542,7 +542,7 @@ impl StructureState {
542542
let stmt_expr = els_stmts.swap_remove(0);
543543
let stmt_expr_span = stmt_expr.span();
544544
let mut els_expr = match stmt_expr {
545-
Stmt::Expr(e) => e,
545+
Stmt::Expr(e, _semi) => e,
546546
_ => panic!("is_els_expr out of sync"),
547547
};
548548
els_expr.set_span(stmt_expr_span);
@@ -572,12 +572,12 @@ impl StructureState {
572572
.map(|(lbl, stmts)| -> Arm {
573573
let (stmts, stmts_span) = self.to_stmt(stmts, comment_store);
574574

575-
let lbl_expr = if self.debug_labels {
576-
lbl.to_string_expr()
575+
let lbl_lit = if self.debug_labels {
576+
lbl.to_string_lit()
577577
} else {
578-
lbl.to_num_expr()
578+
lbl.to_int_lit()
579579
};
580-
let pat = mk().lit_pat(lbl_expr);
580+
let pat = mk().lit_pat(lbl_lit);
581581
let body = mk().block_expr(mk().span(stmts_span).block(stmts));
582582
mk().arm(pat, None, body)
583583
})
@@ -605,7 +605,7 @@ impl StructureState {
605605
let (body, body_span) = self.to_stmt(*body, comment_store);
606606

607607
// TODO: this is ugly but it needn't be. We are just pattern matching on particular ASTs.
608-
if let Some(stmt @ &Stmt::Expr(ref expr)) = body.first() {
608+
if let Some(stmt @ &Stmt::Expr(ref expr, _semi)) = body.first() {
609609
let stmt_span = stmt.span();
610610
let span = if !stmt_span.is_dummy() {
611611
stmt_span
@@ -619,7 +619,7 @@ impl StructureState {
619619
..
620620
}) = expr
621621
{
622-
if let [Stmt::Semi(
622+
if let [Stmt::Expr(
623623
syn::Expr::Break(ExprBreak {
624624
label: None,
625625
expr: None,

c2rust-transpile/src/convert_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl TypeConverter {
246246
Some(ret) => self.convert(ctxt, ret.ctype)?,
247247
};
248248

249-
let variadic = is_variadic.then(|| mk().variadic_arg(vec![]));
249+
let variadic = is_variadic.then(|| mk().bare_variadic_arg());
250250

251251
let fn_ty = (
252252
barefn_inputs,

c2rust-transpile/src/rust_ast/comment_store.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ pub fn insert_comment_attrs(attrs: &mut Vec<Attribute>, new_comments: SmallVec<[
302302
pound_token: Default::default(),
303303
style: AttrStyle::Inner(Default::default()),
304304
bracket_token: Default::default(),
305-
path: make_comment_path(),
306-
tokens,
305+
meta: Meta::Path(make_comment_path()),
307306
};
308307
attrs.push(attr);
309308
}

0 commit comments

Comments
 (0)