@@ -117,6 +117,20 @@ pub trait ProcMacroClientInterface {
117117
118118const EXPANDER_STACK_SIZE : usize = 8 * 1024 * 1024 ;
119119
120+ pub enum ExpandError {
121+ Panic ( PanicMessage ) ,
122+ Cancelled { reason : Option < String > } ,
123+ }
124+
125+ impl ExpandError {
126+ pub fn into_string ( self ) -> Option < String > {
127+ match self {
128+ ExpandError :: Panic ( panic_message) => panic_message. into_string ( ) ,
129+ ExpandError :: Cancelled { reason } => reason,
130+ }
131+ }
132+ }
133+
120134impl ProcMacroSrv < ' _ > {
121135 pub fn expand < S : ProcMacroSrvSpan > (
122136 & self ,
@@ -130,10 +144,12 @@ impl ProcMacroSrv<'_> {
130144 call_site : S ,
131145 mixed_site : S ,
132146 callback : Option < ProcMacroClientHandle < ' _ > > ,
133- ) -> Result < token_stream:: TokenStream < S > , PanicMessage > {
147+ ) -> Result < token_stream:: TokenStream < S > , ExpandError > {
134148 let snapped_env = self . env ;
135- let expander = self . expander ( lib. as_ref ( ) ) . map_err ( |err| PanicMessage {
136- message : Some ( format ! ( "failed to load macro: {err}" ) ) ,
149+ let expander = self . expander ( lib. as_ref ( ) ) . map_err ( |err| {
150+ ExpandError :: Panic ( PanicMessage {
151+ message : Some ( format ! ( "failed to load macro: {err}" ) ) ,
152+ } )
137153 } ) ?;
138154
139155 let prev_env = EnvChange :: apply ( snapped_env, env, current_dir. as_ref ( ) . map ( <_ >:: as_ref) ) ;
@@ -151,11 +167,11 @@ impl ProcMacroSrv<'_> {
151167 )
152168 } ) ;
153169 match thread. unwrap ( ) . join ( ) {
154- Ok ( res) => res,
170+ Ok ( res) => res. map_err ( ExpandError :: Panic ) ,
155171
156172 Err ( payload) => {
157173 if let Some ( cancel) = payload. downcast_ref :: < ProcMacroCancelMarker > ( ) {
158- return Err ( PanicMessage { message : Some ( cancel. reason . clone ( ) ) } ) ;
174+ return Err ( ExpandError :: Cancelled { reason : Some ( cancel. reason . clone ( ) ) } ) ;
159175 }
160176 std:: panic:: resume_unwind ( payload)
161177 }
0 commit comments