11//! The main loop of the proc-macro server.
2+ use proc_macro_api:: bidirectional_protocol:: msg:: { ApiVersionCheck , ListMacros } ;
23use proc_macro_api:: {
34 ProtocolFormat , bidirectional_protocol:: msg as bidirectional, legacy_protocol:: msg as legacy,
45 version:: CURRENT_API_VERSION ,
@@ -72,7 +73,7 @@ fn run_new(
7273
7374 match req {
7475 bidirectional:: BidirectionalMessage :: Request ( request) => match request {
75- bidirectional:: Request :: ListMacros { dylib_path } => {
76+ bidirectional:: Request :: ListMacros ( ListMacros { dylib_path } ) => {
7677 let res = srv. list_macros ( & dylib_path) . map ( |macros| {
7778 macros
7879 . into_iter ( )
@@ -83,7 +84,7 @@ fn run_new(
8384 send_response ( stdout, bidirectional:: Response :: ListMacros ( res) ) ?;
8485 }
8586
86- bidirectional:: Request :: ApiVersionCheck { } => {
87+ bidirectional:: Request :: ApiVersionCheck ( ApiVersionCheck { } ) => {
8788 send_response (
8889 stdout,
8990 bidirectional:: Response :: ApiVersionCheck ( CURRENT_API_VERSION ) ,
@@ -142,6 +143,7 @@ fn handle_expand_id(
142143 let attributes = attributes
143144 . map ( |it| it. to_tokenstream_unresolved :: < SpanTrans > ( CURRENT_API_VERSION , |_, b| b) ) ;
144145
146+ let mut tracked_env = Default :: default ( ) ;
145147 let res = srv
146148 . expand (
147149 lib,
@@ -153,11 +155,18 @@ fn handle_expand_id(
153155 def_site,
154156 call_site,
155157 mixed_site,
158+ & mut tracked_env,
156159 None ,
157160 )
158161 . map ( |it| {
159162 legacy:: FlatTree :: from_tokenstream_raw :: < SpanTrans > ( it, call_site, CURRENT_API_VERSION )
160163 } )
164+ . map ( |tree| bidirectional:: ExpandMacroResponse {
165+ tree,
166+ span_data_table : vec ! [ ] ,
167+ tracked_env_vars : tracked_env. env_vars ,
168+ tracked_paths : tracked_env. paths ,
169+ } )
161170 . map_err ( |e| legacy:: PanicMessage ( e. into_string ( ) . unwrap_or_default ( ) ) ) ;
162171
163172 send_response ( stdout, bidirectional:: Response :: ExpandMacro ( res) )
@@ -343,6 +352,46 @@ impl proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandle<'_> {
343352 other => handle_failure ( other) ,
344353 }
345354 }
355+
356+ fn span_join (
357+ & mut self ,
358+ first : proc_macro_srv:: span:: Span ,
359+ second : proc_macro_srv:: span:: Span ,
360+ ) -> Option < proc_macro_srv:: span:: Span > {
361+ assert_eq ! ( first. anchor. file_id, second. anchor. file_id) ;
362+ let response = self . roundtrip ( bidirectional:: SubRequest :: SpanJoin {
363+ file_id : first. anchor . file_id . as_u32 ( ) ,
364+ ast_id_first : first. anchor . ast_id . into_raw ( ) ,
365+ start_first : first. range . start ( ) . into ( ) ,
366+ end_first : first. range . end ( ) . into ( ) ,
367+ ctx_first : first. ctx . into_u32 ( ) ,
368+ ast_id_second : second. anchor . ast_id . into_raw ( ) ,
369+ start_second : second. range . start ( ) . into ( ) ,
370+ end_second : second. range . end ( ) . into ( ) ,
371+ ctx_second : second. ctx . into_u32 ( ) ,
372+ } ) ;
373+
374+ match response {
375+ Ok ( bidirectional:: SubResponse :: SpanJoinResult { span } ) => {
376+ span. map ( |bidirectional:: SpanJoin { ast_id, start, end, ctx } | {
377+ proc_macro_srv:: span:: Span {
378+ range : proc_macro_srv:: span:: TextRange :: new (
379+ proc_macro_srv:: span:: TextSize :: new ( start) ,
380+ proc_macro_srv:: span:: TextSize :: new ( end) ,
381+ ) ,
382+ anchor : proc_macro_srv:: span:: SpanAnchor {
383+ file_id : first. anchor . file_id ,
384+ ast_id : proc_macro_srv:: span:: ErasedFileAstId :: from_raw ( ast_id) ,
385+ } ,
386+ // SAFETY: spans originate from the server. If the protocol is violated,
387+ // undefined behavior is the caller’s responsibility.
388+ ctx : unsafe { proc_macro_srv:: span:: SyntaxContext :: from_u32 ( ctx) } ,
389+ }
390+ } )
391+ }
392+ other => handle_failure ( other) ,
393+ }
394+ }
346395}
347396
348397fn handle_expand_ra (
@@ -383,6 +432,8 @@ fn handle_expand_ra(
383432 } )
384433 } ) ;
385434
435+ let mut tracked_env = Default :: default ( ) ;
436+
386437 let res = srv
387438 . expand (
388439 lib,
@@ -394,6 +445,7 @@ fn handle_expand_ra(
394445 def_site,
395446 call_site,
396447 mixed_site,
448+ & mut tracked_env,
397449 Some ( & mut ProcMacroClientHandle { stdin, stdout, buf } ) ,
398450 )
399451 . map ( |it| {
@@ -407,10 +459,15 @@ fn handle_expand_ra(
407459 legacy:: serialize_span_data_index_map ( & span_data_table) ,
408460 )
409461 } )
410- . map ( |( tree, span_data_table) | bidirectional:: ExpandMacroExtended { tree, span_data_table } )
462+ . map ( |( tree, span_data_table) | bidirectional:: ExpandMacroResponse {
463+ tree,
464+ span_data_table,
465+ tracked_env_vars : tracked_env. env_vars ,
466+ tracked_paths : tracked_env. paths ,
467+ } )
411468 . map_err ( |e| legacy:: PanicMessage ( e. into_string ( ) . unwrap_or_default ( ) ) ) ;
412469
413- send_response ( stdout, bidirectional:: Response :: ExpandMacroExtended ( res) )
470+ send_response ( stdout, bidirectional:: Response :: ExpandMacro ( res) )
414471}
415472
416473fn run_old (
@@ -480,6 +537,7 @@ fn run_old(
480537 def_site,
481538 call_site,
482539 mixed_site,
540+ & mut Default :: default ( ) ,
483541 None ,
484542 )
485543 . map ( |it| {
@@ -522,6 +580,7 @@ fn run_old(
522580 def_site,
523581 call_site,
524582 mixed_site,
583+ & mut Default :: default ( ) ,
525584 None ,
526585 )
527586 . map ( |it| {
0 commit comments