diff --git a/library/proc_macro/src/bridge/client.rs b/library/proc_macro/src/bridge/client.rs index 31741a1646958..a5966f767596e 100644 --- a/library/proc_macro/src/bridge/client.rs +++ b/library/proc_macro/src/bridge/client.rs @@ -1,8 +1,14 @@ //! Client-side types. use std::cell::RefCell; - -use super::*; +use std::ops::{Bound, Range}; +use std::sync::Once; +use std::{fmt, mem, panic}; + +use crate::bridge::{ + ApiTags, BridgeConfig, Buffer, Decode, Diagnostic, Encode, ExpnGlobals, Literal, PanicMessage, + TokenTree, closure, handle, +}; pub(crate) struct TokenStream { handle: handle::Handle, diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index 8a0822298c38b..8b60043f5dc2a 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -9,9 +9,8 @@ #![deny(unsafe_code)] use std::hash::Hash; +use std::marker; use std::ops::{Bound, Range}; -use std::sync::Once; -use std::{fmt, marker, mem, panic, thread}; use crate::{Delimiter, Level}; @@ -100,6 +99,8 @@ mod handle; #[macro_use] #[forbid(unsafe_code)] mod rpc; +#[forbid(unsafe_code)] +mod panic_message; #[allow(unsafe_code)] mod selfless_reify; #[forbid(unsafe_code)] @@ -108,7 +109,7 @@ pub mod server; mod symbol; use buffer::Buffer; -pub use rpc::PanicMessage; +pub use panic_message::PanicMessage; use rpc::{Decode, Encode}; /// Configuration for establishing an active connection between a server and a diff --git a/library/proc_macro/src/bridge/panic_message.rs b/library/proc_macro/src/bridge/panic_message.rs new file mode 100644 index 0000000000000..a91bdddea576c --- /dev/null +++ b/library/proc_macro/src/bridge/panic_message.rs @@ -0,0 +1,71 @@ +use std::any::Any; + +use crate::bridge::{Buffer, Decode, Encode}; + +/// Simplified version of panic payloads, ignoring +/// types other than `&'static str` and `String`. +pub enum PanicMessage { + StaticStr(&'static str), + String(String), + Unknown, +} + +impl From> for PanicMessage { + fn from(payload: Box) -> Self { + if let Some(s) = payload.downcast_ref::<&'static str>() { + return PanicMessage::StaticStr(s); + } + if let Ok(s) = payload.downcast::() { + return PanicMessage::String(*s); + } + PanicMessage::Unknown + } +} + +impl From for Box { + fn from(val: PanicMessage) -> Self { + match val { + PanicMessage::StaticStr(s) => Box::new(s), + PanicMessage::String(s) => Box::new(s), + PanicMessage::Unknown => { + struct UnknownPanicMessage; + Box::new(UnknownPanicMessage) + } + } + } +} + +impl PanicMessage { + pub fn as_str(&self) -> Option<&str> { + match self { + PanicMessage::StaticStr(s) => Some(s), + PanicMessage::String(s) => Some(s), + PanicMessage::Unknown => None, + } + } + + pub fn into_string(self) -> Option { + match self { + PanicMessage::StaticStr(s) => Some(s.into()), + PanicMessage::String(s) => Some(s), + PanicMessage::Unknown => None, + } + } +} + +impl Encode for PanicMessage { + #[inline] + fn encode(self, w: &mut Buffer, s: &mut S) { + self.as_str().encode(w, s); + } +} + +impl Decode<'_, '_, S> for PanicMessage { + #[inline] + fn decode(r: &mut &[u8], s: &mut S) -> Self { + match Option::::decode(r, s) { + Some(s) => PanicMessage::String(s), + None => PanicMessage::Unknown, + } + } +} diff --git a/library/proc_macro/src/bridge/rpc.rs b/library/proc_macro/src/bridge/rpc.rs index 63a246bac515c..3459426b5cd3f 100644 --- a/library/proc_macro/src/bridge/rpc.rs +++ b/library/proc_macro/src/bridge/rpc.rs @@ -1,6 +1,5 @@ //! Serialization for client-server communication. -use std::any::Any; use std::io::Write; use std::num::NonZero; @@ -259,71 +258,3 @@ impl<'a, S, T: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S> for Vec { vec } } - -/// Simplified version of panic payloads, ignoring -/// types other than `&'static str` and `String`. -pub enum PanicMessage { - StaticStr(&'static str), - String(String), - Unknown, -} - -impl From> for PanicMessage { - fn from(payload: Box) -> Self { - if let Some(s) = payload.downcast_ref::<&'static str>() { - return PanicMessage::StaticStr(s); - } - if let Ok(s) = payload.downcast::() { - return PanicMessage::String(*s); - } - PanicMessage::Unknown - } -} - -impl From for Box { - fn from(val: PanicMessage) -> Self { - match val { - PanicMessage::StaticStr(s) => Box::new(s), - PanicMessage::String(s) => Box::new(s), - PanicMessage::Unknown => { - struct UnknownPanicMessage; - Box::new(UnknownPanicMessage) - } - } - } -} - -impl PanicMessage { - pub fn as_str(&self) -> Option<&str> { - match self { - PanicMessage::StaticStr(s) => Some(s), - PanicMessage::String(s) => Some(s), - PanicMessage::Unknown => None, - } - } - - pub fn into_string(self) -> Option { - match self { - PanicMessage::StaticStr(s) => Some(s.into()), - PanicMessage::String(s) => Some(s), - PanicMessage::Unknown => None, - } - } -} - -impl Encode for PanicMessage { - #[inline] - fn encode(self, w: &mut Buffer, s: &mut S) { - self.as_str().encode(w, s); - } -} - -impl Decode<'_, '_, S> for PanicMessage { - #[inline] - fn decode(r: &mut &[u8], s: &mut S) -> Self { - match Option::::decode(r, s) { - Some(s) => PanicMessage::String(s), - None => PanicMessage::Unknown, - } - } -} diff --git a/library/proc_macro/src/bridge/server.rs b/library/proc_macro/src/bridge/server.rs index c97e07fe4eae1..ff525c6cd7735 100644 --- a/library/proc_macro/src/bridge/server.rs +++ b/library/proc_macro/src/bridge/server.rs @@ -1,10 +1,16 @@ //! Server-side traits. use std::cell::Cell; +use std::hash::Hash; +use std::ops::{Bound, Range}; use std::sync::atomic::AtomicU32; use std::sync::mpsc; +use std::{panic, thread}; -use super::*; +use crate::bridge::{ + ApiTags, BridgeConfig, Buffer, Decode, Diagnostic, Encode, ExpnGlobals, Literal, Mark, Marked, + PanicMessage, TokenTree, client, handle, +}; pub(super) struct HandleStore { token_stream: handle::OwnedStore>, diff --git a/library/proc_macro/src/bridge/symbol.rs b/library/proc_macro/src/bridge/symbol.rs index 2a04f7d808bd5..1e632489f31e1 100644 --- a/library/proc_macro/src/bridge/symbol.rs +++ b/library/proc_macro/src/bridge/symbol.rs @@ -10,9 +10,10 @@ //! proc_macro, this module should probably be removed or simplified. use std::cell::RefCell; +use std::fmt; use std::num::NonZero; -use super::*; +use crate::bridge::{Buffer, Decode, Encode, Mark, arena, client, fxhash, server}; /// Handle for a symbol string stored within the Interner. #[derive(Copy, Clone, PartialEq, Eq, Hash)]