Closed
Description
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