Open
Description
Currently this compiles in stable.
struct Foo<T, const N: usize>(T);
impl<T, const N: usize> Foo<T, N> {
const BAR: () = if N == 0 {
panic!()
};
}
struct Invoke<T, const N: usize>(T);
impl<T, const N: usize> Invoke<T, N> {
const FUN: fn() = if N != 0 {
|| Foo::<T, N>::BAR
} else {
|| {}
};
}
fn main() {
Invoke::<(), 0>::FUN();
}
This is a useful property¹ but if the panic path were not completely excluded this could lead to monomorphization-time errors which are undesirable since they only show up in cargo build
and not in cargo check
. But not as undesirable as #107503 where the errors are optimization-dependent.
Still, @RalfJung indicated that the current behavior might not be intentional so I'm filing this issue for clarification.
(Also see rust-lang/rfcs#3582 for more discussion of the same question.)
¹actual use could look like this:
// library-provided function:
/// Fails to compile if N == 0
fn chunks<const N: usize>(foo: Foo) -> &[[u8; N]] {
const {
assert!(N > 0)!
}
todo!()
}
// user-code, generic over all N
fn library_user_code<const N: usize>(foo: Foo) -> {
// this could be generated by a `const_if!()` macro or use inline const blocks
struct ConstIf< const N: usize>;
impl<const N: usize> ConstIf<N> {
const FUN: fn() = if N != 0 {
|foo| chunks::<N>(foo)
} else {
|foo| { /* fallback code here */ }
};
}
ConstIf::<N>::FUN(foo)
}
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
RalfJung commentedon Mar 10, 2024
Cc @rust-lang/wg-const-eval
Ultimately this is a t-lang question though so might need a t-lang design meeting or some such thing.
RalfJung commentedon Mar 10, 2024
In fact you don't need closures, this builds as well:
RalfJung commentedon Mar 10, 2024
Your example doesn't quite seem to have the right shape for that use-case; could you flesh out the example a bit more to make this more clear?
the8472 commentedon Mar 10, 2024
Added it to the top post.
RalfJung commentedon Mar 10, 2024
Makes sense, thanks!
Note that without generic_const_exprs such a
const_if!
macro will be very limited.add test for rust-lang#122301 to cover behavior that's on stable
add test for rust-lang#122301 to cover behavior that's on stable
Rollup merge of rust-lang#122572 - the8472:test-const-deadness, r=Ral…
32 remaining items