rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.18k stars 12.7k forks source link

Failed to normalize struct with generic const expression parameter as associated type #93946

Closed zyansheep closed 2 years ago

zyansheep commented 2 years ago

Code

// subcrate, lib.rs
#![feature(generic_const_exprs)]
#![feature(adt_const_params)]

#[derive(PartialEq, Eq)]
pub struct Const {}
impl Const {
    pub const fn func(self) -> usize { 1 }
}

pub struct Foo<const C: Const>
where
    [(); C.func()]: Sized
{}

pub trait Bar {
    type Associated;
    fn associated() -> Self::Associated;
}

impl<const C: Const> Bar for Foo<C>
where
    [(); C.func()]: Sized
{
    type Associated = [(); C.func()];
    fn associated() -> Self::Associated {
        [(); C.func()]
    }
}
// main.rs
use subcrate::*;

fn main() {
    <Foo::<{Const{}}> as Bar>::associated();
}

Meta

rustc --version --verbose:

rustc 1.60.0-nightly (bd3cb5256 2022-01-16)
binary: rustc
commit-hash: bd3cb52565faab2755ff1bdb54d88bc91f47b4b9
commit-date: 2022-01-16
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Error output

error: internal compiler error: compiler/rustc_middle/src/ty/normalize_erasing_regions.rs:179:90: Failed to normalize <subcrate::Foo<C> as subcrate::Bar>::Associated, maybe try to call `try_normalize_erasing_regions` instead

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1169:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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.60.0-nightly (bd3cb5256 2022-01-16) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin

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

query stack during panic:
#0 [fn_abi_of_instance] computing call ABI of `<subcrate::Foo<subcrate::Const {  }> as subcrate::Bar>::associated`
end of query stack
error: could not compile `rust_cargo_rkyv_bug`
Backtrace

``` error: internal compiler error: compiler/rustc_middle/src/ty/normalize_erasing_regions.rs:179:90: Failed to normalize as subcrate::Bar>::Associated, maybe try to call `try_normalize_erasing_regions` instead thread 'rustc' panicked at 'Box', compiler/rustc_errors/src/lib.rs:1169:9 stack backtrace: 0: std::panicking::begin_panic:: 1: std::panic::panic_any:: 2: ::bug 3: ::bug 4: rustc_middle::ty::context::tls::with_opt::::{closure#0}, ()> 5: rustc_middle::util::bug::opt_span_bug_fmt:: 6: rustc_middle::util::bug::bug_fmt 7: rustc_middle::ty::util::fold_list:: as rustc_middle::ty::fold::TypeFoldable>::try_super_fold_with::{closure#0}> 8: rustc_middle::ty::layout::fn_abi_of_instance 9: >::with_task::)>, core::result::Result<&rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS>, rustc_middle::ty::layout::FnAbiError>> 10: rustc_data_structures::stack::ensure_sufficient_stack::<(core::result::Result<&rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS>, rustc_middle::ty::layout::FnAbiError>, rustc_query_system::dep_graph::graph::DepNodeIndex), rustc_query_system::query::plumbing::execute_job)>, core::result::Result<&rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS>, rustc_middle::ty::layout::FnAbiError>>::{closure#3}> 11: rustc_query_system::query::plumbing::try_execute_query::)>, core::result::Result<&rustc_target::abi::call::FnAbi<&rustc_middle::ty::TyS>, rustc_middle::ty::layout::FnAbiError>>> 12: ::fn_abi_of_instance 13: rustc_codegen_ssa::mir::codegen_mir:: 14: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen 15: rustc_codegen_llvm::base::compile_codegen_unit 16: rustc_codegen_ssa::base::codegen_crate:: 17: ::codegen_crate 18: ::time::, rustc_interface::passes::start_codegen::{closure#0}> 19: ::ongoing_codegen 20: ::enter::, rustc_errors::ErrorReported>> 21: rustc_span::with_source_map::, rustc_interface::interface::create_compiler_and_run, rustc_driver::run_compiler::{closure#1}>::{closure#1}> 22: rustc_interface::interface::create_compiler_and_run::, rustc_driver::run_compiler::{closure#1}> 23: >::set::, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>> note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. 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.60.0-nightly (bd3cb5256 2022-01-16) running on x86_64-unknown-linux-gnu note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [fn_abi_of_instance] computing call ABI of ` as subcrate::Bar>::associated` end of query stack error: could not compile `rust_cargo_rkyv_bug` ```

One weird thing is that it only works if the offending code is in a subcrate, not in the main crate. Example code is here: https://github.com/zyansheep/rust_const_rkyv_bug

zyansheep commented 2 years ago

I have reran the test on nightly with the most recent compiler:

rustc 1.66.0-nightly (a6b7274a4 2022-10-10)
binary: rustc
commit-hash: a6b7274a462829f8ef08a1ddcdcec7ac80dbf3e1
commit-date: 2022-10-10
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2

and it seems to not error anymore.

Just in case it was a fluke, I went back to where I originally discovered the bug and everything seems to work just fine :)