rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.79k stars 12.66k forks source link

llvm lint: 0-div in flatmappy code: issue-49685 #75840

Open matthiaskrgr opened 4 years ago

matthiaskrgr commented 4 years ago

This code from src/test/ui/issues/issue-49685.rs

// run-pass
// Regression test for #49685: drop elaboration was not revealing the
// value of `impl Trait` returns, leading to an ICE.

fn main() {
    let _ = Some(())
        .into_iter()
        .flat_map(|_| Some(()).into_iter().flat_map(func));
}

fn func(_: ()) -> impl Iterator<Item = ()> {
    Some(()).into_iter().flat_map(|_| vec![])
}

Causes the following llvm lint message

Undefined behavior: Division by zero
  %21 = sdiv exact i64 %20, 0

when checked with ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc src/test/ui/issues/issue-49685.rs -Cpasses=lint

repo @ 2342cc33333d0e87f692cf0b95b762743c74324d

tmiasko commented 4 years ago

Lint pass "assumes all code will be executed". This particular division is preceded by an assert and as a result it is unreachable:

https://github.com/rust-lang/rust/blob/9d606d939a61c2f4c7bb4d89d959b60a53f50241/library/core/src/ptr/const_ptr.rs#L371-L374

nikic commented 4 years ago

Yeah, I don't think there's anything actionable here.

tmiasko commented 4 years ago

The offset_from itself is referenced from size_hint but again protected by ZST check, so neither is there an issue at Rust level:

https://github.com/rust-lang/rust/blob/ceedf1d5febd65b012b8bcd513d70a0a6a091210/library/alloc/src/vec.rs#L2722-L2730

cuviper commented 4 years ago

Couldn't we just emit a constant like 0 or isize::MIN for ZST intrinsics::ptr_offset_from? Sure it's unreachable, but we might as well give LLVM something trivial to optimize away, rather than an instruction that will trigger lints.