Skip to content

Commit 83dea35

Browse files
committed
replace guess_head_span with def_span
1 parent 5b8cf49 commit 83dea35

File tree

24 files changed

+111
-177
lines changed

24 files changed

+111
-177
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,12 +812,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
812812
return FnSelfUse {
813813
var_span: stmt.source_info.span,
814814
fn_call_span: *fn_span,
815-
fn_span: self
816-
.infcx
817-
.tcx
818-
.sess
819-
.source_map()
820-
.guess_head_span(self.infcx.tcx.def_span(method_did)),
815+
fn_span: self.infcx.tcx.def_span(method_did),
821816
kind,
822817
};
823818
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
155155
});
156156

157157
if let Ok(Some(ImplSource::UserDefined(data))) = implsrc {
158-
let span =
159-
tcx.sess.source_map().guess_head_span(tcx.def_span(data.impl_def_id));
158+
let span = tcx.def_span(data.impl_def_id);
160159
err.span_note(span, "impl defined here, but it is not `const`");
161160
}
162161
}
@@ -205,7 +204,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
205204

206205
match self_ty.kind() {
207206
FnDef(def_id, ..) => {
208-
let span = tcx.sess.source_map().guess_head_span(tcx.def_span(*def_id));
207+
let span = tcx.def_span(*def_id);
209208
if ccx.tcx.is_const_fn_raw(*def_id) {
210209
span_bug!(span, "calling const FnDef errored when it shouldn't");
211210
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
148148
tcx: TyCtxt<'tcx>,
149149
region: ty::Region<'tcx>,
150150
) -> (String, Span) {
151-
let sm = tcx.sess.source_map();
152-
153151
let scope = region.free_region_binding_scope(tcx).expect_local();
154152
match *region {
155153
ty::ReEarlyBound(ref br) => {
156-
let mut sp = sm.guess_head_span(tcx.def_span(scope));
154+
let mut sp = tcx.def_span(scope);
157155
if let Some(param) =
158156
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
159157
{
@@ -174,7 +172,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
174172
} else {
175173
match fr.bound_region {
176174
ty::BoundRegionKind::BrNamed(_, name) => {
177-
let mut sp = sm.guess_head_span(tcx.def_span(scope));
175+
let mut sp = tcx.def_span(scope);
178176
if let Some(param) =
179177
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
180178
{
@@ -193,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
193191
),
194192
_ => (
195193
format!("the lifetime `{}` as defined here", region),
196-
sm.guess_head_span(tcx.def_span(scope)),
194+
tcx.def_span(scope),
197195
),
198196
}
199197
}

compiler/rustc_infer/src/traits/error_reporting/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2323

2424
let mut err = struct_span_err!(self.tcx.sess, sp, E0276, "{}", msg);
2525

26-
if let Some(trait_item_span) = self.tcx.hir().span_if_local(trait_item_def_id) {
27-
let span = self.tcx.sess.source_map().guess_head_span(trait_item_span);
26+
if trait_item_def_id.is_local() {
2827
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
29-
err.span_label(span, format!("definition of `{}` from trait", item_name));
28+
err.span_label(
29+
self.tcx.def_span(trait_item_def_id),
30+
format!("definition of `{}` from trait", item_name),
31+
);
3032
}
3133

3234
err.span_label(sp, format!("impl has extra requirement {}", requirement));

compiler/rustc_lint/src/builtin.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ impl MissingDoc {
551551
&self,
552552
cx: &LateContext<'_>,
553553
def_id: LocalDefId,
554-
sp: Span,
555554
article: &'static str,
556555
desc: &'static str,
557556
) {
@@ -610,13 +609,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
610609
}
611610

612611
fn check_crate(&mut self, cx: &LateContext<'_>) {
613-
self.check_missing_docs_attrs(
614-
cx,
615-
CRATE_DEF_ID,
616-
cx.tcx.def_span(CRATE_DEF_ID),
617-
"the",
618-
"crate",
619-
);
612+
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, "the", "crate");
620613
}
621614

622615
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
@@ -646,13 +639,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
646639

647640
let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
648641

649-
self.check_missing_docs_attrs(cx, it.def_id, it.span, article, desc);
642+
self.check_missing_docs_attrs(cx, it.def_id, article, desc);
650643
}
651644

652645
fn check_trait_item(&mut self, cx: &LateContext<'_>, trait_item: &hir::TraitItem<'_>) {
653646
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
654647

655-
self.check_missing_docs_attrs(cx, trait_item.def_id, trait_item.span, article, desc);
648+
self.check_missing_docs_attrs(cx, trait_item.def_id, article, desc);
656649
}
657650

658651
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
@@ -680,23 +673,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
680673
}
681674

682675
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
683-
self.check_missing_docs_attrs(cx, impl_item.def_id, impl_item.span, article, desc);
676+
self.check_missing_docs_attrs(cx, impl_item.def_id, article, desc);
684677
}
685678

686679
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'_>) {
687680
let (article, desc) = cx.tcx.article_and_description(foreign_item.def_id.to_def_id());
688-
self.check_missing_docs_attrs(cx, foreign_item.def_id, foreign_item.span, article, desc);
681+
self.check_missing_docs_attrs(cx, foreign_item.def_id, article, desc);
689682
}
690683

691684
fn check_field_def(&mut self, cx: &LateContext<'_>, sf: &hir::FieldDef<'_>) {
692685
if !sf.is_positional() {
693686
let def_id = cx.tcx.hir().local_def_id(sf.hir_id);
694-
self.check_missing_docs_attrs(cx, def_id, sf.span, "a", "struct field")
687+
self.check_missing_docs_attrs(cx, def_id, "a", "struct field")
695688
}
696689
}
697690

698691
fn check_variant(&mut self, cx: &LateContext<'_>, v: &hir::Variant<'_>) {
699-
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), v.span, "a", "variant");
692+
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), "a", "variant");
700693
}
701694
}
702695

