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

thread 'rustc' panicked at 'called Result::unwrap() on an Err #51301

Closed
IllusiveS opened this issue Jun 2, 2018 · 4 comments · Fixed by #61792
Closed

thread 'rustc' panicked at 'called Result::unwrap() on an Err #51301

IllusiveS opened this issue Jun 2, 2018 · 4 comments · Fixed by #61792
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@IllusiveS
Copy link

IllusiveS commented Jun 2, 2018

I tried this code:

#![allow(dead_code)]
#![allow(unused_must_use)]

use std::collections::HashMap;
use std::any::{Any, TypeId};
use std::result::Result::Ok;
use std::hash::Hash;

trait State {
	type EventType;

	fn on_enter(&mut self);
	fn on_event_enter(&mut self, event: Self::EventType);

	fn get_type_id_of_state(&self) -> TypeId;
}

struct StateMachine<EventType : Hash + Eq> {
	current_state : Box<State<EventType=EventType>>,
	transition_table : HashMap<TypeId, HashMap<EventType, fn() -> Box<State<EventType=EventType>>>>
}

impl <EventType : Hash + Eq> StateMachine<EventType> {
	fn process_event(&mut self, event : EventType) {
		self.inner_process_event(event);
	}

	fn is_state<T: ?Sized + Any>(&mut self) -> bool {
		self.current_state.get_type_id_of_state() == TypeId::of::<T>()
	}

