]> git.lizzy.rs Git - rust.git/commitdiff
Do not run the default panic hook inside procedural macros. Fixes #47812
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Thu, 1 Feb 2018 17:10:56 +0000 (18:10 +0100)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Tue, 20 Feb 2018 18:16:49 +0000 (19:16 +0100)
src/Cargo.lock
src/libproc_macro/lib.rs
src/librustc/Cargo.toml
src/librustc/lib.rs
src/librustc/util/common.rs
src/librustc_driver/driver.rs
src/test/ui-fulldeps/proc-macro/load-panic.rs
src/test/ui-fulldeps/proc-macro/load-panic.stderr

index d8306c66daf84d6ecad292048407ef99ef341933..5b56c6e12d6241e5c21c5a98626867f2dfee120f 100644 (file)
@@ -1623,7 +1623,9 @@ dependencies = [
  "fmt_macros 0.0.0",
  "graphviz 0.0.0",
  "jobserver 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "proc_macro 0.0.0",
  "rustc_apfloat 0.0.0",
  "rustc_back 0.0.0",
  "rustc_const_math 0.0.0",
index 6768e0ade43049387510f6dd65716b06917afa95..878a536836d23a4450e092fa3505a20c6a706248 100644 (file)
@@ -844,6 +844,12 @@ fn drop(&mut self) {
         })
     }
 
+    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
     {
index 2c4898cb2c006dc61f1dfbe69d9910c6d94bd319..7e84a69dd7913b99356acd128321d93a0cf5602e 100644 (file)
@@ -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" }
index a7a261950593164b3f4736bd86c8e073a722c8d5..79333d1018ab15f75559cf79116c67b7fa4f3a2d 100644 (file)
@@ -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)]
@@ -80,6 +81,7 @@
 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;
@@ -91,6 +93,7 @@
 #[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
 
index 2971f3e853a99d06f38e21e3b13b60ada46ebe20..55e9a98e7ef1329e4b12cf397699ebc46dc8b4c1 100644 (file)
@@ -16,6 +16,7 @@
 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};
 
@@ -23,6 +24,8 @@
 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";
 
 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));
 
index b8a1fe99105406b63edbe42f6bb6e343a38578d6..eb67c9ce4b7d8be2e614f59df1ff71bb87823e11 100644 (file)
@@ -24,7 +24,7 @@
 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;
@@ -123,6 +123,8 @@ macro_rules! controller_entry_point {
         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,
index 328f398efd5c6c9e6bd7640b1b678bb1198b7539..462eaf0341704a66987eb650cb621df47f5acaf0 100644 (file)
@@ -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;
index 1be1609d45b2b2ded2fadcc3ca8d55b5183836fc..ab2ab84315a9390a84ecd646fe430c901568b876 100644 (file)
@@ -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!