Move unimpl! macro to unimpl.rs

This commit is contained in:
bjorn3 2018-11-16 19:53:27 +01:00
parent b2a8996f88
commit 2c38313403
3 changed files with 60 additions and 26 deletions

View File

@ -104,8 +104,10 @@ fn trans_fn<'a, 'tcx: 'a>(
};
// Step 6. Codegen function
crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
codegen_fn_content(&mut fx);
with_unimpl_span(fx.mir.span, || {
crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
codegen_fn_content(&mut fx);
});
// Step 7. Write function to file for debugging
let mut writer = crate::pretty_clif::CommentWriter(fx.comments);

View File

@ -2,7 +2,8 @@
rustc_private,
macro_at_most_once_rep,
never_type,
extern_crate_item_prelude
extern_crate_item_prelude,
decl_macro,
)]
#![allow(intra_doc_link_resolution_failure)]
@ -34,7 +35,6 @@ extern crate target_lexicon;
use std::any::Any;
use std::fs::File;
use std::io::Write;
use std::sync::mpsc;
use syntax::symbol::Symbol;
@ -60,14 +60,6 @@ use cranelift_faerie::*;
use crate::constant::ConstantCx;
use crate::prelude::*;
struct NonFatal(pub String);
macro_rules! unimpl {
($($tt:tt)*) => {
panic!(crate::NonFatal(format!($($tt)*)));
};
}
mod abi;
mod allocator;
mod analyze;
@ -82,6 +74,7 @@ mod main_shim;
mod metadata;
mod pretty_clif;
mod trap;
mod unimpl;
mod vtable;
mod prelude {
@ -123,6 +116,7 @@ mod prelude {
pub use crate::base::{trans_operand, trans_place};
pub use crate::common::*;
pub use crate::trap::*;
pub use crate::unimpl::{unimpl, with_unimpl_span};
pub use crate::{Caches, CodegenResults, CrateInfo};
}
@ -482,21 +476,9 @@ fn codegen_mono_items<'a, 'tcx: 'a>(
println!("[codegen mono items] start");
for (&mono_item, &(_linkage, _vis)) in mono_items {
let res = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
unimpl::try_unimpl(tcx, log, || {
base::trans_mono_item(tcx, module, &mut caches, &mut ccx, mono_item);
}));
if let Err(err) = res {
match err.downcast::<NonFatal>() {
Ok(non_fatal) => {
if cfg!(debug_assertions) {
writeln!(log.as_mut().unwrap(), "{}", &non_fatal.0).unwrap();
}
tcx.sess.err(&non_fatal.0)
}
Err(err) => ::std::panic::resume_unwind(err),
}
}
});
}
crate::main_shim::maybe_create_entry_wrapper(tcx, module);

50
src/unimpl.rs Normal file
View File

@ -0,0 +1,50 @@
//! The unimpl! macro is defined here. It is used to generate
//! a non-fatal error on not yet implemented things.
use std::cell::RefCell;
use std::fs::File;
use std::io::Write;
use syntax::source_map::Span;
use rustc::ty::TyCtxt;
thread_local! {
static SPAN_STACK: RefCell<Vec<Span>> = RefCell::new(vec![]);
}
// Just public, because of the unimpl macro
pub struct NonFatal(pub String);
pub macro unimpl($($tt:tt)*) {
panic!(NonFatal(format!($($tt)*)));
}
pub fn try_unimpl(tcx: TyCtxt, log: &mut Option<File>, f: impl FnOnce()) {
let res = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
f()
}));
if let Err(err) = res {
SPAN_STACK.with(|span_stack| {
match err.downcast::<NonFatal>() {
Ok(non_fatal) => {
if cfg!(debug_assertions) {
writeln!(log.as_mut().unwrap(), "{} at {:?}", &non_fatal.0, span_stack.borrow()).unwrap();
}
tcx.sess.err(&non_fatal.0)
}
Err(err) => ::std::panic::resume_unwind(err),
}
span_stack.borrow_mut().clear();
});
}
}
pub fn with_unimpl_span(span: Span, f: impl FnOnce()) {
SPAN_STACK.with(|span_stack| {
span_stack.borrow_mut().push(span);
f();
span_stack.borrow_mut().pop();
});
}