Skip to content

Commit 3157691

Browse files
committed
Auto merge of #33330 - birkenfeld:issue-29121, r=Manishearth
typeck: when suggesting associated fns, do not show call site as fallback In case we cannot produce a span for the location of the definition, just do not show a span at all. cc: #29121
2 parents 7d8100a + 780f725 commit 3157691

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/librustc_typeck/check/method/suggest.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
233233
match *source {
234234
CandidateSource::ImplSource(impl_did) => {
235235
// Provide the best span we can. Use the item, if local to crate, else
236-
// the impl, if local to crate (item may be defaulted), else the call site.
236+
// the impl, if local to crate (item may be defaulted), else nothing.
237237
let item = impl_item(fcx.tcx(), impl_did, item_name)
238238
.or_else(|| {
239239
trait_item(
@@ -242,8 +242,9 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
242242
item_name
243243
)
244244
}).unwrap();
245-
let impl_span = fcx.tcx().map.def_id_span(impl_did, span);
246-
let item_span = fcx.tcx().map.def_id_span(item.def_id(), impl_span);
245+
let note_span = fcx.tcx().map.span_if_local(item.def_id()).or_else(|| {
246+
fcx.tcx().map.span_if_local(impl_did)
247+
});
247248

248249
let impl_ty = check::impl_self_ty(fcx, span, impl_did).ty;
249250

@@ -255,11 +256,17 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
255256
}
256257
};
257258

258-
span_note!(err, item_span,
259-
"candidate #{} is defined in an impl{} for the type `{}`",
260-
idx + 1,
261-
insertion,
262-
impl_ty);
259+
let note_str = format!("candidate #{} is defined in an impl{} \
260+
for the type `{}`",
261+
idx + 1,
262+
insertion,
263+
impl_ty);
264+
if let Some(note_span) = note_span {
265+
// We have a span pointing to the method. Show note with snippet.
266+
err.span_note(note_span, &note_str);
267+
} else {
268+
err.note(&note_str);
269+
}
263270
}
264271
CandidateSource::TraitSource(trait_did) => {
265272
let item = trait_item(fcx.tcx(), trait_did, item_name).unwrap();

0 commit comments

Comments
 (0)