The following file, when compiled without optimizations, correctly generates an overflow check in add_one:
#![feature(no_core, lang_items, rustc_attrs)]#![no_core]#![crate_type="lib"]#[lang="sized"]traitSized{}#[lang="add"]traitAdd<RHS=Self>{typeOutput;fnadd(self,rhs:RHS) -> Self::Output;}implAddfori32{typeOutput = i32;#[inline]#[rustc_inherit_overflow_checks]fnadd(self,other:i32) -> i32{self + other }}#[cold]#[inline(never)]// this is the slow path, always#[lang = "panic"]pubfnpanic(_expr_file_line:&(&'staticstr,&'staticstr,u32)) -> ! {loop{}}#[lang="copy"]traitCopy{}pubfnadd_one(x:i32) -> i32{
x + 1}However, compiling with optimizations plus -C debug-assertions=on or -Z force-overflow-checks=on generates code without the overflow check.
However, if I move add_one to a separate no_core crate like this, the issue disappears and the overflow check is generated:
#![feature(no_core)]#![no_core]#![crate_type="lib"]externcrate minimal;pubfnadd_one(x:i32) -> i32{
x + 1}
The following file, when compiled without optimizations, correctly generates an overflow check in
add_one:However, compiling with optimizations plus
-C debug-assertions=onor-Z force-overflow-checks=ongenerates code without the overflow check.However, if I move
add_oneto a separate no_core crate like this, the issue disappears and the overflow check is generated: