Skip to content

Commit 024df9c

Browse files
committed
Auto merge of rust-lang#75082 - Aaron1011:feature/proc-macro-backtrace, r=petrochenkov
Add `-Z proc-macro-backtrace` to allow showing proc-macro panics Fixes rust-lang#75050 Previously, we would unconditionally suppress the panic hook during proc-macro execution. This commit adds a new flag `-Z proc-macro-backtrace`, which allows running the panic hook for easier debugging.
2 parents ea31d86 + 0cae45b commit 024df9c

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

proc_macro/src/bridge/client.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,13 @@ impl Bridge<'_> {
311311
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
312312
let prev = panic::take_hook();
313313
panic::set_hook(Box::new(move |info| {
314-
let hide = BridgeState::with(|state| match state {
315-
BridgeState::NotConnected => false,
316-
BridgeState::Connected(_) | BridgeState::InUse => true,
314+
let show = BridgeState::with(|state| match state {
315+
BridgeState::NotConnected => true,
316+
// Something weird is going on, so don't suppress any backtraces
317+
BridgeState::InUse => true,
318+
BridgeState::Connected(bridge) => bridge.force_show_panics,
317319
});
318-
if !hide {
320+
if show {
319321
prev(info)
320322
}
321323
}));

proc_macro/src/bridge/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ pub struct Bridge<'a> {
220220

221221
/// Server-side function that the client uses to make requests.
222222
dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
223+
224+
/// If 'true', always invoke the default panic hook
225+
force_show_panics: bool,
223226
}
224227

225228
impl<'a> !Sync for Bridge<'a> {}

proc_macro/src/bridge/server.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub trait ExecutionStrategy {
135135
input: Buffer<u8>,
136136
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
137137
client_data: D,
138+
force_show_panics: bool,
138139
) -> Buffer<u8>;
139140
}
140141

@@ -147,10 +148,14 @@ impl ExecutionStrategy for SameThread {
147148
input: Buffer<u8>,
148149
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
149150
client_data: D,
151+
force_show_panics: bool,
150152
) -> Buffer<u8> {
151153
let mut dispatch = |b| dispatcher.dispatch(b);
152154

153-
run_client(Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, client_data)
155+
run_client(
156+
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into(), force_show_panics },
157+
client_data,
158+
)
154159
}
155160
}
156161

@@ -166,6 +171,7 @@ impl ExecutionStrategy for CrossThread1 {
166171
input: Buffer<u8>,
167172
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
168173
client_data: D,
174+
force_show_panics: bool,
169175
) -> Buffer<u8> {
170176
use std::sync::mpsc::channel;
171177

@@ -179,7 +185,11 @@ impl ExecutionStrategy for CrossThread1 {
179185
};
180186

181187
run_client(
182-
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() },
188+
Bridge {
189+
cached_buffer: input,
190+
dispatch: (&mut dispatch).into(),
191+
force_show_panics,
192+
},
183193
client_data,
184194
)
185195
});
@@ -201,6 +211,7 @@ impl ExecutionStrategy for CrossThread2 {
201211
input: Buffer<u8>,
202212
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
203213
client_data: D,
214+
force_show_panics: bool,
204215
) -> Buffer<u8> {
205216
use std::sync::{Arc, Mutex};
206217

@@ -226,7 +237,11 @@ impl ExecutionStrategy for CrossThread2 {
226237
};
227238

228239
let r = run_client(
229-
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() },
240+
Bridge {
241+
cached_buffer: input,
242+
dispatch: (&mut dispatch).into(),
243+
force_show_panics,
244+
},
230245
client_data,
231246
);
232247

@@ -265,14 +280,21 @@ fn run_server<
265280
input: I,
266281
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
267282
client_data: D,
283+
force_show_panics: bool,
268284
) -> Result<O, PanicMessage> {
269285
let mut dispatcher =
270286
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
271287

272288
let mut b = Buffer::new();
273289
input.encode(&mut b, &mut dispatcher.handle_store);
274290

275-
b = strategy.run_bridge_and_client(&mut dispatcher, b, run_client, client_data);
291+
b = strategy.run_bridge_and_client(
292+
&mut dispatcher,
293+
b,
294+
run_client,
295+
client_data,
296+
force_show_panics,
297+
);
276298

277299
Result::decode(&mut &b[..], &mut dispatcher.handle_store)
278300
}
@@ -283,6 +305,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
283305
strategy: &impl ExecutionStrategy,
284306
server: S,
285307
input: S::TokenStream,
308+
force_show_panics: bool,
286309
) -> Result<S::TokenStream, PanicMessage> {
287310
let client::Client { get_handle_counters, run, f } = *self;
288311
run_server(
@@ -292,6 +315,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
292315
<MarkedTypes<S> as Types>::TokenStream::mark(input),
293316
run,
294317
f,
318+
force_show_panics,
295319
)
296320
.map(<MarkedTypes<S> as Types>::TokenStream::unmark)
297321
}
@@ -304,6 +328,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
304328
server: S,
305329
input: S::TokenStream,
306330
input2: S::TokenStream,
331+
force_show_panics: bool,
307332
) -> Result<S::TokenStream, PanicMessage> {
308333
let client::Client { get_handle_counters, run, f } = *self;
309334
run_server(
@@ -316,6 +341,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
316341
),
317342
run,
318343
f,
344+
force_show_panics,
319345
)
320346
.map(<MarkedTypes<S> as Types>::TokenStream::unmark)
321347
}

0 commit comments

Comments
 (0)