Skip to content

Commit 0fd75c9

Browse files
Merge #7047
7047: Add force_show_panics flag for proc-macro bridge r=jonas-schievink a=edwin0cheng rust-lang/rust#75082 and rust-lang/rust#76292 added a new flag in `proc_macro::Bridge` such that the ABI was changed. These ABI changing are the reason of some weird panics which caused #6880 and maybe related to the panic mentioned in #6820. These changes are landed on rust stable 1.48 so I think it is okay to apply it now. fixes #6880 r @jonas-schievink Co-authored-by: Edwin Cheng <[email protected]>
2 parents 1d874a2 + da92f46 commit 0fd75c9

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

crates/proc_macro_srv/src/dylib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl Expander {
136136
&crate::proc_macro::bridge::server::SameThread,
137137
crate::rustc_server::Rustc::default(),
138138
parsed_body,
139+
false,
139140
);
140141
return res.map(|it| it.subtree);
141142
}
@@ -144,6 +145,7 @@ impl Expander {
144145
&crate::proc_macro::bridge::server::SameThread,
145146
crate::rustc_server::Rustc::default(),
146147
parsed_body,
148+
false,
147149
);
148150
return res.map(|it| it.subtree);
149151
}
@@ -153,6 +155,7 @@ impl Expander {
153155
crate::rustc_server::Rustc::default(),
154156
parsed_attributes,
155157
parsed_body,
158+
false,
156159
);
157160
return res.map(|it| it.subtree);
158161
}

crates/proc_macro_srv/src/proc_macro/bridge/client.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,21 @@ impl BridgeState<'_> {
303303

304304
impl Bridge<'_> {
305305
fn enter<R>(self, f: impl FnOnce() -> R) -> R {
306+
let force_show_panics = self.force_show_panics;
307+
306308
// Hide the default panic output within `proc_macro` expansions.
307309
// NB. the server can't do this because it may use a different libstd.
308310
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
309311
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
310312
let prev = panic::take_hook();
311313
panic::set_hook(Box::new(move |info| {
312-
let hide = BridgeState::with(|state| match state {
313-
BridgeState::NotConnected => false,
314-
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) => force_show_panics,
315319
});
316-
if !hide {
320+
if show {
317321
prev(info)
318322
}
319323
}));

crates/proc_macro_srv/src/proc_macro/bridge/mod.rs

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

226226
/// Server-side function that the client uses to make requests.
227227
dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
228+
229+
/// If 'true', always invoke the default panic hook
230+
force_show_panics: bool,
228231
}
229232

230233
// impl<'a> !Sync for Bridge<'a> {}

crates/proc_macro_srv/src/proc_macro/bridge/server.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub trait ExecutionStrategy {
138138
input: Buffer<u8>,
139139
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
140140
client_data: D,
141+
force_show_panics: bool,
141142
) -> Buffer<u8>;
142143
}
143144

@@ -150,10 +151,14 @@ impl ExecutionStrategy for SameThread {
150151
input: Buffer<u8>,
151152
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
152153
client_data: D,
154+
force_show_panics: bool,
153155
) -> Buffer<u8> {
154156
let mut dispatch = |b| dispatcher.dispatch(b);
155157

156-
run_client(Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, client_data)
158+
run_client(
159+
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into(), force_show_panics },
160+
client_data,
161+
)
157162
}
158163
}
159164

@@ -169,6 +174,7 @@ impl ExecutionStrategy for CrossThread1 {
169174
input: Buffer<u8>,
170175
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
171176
client_data: D,
177+
force_show_panics: bool,
172178
) -> Buffer<u8> {
173179
use std::sync::mpsc::channel;
174180

@@ -182,7 +188,11 @@ impl ExecutionStrategy for CrossThread1 {
182188
};
183189

184190
run_client(
185-
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() },
191+
Bridge {
192+
cached_buffer: input,
193+
dispatch: (&mut dispatch).into(),
194+
force_show_panics,
195+
},
186196
client_data,
187197
)
188198
});
@@ -204,6 +214,7 @@ impl ExecutionStrategy for CrossThread2 {
204214
input: Buffer<u8>,
205215
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
206216
client_data: D,
217+
force_show_panics: bool,
207218
) -> Buffer<u8> {
208219
use std::sync::{Arc, Mutex};
209220

@@ -229,7 +240,11 @@ impl ExecutionStrategy for CrossThread2 {
229240
};
230241

231242
let r = run_client(
232-
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() },
243+
Bridge {
244+
cached_buffer: input,
245+
dispatch: (&mut dispatch).into(),
246+
force_show_panics,
247+
},
233248
client_data,
234249
);
235250

@@ -268,14 +283,21 @@ fn run_server<
268283
input: I,
269284
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
270285
client_data: D,
286+
force_show_panics: bool,
271287
) -> Result<O, PanicMessage> {
272288
let mut dispatcher =
273289
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
274290

275291
let mut b = Buffer::new();
276292
input.encode(&mut b, &mut dispatcher.handle_store);
277293

278-
b = strategy.run_bridge_and_client(&mut dispatcher, b, run_client, client_data);
294+
b = strategy.run_bridge_and_client(
295+
&mut dispatcher,
296+
b,
297+
run_client,
298+
client_data,
299+
force_show_panics,
300+
);
279301

280302
Result::decode(&mut &b[..], &mut dispatcher.handle_store)
281303
}
@@ -286,6 +308,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
286308
strategy: &impl ExecutionStrategy,
287309
server: S,
288310
input: S::TokenStream,
311+
force_show_panics: bool,
289312
) -> Result<S::TokenStream, PanicMessage> {
290313
let client::Client { get_handle_counters, run, f } = *self;
291314
run_server(
@@ -295,6 +318,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
295318
<MarkedTypes<S> as Types>::TokenStream::mark(input),
296319
run,
297320
f,
321+
force_show_panics,
298322
)
299323
.map(<MarkedTypes<S> as Types>::TokenStream::unmark)
300324
}
@@ -307,6 +331,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
307331
server: S,
308332
input: S::TokenStream,
309333
input2: S::TokenStream,
334+
force_show_panics: bool,
310335
) -> Result<S::TokenStream, PanicMessage> {
311336
let client::Client { get_handle_counters, run, f } = *self;
312337
run_server(
@@ -319,6 +344,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
319344
),
320345
run,
321346
f,
347+
force_show_panics,
322348
)
323349
.map(<MarkedTypes<S> as Types>::TokenStream::unmark)
324350
}

0 commit comments

Comments
 (0)