Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename def_span to guess_head_span #69866

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl<'tcx> ObligationCause<'tcx> {
match self.code {
ObligationCauseCode::CompareImplMethodObligation { .. }
| ObligationCauseCode::MainFunctionType
| ObligationCauseCode::StartFunctionType => tcx.sess.source_map().def_span(self.span),
| ObligationCauseCode::StartFunctionType => {
tcx.sess.source_map().guess_head_span(self.span)
}
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
arm_span,
..
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<'tcx> TyCtxt<'tcx> {
assert!(!stack.is_empty());

let fix_span = |span: Span, query: &Query<'tcx>| {
self.sess.source_map().def_span(query.default_span(self, span))
self.sess.source_map().guess_head_span(query.default_span(self, span))
};

// Disable naming impls with types in this path, since that
Expand Down Expand Up @@ -456,7 +456,8 @@ impl<'tcx> TyCtxt<'tcx> {
query_info.info.query.describe(icx.tcx)
),
);
diag.span = icx.tcx.sess.source_map().def_span(query_info.info.span).into();
diag.span =
icx.tcx.sess.source_map().guess_head_span(query_info.info.span).into();
handler.force_print_diagnostic(diag);

current_query = query_info.job.parent;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl<'a> AstValidator<'a> {

fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
if let Defaultness::Default(def_span) = defaultness {
let span = self.session.source_map().def_span(span);
let span = self.session.source_map().guess_head_span(span);
self.err_handler()
.struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
.span_label(def_span, "`default` because of this")
Expand Down Expand Up @@ -517,7 +517,7 @@ impl<'a> AstValidator<'a> {
}

fn current_extern_span(&self) -> Span {
self.session.source_map().def_span(self.extern_mod.unwrap().span)
self.session.source_map().guess_head_span(self.extern_mod.unwrap().span)
}

/// An `fn` in `extern { ... }` cannot have qualfiers, e.g. `async fn`.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_passes/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
gate_feature_post!(
&self,
non_ascii_idents,
self.parse_sess.source_map().def_span(sp),
self.parse_sess.source_map().guess_head_span(sp),
"non-ascii idents are not fully supported"
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_builtin_macros/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a> CollectProcMacros<'a> {
} else {
"functions tagged with `#[proc_macro_derive]` must be `pub`"
};
self.handler.span_err(self.source_map.def_span(item.span), msg);
self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
}
}

Expand All @@ -217,7 +217,7 @@ impl<'a> CollectProcMacros<'a> {
} else {
"functions tagged with `#[proc_macro_attribute]` must be `pub`"
};
self.handler.span_err(self.source_map.def_span(item.span), msg);
self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
}
}

Expand All @@ -236,7 +236,7 @@ impl<'a> CollectProcMacros<'a> {
} else {
"functions tagged with `#[proc_macro]` must be `pub`"
};
self.handler.span_err(self.source_map.def_span(item.span), msg);
self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
}
}
}
Expand All @@ -247,7 +247,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) {
let msg =
"cannot export macro_rules! macros from a `proc-macro` crate type currently";
self.handler.span_err(self.source_map.def_span(item.span), msg);
self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
}
}

