rust-lang / rust

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

[ER] Attempt to divide by zero unconditional panic in const generic function #76435

Closed leonardo-m closed 9 months ago

leonardo-m commented 4 years ago

This is an enhancement request. This code gives no warnings nor errors in rustc (1.48.0-nightly 73dc675b9 2020-09-06):

#![feature(min_const_generics)]
#![allow(unused_variables)]

fn foo<const Y: u32>(x: u32) -> u32 {
    x / Y
}

fn main() {
    const Z: u32 = 0;
    let x = 10 / Z;
    let y = foo::<Z>(10);
}

Clippy gives one error:

error: this operation will panic at runtime
  --> src/main.rs:10:13
   |
10 |     let x = 10 / Z;
   |             ^^^^^^ attempt to divide 10_u32 by zero
   |
   = note: `#[deny(unconditional_panic)]` on by default

But I'd like rustc to give two errors, both the error given by Clippy and the similar error inside the const generic.

pickfire commented 4 years ago

rustc does give an error, I just tried it in playground. https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=3738d9497ef1eb7f17534a96d45bfb0e

But still, rustc shouldn't give clippy error which needs to run clippy.

tesuji commented 4 years ago

what do you mean? In playground it is panic: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=5d058b58ab0a07db5ec2143c6e0a45c3

tesuji commented 4 years ago

also, the clippy error is actually a rustc error. lints in clippy start with clippy::, like clippy::<lint_name>.

pickfire commented 4 years ago

Oh, how come @lzutao your comment was 10 seconds (mine at 15 seconds) then suddenly became 20 seconds even though I post it first but your timing was faster?

leonardo-m commented 4 years ago

Oh, thank you, then this is just about extending that to const generics.

jonas-schievink commented 4 years ago

We don't do any post-monomorphization constant evaluation, so perhaps this should be closed as wontfix?