@@ -158,7 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
158158 self . typeck_results . borrow_mut ( ) . used_trait_imports . insert ( import_id) ;
159159 }
160160
161- let ( span, sugg_span , source, item_name, args) = match self . tcx . hir_node ( call_id) {
161+ let ( span, expr_span , source, item_name, args) = match self . tcx . hir_node ( call_id) {
162162 hir:: Node :: Expr ( & hir:: Expr {
163163 kind : hir:: ExprKind :: MethodCall ( segment, rcvr, args, _) ,
164164 span,
@@ -194,6 +194,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
194194 node => unreachable ! ( "{node:?}" ) ,
195195 } ;
196196
197+ // Try to get the span of the identifier within the expression's syntax context
198+ // (if that's different).
199+ let within_macro_span = span. within_macro ( expr_span, self . tcx . sess . source_map ( ) ) ;
200+
197201 // Avoid suggestions when we don't know what's going on.
198202 if let Err ( guar) = rcvr_ty. error_reported ( ) {
199203 return guar;
@@ -207,10 +211,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207211 call_id,
208212 source,
209213 args,
210- sugg_span ,
214+ expr_span ,
211215 & mut no_match_data,
212216 expected,
213217 trait_missing_method,
218+ within_macro_span,
214219 ) ,
215220
216221 MethodError :: Ambiguity ( mut sources) => {
@@ -221,6 +226,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
221226 "multiple applicable items in scope"
222227 ) ;
223228 err. span_label ( item_name. span , format ! ( "multiple `{item_name}` found" ) ) ;
229+ if let Some ( within_macro_span) = within_macro_span {
230+ err. span_label ( within_macro_span, "due to this macro variable" ) ;
231+ }
224232
225233 self . note_candidates_on_method_error (
226234 rcvr_ty,
@@ -230,7 +238,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
230238 span,
231239 & mut err,
232240 & mut sources,
233- Some ( sugg_span ) ,
241+ Some ( expr_span ) ,
234242 ) ;
235243 err. emit ( )
236244 }
@@ -252,6 +260,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
252260 . span_if_local ( def_id)
253261 . unwrap_or_else ( || self . tcx . def_span ( def_id) ) ;
254262 err. span_label ( sp, format ! ( "private {kind} defined here" ) ) ;
263+ if let Some ( within_macro_span) = within_macro_span {
264+ err. span_label ( within_macro_span, "due to this macro variable" ) ;
265+ }
255266 self . suggest_valid_traits ( & mut err, item_name, out_of_scope_traits, true ) ;
256267 err. emit ( )
257268 }
@@ -268,6 +279,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
268279 if !needs_mut {
269280 err. span_label ( bound_span, "this has a `Sized` requirement" ) ;
270281 }
282+ if let Some ( within_macro_span) = within_macro_span {
283+ err. span_label ( within_macro_span, "due to this macro variable" ) ;
284+ }
271285 if !candidates. is_empty ( ) {
272286 let help = format ! (
273287 "{an}other candidate{s} {were} found in the following trait{s}" ,
@@ -581,6 +595,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
581595 no_match_data : & mut NoMatchData < ' tcx > ,
582596 expected : Expectation < ' tcx > ,
583597 trait_missing_method : bool ,
598+ within_macro_span : Option < Span > ,
584599 ) -> ErrorGuaranteed {
585600 let mode = no_match_data. mode ;
586601 let tcx = self . tcx ;
@@ -721,6 +736,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
721736 if tcx. sess . source_map ( ) . is_multiline ( sugg_span) {
722737 err. span_label ( sugg_span. with_hi ( span. lo ( ) ) , "" ) ;
723738 }
739+ if let Some ( within_macro_span) = within_macro_span {
740+ err. span_label ( within_macro_span, "due to this macro variable" ) ;
741+ }
724742
725743 if short_ty_str. len ( ) < ty_str. len ( ) && ty_str. len ( ) > 10 {
726744 ty_str = short_ty_str;
0 commit comments