Skip to content

ICE const generic with where clause #87964

Closed
@mattorib

Description

@mattorib

I found a compiler bug when trying out const generics on the nightly compiler. The key part here seems to be the where [(); T::LENGTH]: Sized addition as removing that allows it to build with no issues (the where clause is there so the Container struct can have a field that takes T::LENGTH as a type parameter). I also managed to replicate this in the Rust Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=65d8aa6be2347f0b9af2e5afb6e7d5c9.

Interestingly, by adding 'static + to both the T definitions a different error message appears, although I'm not sure if they're directly related or not. When I try to add that to <details> sections it seems to break GitHub Markdown, but you can find that in this Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=45a573b960365ffa48a56e0f246a43a8. If it would be better to open a separate issue for that I can do so as well.

Code

#![feature(const_generics)]
#![feature(const_evaluatable_checked)]

pub trait Target {
    const LENGTH: usize;
}


pub struct Container<T: Target>
where
    [(); T::LENGTH]: Sized,
{
    target: T,
}

impl<T: Target> Container<T>
where
    [(); T::LENGTH]: Sized,
{
    pub fn start(
        target: T,
    ) -> Container<T> {
        Container { target }
    }
}

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (ccffcafd5 2021-08-11)
binary: rustc
commit-hash: ccffcafd55e58f769d4b0efc0064bf65e76998e4
commit-date: 2021-08-11
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 12.0.1

Error output

error: internal compiler error: Encountered error `Unimplemented` selecting `Binder(<^0 as Target>, [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/ccffcafd55e58f769d4b0efc0064bf65e76998e4/compiler/rustc_middle/src/ty/consts.rs:184:43
Backtrace

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1134:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/ccffcafd55e58f769d4b0efc0064bf65e76998e4/library/std/src/panicking.rs:517:5
   1: std::panicking::begin_panic_fmt
             at /rustc/ccffcafd55e58f769d4b0efc0064bf65e76998e4/library/std/src/panicking.rs:460:5
   2: rustc_errors::HandlerInner::flush_delayed
   3: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
   4: core::ptr::drop_in_place<rustc_session::parse::ParseSess>
   5: <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop
   6: core::ptr::drop_in_place<rustc_interface::interface::Compiler>
   7: rustc_span::with_source_map
   8: rustc_interface::interface::create_compiler_and_run
   9: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`F-generic_const_exprs`#![feature(generic_const_exprs)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions