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 when returning Vec from const fn #55063

Closed
Voultapher opened this issue Oct 14, 2018 · 5 comments · Fixed by #55071
Closed

ICE when returning Vec from const fn #55063

Voultapher opened this issue Oct 14, 2018 · 5 comments · Fixed by #55071
Assignees
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) 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

@Voultapher
Copy link
Contributor

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`.
@Centril Centril added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Oct 14, 2018
@Centril Centril added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-fn A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels Oct 14, 2018
@killercup
Copy link
Member

Link to playground that gives the exact backtrace above.

@matthewjasper
Copy link
Contributor

Relavent code:

let cast_in = CastTy::from_ty(operand.ty(mir, tcx)).expect("bad input type for cast");
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");

The types here are Box<[i32; 3]> and Box<[i32]>.

@RalfJung
Copy link
Member

The actual bug would be here then, right?

pub fn from_ty(t: Ty<'tcx>) -> Option<CastTy<'tcx>> {
match t.sty {
ty::Bool => Some(CastTy::Int(IntTy::Bool)),
ty::Char => Some(CastTy::Int(IntTy::Char)),
ty::Int(_) => Some(CastTy::Int(IntTy::I)),
ty::Infer(ty::InferTy::IntVar(_)) => Some(CastTy::Int(IntTy::I)),
ty::Infer(ty::InferTy::FloatVar(_)) => Some(CastTy::Float),
ty::Uint(u) => Some(CastTy::Int(IntTy::U(u))),
ty::Float(_) => Some(CastTy::Float),
ty::Adt(d,_) if d.is_enum() && d.is_payloadfree() =>
Some(CastTy::Int(IntTy::CEnum)),
ty::RawPtr(mt) => Some(CastTy::Ptr(mt)),
ty::Ref(_, ty, mutbl) => Some(CastTy::RPtr(ty::TypeAndMut { ty, mutbl })),
ty::FnPtr(..) => Some(CastTy::FnPtr),
_ => None,
}
}

@oli-obk
Copy link
Contributor

oli-obk commented Oct 14, 2018

Not quite sure, but the real bug is that we allow the box keyword in const fn XD I already have a fix since yesterday, but the german bahn didn't give me electricity so my notebook died while tests were running

@est31
Copy link
Member

est31 commented Oct 14, 2018

@oli-obk no ICE fix from the ICE? :p

kennytm added a commit to kennytm/rust that referenced this issue Oct 18, 2018
kennytm added a commit to kennytm/rust that referenced this issue Oct 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) 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.

7 participants