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