Skip to content

Const generic ICE: assertion failed: key.value.promoted.is_none() #61432

Closed
@goodmanjonathan

Description

@goodmanjonathan
Contributor

Promotion of const generic parameters causes the compiler to crash.

#![feature(const_generics)]

fn promote<const N: i32>() {
    // works:
    //
    // let n = N;
    // &n;
    
    &N;
}

fn main() {
    promote::<0>();
}
$ RUST_BACKTRACE=1 rustc +nightly promotion_const_generics_ice.rs
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
 --> promotion_const_generics_ice.rs:1:12
  |
1 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^

thread 'rustc' panicked at 'assertion failed: key.value.promoted.is_none()', src/librustc_mir/const_eval.rs:577:17
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.25/src/backtrace/libunwind.rs:97
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.25/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:197
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:211
   6: rustc::util::common::panic_hook
   7: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:478
   8: std::panicking::begin_panic
   9: rustc_mir::const_eval::const_eval_provider
  10: rustc::ty::query::__query_compute::const_eval
  11: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval>::compute
  12: rustc::dep_graph::graph::DepGraph::with_task_impl
  13: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  14: rustc_mir::monomorphize::collector::collect_items_rec
  15: rustc_mir::monomorphize::collector::collect_items_rec
  16: rustc_mir::monomorphize::collector::collect_crate_mono_items::{{closure}}
  17: rustc::util::common::time
  18: rustc_mir::monomorphize::collector::collect_crate_mono_items
  19: rustc::util::common::time
  20: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items
  21: rustc::ty::query::__query_compute::collect_and_partition_mono_items
  22: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::collect_and_partition_mono_items>::compute
  23: rustc::dep_graph::graph::DepGraph::with_task_impl
  24: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  25: rustc_codegen_ssa::base::codegen_crate
  26: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  27: rustc::util::common::time
  28: rustc_interface::passes::start_codegen
  29: rustc::ty::context::tls::enter_global
  30: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  31: rustc_interface::passes::create_global_ctxt::{{closure}}
  32: rustc_interface::passes::BoxedGlobalCtxt::enter
  33: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::ongoing_codegen
  34: rustc_interface::interface::run_compiler_in_existing_thread_pool
  35: std::thread::local::LocalKey<T>::with
  36: scoped_tls::ScopedKey<T>::set
  37: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
query stack during panic:
#0 [const_eval] const-evaluating + checking `promote`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack

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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.37.0-nightly (7840a0b75 2019-05-31) running on x86_64-unknown-linux-gnu

Activity

added
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
A-const-genericsArea: const generics (parameters and arguments)
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.
on Jun 1, 2019
czipperz

czipperz commented on Jun 2, 2019

@czipperz
Contributor

So what is the expected behavior here?

oli-obk

oli-obk commented on Jun 2, 2019

@oli-obk
Contributor

@varkor why shouldn't we? Like ok, we shouldn't right now, but why shouldn't ever? We are already promoting &std::mem::size_of::<T>() which seems just as problematic. This is just a limitation of the miri engine, that I wager will be simple to solve. Especially when looking at where this is happening (in the collector). Everything is monomorphic at that point I believe?

varkor

varkor commented on Jun 2, 2019

@varkor
Member

Ah, that is true. It should be okay, you're right.

oli-obk

oli-obk commented on Jun 2, 2019

@oli-obk
Contributor

Ok, awesome, I was worried for a bit that we screwed up promotion. Now let's figure out what's going wrong here.

As far as I can tell, the only way to get to a TooGeneric is by substituting and ending up with an unsubstituted (needs_subst) value. Our substitution machinery is a little whacky as clippy has already encountered, but if size_of calls can get promoted, I don't know why const generics would go down a different path. We'll need to run the repro from this issue through a rustc with RUSTC_LOG=rustc_mir::interpret,rustc::mir::interpret RUST_CTFE_BACKTRACE=immediate to get some detailed info on those errors.

jplatte

jplatte commented on Jun 11, 2019

@jplatte
Contributor

This also happens when passing a const generic variable (directly) to println! and friends.

oli-obk

oli-obk commented on Jun 21, 2019

@oli-obk
Contributor

🤣 uh, so, I found this

ConstValue::Param(_) => return err!(TooGeneric), // FIXME(oli-obk): try to monomorphize

Basically the fix is to call self.monomorphize on constant.literal in

Constant(ref constant) => self.eval_const_to_op(constant.literal, layout)?,
and turn the return in
ConstValue::Param(_) => return err!(TooGeneric), // FIXME(oli-obk): try to monomorphize
into a bug! call

This change is blocked on the eval_context.rs changes from #62012

added
E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
on Jun 21, 2019
varkor

varkor commented on Jun 25, 2019

@varkor
Member

Making this change:

-            Constant(ref constant) => self.eval_const_to_op(constant.literal, layout)?,
+            Constant(ref constant) => {
+                self.eval_const_to_op(self.monomorphize(constant.literal)?, layout)?
+            }

results in:

error: internal compiler error: src/librustc/ty/subst.rs:557: type parameter `Self/#0` (Self/0) out of range when substituting (root type=Some(<Self as float::Float>::Int)) substs=[]

while compiling rustc.

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:578:9
stack backtrace:
   0: std::sys_common::backtrace::print
   1: std::panicking::default_hook::{{closure}}
   2: std::panicking::default_hook
   3: rustc::util::common::panic_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::begin_panic
   6: rustc_errors::Handler::span_bug
   7: rustc::util::bug::opt_span_bug_fmt::{{closure}}
   8: rustc::ty::context::tls::with_opt::{{closure}}
   9: rustc::ty::context::tls::with_context_opt
  10: rustc::ty::context::tls::with_opt
  11: rustc::util::bug::opt_span_bug_fmt
  12: rustc::util::bug::span_bug_fmt
  13: <rustc::ty::subst::SubstFolder as rustc::ty::fold::TypeFolder>::fold_ty
  14: <smallvec::SmallVec<A> as core::iter::traits::collect::FromIterator<<A as smallvec::Array>::Item>>::from_iter
  15: rustc::ty::fold::TypeFoldable::fold_with
  16: rustc::ty::structural_impls::<impl rustc::ty::fold::TypeFoldable for &rustc::ty::TyS>::super_fold_with
  17: <rustc::ty::subst::SubstFolder as rustc::ty::fold::TypeFolder>::fold_ty
  18: <rustc::ty::subst::SubstFolder as rustc::ty::fold::TypeFolder>::fold_const
  19: rustc_mir::interpret::operand::<impl rustc_mir::interpret::eval_context::InterpretCx<M>>::eval_operand
  20: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpretCx<M>>::run
  21: rustc_mir::const_eval::eval_body_using_ecx
  22: rustc_mir::const_eval::const_eval_raw_provider
  23: rustc::ty::query::__query_compute::const_eval_raw
  24: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval_raw>::compute
  25: rustc::dep_graph::graph::DepGraph::with_task_impl
  26: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  27: rustc_mir::const_eval::const_eval_provider
  28: rustc::ty::query::__query_compute::const_eval
  29: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval>::compute
  30: rustc::dep_graph::graph::DepGraph::with_task_impl
  31: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  32: rustc_mir::const_eval::const_eval_provider
  33: rustc::ty::query::__query_compute::const_eval
  34: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval>::compute
  35: rustc::dep_graph::graph::DepGraph::with_task_impl
  36: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  37: rustc_mir::monomorphize::collector::collect_const
  38: rustc::mir::visit::Visitor::super_rvalue
  39: <rustc_mir::monomorphize::collector::MirNeighborCollector as rustc::mir::visit::Visitor>::visit_rvalue
  40: rustc::mir::visit::Visitor::visit_body
  41: rustc_mir::monomorphize::collector::collect_items_rec
  42: rustc_mir::monomorphize::collector::collect_items_rec
  43: rustc_mir::monomorphize::collector::collect_crate_mono_items::{{closure}}
  44: rustc::util::common::time
  45: rustc_mir::monomorphize::collector::collect_crate_mono_items
  46: rustc::util::common::time
  47: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items
  48: rustc::ty::query::__query_compute::collect_and_partition_mono_items
  49: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::collect_and_partition_mono_items>::compute
  50: rustc::dep_graph::graph::DepGraph::with_task_impl
  51: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  52: rustc_codegen_ssa::base::codegen_crate
  53: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  54: rustc::util::common::time
  55: rustc_interface::passes::start_codegen
  56: rustc::ty::context::tls::enter_global
  57: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  58: rustc_interface::passes::create_global_ctxt::{{closure}}
  59: rustc_interface::passes::BoxedGlobalCtxt::enter
  60: rustc_interface::queries::Query<T>::compute
  61: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::ongoing_codegen
  62: rustc_interface::interface::run_compiler_in_existing_thread_pool
  63: std::thread::local::LocalKey<T>::with
  64: scoped_tls::ScopedKey<T>::set
  65: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
query stack during panic:
#0 [const_eval_raw] const-evaluating `<f32 as float::Float>::EXPONENT_MASK`
#1 [const_eval] const-evaluating + checking `<f32 as float::Float>::EXPONENT_MASK`
#2 [const_eval] const-evaluating + checking `<f32 as float::Float>::EXPONENT_MASK`
#3 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: aborting due to previous error
(I haven't looked into the error yet.)
oli-obk

oli-obk commented on Jun 25, 2019

@oli-obk
Contributor

heh, one can always trust in libapfloat to expose const eval bugs

can you run with RUSTC_LOG=rustc_mir::interpret and dump that log, too? It looks to me like the instance field of the Frame has the wrong Substs

JohnTitor

JohnTitor commented on Jun 25, 2019

@JohnTitor
Member

@oli-obk FWIW I got this log.

17 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.F-const_generics`#![feature(const_generics)]`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.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @oli-obk@Centril@jplatte@jonas-schievink@goodmanjonathan

      Issue actions

        Const generic ICE: assertion failed: key.value.promoted.is_none() · Issue #61432 · rust-lang/rust