Expand Down Expand Up @@ -298,7 +298,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {

let attr = match found_attr {
None => {
self.check_not_pub_in_root(&item.vis, self.source_map.def_span(item.span));
self.check_not_pub_in_root(&item.vis, self.source_map.guess_head_span(item.span));
let prev_in_root = mem::replace(&mut self.in_root, false);
visit::walk_item(self, item);
self.in_root = prev_in_root;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn generic_extension<'cx>(
let mut err = cx.struct_span_err(span, &parse_failure_msg(&token));
err.span_label(span, label);
if !def_span.is_dummy() && !cx.source_map().is_imported(def_span) {
err.span_label(cx.source_map().def_span(def_span), "when calling this macro");
err.span_label(cx.source_map().guess_head_span(def_span), "when calling this macro");
}

// Check whether there's a missing comma in this macro call, like `println!("{}" a);`
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn msg_span_from_early_bound_and_free_regions(
};
let (prefix, span) = match *region {
ty::ReEarlyBound(ref br) => {
let mut sp = sm.def_span(tcx.hir().span(node));
let mut sp = sm.guess_head_span(tcx.hir().span(node));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
Expand All @@ -209,7 +209,7 @@ fn msg_span_from_early_bound_and_free_regions(
(format!("the lifetime `{}` as defined on", br.name), sp)
}
ty::ReFree(ty::FreeRegion { bound_region: ty::BoundRegion::BrNamed(_, name), .. }) => {
let mut sp = sm.def_span(tcx.hir().span(node));
let mut sp = sm.guess_head_span(tcx.hir().span(node));
if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
{
Expand All @@ -223,7 +223,7 @@ fn msg_span_from_early_bound_and_free_regions(
}
_ => (
format!("the lifetime `{}` as defined on", region),
sm.def_span(tcx.hir().span(node)),
sm.guess_head_span(tcx.hir().span(node)),
),
},
_ => bug!(),
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_infer/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
requirement: &dyn fmt::Display,
) -> DiagnosticBuilder<'tcx> {
let msg = "impl has stricter requirements than trait";
let sp = self.tcx.sess.source_map().def_span(error_span);
let sp = self.tcx.sess.source_map().guess_head_span(error_span);

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().def_span(trait_item_span);
let span = self.tcx.sess.source_map().guess_head_span(trait_item_span);
err.span_label(span, format!("definition of `{}` from trait", item_name));
}

Expand All @@ -46,7 +46,7 @@ pub fn report_object_safety_error(
hir::Node::Item(item) => Some(item.ident.span),
_ => None,
});
let span = tcx.sess.source_map().def_span(span);
let span = tcx.sess.source_map().guess_head_span(span);
let mut err = struct_span_err!(
tcx.sess,
span,
Expand Down
16 changes: 10 additions & 6 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl EarlyLintPass for WhileTrue {
if let ast::LitKind::Bool(true) = lit.kind {
if !lit.span.from_expansion() {
let msg = "denote infinite loops with `loop { ... }`";
let condition_span = cx.sess.source_map().def_span(e.span);
let condition_span = cx.sess.source_map().guess_head_span(e.span);
cx.struct_span_lint(WHILE_TRUE, condition_span, |lint| {
lint.build(msg)
.span_suggestion_short(
Expand Down Expand Up @@ -374,9 +374,13 @@ impl MissingDoc {

let has_doc = attrs.iter().any(|a| has_doc(a));
if !has_doc {
cx.struct_span_lint(MISSING_DOCS, cx.tcx.sess.source_map().def_span(sp), |lint| {
lint.build(&format!("missing documentation for {} {}", article, desc)).emit()
});
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()
},
);
}
}
}
Expand Down Expand Up @@ -406,7 +410,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
if !has_doc {
cx.struct_span_lint(
MISSING_DOCS,
cx.tcx.sess.source_map().def_span(macro_def.span),
cx.tcx.sess.source_map().guess_head_span(macro_def.span),
|lint| lint.build("missing documentation for macro").emit(),
);
}
Expand Down Expand Up @@ -978,7 +982,7 @@ impl UnreachablePub {
if span.from_expansion() {
applicability = Applicability::MaybeIncorrect;
}
let def_span = cx.tcx.sess.source_map().def_span(span);
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
let mut err = lint.build(&format!("unreachable `pub` {}", what));
let replacement = if cx.tcx.features().crate_visibility_modifier {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,14 @@ fn is_enclosed(
}

fn report_unused_unsafe(tcx: TyCtxt<'_>, used_unsafe: &FxHashSet<hir::HirId>, id: hir::HirId) {
let span = tcx.sess.source_map().def_span(tcx.hir().span(id));
let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id));
tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
let msg = "unnecessary `unsafe` block";
let mut db = lint.build(msg);
db.span_label(span, msg);
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {
db.span_label(
tcx.sess.source_map().def_span(tcx.hir().span(id)),
tcx.sess.source_map().guess_head_span(tcx.hir().span(id)),
format!("because it's nested under this `unsafe` {}", kind),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn check_fn_for_unconditional_recursion<'tcx>(
// recurs.
if !reached_exit_without_self_call && !self_call_locations.is_empty() {
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
let sp = tcx.sess.source_map().def_span(tcx.hir().span(hir_id));
let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span(hir_id));
tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| {
let mut db = lint.build("function cannot return without recursing");
db.span_label(sp, "cannot return without recursing");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ impl<'a> Parser<'a> {
}

fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
let span = self.sess.source_map().def_span(span);
let span = self.sess.source_map().guess_head_span(span);
let msg = format!("{} is not supported in {}", kind.descr(), ctx);
self.struct_span_err(span, &msg).emit();
None
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_passes/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
// We should probably annotate ident.span with the macro
// context, but that's a larger change.
if item.span.source_callee().is_some() {
self.tcx.sess.source_map().def_span(item.span)
self.tcx.sess.source_map().guess_head_span(item.span)
} else {
item.ident.span
}
Expand Down Expand Up @@ -663,7 +663,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
}
hir::ImplItemKind::Fn(_, body_id) => {
if !self.symbol_is_live(impl_item.hir_id) {
let span = self.tcx.sess.source_map().def_span(impl_item.span);
let span = self.tcx.sess.source_map().guess_head_span(impl_item.span);
self.warn_dead_code(
impl_item.hir_id,
span,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,12 @@ impl<'a> Resolver<'a> {
_ => Some(
self.session
.source_map()
.def_span(self.cstore().get_span_untracked(def_id, self.session)),
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
),
});
if let Some(span) = def_span {
err.span_label(
self.session.source_map().def_span(span),
self.session.source_map().guess_head_span(span),
&format!(
"similarly named {} `{}` defined here",
suggestion.res.descr(),
Expand Down Expand Up @@ -986,7 +986,7 @@ impl<'a> Resolver<'a> {
which = if first { "" } else { " which" },
dots = if next_binding.is_some() { "..." } else { "" },
);
let def_span = self.session.source_map().def_span(binding.span);
let def_span = self.session.source_map().guess_head_span(binding.span);
let mut note_span = MultiSpan::from_span(def_span);
if !first && binding.vis == ty::Visibility::Public {
note_span.push_span_label(def_span, "consider importing it directly".into());
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let enum_resolution = resolutions.get(&key).expect("resolution should exist");
let enum_span =
enum_resolution.borrow().binding.expect("binding should exist").span;
let enum_def_span = this.session.source_map().def_span(enum_span);
let enum_def_span = this.session.source_map().guess_head_span(enum_span);
let enum_def_snippet = this
.session
.source_map()
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2517,7 +2517,8 @@ impl<'a> Resolver<'a> {
false => "defined",
};

let (name, span) = (ident.name, self.session.source_map().def_span(new_binding.span));
let (name, span) =
(ident.name, self.session.source_map().guess_head_span(new_binding.span));

if let Some(s) = self.name_already_seen.get(&name) {
if s == &span {
Expand Down Expand Up @@ -2558,7 +2559,7 @@ impl<'a> Resolver<'a> {

err.span_label(span, format!("`{}` re{} here", name, new_participle));
err.span_label(
self.session.source_map().def_span(old_binding.span),
self.session.source_map().guess_head_span(old_binding.span),
format!("previous {} of the {} `{}` here", old_noun, old_kind, name),
);

Expand Down
9 changes: 8 additions & 1 deletion src/librustc_span/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,14 @@ impl SourceMap {
}
}

pub fn def_span(&self, sp: Span) -> Span {
/// Given a `Span`, return a span ending in the closest `{`. This is useful when you have a
/// `Span` enclosing a whole item but we need to point at only the head (usually the first
/// line) of that item.
///
/// *Only suitable for diagnostics.*
pub fn guess_head_span(&self, sp: Span) -> Span {
// FIXME: extend the AST items to have a head span, or replace callers with pointing at
// the item's ident when appropriate.
self.span_until_char(sp, '{')
estebank marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
19 changes: 9 additions & 10 deletions src/librustc_trait_selection/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {

ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
let found_kind = self.closure_kind(closure_substs).unwrap();
let closure_span = self
.tcx
.sess
.source_map()
.def_span(self.tcx.hir().span_if_local(closure_def_id).unwrap());
let closure_span =
self.tcx.sess.source_map().guess_head_span(
self.tcx.hir().span_if_local(closure_def_id).unwrap(),
);
let hir_id = self.tcx.hir().as_local_hir_id(closure_def_id).unwrap();
let mut err = struct_span_err!(
self.tcx.sess,
Expand Down Expand Up @@ -580,7 +579,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {

let found_span = found_did
.and_then(|did| self.tcx.hir().span_if_local(did))
.map(|sp| self.tcx.sess.source_map().def_span(sp)); // the sp could be an fn def
.map(|sp| self.tcx.sess.source_map().guess_head_span(sp)); // the sp could be an fn def

if self.reported_closure_mismatch.borrow().contains(&(span, found_span)) {
// We check closures twice, with obligations flowing in different directions,
Expand Down Expand Up @@ -680,7 +679,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
kind: hir::ExprKind::Closure(_, ref _decl, id, span, _),
..
}) => (
self.tcx.sess.source_map().def_span(span),
self.tcx.sess.source_map().guess_head_span(span),
self.tcx
.hir()
.body(id)
Expand Down Expand Up @@ -723,7 +722,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
kind: hir::TraitItemKind::Fn(ref sig, _),
..
}) => (
self.tcx.sess.source_map().def_span(span),
self.tcx.sess.source_map().guess_head_span(span),
sig.decl
.inputs
.iter()
Expand All @@ -741,7 +740,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
.ctor_hir_id()
.map(|hir_id| self.tcx.hir().span(hir_id))
.unwrap_or(DUMMY_SP);
let span = self.tcx.sess.source_map().def_span(span);
let span = self.tcx.sess.source_map().guess_head_span(span);

(span, vec![ArgKind::empty(); variant_data.fields().len()])
}
Expand Down Expand Up @@ -1624,7 +1623,7 @@ pub fn recursive_type_with_infinite_size_error(
) -> DiagnosticBuilder<'tcx> {
assert!(type_def_id.is_local());
let span = tcx.hir().span_if_local(type_def_id).unwrap();
let span = tcx.sess.source_map().def_span(span);
let span = tcx.sess.source_map().guess_head_span(span);
let mut err = struct_span_err!(
tcx.sess,
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let msg = format!("required by `{}`", item_name);

if let Some(sp) = tcx.hir().span_if_local(item_def_id) {
let sp = tcx.sess.source_map().def_span(sp);
let sp = tcx.sess.source_map().guess_head_span(sp);
err.span_label(sp, &msg);
} else {
err.note(&msg);
Expand Down
Loading