	fn inner_process_event(&mut self, event : EventType) -> Result<(), i8> {
		let new_state_creation_function = self.transition_table
			.iter()
			.find(|(&event_typeid, _)| event_typeid == self.current_state.get_type_id_of_state())
			.ok_or(1)?
			.1
			.iter()
			.find(|(&event_type, _)|event == event_type)
			.ok_or(2)?
			.1;

		//TODO on_exit from state
		self.current_state = new_state_creation_function();
		Ok(())
	}
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

I expected to see this happen: for this code to compile, or throw an error

Instead, this happened: i broke the compiler

Meta

rustc --version --verbose:
rustc 1.26.1 (827013a 2018-05-25)
binary: rustc
commit-hash: 827013a
commit-date: 2018-05-25
host: x86_64-unknown-linux-gnu
release: 1.26.1
LLVM version: 6.0

Backtrace:

thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: (MoveData { move_paths: [MovePath { place: _0 }, MovePath { place: _1 }, MovePath { place: _2 }, MovePath { place: _3 }, MovePath { place: _4 }, MovePath { place: _5 }], moves: [mp4@bb0[6], mp5@bb0[6], mp5@bb2[0], mp4@bb2[1], mp3@bb2[2], mp3@bb3[0], mp3@bb4[0], mp0@bb4[1]], loc_map: LocationMap { map: [[[], [], [], [], [], [], [mo0, mo1]], [[]], [[mo2], [mo3], [mo4]], [[mo5]], [[mo6], [mo7]]] }, path_map: [[mo7], [], [], [mo4, mo5, mo6], [mo0, mo3], [mo1, mo2]], rev_lookup: MovePathLookup { locals: [mp0, mp1, mp2, mp3, mp4, mp5], projections: {} }, inits: [mp1@src/lib.rs:39:10: 39:47 (Deep), mp2@src/lib.rs:39:11: 39:27 (Deep), mp3@src/lib.rs:39:13: 39:23 (Deep), mp4@src/lib.rs:39:28: 39:33 (Deep), mp5@src/lib.rs:39:37: 39:47 (Deep), mp0@src/lib.rs:39:28: 39:47 (NonPanicPathOnly)], init_loc_map: LocationMap { map: [[[], [in2], [], [in3], [], [in4], [in5]], [[]], [[], [], []], [[]], [[], []]] }, init_path_map: [[in5], [in0], [in1], [in2], [in3], [in4]] }, [IllegalMove { cannot_move_out_of: IllegalMoveOrigin { span: src/lib.rs:39:13: 39:23, kind: BorrowedContent } }])', libcore/result.rs:945:5
stack backtrace:
   0:     0x7f65eff6366b - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::hc94fccaa2a711028
                               at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1:     0x7f65eff317f0 - std::sys_common::backtrace::print::h8c9041d44f9f1a2c
                               at libstd/sys_common/backtrace.rs:71
                               at libstd/sys_common/backtrace.rs:59
   2:     0x7f65eff570ad - std::panicking::default_hook::{{closure}}::ha2f043e73975ac4b
                               at libstd/panicking.rs:207
   3:     0x7f65eff56e0b - std::panicking::default_hook::h1db43126ea6aa931
                               at libstd/panicking.rs:223
   4:     0x7f65ec581c7d - core::ops::function::Fn::call::h4c0721031e98bbc2
   5:     0x7f65eff575f9 - std::panicking::rust_panic_with_hook::h4de9fd85b55cffd7
                               at libstd/panicking.rs:403
   6:     0x7f65eff573e2 - std::panicking::begin_panic_fmt::h2cd49505797e619d
                               at libstd/panicking.rs:349
   7:     0x7f65eff57312 - rust_begin_unwind
                               at libstd/panicking.rs:325
   8:     0x7f65effc9650 - core::panicking::panic_fmt::h2bc4e257e1a74d55
                               at libcore/panicking.rs:72
   9:     0x7f65ed222fe8 - core::result::unwrap_failed::h3a709c5b32360d64
  10:     0x7f65ed0f354e - <rustc_mir::transform::elaborate_drops::ElaborateDrops as rustc_mir::transform::MirPass>::run_pass::h660045041abc7571
  11:     0x7f65ed173de7 - rustc_mir::transform::optimized_mir::{{closure}}::h7cb0d0add4eb4184
  12:     0x7f65ed16e4e6 - rustc_mir::transform::optimized_mir::h7cb7fa1e30d4354e
  13:     0x7f65ec19d2c4 - rustc::dep_graph::graph::DepGraph::with_task_impl::h878c22fb5122f9c6
  14:     0x7f65ec130299 - rustc_errors::Handler::track_diagnostics::h918b4586db0c6597
  15:     0x7f65ec3f46f7 - rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::cycle_check::h0eef73757e8f6ac7
  16:     0x7f65ec062c10 - rustc::ty::maps::<impl rustc::ty::maps::queries::optimized_mir<'tcx>>::force::hf2f063865577b696
  17:     0x7f65ec063569 - rustc::ty::maps::<impl rustc::ty::maps::queries::optimized_mir<'tcx>>::try_get::hce2244b4b273c61b
  18:     0x7f65ec4759fa - rustc::ty::maps::TyCtxtAt::optimized_mir::hc7898fa6d1e4cf46
  19:     0x7f65ec4728e0 - rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::optimized_mir::h151511e9befcfbcc
  20:     0x7f65ee1996d7 - rustc_metadata::encoder::<impl rustc_metadata::isolated_encoder::IsolatedEncoder<'a, 'b, 'tcx>>::encode_optimized_mir::hc555dd2c28fae047
  21:     0x7f65ee19b2ed - rustc_metadata::encoder::<impl rustc_metadata::isolated_encoder::IsolatedEncoder<'a, 'b, 'tcx>>::encode_info_for_closure::h16853ae6f89c1dc6
  22:     0x7f65ee0b09fa - rustc::dep_graph::graph::DepGraph::with_ignore::h05704be9e04a9fb6
  23:     0x7f65ee1864da - rustc_metadata::encoder::<impl rustc_metadata::index_builder::IndexBuilder<'a, 'b, 'tcx>>::encode_info_for_expr::hbcf521968e6680b1
  24:     0x7f65ee088569 - rustc::hir::intravisit::walk_expr::haa934655629b9d52
  25:     0x7f65ee08855a - rustc::hir::intravisit::walk_expr::haa934655629b9d52
  26:     0x7f65ee08875a - rustc::hir::intravisit::walk_expr::haa934655629b9d52
  27:     0x7f65ee088783 - rustc::hir::intravisit::walk_expr::haa934655629b9d52
  28:     0x7f65ee088bc6 - rustc::hir::intravisit::walk_expr::haa934655629b9d52
  29:     0x7f65ee08503e - rustc::hir::intravisit::walk_local::hfbe4bc5f617e268b
  30:     0x7f65ee08897f - rustc::hir::intravisit::walk_expr::haa934655629b9d52
  31:     0x7f65edff2dd4 - rustc::hir::intravisit::Visitor::visit_nested_body::hef2dfe74809641b6
  32:     0x7f65ee1a9ada - rustc::hir::Crate::visit_all_item_likes::hbf8265e514e7d5ba
  33:     0x7f65ee04388b - rustc_metadata::encoder::encode_metadata::he4cc4242b9c6db42
  34:     0x7f65ee1bca41 - rustc_metadata::cstore_impl::<impl rustc::middle::cstore::CrateStore for rustc_metadata::cstore::CStore>::encode_metadata::h8690a83dd23db74e
  35:     0x7f65ec459c40 - rustc::ty::context::TyCtxt::encode_metadata::h4066fda02cc5f5b3
  36:     0x7f65e484b4f4 - rustc_trans::base::write_metadata::h92808802d3f10fb1
  37:     0x7f65e48eeb86 - rustc::util::common::time::h7afbe841455208f3
  38:     0x7f65e484bd99 - rustc_trans::base::trans_crate::h14dc7fce48712af1
  39:     0x7f65e47fa367 - <rustc_trans::LlvmTransCrate as rustc_trans_utils::trans_crate::TransCrate>::trans_crate::hdaf690ee5d727ebc
  40:     0x7f65f02edce5 - rustc::util::common::time::h3cd3b736e582b0e7
  41:     0x7f65f02e1d64 - rustc_driver::driver::phase_4_translate_to_llvm::h464b5474b0ef515a
  42:     0x7f65f036a159 - rustc_driver::driver::compile_input::{{closure}}::h23e22af9bd83b9e4
  43:     0x7f65f036650a - <std::thread::local::LocalKey<T>>::with::hc77f0fdf41eb34ff
  44:     0x7f65f0362c96 - <std::thread::local::LocalKey<T>>::with::h62237ca090d08843
  45:     0x7f65f03e029e - rustc::ty::context::TyCtxt::create_and_enter::hbfec67eee3dde235
  46:     0x7f65f02db217 - rustc_driver::driver::compile_input::hef0281f2d009ea71
  47:     0x7f65f038facf - rustc_driver::run_compiler_impl::hfe6dafc265c4aa95
  48:     0x7f65f02b5578 - syntax::with_globals::h0c3db56419a08c06
  49:     0x7f65f02f5b8d - std::sys_common::backtrace::__rust_begin_short_backtrace::h78942b1d792cc36a
  50:     0x7f65eff71cce - __rust_maybe_catch_panic
                               at libpanic_unwind/lib.rs:102
  51:     0x7f65f030189d - <F as alloc::boxed::FnBox<A>>::call_box::h7dcda0c9ae6259a6
  52:     0x7f65eff68d57 - std::sys_common::thread::start_thread::hbdb0265288bf7dcc
                               at /checkout/src/liballoc/boxed.rs:794
                               at libstd/sys_common/thread.rs:24
  53:     0x7f65eff37e18 - std::sys::unix::thread::Thread::new::thread_start::h99a7fe00c40fd1be
                               at libstd/sys/unix/thread.rs:90
  54:     0x7f65ea2db6b9 - start_thread
  55:     0x7f65efc1741c - clone
  56:                0x0 - <unknown>

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.26.1 (827013a31 2018-05-25) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=2 -C incremental --crate-type lib

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

error: Could not compile `rust_state_machine`.
warning: build failed, waiting for other jobs to finish...
error: build failed

Process finished with exit code 101
@IllusiveS IllusiveS changed the title thread 'rustc' panicked at 'called Result::unwrap() on an Err value when expecting Eq on template parameter thread 'rustc' panicked at 'called Result::unwrap() on an Err value when having template parameter named the same way as the associated type Jun 3, 2018
@IllusiveS IllusiveS changed the title thread 'rustc' panicked at 'called Result::unwrap() on an Err value when having template parameter named the same way as the associated type thread 'rustc' panicked at 'called Result::unwrap() on an Err Jun 3, 2018
@shingtaklam1324
Copy link

shingtaklam1324 commented Jun 4, 2018

Latest nightly seems to give more information, it might be a mir issue.

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: (MoveData { move_paths: [MovePath { place: _0 }, MovePath { place: _1 }, MovePath { place: _2 }, MovePath { place: _3 }, MovePath { place: _4 }, MovePath { place: _5 }], moves: [mp4@bb0[6], mp5@bb0[6], mp5@bb2[0], mp4@bb2[1], mp3@bb2[2], mp3@bb3[0], mp3@bb4[0], mp0@bb4[1]], loc_map: LocationMap { map: [[[], [], [], [], [], [], [mo0, mo1]], [[]], [[mo2], [mo3], [mo4]], [[mo5]], [[mo6], [mo7]]] }, path_map: [[mo7], [], [], [mo4, mo5, mo6], [mo0, mo3], [mo1, mo2]], rev_lookup: MovePathLookup { locals: [mp0, mp1, mp2, mp3, mp4, mp5], projections: {} }, inits: [mp1@src/lib.rs:40:19: 40:57 (Deep), mp2@src/lib.rs:40:20: 40:36 (Deep), mp3@src/lib.rs:40:22: 40:32 (Deep), mp4@src/lib.rs:40:38: 40:43 (Deep), mp5@src/lib.rs:40:47: 40:57 (Deep), mp0@src/lib.rs:40:38: 40:57 (NonPanicPathOnly)], init_loc_map: LocationMap { map: [[[], [in2], [], [in3], [], [in4], [in5]], [[]], [[], [], []], [[]], [[], []]] }, init_path_map: [[in5], [in0], [in1], [in2], [in3], [in4]] }, [IllegalMove { cannot_move_out_of: IllegalMoveOrigin { span: src/lib.rs:40:22: 40:32, kind: BorrowedContent } }])', libcore/result.rs:945: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:515
   6: std::panicking::continue_panic_fmt
             at libstd/panicking.rs:426
   7: rust_begin_unwind
             at libstd/panicking.rs:337
   8: core::panicking::panic_fmt
             at libcore/panicking.rs:92
   9: core::result::unwrap_failed
  10: <rustc_mir::transform::elaborate_drops::ElaborateDrops as rustc_mir::transform::MirPass>::run_pass
  11: rustc_mir::transform::optimized_mir::{{closure}}
  12: rustc_mir::transform::optimized_mir
  13: rustc::ty::maps::__query_compute::optimized_mir
  14: rustc::ty::maps::<impl rustc::ty::maps::config::QueryConfig<'tcx> for rustc::ty::maps::queries::optimized_mir<'tcx>>::compute
  15: rustc::ty::context::tls::with_context
  16: rustc::dep_graph::graph::DepGraph::with_task_impl
  17: rustc::ty::context::tls::with_related_context
  18: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  19: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  20: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::optimized_mir
  21: rustc_metadata::encoder::<impl rustc_metadata::isolated_encoder::IsolatedEncoder<'a, 'b, 'tcx>>::encode_optimized_mir
  22: rustc_metadata::encoder::<impl rustc_metadata::isolated_encoder::IsolatedEncoder<'a, 'b, 'tcx>>::encode_info_for_closure
  23: rustc::ty::context::tls::with_context
  24: rustc_metadata::encoder::<impl rustc_metadata::index_builder::IndexBuilder<'a, 'b, 'tcx>>::encode_info_for_expr
  25: rustc::hir::intravisit::walk_expr
  26: rustc::hir::intravisit::walk_expr
  27: rustc::hir::intravisit::walk_expr
  28: rustc::hir::intravisit::walk_expr
  29: rustc::hir::intravisit::walk_expr
  30: rustc::hir::intravisit::walk_local
  31: rustc::hir::intravisit::walk_expr
  32: rustc::hir::intravisit::Visitor::visit_nested_body
  33: rustc::hir::Crate::visit_all_item_likes
  34: rustc_metadata::encoder::encode_metadata
  35: rustc_metadata::cstore_impl::<impl rustc::middle::cstore::CrateStore for rustc_metadata::cstore::CStore>::encode_metadata
  36: rustc::ty::context::TyCtxt::encode_metadata
  37: rustc_codegen_llvm::base::write_metadata
  38: rustc::util::common::time
  39: rustc_codegen_llvm::base::codegen_crate
  40: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  41: rustc::util::common::time
  42: rustc_driver::driver::phase_4_codegen
  43: rustc_driver::driver::compile_input::{{closure}}
  44: rustc::ty::context::tls::enter_context
  45: <std::thread::local::LocalKey<T>>::with
  46: rustc::ty::context::TyCtxt::create_and_enter
  47: rustc_driver::driver::compile_input
  48: rustc_driver::run_compiler_with_pool
  49: <scoped_tls::ScopedKey<T>>::set
  50: syntax::with_globals
  51: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  52: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:105
  53: rustc_driver::run
  54: rustc_driver::main
  55: std::rt::lang_start::{{closure}}
  56: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:310
  57: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:105
  58: std::rt::lang_start_internal
             at libstd/panicking.rs:289
             at libstd/panic.rs:374
             at libstd/rt.rs:58
  59: main
  60: __libc_start_main
  61: <unknown>
query stack during panic:
#0 [optimized_mir] processing `<StateMachine<EventType>>::inner_process_event::{{closure}}`
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.28.0-nightly (29f48ccf3 2018-06-03) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=2 -C incremental --crate-type lib

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

error: Could not compile `tst`.

@estebank estebank added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html labels Jun 4, 2018
@estebank
Copy link
Contributor

estebank commented May 8, 2019

Current output (fixed to run):

error[E0507]: cannot move out of borrowed content
  --> src/lib.rs:39:11
   |
39 |             .find(|(&event_type, _)|event == event_type)
   |                    ^^----------^^^^
   |                    | |
   |                    | data moved here
   |                    cannot move out of borrowed content
   |
note: move occurs because `event_type` has type `EventType`, which does not implement the `Copy` trait
  --> src/lib.rs:39:13
   |
39 |             .find(|(&event_type, _)|event == event_type)
   |                      ^^^^^^^^^^

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels May 8, 2019
@tesuji
Copy link
Contributor

tesuji commented Jun 12, 2019

Is this test OK? Where can I store this?

use std::any::TypeId;
use std::collections::HashMap;
use std::hash::Hash;

trait State {
    type EventType;
    fn get_type_id_of_state(&self) -> TypeId;
}

struct StateMachine<EventType: Hash + Eq> {
    current_state: Box<dyn State<EventType = EventType>>,
    transition_table:
        HashMap<TypeId, HashMap<EventType, fn() -> Box<dyn State<EventType = EventType>>>>,
}

impl<EventType: Hash + Eq> StateMachine<EventType> {
    fn inner_process_event(&mut self, event: EventType) -> Result<(), i8> {
        let new_state_creation_function = self
            .transition_table
            .iter()
            .find(|(&event_typeid, _)| event_typeid == self.current_state.get_type_id_of_state())
            .ok_or(1)?
            .1
            .iter()
            .find(|(&event_type, _)| event == event_type)
            .ok_or(2)?
            .1;

        self.current_state = new_state_creation_function();
        Ok(())
    }
}

@estebank
Copy link
Contributor

That seems about right add the file to src/test/ui/issues and run ./x.py test src/test/ui --stage 1 --bless.

Centril added a commit to Centril/rust that referenced this issue Jun 14, 2019
Centril added a commit to Centril/rust that referenced this issue Jun 14, 2019
bors added a commit that referenced this issue Jun 14, 2019
Add ui test for issue 51301

Closes #51301
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants