Skip to content

Commit f68e46b

Browse files
committed
replace guess_head_span with def_span
1 parent 9a6fa4f commit f68e46b

File tree

24 files changed

+114
-184
lines changed

24 files changed

+114
-184
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-6
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

+2-3
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

+3-5
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

+5-3
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

+10-21
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ impl MissingDoc {
556556
&self,
557557
cx: &LateContext<'_>,
558558
def_id: LocalDefId,
559-
sp: Span,
560559
article: &'static str,
561560
desc: &'static str,
562561
) {
@@ -583,13 +582,9 @@ impl MissingDoc {
583582
let attrs = cx.tcx.hir().attrs(cx.tcx.hir().local_def_id_to_hir_id(def_id));
584583
let has_doc = attrs.iter().any(has_doc);
585584
if !has_doc {
586-
cx.struct_span_lint(
587-
MISSING_DOCS,
588-
cx.tcx.sess.source_map().guess_head_span(sp),
589-
|lint| {
590-
lint.build(&format!("missing documentation for {} {}", article, desc)).emit();
591-
},
592-
);
585+
cx.struct_span_lint(MISSING_DOCS, cx.tcx.def_span(def_id), |lint| {
586+
lint.build(&format!("missing documentation for {} {}", article, desc)).emit();
587+
});
593588
}
594589
}
595590
}
@@ -612,13 +607,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
612607
}
613608

614609
fn check_crate(&mut self, cx: &LateContext<'_>) {
615-
self.check_missing_docs_attrs(
616-
cx,
617-
CRATE_DEF_ID,
618-
cx.tcx.def_span(CRATE_DEF_ID),
619-
"the",
620-
"crate",
621-
);
610+
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, "the", "crate");
622611
}
623612

624613
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
@@ -648,13 +637,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
648637

649638
let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
650639

651-
self.check_missing_docs_attrs(cx, it.def_id, it.span, article, desc);
640+
self.check_missing_docs_attrs(cx, it.def_id, article, desc);
652641
}
653642

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

657-
self.check_missing_docs_attrs(cx, trait_item.def_id, trait_item.span, article, desc);
646+
self.check_missing_docs_attrs(cx, trait_item.def_id, article, desc);
658647
}
659648

660649
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
@@ -682,23 +671,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
682671
}
683672

684673
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
685-
self.check_missing_docs_attrs(cx, impl_item.def_id, impl_item.span, article, desc);
674+
self.check_missing_docs_attrs(cx, impl_item.def_id, article, desc);
686675
}
687676

688677
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'_>) {
689678
let (article, desc) = cx.tcx.article_and_description(foreign_item.def_id.to_def_id());
690-
self.check_missing_docs_attrs(cx, foreign_item.def_id, foreign_item.span, article, desc);
679+
self.check_missing_docs_attrs(cx, foreign_item.def_id, article, desc);
691680
}
692681

693682
fn check_field_def(&mut self, cx: &LateContext<'_>, sf: &hir::FieldDef<'_>) {
694683
if !sf.is_positional() {
695684
let def_id = cx.tcx.hir().local_def_id(sf.hir_id);
696-
self.check_missing_docs_attrs(cx, def_id, sf.span, "a", "struct field")
685+
self.check_missing_docs_attrs(cx, def_id, "a", "struct field")
697686
}
698687
}
699688

700689
fn check_variant(&mut self, cx: &LateContext<'_>, v: &hir::Variant<'_>) {
701-
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), v.span, "a", "variant");
690+
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), "a", "variant");
702691
}
703692
}
704693

compiler/rustc_middle/src/ty/error.rs

+1-1
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

