Panic related strings are still in binary with custom panic_fmt · Issue #47526 · rust-lang/rust (original) (raw)

It seems that custom panic_fmt that doesn't touches Arguments doesnt not help to strip all panic-related strings from final binary while compiling to wasm (either emscripten or wasm32-unknown-unknown)

#![feature(lang_items)] #![no_std] #![no_main]

extern "C" { fn halt(); }

#[no_mangle] #[lang = "panic_fmt"] pub extern "C" fn panic_fmt( _args: ::core::fmt::Arguments, _file: &'static str, _line: u32, _col: u32, ) -> ! { loop { unsafe { halt() } } }

#[lang = "eh_personality"] extern fn eh_personality() {}

#[no_mangle] pub fn call(descriptor: u8) -> u8 { assert!(descriptor > 0); descriptor }

Invocation:
rustc --target=wasm32-unknown-emscripten --emit llvm-ir -C lto -C opt-level=3 src/main.rs

$ rustc --version
rustc 1.24.0-nightly (1956d5535 2017-12-03)

this produces the following LLVM IR.

The problem is that panic_fmt is not using it's _args, so it is a dead arg. However, despite this, strings for panic messages are still end up in the LLVM IR}.

It seems that running opt -deadargelim -globaldce helps to strip this strings.