compiler/rustc_middle/src/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ fn foo(&self) -> Self::T { String::new() }
795795
if item_def_id == proj_ty_item_def_id =>
796796
{
797797
Some((
798-
self.sess.source_map().guess_head_span(self.def_span(item.def_id)),
798+
self.def_span(item.def_id),
799799
format!("consider calling `{}`", self.def_path_str(item.def_id)),
800800
))
801801
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,18 +1112,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11121112
})
11131113
.collect::<Option<Vec<ArgKind>>>()?,
11141114
),
1115-
Node::Item(&hir::Item { span, kind: hir::ItemKind::Fn(ref sig, ..), .. })
1116-
| Node::ImplItem(&hir::ImplItem {
1117-
span,
1118-
kind: hir::ImplItemKind::Fn(ref sig, _),
1119-
..
1120-
})
1115+
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref sig, ..), .. })
1116+
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref sig, _), .. })
11211117
| Node::TraitItem(&hir::TraitItem {
1122-
span,
1123-
kind: hir::TraitItemKind::Fn(ref sig, _),
1124-
..
1118+
kind: hir::TraitItemKind::Fn(ref sig, _), ..
11251119
}) => (
1126-
sm.guess_head_span(span),
1120+
sig.span,
11271121
sig.decl
11281122
.inputs
11291123
.iter()
@@ -1138,7 +1132,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11381132
),
11391133
Node::Ctor(ref variant_data) => {
11401134
let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| hir.span(id));
1141-
let span = sm.guess_head_span(span);
11421135
(span, vec![ArgKind::empty(); variant_data.fields().len()])
11431136
}
11441137
_ => panic!("non-FnLike node found: {:?}", node),
@@ -2185,7 +2178,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21852178
let mut post = vec![];
21862179
for def_id in impls {
21872180
match self.tcx.span_of_impl(*def_id) {
2188-
Ok(span) => spans.push(self.tcx.sess.source_map().guess_head_span(span)),
2181+
Ok(span) => spans.push(span),
21892182
Err(name) => {
21902183
crates.push(name);
21912184
if let Some(header) = to_pretty_impl_header(self.tcx, *def_id) {
@@ -2532,8 +2525,7 @@ pub fn recursive_type_with_infinite_size_error<'tcx>(
25322525
spans: Vec<(Span, Option<hir::HirId>)>,
25332526
) {
25342527
assert!(type_def_id.is_local());
2535-
let span = tcx.hir().span_if_local(type_def_id).unwrap();
2536-
let span = tcx.sess.source_map().guess_head_span(span);
2528+
let span = tcx.def_span(type_def_id);
25372529
let path = tcx.def_path_str(type_def_id);
25382530
let mut err =
25392531
struct_span_err!(tcx.sess, span, E0072, "recursive type `{}` has infinite size", path);

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,7 @@ fn report_negative_positive_conflict(
341341
positive_impl_def_id: DefId,
342342
sg: &mut specialization_graph::Graph,
343343
) {
344-
let impl_span = tcx
345-
.sess
346-
.source_map()
347-
.guess_head_span(tcx.span_of_impl(local_impl_def_id.to_def_id()).unwrap());
344+
let impl_span = tcx.span_of_impl(local_impl_def_id.to_def_id()).unwrap();
348345

349346
let mut err = struct_span_err!(
350347
tcx.sess,
@@ -357,10 +354,7 @@ fn report_negative_positive_conflict(
357354

358355
match tcx.span_of_impl(negative_impl_def_id) {
359356
Ok(span) => {
360-
err.span_label(
361-
tcx.sess.source_map().guess_head_span(span),
362-
"negative implementation here".to_string(),
363-
);
357+
err.span_label(span, "negative implementation here");
364358
}
365359
Err(cname) => {
366360
err.note(&format!("negative implementation in crate `{}`", cname));
@@ -369,10 +363,7 @@ fn report_negative_positive_conflict(
369363

370364
match tcx.span_of_impl(positive_impl_def_id) {
371365
Ok(span) => {
372-
err.span_label(
373-
tcx.sess.source_map().guess_head_span(span),
374-
"positive implementation here".to_string(),
375-
);
366+
err.span_label(span, "positive implementation here");
376367
}
377368
Err(cname) => {
378369
err.note(&format!("positive implementation in crate `{}`", cname));
@@ -389,8 +380,7 @@ fn report_conflicting_impls(
389380
used_to_be_allowed: Option<FutureCompatOverlapErrorKind>,
390381
sg: &mut specialization_graph::Graph,
391382
) {
392-
let impl_span =
393-
tcx.sess.source_map().guess_head_span(tcx.span_of_impl(impl_def_id.to_def_id()).unwrap());
383+
let impl_span = tcx.def_span(impl_def_id);
394384

395385
// Work to be done after we've built the DiagnosticBuilder. We have to define it
396386
// now because the struct_lint methods don't return back the DiagnosticBuilder
@@ -417,10 +407,7 @@ fn report_conflicting_impls(
417407
let mut err = err.build(&msg);
418408
match tcx.span_of_impl(overlap.with_impl) {
419409
Ok(span) => {
420-
err.span_label(
421-
tcx.sess.source_map().guess_head_span(span),
422-
"first implementation here".to_string(),
423-
);
410+
err.span_label(span, "first implementation here".to_string());
424411

425412
err.span_label(
426413
impl_span,

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,9 +1958,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19581958
);
19591959
}
19601960

1961-
if let Some(sp) = tcx.hir().span_if_local(adt_def.did()) {
1962-
let sp = tcx.sess.source_map().guess_head_span(sp);
1963-
err.span_label(sp, format!("variant `{}` not found here", assoc_ident));
1961+
if adt_def.did().is_local() {
1962+
err.span_label(
1963+
tcx.def_span(adt_def.did()),
1964+
format!("variant `{assoc_ident}` not found for this enum"),
1965+
);
19641966
}
19651967

19661968
err.emit()
@@ -2450,7 +2452,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24502452

24512453
let msg = format!("`Self` is of type `{ty}`");
24522454
if let (Ok(i_sp), Some(t_sp)) = (span_of_impl, span_of_ty) {
2453-
let i_sp = tcx.sess.source_map().guess_head_span(i_sp);
24542455
let mut span: MultiSpan = vec![t_sp].into();
24552456
span.push_span_label(
24562457
i_sp,

compiler/rustc_typeck/src/check/check.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,9 @@ fn check_panic_info_fn(
288288
tcx.sess.span_err(decl.output.span(), "return type should be `!`");
289289
}
290290

291-
let span = tcx.def_span(fn_id);
292291
let inputs = fn_sig.inputs();
293292
if inputs.len() != 1 {
294-
let span = tcx.sess.source_map().guess_head_span(span);
295-
tcx.sess.span_err(span, "function should have one argument");
293+
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
296294
return;
297295
}
298296

@@ -345,9 +343,7 @@ fn check_alloc_error_fn(
345343

346344
let inputs = fn_sig.inputs();
347345
if inputs.len() != 1 {
348-
let span = tcx.def_span(fn_id);
349-
let span = tcx.sess.source_map().guess_head_span(span);
350-
tcx.sess.span_err(span, "function should have one argument");
346+
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
351347
return;
352348
}
353349

@@ -1034,7 +1030,6 @@ fn check_impl_items_against_trait<'tcx>(
10341030
compare_impl_method(
10351031
tcx,
10361032
&ty_impl_item,
1037-
impl_item.span,
10381033
&ty_trait_item,
10391034
impl_trait_ref,
10401035
opt_trait_span,
@@ -1094,17 +1089,20 @@ fn check_impl_items_against_trait<'tcx>(
10941089
}
10951090

10961091
if !missing_items.is_empty() {
1097-
let impl_span = tcx.sess.source_map().guess_head_span(full_impl_span);
1098-
missing_items_err(tcx, impl_span, &missing_items, full_impl_span);
1092+
missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
10991093
}
11001094

11011095
if let Some(missing_items) = must_implement_one_of {
1102-
let impl_span = tcx.sess.source_map().guess_head_span(full_impl_span);
11031096
let attr_span = tcx
11041097
.get_attr(impl_trait_ref.def_id, sym::rustc_must_implement_one_of)
11051098
.map(|attr| attr.span);
11061099

1107-
missing_items_must_implement_one_of_err(tcx, impl_span, missing_items, attr_span);
1100+
missing_items_must_implement_one_of_err(
1101+
tcx,
1102+
tcx.def_span(impl_id),
1103+
missing_items,
1104+
attr_span,
1105+
);
11081106
}
11091107
}
11101108
}

compiler/rustc_typeck/src/check/compare_method.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ use super::{potentially_plural_count, FnCtxt, Inherited};
3333
pub(crate) fn compare_impl_method<'tcx>(
3434
tcx: TyCtxt<'tcx>,
3535
impl_m: &ty::AssocItem,
36-
impl_m_span: Span,
3736
trait_m: &ty::AssocItem,
3837
impl_trait_ref: ty::TraitRef<'tcx>,
3938
trait_item_span: Option<Span>,
4039
) {
4140
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);
4241

43-
let impl_m_span = tcx.sess.source_map().guess_head_span(impl_m_span);
42+
let impl_m_span = tcx.def_span(impl_m.def_id);
4443

4544
if let Err(_) = compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref) {
4645
return;
@@ -444,13 +443,9 @@ fn check_region_bounds_on_impl_item<'tcx>(
444443
.as_local()
445444
.and_then(|did| tcx.hir().get_generics(did))
446445
.map_or(def_span, |g| g.span);
447-
let generics_span = tcx.hir().span_if_local(trait_m.def_id).map(|sp| {
448-
let def_sp = tcx.sess.source_map().guess_head_span(sp);
449-
trait_m
450-
.def_id
451-
.as_local()
452-
.and_then(|did| tcx.hir().get_generics(did))
453-
.map_or(def_sp, |g| g.span)
446+
let generics_span = trait_m.def_id.as_local().map(|did| {
447+
let def_sp = tcx.def_span(did);
448+
tcx.hir().get_generics(did).map_or(def_sp, |g| g.span)
454449
});
455450

456451
let reported = tcx.sess.emit_err(LifetimesOrBoundsMismatchOnTrait {
@@ -1044,8 +1039,7 @@ fn compare_generic_param_kinds<'tcx>(
10441039
err.span_label(trait_header_span, "");
10451040
err.span_label(param_trait_span, make_param_message("expected", param_trait));
10461041

1047-
let impl_header_span =
1048-
tcx.sess.source_map().guess_head_span(tcx.def_span(tcx.parent(impl_item.def_id)));
1042+
let impl_header_span = tcx.def_span(tcx.parent(impl_item.def_id));
10491043
err.span_label(impl_header_span, "");
10501044
err.span_label(param_impl_span, make_param_message("found", param_impl));
10511045

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
184184
} else if let (ty::FnDef(def_id, ..), true) =
185185
(&found.kind(), self.suggest_fn_call(err, expr, expected, found))
186186
{
187-
if let Some(sp) = self.tcx.hir().span_if_local(*def_id) {
188-
let sp = self.sess().source_map().guess_head_span(sp);
189-
err.span_label(sp, &format!("{} defined here", found));
187+
if def_id.is_local() {
188+
err.span_label(self.tcx.def_span(def_id), &format!("{} defined here", found));
190189
}
191190
} else if !self.check_for_cast(err, expr, found, expected, expected_ty_expr) {
192191
let is_struct_pat_shorthand_field =

0 commit comments

Comments
 (0)