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

ICE using unknown variable for array length in const generic function #66442

Closed
Michael-F-Bryan opened this issue Nov 15, 2019 · 3 comments
Closed

Comments

@Michael-F-Bryan
Copy link

Michael-F-Bryan commented Nov 15, 2019

I found this while playing around with someone's const-generic array concatentation function on Reddit.

(playground)

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

fn concat<const N: usize, const M: usize>(
    a: &[i32; N],
    b: &[i32; M],
) -> [i32; N+M] {
    // i32 is safely zeroable.
    let mut res: [i32; NM] = unsafe { std::mem::zeroed() };  // <-- note NM, not N+M
    for i in 0..N {
        res[i] = a[i]
    }
    let mut j = N;
    for i in 0..M {
        res[j] = b[i];
        j += 1;
    }
    res
}

fn main() {
    println!("{:?}", concat(&[1, 2, 3], &[4, 5, 6]));
}

Backtrace:

 Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `NM` in this scope
 --> src/main.rs:9:24
  |
4 | fn concat<const N: usize, const M: usize>(
  |                                 - similarly named const parameter `M` defined here
...
9 |     let mut res: [i32; NM] = unsafe { std::mem::zeroed() };
  |                        ^^ help: a const parameter with a similar name exists: `M`

error: internal compiler error: src/librustc/ty/subst.rs:650: const parameter `N/#0` (Const { ty: usize, val: Param(N/#0) }/0) out of range when substituting substs=[]

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:896:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:84
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:61
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1030
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1412
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:188
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:205
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:468
  12: std::panicking::begin_panic
  13: rustc_errors::HandlerInner::span_bug
  14: rustc_errors::Handler::span_bug
  15: rustc::util::bug::opt_span_bug_fmt::{{closure}}
  16: rustc::ty::context::tls::with_opt::{{closure}}
  17: rustc::ty::context::tls::with_context_opt
  18: rustc::ty::context::tls::with_opt
  19: rustc::util::bug::opt_span_bug_fmt
  20: rustc::util::bug::span_bug_fmt
  21: <rustc::ty::subst::SubstFolder as rustc::ty::fold::TypeFolder>::fold_const
  22: rustc::traits::codegen::<impl rustc::ty::context::TyCtxt>::subst_and_normalize_erasing_regions
  23: rustc_mir::interpret::operand::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_operand
  24: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_rvalue_into_place
  25: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::run
  26: rustc_mir::const_eval::const_eval_raw_provider
  27: rustc::ty::query::__query_compute::const_eval_raw
  28: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval_raw>::compute
  29: rustc::dep_graph::graph::DepGraph::with_task_impl
  30: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  31: rustc_mir::const_eval::const_eval_provider
  32: rustc::ty::query::__query_compute::const_eval
  33: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval>::compute
  34: rustc::dep_graph::graph::DepGraph::with_task_impl
  35: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  36: rustc_mir::const_eval::const_eval_provider
  37: rustc::ty::query::__query_compute::const_eval
  38: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::const_eval>::compute
  39: rustc::dep_graph::graph::DepGraph::with_task_impl
  40: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  41: rustc::ty::sty::Const::eval
  42: <rustc::traits::project::AssocTypeNormalizer as rustc::ty::fold::TypeFolder>::fold_ty
  43: <smallvec::SmallVec<A> as core::iter::traits::collect::FromIterator<<A as smallvec::Array>::Item>>::from_iter
  44: rustc::ty::fold::TypeFoldable::fold_with
  45: rustc::traits::project::normalize
  46: rustc_typeck::check::FnCtxt::normalize_associated_types_in
  47: rustc::ty::context::GlobalCtxt::enter_local
  48: rustc_typeck::check::wfcheck::check_item_well_formed
  49: rustc::ty::query::__query_compute::check_item_well_formed
  50: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::check_item_well_formed>::compute
  51: rustc::dep_graph::graph::DepGraph::with_task_impl
  52: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  53: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::ensure_query
  54: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:81
  55: rustc_data_structures::sync::par_for_each_in
  56: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:81
  57: rustc::hir::Crate::par_visit_all_item_likes
  58: rustc::util::common::time
  59: rustc_typeck::check_crate
  60: rustc_interface::passes::analysis
  61: rustc::ty::query::__query_compute::analysis
  62: rustc::dep_graph::graph::DepGraph::with_task_impl
  63: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  64: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  65: rustc_interface::passes::create_global_ctxt::{{closure}}
  66: rustc_interface::passes::BoxedGlobalCtxt::enter
  67: rustc_interface::interface::run_compiler_in_existing_thread_pool
  68: std::thread::local::LocalKey<T>::with
  69: scoped_tls::ScopedKey<T>::set
  70: syntax::with_globals
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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.41.0-nightly (82cf3a448 2019-11-14) running on x86_64-unknown-linux-gnu

note: compiler flags: -C opt-level=3 -C codegen-units=1 --crate-type bin

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

query stack during panic:
#0 [const_eval_raw] const-evaluating `concat::{{constant}}#2`
#1 [const_eval] const-evaluating + checking `concat::{{constant}}#2`
#2 [const_eval] const-evaluating + checking `concat::{{constant}}#2`
#3 [check_item_well_formed] processing `concat`
#4 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.
@hellow554
Copy link
Contributor

ICEs also on stable and beta

@hellow554
Copy link
Contributor

hellow554 commented Nov 15, 2019

probably a dup of #66205

it's not because of NM, but because of N+M in your return type. Reduced example:

#![feature(const_generics)]

fn concat<const N: usize>() -> [i32; N+1] {
    unimplemented!()
}

@Michael-F-Bryan
Copy link
Author

Looking at your minified example I'd say it's a dupe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants