Description
When using const generics for array lengths, the compiler can emit an ICE if the constraints used are unconventional.
MCVE
The following MCVE emits an ICE on the Nightly compiler on the playground.
Note that the Clone
derive macro is the source of the error.
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]
pub trait Enumerable {
const N: usize;
}
#[derive(Clone)]
pub struct SymmetricGroup<S>
where
S: Enumerable,
// The following constraint comes from the compiler's message,
// but I misunderstood it and added a `Sized` constraint
// instead of no constraint at all.
[(); S::N]: Sized,
{
_phantom: std::marker::PhantomData<S>,
}
Background
Originally, my code was somewhat like this:
#[derive(Clone)]
pub struct SymmetricGroup<S>
where
S: Enumerable,
{
map: [S; S::N]
}
The compiler provided this message:
error: unconstrained generic constant
--> src/lib.rs:13:10
|
13 | map: [S; S::N]
| ^^^^^^^^^
|
= help: try adding a `where` bound using this expression: `where [(); S::N]:`
error: could not compile `playground` due to previous error
I took "add a where
bound" to mean "give [(); S::N]
a simple constraint", so I added Sized
.
The MCVE does not include the map
field because it is not relevant to the bug itself, I just included it for completeness to show how I arrived at the bug.
I still don't really understand why the line is needed at all - I've read through issues like #68366 and rust-lang/compiler-team#340 but can't find an explanation anywhere yet. I assume once this is stabilised it'll get its own entry in the Rustonomicon to help others.
Error output
Compiling playground v0.0.1 (/playground)
error: internal compiler error: Encountered error `Unimplemented` selecting `Binder(<^0 as Enumerable>, [Ty(Anon)])` during codegen
|
= note: delayed at compiler/rustc_trait_selection/src/traits/codegen.rs:68:32
error: internal compiler error: ty::ConstKind::Error constructed but no error reported.
|
= note: delayed at /rustc/98c8619502093f34ca82f8f26ccf32e753924440/compiler/rustc_middle/src/ty/consts.rs:183:43
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1165:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: rustc 1.57.0-nightly (98c861950 2021-09-27) running on x86_64-unknown-linux-gnu
note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type lib
note: some of the compiler flags provided by cargo are hidden
query stack during panic:
end of query stack
error: could not compile `playground`
Backtrace
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1165:13
stack backtrace:
0: rust_begin_unwind
at /rustc/98c8619502093f34ca82f8f26ccf32e753924440/library/std/src/panicking.rs:517:5
1: core::panicking::panic_fmt
at /rustc/98c8619502093f34ca82f8f26ccf32e753924440/library/core/src/panicking.rs:100:14
2: core::panicking::panic_display
3: rustc_errors::HandlerInner::flush_delayed
4: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
5: core::ptr::drop_in_place<rustc_session::parse::ParseSess>
6: <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop
7: core::ptr::drop_in_place<rustc_interface::interface::Compiler>
8: rustc_span::with_source_map
9: rustc_interface::interface::create_compiler_and_run
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.