Skip to content

Compiler error when using const generics #89146

Closed
@iquerejeta

Description

@iquerejeta

When using a trait function that uses an associated generic constant, the compiler requests a bound to the function using

where
    [(); G::SIZE]: ,

If we use that, the code compiles fine. The way I reached to this error is by, instead, using the following bound:

where
    [(); G::SIZE]: Sized,

This case works well if the "deepness" of calls of such functions is one. However, if we have such a bounded function, that calls another bounded function as such, we get the compiler error. Playground example.

Code

Here is the WORKING example:

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

pub trait Foo {
    const SIZE: usize;

    fn to_bytes(&self) -> [u8; Self::SIZE];
}

pub fn bar<G: Foo>(a: &G) -> u8
where
    [(); G::SIZE]: ,
{
    deeper_bar(a)
}

fn deeper_bar<G: Foo>(a: &G) -> u8
where
    [(); G::SIZE]: Sized,
{
    a.to_bytes()[0]
}

And here is the NON-WORKING example

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

pub trait Foo {
    const SIZE: usize;

    fn to_bytes(&self) -> [u8; Self::SIZE];
}

pub fn bar<G: Foo>(a: &G) -> u8
where
    [(); G::SIZE]: Sized,
{
    deeper_bar(a)
}

fn deeper_bar<G: Foo>(a: &G) -> u8
where
    [(); G::SIZE]: Sized,
{
    a.to_bytes()[0]
}

Note that in the NON-WORKING version, we bound bar with

where
    [(); G::SIZE]: Sized,

instead of

where
    [(); G::SIZE]: ,

Meta

rustc --version --verbose:

rustc 1.57.0-nightly (5ecc8ad84 2021-09-19)
binary: rustc
commit-hash: 5ecc8ad8462574354a55162a0c16b10eb95e3e70
commit-date: 2021-09-19
host: x86_64-apple-darwin
release: 1.57.0-nightly
LLVM version: 13.0.0

Error output

warning: Error finalizing incremental compilation session directory `error_handling/target/debug/incremental/compiler_error-351fbe10uw0cg/s-g2kp181621-1e2ohwr-working`: No such file or directory (os error 2)

error: internal compiler error: Encountered error `Unimplemented` selecting `Binder(<^0 as Foo>, [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/5ecc8ad8462574354a55162a0c16b10eb95e3e70/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 (5ecc8ad84 2021-09-19) running on x86_64-apple-darwin

note: compiler flags: -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
warning: `compiler_error` (lib) generated 1 warning
error: could not compile `compiler_error`; 1 warning emitted

Backtrace

stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   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: scoped_tls::ScopedKey<T>::set

Metadata

Metadata

Assignees

Labels

A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.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