Skip to content

Commit

Permalink
Rollup merge of rust-lang#47933 - Zoxc:plugin-panics, r=nikomatsakis
Browse files Browse the repository at this point in the history
Do not run the default panic hook inside procedural macros.

Fixes rust-lang#47812

r? @nikomatsakis
  • Loading branch information
Manishearth committed Feb 23, 2018
2 parents 063deba + 9d3719b commit abf4d70
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,12 @@ pub mod __internal {
})
}

pub fn in_sess() -> bool
{
let p = CURRENT_SESS.with(|p| p.get());
!p.0.is_null()
}

pub fn with_sess<F, R>(f: F) -> R
where F: FnOnce((&ParseSess, Mark)) -> R
{
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ bitflags = "1.0"
fmt_macros = { path = "../libfmt_macros" }
graphviz = { path = "../libgraphviz" }
jobserver = "0.1"
lazy_static = "1.0.0"
log = { version = "0.4", features = ["release_max_level_info", "std"] }
proc_macro = { path = "../libproc_macro" }
rustc_apfloat = { path = "../librustc_apfloat" }
rustc_back = { path = "../librustc_back" }
rustc_const_math = { path = "../librustc_const_math" }
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#![feature(never_type)]
#![feature(non_exhaustive)]
#![feature(nonzero)]
#![feature(proc_macro_internals)]
#![feature(quote)]
#![feature(refcell_replace_swap)]
#![feature(rustc_diagnostic_macros)]
Expand All @@ -81,6 +82,7 @@ extern crate core;
extern crate fmt_macros;
extern crate getopts;
extern crate graphviz;
#[macro_use] extern crate lazy_static;
#[cfg(windows)]
extern crate libc;
extern crate rustc_back;
Expand All @@ -92,6 +94,7 @@ extern crate rustc_errors as errors;
#[macro_use] extern crate syntax;
extern crate syntax_pos;
extern crate jobserver;
extern crate proc_macro;

extern crate serialize as rustc_serialize; // used by deriving

Expand Down
21 changes: 21 additions & 0 deletions src/librustc/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ use std::ffi::CString;
use std::fmt::Debug;
use std::hash::{Hash, BuildHasher};
use std::iter::repeat;
use std::panic;
use std::path::Path;
use std::time::{Duration, Instant};

use std::sync::mpsc::{Sender};
use syntax_pos::{SpanData};
use ty::maps::{QueryMsg};
use dep_graph::{DepNode};
use proc_macro;
use lazy_static;

// The name of the associated type for `Fn` return types
pub const FN_OUTPUT_NAME: &'static str = "Output";
Expand All @@ -34,6 +37,24 @@ pub struct ErrorReported;

thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));

lazy_static! {
static ref DEFAULT_HOOK: Box<Fn(&panic::PanicInfo) + Sync + Send + 'static> = {
let hook = panic::take_hook();
panic::set_hook(Box::new(panic_hook));
hook
};
}

fn panic_hook(info: &panic::PanicInfo) {
if !proc_macro::__internal::in_sess() {
(*DEFAULT_HOOK)(info)
}
}

pub fn install_panic_hook() {
lazy_static::initialize(&DEFAULT_HOOK);
}

/// Initialized for -Z profile-queries
thread_local!(static PROFQ_CHAN: RefCell<Option<Sender<ProfileQueriesMsg>>> = RefCell::new(None));

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc::middle::cstore::CrateStore;
use rustc::middle::privacy::AccessLevels;
use rustc::ty::{self, TyCtxt, Resolutions, AllArenas};
use rustc::traits;
use rustc::util::common::{ErrorReported, time};
use rustc::util::common::{ErrorReported, time, install_panic_hook};
use rustc_allocator as allocator;
use rustc_borrowck as borrowck;
use rustc_incremental;
Expand Down Expand Up @@ -123,6 +123,8 @@ pub fn compile_input(trans: Box<TransCrate>,
let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
let crate_name =
::rustc_trans_utils::link::find_crate_name(Some(sess), &krate.attrs, input);
install_panic_hook();

let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
phase_2_configure_and_expand(
sess,
Expand Down
1 change: 1 addition & 0 deletions src/test/ui-fulldeps/proc-macro/load-panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

// aux-build:derive-panic.rs
// compile-flags:--error-format human

#[macro_use]
extern crate derive_panic;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui-fulldeps/proc-macro/load-panic.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: proc-macro derive panicked
--> $DIR/load-panic.rs:16:10
--> $DIR/load-panic.rs:17:10
|
16 | #[derive(A)]
17 | #[derive(A)]
| ^
|
= help: message: nope!
Expand Down

0 comments on commit abf4d70

Please sign in to comment.