From 92459e507db903f30cf3de3391f2cf3029c866cc Mon Sep 17 00:00:00 2001 From: fw Date: Thu, 11 May 2023 18:40:22 -0400 Subject: [PATCH 1/2] transpile: do not panic in `translate_failure` with `--fail-on-error` it might seem like panicking here would speed up debugging, because it would provide a backtrace directly to something approximating the root cause of the translation failure. sadly, this is not at all the case: `translate_failure` is only called from the top-level `translator::translate` function, which decides whether to call it based on whether the more interesting functions such as `convert_decl` and `convert_main` return `Err(_)`. by the time `translate_failure` runs, the interesting functions have returned and the only relevant info is in the error string that we print *before* panicking. as such, the backtraces always look like this: thread 'main' panicked at 'Translation failed, see error above', c2rust-transpile/src/translator/mod.rs:486:9 stack backtrace: 0: rust_begin_unwind at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/std/src/panicking.rs:584:5 1: core::panicking::panic_fmt at /rustc/d394408fb38c4de61f765a3ed5189d2731a1da91/library/core/src/panicking.rs:142:14 2: c2rust_transpile::translator::translate 3: c2rust_transpile::transpile_single 4: as core::iter::traits::iterator::Iterator>::fold 5: as alloc::vec::spec_from_iter::SpecFromIter>::from_iter 6: c2rust_transpile::transpile 7: c2rust_transpile::main this does not bring joy. we should just exit(1) here. --- c2rust-transpile/src/translator/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index d269e023f1..6d7d1b77da 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -483,7 +483,8 @@ fn clean_path(mod_names: &RefCell>, path: Option<&path pub fn translate_failure(tcfg: &TranspilerConfig, msg: &str) { error!("{}", msg); if tcfg.fail_on_error { - panic!("Translation failed, see error above"); + error!("Translation failed, exiting"); + std::process::exit(1); } } From 5625362725ee4c6014fe23f1e08b8aa46d446dac Mon Sep 17 00:00:00 2001 From: fw Date: Thu, 11 May 2023 18:46:02 -0400 Subject: [PATCH 2/2] test_translator.py: pass `--fail-on-error` this makes tests fail at the translation step rather than when C compilation fails due to the failed translation step --- scripts/test_translator.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/test_translator.py b/scripts/test_translator.py index ef1c8d675e..9aff69a980 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -86,6 +86,9 @@ def translate(self, cc_db: str, ld_lib_path: str, extra_args: List[str] = []) -> "--overwrite-existing", ] + # return nonzero if translation fails + args.append("--fail-on-error") + if self.disable_incremental_relooper: args.append("--no-incremental-relooper") if self.disallow_current_block: