@@ -158,7 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
158
158
self . typeck_results . borrow_mut ( ) . used_trait_imports . insert ( import_id) ;
159
159
}
160
160
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) {
162
162
hir:: Node :: Expr ( & hir:: Expr {
163
163
kind : hir:: ExprKind :: MethodCall ( segment, rcvr, args, _) ,
164
164
span,
@@ -194,6 +194,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
194
194
node => unreachable ! ( "{node:?}" ) ,
195
195
} ;
196
196
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
+
197
201
// Avoid suggestions when we don't know what's going on.
198
202
if let Err ( guar) = rcvr_ty. error_reported ( ) {
199
203
return guar;
@@ -207,10 +211,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207
211
call_id,
208
212
source,
209
213
args,
210
- sugg_span ,
214
+ expr_span ,
211
215
& mut no_match_data,
212
216
expected,
213
217
trait_missing_method,
218
+ within_macro_span,
214
219
) ,
215
220
216
221
MethodError :: Ambiguity ( mut sources) => {
@@ -221,6 +226,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
221
226
"multiple applicable items in scope"
222
227
) ;
223
228
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
+ }
224
232
225
233
self . note_candidates_on_method_error (
226
234
rcvr_ty,
@@ -230,7 +238,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
230
238
span,
231
239
& mut err,
232
240
& mut sources,
233
- Some ( sugg_span ) ,
241
+ Some ( expr_span ) ,
234
242
) ;
235
243
err. emit ( )
236
244
}
@@ -252,6 +260,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
252
260
. span_if_local ( def_id)
253
261
. unwrap_or_else ( || self . tcx . def_span ( def_id) ) ;
254
262
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
+ }
255
266
self . suggest_valid_traits ( & mut err, item_name, out_of_scope_traits, true ) ;
256
267
err. emit ( )
257
268
}
@@ -268,6 +279,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
268
279
if !needs_mut {
269
280
err. span_label ( bound_span, "this has a `Sized` requirement" ) ;
270
281
}
282
+ if let Some ( within_macro_span) = within_macro_span {
283
+ err. span_label ( within_macro_span, "due to this macro variable" ) ;
284
+ }
271
285
if !candidates. is_empty ( ) {
272
286
let help = format ! (
273
287
"{an}other candidate{s} {were} found in the following trait{s}" ,
@@ -581,6 +595,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
581
595
no_match_data : & mut NoMatchData < ' tcx > ,
582
596
expected : Expectation < ' tcx > ,
583
597
trait_missing_method : bool ,
598
+ within_macro_span : Option < Span > ,
584
599
) -> ErrorGuaranteed {
585
600
let mode = no_match_data. mode ;
586
601
let tcx = self . tcx ;
@@ -721,6 +736,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
721
736
if tcx. sess . source_map ( ) . is_multiline ( sugg_span) {
722
737
err. span_label ( sugg_span. with_hi ( span. lo ( ) ) , "" ) ;
723
738
}
739
+ if let Some ( within_macro_span) = within_macro_span {
740
+ err. span_label ( within_macro_span, "due to this macro variable" ) ;
741
+ }
724
742
725
743
if short_ty_str. len ( ) < ty_str. len ( ) && ty_str. len ( ) > 10 {
726
744
ty_str = short_ty_str;
0 commit comments