Skip to content

#[no_core] loses overflow checks with optimizations #38136

Closed
@hanna-kruppe

Description

@hanna-kruppe

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"]
trait Sized {}

#[lang="add"]
trait Add<RHS=Self> {
    type Output;
    fn add(self, rhs: RHS) -> Self::Output;
}

impl Add for i32 {
    type Output = i32;

    #[inline]
    #[rustc_inherit_overflow_checks]
    fn add(self, other: i32) -> i32 { self + other }
}

#[cold] #[inline(never)] // this is the slow path, always
#[lang = "panic"]
pub fn panic(_expr_file_line: &(&'static str, &'static str, u32)) -> ! {
	loop {}
}

#[lang="copy"]
trait Copy {}

pub fn add_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"]
extern crate minimal;

pub fn add_one(x: i32) -> i32 {
  x + 1
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions