Skip to content

Instantly share code, notes, and snippets.

@mickvangelderen
Last active February 22, 2018 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickvangelderen/ca8c55b0355128ba129d46dfd47b8aac to your computer and use it in GitHub Desktop.
Save mickvangelderen/ca8c55b0355128ba129d46dfd47b8aac to your computer and use it in GitHub Desktop.
Does the compiler remove unreachable code? Compile with `rustc -C opt-level=2 --emit asm remove-unreachable-code.rs`.
#[no_mangle]
#[inline(never)]
pub fn on_change(x: u32) {
println!("x is now {}", x);
}
struct X(u32);
impl X {
fn change(&mut self, y: u32) {
if self.0 != y {
self.0 = y;
on_change(self.0);
}
}
}
#[no_mangle]
#[inline(never)]
pub fn experiment() {
let mut x = X(11111);
// x is 11111, should enter if.
x.change(22222);
// x is now 22222, should not enter if.
x.change(22222);
// x is still 22222, should enter if.
x.change(33333);
// x is now 33333, should not enter if.
x.change(33333);
// this should all be compiled and optimized down to
// on_change(22222);
// on_change(33333);
}
fn main() {
experiment()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment