Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler error when using const generics #89146

Closed
iquerejeta opened this issue Sep 21, 2021 · 1 comment · Fixed by #90023
Closed

Compiler error when using const generics #89146

iquerejeta opened this issue Sep 21, 2021 · 1 comment · Fixed by #90023
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@iquerejeta
Copy link

iquerejeta commented Sep 21, 2021

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

@iquerejeta iquerejeta added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 21, 2021
@BoxyUwU BoxyUwU added A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Sep 21, 2021
@b-naber
Copy link
Contributor

b-naber commented Sep 22, 2021

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants