@@ -2,7 +2,7 @@ use proc_macro2::TokenStream;
22use quote:: quote;
33use syn:: Ident ;
44
5- use macro_registry:: callsite:: current_source_info;
5+ use macro_registry:: callsite:: { current_source_info, source_info_for_span_or_callsite } ;
66use macro_registry:: query;
77
88use crate :: {
@@ -14,12 +14,14 @@ pub(super) fn resolve_machine_metadata(
1414 module_path : & str ,
1515 machine_ident : & Ident ,
1616) -> Result < MachineInfo , TokenStream > {
17+ let source_info = source_info_for_span_or_callsite ( machine_ident. span ( ) ) ;
1718 let module_path_key: MachinePath = module_path. into ( ) ;
1819 let machine_name = machine_ident. to_string ( ) ;
1920 lookup_loaded_machine_in_module ( & module_path_key, & machine_name) . map_err ( |failure| {
20- let current_line = current_source_info ( ) . map ( |( _, line) | line) . unwrap_or_default ( ) ;
21- let available = available_machine_candidates_in_module ( module_path) ;
22- let same_named_elsewhere = same_named_machine_candidates_elsewhere ( & machine_name, module_path) ;
21+ let current_line = source_info. as_ref ( ) . map ( |( _, line) | * line) . unwrap_or_default ( ) ;
22+ let available = available_machine_candidates_in_module ( source_info. as_ref ( ) , module_path) ;
23+ let same_named_elsewhere =
24+ same_named_machine_candidates_elsewhere ( source_info. as_ref ( ) , & machine_name, module_path) ;
2325 let loaded_same_named_elsewhere =
2426 same_named_loaded_machines_elsewhere ( & module_path_key, & machine_name) ;
2527 let suggested_machine_name = available
@@ -72,11 +74,14 @@ pub(super) fn resolve_machine_metadata(
7274 } else {
7375 String :: new ( )
7476 } ;
75- let missing_attr_line = plain_struct_line_in_module ( module_path, & machine_name) . map ( |line| {
76- format ! (
77- "A struct named `{machine_name}` exists on line {line}, but it is not annotated with `#[machine]`."
78- )
79- } ) ;
77+ let missing_attr_line =
78+ plain_struct_line_in_module ( source_info. as_ref ( ) , module_path, & machine_name) . map (
79+ |line| {
80+ format ! (
81+ "A struct named `{machine_name}` exists on line {line}, but it is not annotated with `#[machine]`."
82+ )
83+ } ,
84+ ) ;
8085 let authority_line = match failure {
8186 LoadedMachineLookupFailure :: NotFound => {
8287 "Statum only resolves `#[machine]` items that have already expanded before this `#[validators]` impl." . to_string ( )
@@ -96,18 +101,28 @@ pub(super) fn resolve_machine_metadata(
96101 } )
97102}
98103
99- fn available_machine_candidates_in_module ( module_path : & str ) -> Vec < query:: ItemCandidate > {
100- let Some ( ( file_path, _) ) = current_source_info ( ) else {
104+ fn source_file_from_info ( source_info : Option < & ( String , usize ) > ) -> Option < String > {
105+ source_info
106+ . map ( |( file_path, _) | file_path. clone ( ) )
107+ . or_else ( || current_source_info ( ) . map ( |( file_path, _) | file_path) )
108+ }
109+
110+ fn available_machine_candidates_in_module (
111+ source_info : Option < & ( String , usize ) > ,
112+ module_path : & str ,
113+ ) -> Vec < query:: ItemCandidate > {
114+ let Some ( file_path) = source_file_from_info ( source_info) else {
101115 return Vec :: new ( ) ;
102116 } ;
103117 query:: candidates_in_module ( & file_path, module_path, query:: ItemKind :: Struct , Some ( "machine" ) )
104118}
105119
106120fn same_named_machine_candidates_elsewhere (
121+ source_info : Option < & ( String , usize ) > ,
107122 machine_name : & str ,
108123 module_path : & str ,
109124) -> Option < Vec < query:: ItemCandidate > > {
110- let ( file_path, _ ) = current_source_info ( ) ?;
125+ let file_path = source_file_from_info ( source_info ) ?;
111126 let candidates = query:: same_named_candidates_elsewhere (
112127 & file_path,
113128 module_path,
@@ -118,8 +133,12 @@ fn same_named_machine_candidates_elsewhere(
118133 ( !candidates. is_empty ( ) ) . then_some ( candidates)
119134}
120135
121- fn plain_struct_line_in_module ( module_path : & str , struct_name : & str ) -> Option < usize > {
122- let ( file_path, _) = current_source_info ( ) ?;
136+ fn plain_struct_line_in_module (
137+ source_info : Option < & ( String , usize ) > ,
138+ module_path : & str ,
139+ struct_name : & str ,
140+ ) -> Option < usize > {
141+ let file_path = source_file_from_info ( source_info) ?;
123142 query:: plain_item_line_in_module (
124143 & file_path,
125144 module_path,
0 commit comments