profile
viewpoint

Ask questionsSome closures are not inlined in release mode

Consider the following code (playground):

fn main() {
    let err = Err(());
    let _: usize = err.unwrap_or_else(|err| err_exit(err));
    unreachable!();
}

fn err_exit(_: ()) -> ! {
    std::process::exit(1);
}

When compiled with rustc 1.36, it gives the following assembly:

core::result::Result<T,E>::unwrap_or_else:
	pushq	%rax
	callq	playground::main::{{closure}}
	ud2

playground::main:
	pushq	%rax
	callq	core::result::Result<T,E>::unwrap_or_else
	ud2

playground::main::{{closure}}:
	pushq	%rax
	callq	playground::err_exit
	ud2

playground::err_exit:
	pushq	%rax
	movl	$1, %edi
	callq	*std::process::exit@GOTPCREL(%rip)
	ud2

Note how the closure is not inlined, even though it would be trivial to do so (replace callq playground::main::{{closure}} with callq playground::err_exit).

rust-lang/rust

Answer questions dotdash

There was already a bug report at https://bugs.llvm.org/show_bug.cgi?id=26495

I commented there with what I found out so far.

useful!

Related questions

Spurious NaNs produced by trig functions with valid inputs on Windows GNU toolchains hot 2
using 'cargo install xsv' on windows 10 triggers rustc internal error hot 1
chain() make collect very slow hot 1
if/while Some(n) = &mut foo sugar will leak a temporary mutable borrow to current scope in particular situation hot 1
build an empty project failed (undefined reference to `__onexitbegin') hot 1
Invalid collision with TryFrom implementation? hot 1
Crater runs for Rust 1.38.0 hot 1
Spurious NaNs produced by trig functions with valid inputs on Windows GNU toolchains hot 1
under latest MinGW, cannot link with C code using stdout hot 1
Archive all nightlies hot 1
Building LLVM with Clang fails hot 1
Internal compiler error: can't buffer lints after HIR lowering hot 1
Tracking issue for `Option::contains` and `Result::contains` hot 1
async fn + rustfmt don't "just work" inside of RLS hot 1
Include images in rustdoc output hot 1
Github User Rank List