Skip to content

Commit 9d3719b

Browse files
committed
Do not run the default panic hook inside procedural macros. Fixes #47812
1 parent 27a046e commit 9d3719b

File tree

8 files changed

+40
-3
lines changed

8 files changed

+40
-3
lines changed

src/Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/libproc_macro/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,12 @@ pub mod __internal {
844844
})
845845
}
846846

847+
pub fn in_sess() -> bool
848+
{
849+
let p = CURRENT_SESS.with(|p| p.get());
850+
!p.0.is_null()
851+
}
852+
847853
pub fn with_sess<F, R>(f: F) -> R
848854
where F: FnOnce((&ParseSess, Mark)) -> R
849855
{

src/librustc/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ bitflags = "1.0"
1414
fmt_macros = { path = "../libfmt_macros" }
1515
graphviz = { path = "../libgraphviz" }
1616
jobserver = "0.1"
17+
lazy_static = "1.0.0"
1718
log = { version = "0.4", features = ["release_max_level_info", "std"] }
19+
proc_macro = { path = "../libproc_macro" }
1820
rustc_apfloat = { path = "../librustc_apfloat" }
1921
rustc_back = { path = "../librustc_back" }
2022
rustc_const_math = { path = "../librustc_const_math" }

src/librustc/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#![feature(never_type)]
6161
#![feature(non_exhaustive)]
6262
#![feature(nonzero)]
63+
#![feature(proc_macro_internals)]
6364
#![feature(quote)]
6465
#![feature(refcell_replace_swap)]
6566
#![feature(rustc_diagnostic_macros)]
@@ -80,6 +81,7 @@ extern crate core;
8081
extern crate fmt_macros;
8182
extern crate getopts;
8283
extern crate graphviz;
84+
#[macro_use] extern crate lazy_static;
8385
#[cfg(windows)]
8486
extern crate libc;
8587
extern crate rustc_back;
@@ -91,6 +93,7 @@ extern crate rustc_errors as errors;
9193
#[macro_use] extern crate syntax;
9294
extern crate syntax_pos;
9395
extern crate jobserver;
96+
extern crate proc_macro;
9497

9598
extern crate serialize as rustc_serialize; // used by deriving
9699

src/librustc/util/common.rs

+21
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ use std::ffi::CString;
1616
use std::fmt::Debug;
1717
use std::hash::{Hash, BuildHasher};
1818
use std::iter::repeat;
19+
use std::panic;
1920
use std::path::Path;
2021
use std::time::{Duration, Instant};
2122

2223
use std::sync::mpsc::{Sender};
2324
use syntax_pos::{SpanData};
2425
use ty::maps::{QueryMsg};
2526
use dep_graph::{DepNode};
27+
use proc_macro;
28+
use lazy_static;
2629

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

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

40+
lazy_static! {
41+
static ref DEFAULT_HOOK: Box<Fn(&panic::PanicInfo) + Sync + Send + 'static> = {
42+
let hook = panic::take_hook();
43+
panic::set_hook(Box::new(panic_hook));
44+
hook
45+
};
46+
}
47+
48+
fn panic_hook(info: &panic::PanicInfo) {
49+
if !proc_macro::__internal::in_sess() {
50+
(*DEFAULT_HOOK)(info)
51+
}
52+
}
53+
54+
pub fn install_panic_hook() {
55+
lazy_static::initialize(&DEFAULT_HOOK);
56+
}
57+
3758
/// Initialized for -Z profile-queries
3859
thread_local!(static PROFQ_CHAN: RefCell<Option<Sender<ProfileQueriesMsg>>> = RefCell::new(None));
3960

src/librustc_driver/driver.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc::middle::cstore::CrateStore;
2424
use rustc::middle::privacy::AccessLevels;
2525
use rustc::ty::{self, TyCtxt, Resolutions, AllArenas};
2626
use rustc::traits;
27-
use rustc::util::common::{ErrorReported, time};
27+
use rustc::util::common::{ErrorReported, time, install_panic_hook};
2828
use rustc_allocator as allocator;
2929
use rustc_borrowck as borrowck;
3030
use rustc_incremental;
@@ -123,6 +123,8 @@ pub fn compile_input(trans: Box<TransCrate>,
123123
let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
124124
let crate_name =
125125
::rustc_trans_utils::link::find_crate_name(Some(sess), &krate.attrs, input);
126+
install_panic_hook();
127+
126128
let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
127129
phase_2_configure_and_expand(
128130
sess,

src/test/ui-fulldeps/proc-macro/load-panic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
// aux-build:derive-panic.rs
12+
// compile-flags:--error-format human
1213

1314
#[macro_use]
1415
extern crate derive_panic;

src/test/ui-fulldeps/proc-macro/load-panic.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: proc-macro derive panicked
2-
--> $DIR/load-panic.rs:16:10
2+
--> $DIR/load-panic.rs:17:10
33
|
4-
16 | #[derive(A)]
4+
17 | #[derive(A)]
55
| ^
66
|
77
= help: message: nope!

0 commit comments

Comments
 (0)