Last active
February 22, 2018 09:58
-
-
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`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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