+6-14
Original file line numberDiff line numberDiff line change
@@ -1111,18 +1111,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11111111
})
11121112
.collect::<Option<Vec<ArgKind>>>()?,
11131113
),
1114-
Node::Item(&hir::Item { span, kind: hir::ItemKind::Fn(ref sig, ..), .. })
1115-
| Node::ImplItem(&hir::ImplItem {
1116-
span,
1117-
kind: hir::ImplItemKind::Fn(ref sig, _),
1118-
..
1119-
})
1114+
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref sig, ..), .. })
1115+
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref sig, _), .. })
11201116
| Node::TraitItem(&hir::TraitItem {
1121-
span,
1122-
kind: hir::TraitItemKind::Fn(ref sig, _),
1123-
..
1117+
kind: hir::TraitItemKind::Fn(ref sig, _), ..
11241118
}) => (
1125-
sm.guess_head_span(span),
1119+
sig.span,
11261120
sig.decl
11271121
.inputs
11281122
.iter()
@@ -1137,7 +1131,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11371131
),
11381132
Node::Ctor(ref variant_data) => {
11391133
let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| hir.span(id));
1140-
let span = sm.guess_head_span(span);
11411134
(span, vec![ArgKind::empty(); variant_data.fields().len()])
11421135
}
11431136
_ => panic!("non-FnLike node found: {:?}", node),
@@ -2205,7 +2198,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
22052198
let mut post = vec![];
22062199
for def_id in impls {
22072200
match self.tcx.span_of_impl(*def_id) {
2208-
Ok(span) => spans.push(self.tcx.sess.source_map().guess_head_span(span)),
2201+
Ok(span) => spans.push(span),
22092202
Err(name) => {
22102203
crates.push(name);
22112204
if let Some(header) = to_pretty_impl_header(self.tcx, *def_id) {
@@ -2552,8 +2545,7 @@ pub fn recursive_type_with_infinite_size_error<'tcx>(
25522545
spans: Vec<(Span, Option<hir::HirId>)>,
25532546
) {
25542547
assert!(type_def_id.is_local());
2555-
let span = tcx.hir().span_if_local(type_def_id).unwrap();
2556-
let span = tcx.sess.source_map().guess_head_span(span);
2548+
let span = tcx.def_span(type_def_id);
25572549
let path = tcx.def_path_str(type_def_id);
25582550
let mut err =
25592551
struct_span_err!(tcx.sess, span, E0072, "recursive type `{}` has infinite size", path);

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

+5-18
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

+5-4
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

+9-11
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,9 @@ fn check_panic_info_fn(
285285
tcx.sess.span_err(decl.output.span(), "return type should be `!`");
286286
}
287287

288-
let span = tcx.def_span(fn_id);
289288
let inputs = fn_sig.inputs();
290289
if inputs.len() != 1 {
291-
let span = tcx.sess.source_map().guess_head_span(span);
292-
tcx.sess.span_err(span, "function should have one argument");
290+
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
293291
return;
294292
}
295293

@@ -342,9 +340,7 @@ fn check_alloc_error_fn(
342340

343341
let inputs = fn_sig.inputs();
344342
if inputs.len() != 1 {
345-
let span = tcx.def_span(fn_id);
346-
let span = tcx.sess.source_map().guess_head_span(span);
347-
tcx.sess.span_err(span, "function should have one argument");
343+
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
348344
return;
349345
}
350346

@@ -1033,7 +1029,6 @@ fn check_impl_items_against_trait<'tcx>(
10331029
compare_impl_method(
10341030
tcx,
10351031
&ty_impl_item,
1036-
impl_item.span,
10371032
&ty_trait_item,
10381033
impl_trait_ref,
10391034
opt_trait_span,
@@ -1093,17 +1088,20 @@ fn check_impl_items_against_trait<'tcx>(
10931088
}
10941089

10951090
if !missing_items.is_empty() {
1096-
let impl_span = tcx.sess.source_map().guess_head_span(full_impl_span);
1097-
missing_items_err(tcx, impl_span, &missing_items, full_impl_span);
1091+
missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
10981092
}
10991093

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

1106-
missing_items_must_implement_one_of_err(tcx, impl_span, missing_items, attr_span);
1099+
missing_items_must_implement_one_of_err(
1100+
tcx,
1101+
tcx.def_span(impl_id),
1102+
missing_items,
1103+
attr_span,
1104+
);
11071105
}
11081106
}
11091107
}

0 commit comments

Comments
 (0)