Skip to content

Commit 80842e4

Browse files
committed
transpile: Strip tail return only when transpiling a function
1 parent 6921f53 commit 80842e4

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

c2rust-transpile/src/cfg/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,13 +2114,8 @@ impl CfgBuilder {
21142114
};
21152115

21162116
// Run relooper
2117-
let mut stmts = translator.convert_cfg(
2118-
&format!("<substmt_{:?}>", stmt_id),
2119-
graph,
2120-
store,
2121-
live_in,
2122-
false,
2123-
)?;
2117+
let mut stmts =
2118+
translator.convert_cfg(&format!("<substmt_{:?}>", stmt_id), graph, store, live_in)?;
21242119

21252120
let inner_span = stmts.first().map(|stmt| stmt.span());
21262121

c2rust-transpile/src/cfg/structures.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::*;
44
use log::warn;
5-
use syn::{spanned::Spanned as _, ExprBreak, ExprIf, ExprReturn, ExprUnary, Stmt};
5+
use syn::{spanned::Spanned as _, ExprBreak, ExprIf, ExprUnary, Stmt};
66

77
use crate::rust_ast::{comment_store, set_span::SetSpan, BytePos, SpanExt};
88

@@ -12,7 +12,6 @@ pub fn structured_cfg(
1212
comment_store: &mut comment_store::CommentStore,
1313
current_block: Box<Expr>,
1414
debug_labels: bool,
15-
cut_out_trailing_ret: bool,
1615
) -> TranslationResult<Vec<Stmt>> {
1716
let ast: StructuredAST<Box<Expr>, Pat, Label, Stmt> =
1817
structured_cfg_help(vec![], &IndexSet::new(), root, &mut IndexSet::new())?;
@@ -21,15 +20,7 @@ pub fn structured_cfg(
2120
debug_labels,
2221
current_block,
2322
};
24-
let (mut stmts, _span) = s.to_stmt(ast, comment_store);
25-
26-
// If the very last statement in the vector is a `return`, we can either cut it out or replace
27-
// it with the returned value.
28-
if cut_out_trailing_ret {
29-
if let Some(Stmt::Expr(Expr::Return(ExprReturn { expr: None, .. }), _)) = stmts.last() {
30-
stmts.pop();
31-
}
32-
}
23+
let (stmts, _span) = s.to_stmt(ast, comment_store);
3324

3425
Ok(stmts)
3526
}

c2rust-transpile/src/translator/mod.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use proc_macro2::{Punct, Spacing::*, Span, TokenStream, TokenTree};
1414
use syn::spanned::Spanned as _;
1515
use syn::{
1616
AttrStyle, BareVariadic, Block, Expr, ExprBinary, ExprBlock, ExprBreak, ExprCast, ExprField,
17-
ExprIndex, ExprParen, ExprUnary, FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro,
18-
ForeignItemStatic, ForeignItemType, Ident, Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn,
19-
ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait,
20-
ItemTraitAlias, ItemType, ItemUnion, ItemUse, Lit, MacroDelimiter, PathSegment, ReturnType,
21-
Stmt, Type, TypeTuple, UseTree, Visibility,
17+
ExprIndex, ExprParen, ExprReturn, ExprUnary, FnArg, ForeignItem, ForeignItemFn,
18+
ForeignItemMacro, ForeignItemStatic, ForeignItemType, Ident, Item, ItemConst, ItemEnum,
19+
ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct,
20+
ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Lit, MacroDelimiter, PathSegment,
21+
ReturnType, Stmt, Type, TypeTuple, UseTree, Visibility,
2222
};
2323
use syn::{BinOp, UnOp}; // To override `c_ast::{BinOp,UnOp}` from glob import.
2424

@@ -2404,6 +2404,7 @@ impl<'c> Translation<'c> {
24042404
};
24052405
let mut converted_body =
24062406
self.convert_function_body(ctx, name, body_ids, return_type, ret)?;
2407+
strip_tail_return(&mut converted_body);
24072408

24082409
// If `alloca` was used in the function body, include a variable to hold the
24092410
// allocations.
@@ -2520,7 +2521,6 @@ impl<'c> Translation<'c> {
25202521
graph: cfg::Cfg<cfg::Label, cfg::StmtOrDecl>,
25212522
store: cfg::DeclStmtStore,
25222523
live_in: IndexSet<CDeclId>,
2523-
cut_out_trailing_ret: bool,
25242524
) -> TranslationResult<Vec<Stmt>> {
25252525
if self.tcfg.dump_function_cfgs {
25262526
graph
@@ -2582,7 +2582,6 @@ impl<'c> Translation<'c> {
25822582
&mut self.comment_store.borrow_mut(),
25832583
current_block,
25842584
self.tcfg.debug_relooper_labels,
2585-
cut_out_trailing_ret,
25862585
)?);
25872586
Ok(stmts)
25882587
}
@@ -2598,7 +2597,7 @@ impl<'c> Translation<'c> {
25982597
// Function body scope
25992598
self.with_scope(|| {
26002599
let (graph, store) = cfg::Cfg::from_stmts(self, ctx, body_ids, ret, ret_ty)?;
2601-
self.convert_cfg(name, graph, store, IndexSet::new(), true)
2600+
self.convert_cfg(name, graph, store, IndexSet::new())
26022601
})
26032602
}
26042603

@@ -5266,3 +5265,11 @@ impl<'c> Translation<'c> {
52665265
}
52675266
}
52685267
}
5268+
5269+
fn strip_tail_return(stmts: &mut Vec<Stmt>) {
5270+
// If the very last statement in the vector is a `return`, we can either cut it out or replace
5271+
// it with the returned value.
5272+
if let Some(Stmt::Expr(Expr::Return(ExprReturn { expr: None, .. }), _)) = stmts.last() {
5273+
stmts.pop();
5274+
}
5275+
}

c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ pub unsafe extern "C" fn compound_literal() {
6161
#[no_mangle]
6262
pub unsafe extern "C" fn statement_expr() {
6363
puts(b"should execute\0" as *const u8 as *const ::core::ffi::c_char);
64+
return;
6465
puts(b"should be unreachable!\0" as *const u8 as *const ::core::ffi::c_char);
6566
}

0 commit comments

Comments
 (0)