Skip to content

ICE when returning Vec from const fn #55063

Closed
@Voultapher

Description

@Voultapher
Returning `Vec` from a const fn causes internal compiler error, with the message 'bad input type for cast'.

I tried this code:

Minimal example

const fn foo() -> Vec<i32> {
    vec![1, 2, 3]
}

Original example

const fn foo(a: i32) -> Vec<i32> {
    vec![1, 2, 3]
}

const CT: Vec<i32> = foo(3);

fn main() {
    println!("Hello {}", CT[1])
}

I expected to see this happen: program compiles and prints 'Hello 1' or I get a compiler error.

Instead, this happened: ICE

Meta

Compiling playground v0.0.1 (/playground)
thread 'main' panicked at 'bad input type for cast', libcore/option.rs:1008:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:71
             at libstd/sys_common/backtrace.rs:59
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:211
   3: std::panicking::default_hook
             at libstd/panicking.rs:227
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:480
   6: std::panicking::continue_panic_fmt
             at libstd/panicking.rs:390
   7: rust_begin_unwind
             at libstd/panicking.rs:325
   8: core::panicking::panic_fmt
             at libcore/panicking.rs:77
   9: core::option::expect_failed
             at libcore/option.rs:1008
  10: rustc_mir::transform::qualify_min_const_fn::is_min_const_fn
  11: <rustc_mir::transform::qualify_consts::QualifyAndPromoteConstants as rustc_mir::transform::MirPass>::run_pass
  12: rustc_mir::transform::mir_validated::{{closure}}
  13: rustc_mir::transform::mir_validated
  14: rustc::ty::query::__query_compute::mir_validated
  15: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::mir_validated<'tcx>>::compute
  16: rustc::dep_graph::graph::DepGraph::with_task_impl
  17: rustc::ty::context::tls::with_related_context
  18: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  19: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  20: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::ensure_query
  21: rustc_borrowck::borrowck::borrowck
  22: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::borrowck<'tcx>>::compute
  23: rustc::dep_graph::graph::DepGraph::with_task_impl
  24: rustc::ty::context::tls::with_related_context
  25: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  26: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  27: rustc::ty::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::par_body_owners
  28: rustc_borrowck::borrowck::check_crate
  29: rustc::util::common::time
  30: rustc::ty::context::tls::enter_context
  31: <std::thread::local::LocalKey<T>>::with
  32: rustc::ty::context::TyCtxt::create_and_enter
  33: rustc_driver::driver::compile_input
  34: rustc_driver::run_compiler_with_pool
  35: rustc_driver::driver::spawn_thread_pool
  36: rustc_driver::run_compiler
  37: <scoped_tls::ScopedKey<T>>::set
  38: syntax::with_globals
  39: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  40: rustc_driver::run
  41: rustc_driver::main
  42: std::rt::lang_start::{{closure}}
  43: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:310
  44: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  45: std::rt::lang_start_internal
             at libstd/panicking.rs:289
             at libstd/panic.rs:392
             at libstd/rt.rs:58
  46: main
  47: __libc_start_main
  48: <unknown>
query stack during panic:
#0 [mir_validated] processing `foo`
#1 [borrowck] processing `foo`
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.31.0-nightly (4699283c5 2018-10-13) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type lib

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

error: Could not compile `playground`.

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)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