Skip to content

Commit

Permalink
Rollup merge of rust-lang#98519 - TaKO8Ki:add-head-span-field-to-item…
Browse files Browse the repository at this point in the history
…-and-impl-item, r=cjgillot

Replace some `guess_head_span` with `def_span`

This patch fixes a part of rust-lang#97417.
r? `@cjgillot`
  • Loading branch information
Dylan-DPC authored Jul 5, 2022
2 parents 9c66294 + 0330f8b commit 9b2c841
Show file tree
Hide file tree
Showing 39 changed files with 150 additions and 219 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,12 +812,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
return FnSelfUse {
var_span: stmt.source_info.span,
fn_call_span: *fn_span,
fn_span: self
.infcx
.tcx
.sess
.source_map()
.guess_head_span(self.infcx.tcx.def_span(method_did)),
fn_span: self.infcx.tcx.def_span(method_did),
kind,
};
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
});

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

match self_ty.kind() {
FnDef(def_id, ..) => {
let span = tcx.sess.source_map().guess_head_span(tcx.def_span(*def_id));
let span = tcx.def_span(*def_id);
if ccx.tcx.is_const_fn_raw(*def_id) {
span_bug!(span, "calling const FnDef errored when it shouldn't");
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
tcx: TyCtxt<'tcx>,
region: ty::Region<'tcx>,
) -> (String, Span) {
let sm = tcx.sess.source_map();

let scope = region.free_region_binding_scope(tcx).expect_local();
match *region {
ty::ReEarlyBound(ref br) => {
let mut sp = sm.guess_head_span(tcx.def_span(scope));
let mut sp = tcx.def_span(scope);
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
Expand All @@ -174,7 +172,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
} else {
match fr.bound_region {
ty::BoundRegionKind::BrNamed(_, name) => {
let mut sp = sm.guess_head_span(tcx.def_span(scope));
let mut sp = tcx.def_span(scope);
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
{
Expand All @@ -193,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
),
_ => (
format!("the lifetime `{}` as defined here", region),
sm.guess_head_span(tcx.def_span(scope)),
tcx.def_span(scope),
),
}
}
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_infer/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {

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

if let Some(trait_item_span) = self.tcx.hir().span_if_local(trait_item_def_id) {
let span = self.tcx.sess.source_map().guess_head_span(trait_item_span);
if trait_item_def_id.is_local() {
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
err.span_label(span, format!("definition of `{}` from trait", item_name));
err.span_label(
self.tcx.def_span(trait_item_def_id),
format!("definition of `{}` from trait", item_name),
);
}

err.span_label(sp, format!("impl has extra requirement {}", requirement));
Expand Down
31 changes: 10 additions & 21 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,6 @@ impl MissingDoc {
&self,
cx: &LateContext<'_>,
def_id: LocalDefId,
sp: Span,
article: &'static str,
desc: &'static str,
) {
Expand All @@ -583,13 +582,9 @@ impl MissingDoc {
let attrs = cx.tcx.hir().attrs(cx.tcx.hir().local_def_id_to_hir_id(def_id));
let has_doc = attrs.iter().any(has_doc);
if !has_doc {
cx.struct_span_lint(
MISSING_DOCS,
cx.tcx.sess.source_map().guess_head_span(sp),
|lint| {
lint.build(&format!("missing documentation for {} {}", article, desc)).emit();
},
);
cx.struct_span_lint(MISSING_DOCS, cx.tcx.def_span(def_id), |lint| {
lint.build(&format!("missing documentation for {} {}", article, desc)).emit();
});
}
}
}
Expand All @@ -612,13 +607,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}

fn check_crate(&mut self, cx: &LateContext<'_>) {
self.check_missing_docs_attrs(
cx,
CRATE_DEF_ID,
cx.tcx.def_span(CRATE_DEF_ID),
"the",
"crate",
);
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, "the", "crate");
}

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

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

self.check_missing_docs_attrs(cx, it.def_id, it.span, article, desc);
self.check_missing_docs_attrs(cx, it.def_id, article, desc);
}

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

self.check_missing_docs_attrs(cx, trait_item.def_id, trait_item.span, article, desc);
self.check_missing_docs_attrs(cx, trait_item.def_id, article, desc);
}

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

let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
self.check_missing_docs_attrs(cx, impl_item.def_id, impl_item.span, article, desc);
self.check_missing_docs_attrs(cx, impl_item.def_id, article, desc);
}

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

fn check_field_def(&mut self, cx: &LateContext<'_>, sf: &hir::FieldDef<'_>) {
if !sf.is_positional() {
let def_id = cx.tcx.hir().local_def_id(sf.hir_id);
self.check_missing_docs_attrs(cx, def_id, sf.span, "a", "struct field")
self.check_missing_docs_attrs(cx, def_id, "a", "struct field")
}
}

fn check_variant(&mut self, cx: &LateContext<'_>, v: &hir::Variant<'_>) {
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), v.span, "a", "variant");
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), "a", "variant");
}
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,12 +1012,13 @@ impl<'hir> Map<'hir> {
ItemKind::Use(path, _) => path.span,
_ => named_span(item.span, item.ident, item.kind.generics()),
},
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
Node::ForeignItem(item) => match item.kind {
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
_ => named_span(item.span, item.ident, None),
},
Node::Ctor(..) => return self.opt_span(self.get_parent_node(hir_id)),
Node::Ctor(_) => return self.opt_span(self.get_parent_node(hir_id)),
_ => self.span_with_body(hir_id),
};
Some(span)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ fn foo(&self) -> Self::T { String::new() }
if item_def_id == proj_ty_item_def_id =>
{
Some((
self.sess.source_map().guess_head_span(self.def_span(item.def_id)),
self.def_span(item.def_id),
format!("consider calling `{}`", self.def_path_str(item.def_id)),
))
}
Expand Down
20 changes: 6 additions & 14 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,18 +1111,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
})
.collect::<Option<Vec<ArgKind>>>()?,
),
Node::Item(&hir::Item { span, kind: hir::ItemKind::Fn(ref sig, ..), .. })
| Node::ImplItem(&hir::ImplItem {
span,
kind: hir::ImplItemKind::Fn(ref sig, _),
..
})
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref sig, ..), .. })
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref sig, _), .. })
| Node::TraitItem(&hir::TraitItem {
span,
kind: hir::TraitItemKind::Fn(ref sig, _),
..
kind: hir::TraitItemKind::Fn(ref sig, _), ..
}) => (
sm.guess_head_span(span),
sig.span,
sig.decl
.inputs
.iter()
Expand All @@ -1137,7 +1131,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
),
Node::Ctor(ref variant_data) => {
let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| hir.span(id));
let span = sm.guess_head_span(span);
(span, vec![ArgKind::empty(); variant_data.fields().len()])
}
_ => panic!("non-FnLike node found: {:?}", node),
Expand Down Expand Up @@ -2184,7 +2177,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
let mut post = vec![];
for def_id in impls {
match self.tcx.span_of_impl(*def_id) {
Ok(span) => spans.push(self.tcx.sess.source_map().guess_head_span(span)),
Ok(span) => spans.push(span),
Err(name) => {
crates.push(name);
if let Some(header) = to_pretty_impl_header(self.tcx, *def_id) {
Expand Down Expand Up @@ -2531,8 +2524,7 @@ pub fn recursive_type_with_infinite_size_error<'tcx>(
spans: Vec<(Span, Option<hir::HirId>)>,
) {
assert!(type_def_id.is_local());
let span = tcx.hir().span_if_local(type_def_id).unwrap();
let span = tcx.sess.source_map().guess_head_span(span);
let span = tcx.def_span(type_def_id);
let path = tcx.def_path_str(type_def_id);
let mut err =
struct_span_err!(tcx.sess, span, E0072, "recursive type `{}` has infinite size", path);
Expand Down
23 changes: 5 additions & 18 deletions compiler/rustc_trait_selection/src/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ fn report_negative_positive_conflict(
positive_impl_def_id: DefId,
sg: &mut specialization_graph::Graph,
) {
let impl_span = tcx
.sess
.source_map()
.guess_head_span(tcx.span_of_impl(local_impl_def_id.to_def_id()).unwrap());
let impl_span = tcx.def_span(local_impl_def_id);

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

match tcx.span_of_impl(negative_impl_def_id) {
Ok(span) => {
err.span_label(
tcx.sess.source_map().guess_head_span(span),
"negative implementation here".to_string(),
);
err.span_label(span, "negative implementation here");
}
Err(cname) => {
err.note(&format!("negative implementation in crate `{}`", cname));
Expand All @@ -369,10 +363,7 @@ fn report_negative_positive_conflict(

match tcx.span_of_impl(positive_impl_def_id) {
Ok(span) => {
err.span_label(
tcx.sess.source_map().guess_head_span(span),
"positive implementation here".to_string(),
);
err.span_label(span, "positive implementation here");
}
Err(cname) => {
err.note(&format!("positive implementation in crate `{}`", cname));
Expand All @@ -389,8 +380,7 @@ fn report_conflicting_impls(
used_to_be_allowed: Option<FutureCompatOverlapErrorKind>,
sg: &mut specialization_graph::Graph,
) {
let impl_span =
tcx.sess.source_map().guess_head_span(tcx.span_of_impl(impl_def_id.to_def_id()).unwrap());
let impl_span = tcx.def_span(impl_def_id);

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

err.span_label(
impl_span,
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1958,9 +1958,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
);
}

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

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

let msg = format!("`Self` is of type `{ty}`");
if let (Ok(i_sp), Some(t_sp)) = (span_of_impl, span_of_ty) {
let i_sp = tcx.sess.source_map().guess_head_span(i_sp);
let mut span: MultiSpan = vec![t_sp].into();
span.push_span_label(
i_sp,
Expand Down
20 changes: 9 additions & 11 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,9 @@ fn check_panic_info_fn(
tcx.sess.span_err(decl.output.span(), "return type should be `!`");
}

let span = tcx.def_span(fn_id);
let inputs = fn_sig.inputs();
if inputs.len() != 1 {
let span = tcx.sess.source_map().guess_head_span(span);
tcx.sess.span_err(span, "function should have one argument");
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
return;
}

Expand Down Expand Up @@ -342,9 +340,7 @@ fn check_alloc_error_fn(

let inputs = fn_sig.inputs();
if inputs.len() != 1 {
let span = tcx.def_span(fn_id);
let span = tcx.sess.source_map().guess_head_span(span);
tcx.sess.span_err(span, "function should have one argument");
tcx.sess.span_err(tcx.def_span(fn_id), "function should have one argument");
return;
}

Expand Down Expand Up @@ -1033,7 +1029,6 @@ fn check_impl_items_against_trait<'tcx>(
compare_impl_method(
tcx,
&ty_impl_item,
impl_item.span,
&ty_trait_item,
impl_trait_ref,
opt_trait_span,
Expand Down Expand Up @@ -1093,17 +1088,20 @@ fn check_impl_items_against_trait<'tcx>(
}

if !missing_items.is_empty() {
let impl_span = tcx.sess.source_map().guess_head_span(full_impl_span);
missing_items_err(tcx, impl_span, &missing_items, full_impl_span);
missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
}

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

missing_items_must_implement_one_of_err(tcx, impl_span, missing_items, attr_span);
missing_items_must_implement_one_of_err(
tcx,
tcx.def_span(impl_id),
missing_items,
attr_span,
);
}
}
}
Expand Down
Loading

0 comments on commit 9b2c841

Please sign in to comment.