-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-const-genericsArea: const generics (parameters and arguments)Area: const generics (parameters and arguments)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.F-const_generics`#![feature(const_generics)]``#![feature(const_generics)]`
Description
example by @felix91gr
#![feature(const_generics)]
#![allow(const_err)]
fn foo<const C: u8>() {
if C > 0 {
println!("Foo gives {}", 25 / C);
}
else {
println!("Foo gives 0");
}
}
fn main() {
foo::<0>();
}
Note that this currently requires allow(const_err)
because 25 /C
would panic at runtime.
This is however fine as it isn't reachable because of C > 0
.
We probably should relax this slightly here, not sure what's the best approach though
Other example (by @RalfJung)
const D: u8 = 0; // imagine this is in a different crate
pub fn foo() {
if D > 0 {
let _val = 25/D; // ERROR
} else {
panic!()
}
}
Metadata
Metadata
Assignees
Labels
A-const-genericsArea: const generics (parameters and arguments)Area: const generics (parameters and arguments)C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.F-const_generics`#![feature(const_generics)]``#![feature(const_generics)]`