Skip to content

Commit c061369

Browse files
Rollup merge of #157793 - Mark-Simulacrum:proc-macro-refactor, r=lqd,bjorn3
Refactor libproc_macro: remove * imports This factors out (and goes a bit further) some of the work from #157590 which I think can/should be merged independently of the wasm proc macros strategy, since it just makes it easier to read code in the bridge. It was confusing to get warnings/errors when commenting code that looked unused but was actually pulled in due to the `*` imports from other modules. This also factors the panic message into a separate module since it's not really related to `rpc` in a direct sense. Best reviewed by-commit. r? bjorn3 since you've probably at least glanced at this code recently, but happy to re-roll if you'd prefer.
2 parents 4e391cf + 6b91c82 commit c061369

6 files changed

Lines changed: 92 additions & 76 deletions

File tree

library/proc_macro/src/bridge/client.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
//! Client-side types.
22
33
use std::cell::RefCell;
4-
5-
use super::*;
4+
use std::ops::{Bound, Range};
5+
use std::sync::Once;
6+
use std::{fmt, mem, panic};
7+
8+
use crate::bridge::{
9+
ApiTags, BridgeConfig, Buffer, Decode, Diagnostic, Encode, ExpnGlobals, Literal, PanicMessage,
10+
TokenTree, closure, handle,
11+
};
612

713
pub(crate) struct TokenStream {
814
handle: handle::Handle,

library/proc_macro/src/bridge/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#![deny(unsafe_code)]
1010

1111
use std::hash::Hash;
12+
use std::marker;
1213
use std::ops::{Bound, Range};
13-
use std::sync::Once;
14-
use std::{fmt, marker, mem, panic, thread};
1514

1615
use crate::{Delimiter, Level};
1716

@@ -100,6 +99,8 @@ mod handle;
10099
#[macro_use]
101100
#[forbid(unsafe_code)]
102101
mod rpc;
102+
#[forbid(unsafe_code)]
103+
mod panic_message;
103104
#[allow(unsafe_code)]
104105
mod selfless_reify;
105106
#[forbid(unsafe_code)]
@@ -108,7 +109,7 @@ pub mod server;
108109
mod symbol;
109110

110111
use buffer::Buffer;
111-
pub use rpc::PanicMessage;
112+
pub use panic_message::PanicMessage;
112113
use rpc::{Decode, Encode};
113114

114115
/// Configuration for establishing an active connection between a server and a
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::any::Any;
2+
3+
use crate::bridge::{Buffer, Decode, Encode};
4+
5+
/// Simplified version of panic payloads, ignoring
6+
/// types other than `&'static str` and `String`.
7+
pub enum PanicMessage {
8+
StaticStr(&'static str),
9+
String(String),
10+
Unknown,
11+
}
12+
13+
impl From<Box<dyn Any + Send>> for PanicMessage {
14+
fn from(payload: Box<dyn Any + Send + 'static>) -> Self {
15+
if let Some(s) = payload.downcast_ref::<&'static str>() {
16+
return PanicMessage::StaticStr(s);
17+
}
18+
if let Ok(s) = payload.downcast::<String>() {
19+
return PanicMessage::String(*s);
20+
}
21+
PanicMessage::Unknown
22+
}
23+
}
24+
25+
impl From<PanicMessage> for Box<dyn Any + Send> {
26+
fn from(val: PanicMessage) -> Self {
27+
match val {
28+
PanicMessage::StaticStr(s) => Box::new(s),
29+
PanicMessage::String(s) => Box::new(s),
30+
PanicMessage::Unknown => {
31+
struct UnknownPanicMessage;
32+
Box::new(UnknownPanicMessage)
33+
}
34+
}
35+
}
36+
}
37+
38+
impl PanicMessage {
39+
pub fn as_str(&self) -> Option<&str> {
40+
match self {
41+
PanicMessage::StaticStr(s) => Some(s),
42+
PanicMessage::String(s) => Some(s),
43+
PanicMessage::Unknown => None,
44+
}
45+
}
46+
47+
pub fn into_string(self) -> Option<String> {
48+
match self {
49+
PanicMessage::StaticStr(s) => Some(s.into()),
50+
PanicMessage::String(s) => Some(s),
51+
PanicMessage::Unknown => None,
52+
}
53+
}
54+
}
55+
56+
impl<S> Encode<S> for PanicMessage {
57+
#[inline]
58+
fn encode(self, w: &mut Buffer, s: &mut S) {
59+
self.as_str().encode(w, s);
60+
}
61+
}
62+
63+
impl<S> Decode<'_, '_, S> for PanicMessage {
64+
#[inline]
65+
fn decode(r: &mut &[u8], s: &mut S) -> Self {
66+
match Option::<String>::decode(r, s) {
67+
Some(s) => PanicMessage::String(s),
68+
None => PanicMessage::Unknown,
69+
}
70+
}
71+
}

library/proc_macro/src/bridge/rpc.rs

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Serialization for client-server communication.
22
3-
use std::any::Any;
43
use std::io::Write;
54
use std::num::NonZero;
65

@@ -259,71 +258,3 @@ impl<'a, S, T: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S> for Vec<T> {
259258
vec
260259
}
261260
}
262-
263-
/// Simplified version of panic payloads, ignoring
264-
/// types other than `&'static str` and `String`.
265-
pub enum PanicMessage {
266-
StaticStr(&'static str),
267-
String(String),
268-
Unknown,
269-
}
270-
271-
impl From<Box<dyn Any + Send>> for PanicMessage {
272-
fn from(payload: Box<dyn Any + Send + 'static>) -> Self {
273-
if let Some(s) = payload.downcast_ref::<&'static str>() {
274-
return PanicMessage::StaticStr(s);
275-
}
276-
if let Ok(s) = payload.downcast::<String>() {
277-
return PanicMessage::String(*s);
278-
}
279-
PanicMessage::Unknown
280-
}
281-
}
282-
283-
impl From<PanicMessage> for Box<dyn Any + Send> {
284-
fn from(val: PanicMessage) -> Self {
285-
match val {
286-
PanicMessage::StaticStr(s) => Box::new(s),
287-
PanicMessage::String(s) => Box::new(s),
288-
PanicMessage::Unknown => {
289-
struct UnknownPanicMessage;
290-
Box::new(UnknownPanicMessage)
291-
}
292-
}
293-
}
294-
}
295-
296-
impl PanicMessage {
297-
pub fn as_str(&self) -> Option<&str> {
298-
match self {
299-
PanicMessage::StaticStr(s) => Some(s),
300-
PanicMessage::String(s) => Some(s),
301-
PanicMessage::Unknown => None,
302-
}
303-
}
304-
305-
pub fn into_string(self) -> Option<String> {
306-
match self {
307-
PanicMessage::StaticStr(s) => Some(s.into()),
308-
PanicMessage::String(s) => Some(s),
309-
PanicMessage::Unknown => None,
310-
}
311-
}
312-
}
313-
314-
impl<S> Encode<S> for PanicMessage {
315-
#[inline]
316-
fn encode(self, w: &mut Buffer, s: &mut S) {
317-
self.as_str().encode(w, s);
318-
}
319-
}
320-
321-
impl<S> Decode<'_, '_, S> for PanicMessage {
322-
#[inline]
323-
fn decode(r: &mut &[u8], s: &mut S) -> Self {
324-
match Option::<String>::decode(r, s) {
325-
Some(s) => PanicMessage::String(s),
326-
None => PanicMessage::Unknown,
327-
}
328-
}
329-
}

library/proc_macro/src/bridge/server.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
//! Server-side traits.
22
33
use std::cell::Cell;
4+
use std::hash::Hash;
5+
use std::ops::{Bound, Range};
46
use std::sync::atomic::AtomicU32;
57
use std::sync::mpsc;
8+
use std::{panic, thread};
69

7-
use super::*;
10+
use crate::bridge::{
11+
ApiTags, BridgeConfig, Buffer, Decode, Diagnostic, Encode, ExpnGlobals, Literal, Mark, Marked,
12+
PanicMessage, TokenTree, client, handle,
13+
};
814

915
pub(super) struct HandleStore<S: Server> {
1016
token_stream: handle::OwnedStore<MarkedTokenStream<S>>,

library/proc_macro/src/bridge/symbol.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//! proc_macro, this module should probably be removed or simplified.
1111
1212
use std::cell::RefCell;
13+
use std::fmt;
1314
use std::num::NonZero;
1415

15-
use super::*;
16+
use crate::bridge::{Buffer, Decode, Encode, Mark, arena, client, fxhash, server};
1617

1718
/// Handle for a symbol string stored within the Interner.
1819
#[derive(Copy, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)