Skip to content

Commit f18a6dc

Browse files
committed
Rename def_span to guess_head_span
1 parent 3c1d9ad commit f18a6dc

File tree

28 files changed

+89
-71
lines changed

28 files changed

+89
-71
lines changed

src/librustc/traits/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ impl<'tcx> ObligationCause<'tcx> {
108108
match self.code {
109109
ObligationCauseCode::CompareImplMethodObligation { .. }
110110
| ObligationCauseCode::MainFunctionType
111-
| ObligationCauseCode::StartFunctionType => tcx.sess.source_map().def_span(self.span),
111+
| ObligationCauseCode::StartFunctionType => {
112+
tcx.sess.source_map().guess_head_span(self.span)
113+
}
112114
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
113115
arm_span,
114116
..

src/librustc/ty/query/plumbing.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'tcx> TyCtxt<'tcx> {
388388
assert!(!stack.is_empty());
389389

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

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

462463
current_query = query_info.job.parent;

src/librustc_ast_passes/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'a> AstValidator<'a> {
402402

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

519519
fn current_extern_span(&self) -> Span {
520-
self.session.source_map().def_span(self.extern_mod.unwrap().span)
520+
self.session.source_map().guess_head_span(self.extern_mod.unwrap().span)
521521
}
522522

523523
/// An `fn` in `extern { ... }` cannot have qualfiers, e.g. `async fn`.

src/librustc_ast_passes/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
257257
gate_feature_post!(
258258
&self,
259259
non_ascii_idents,
260-
self.parse_sess.source_map().def_span(sp),
260+
self.parse_sess.source_map().guess_head_span(sp),
261261
"non-ascii idents are not fully supported"
262262
);
263263
}

src/librustc_builtin_macros/proc_macro_harness.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'a> CollectProcMacros<'a> {
198198
} else {
199199
"functions tagged with `#[proc_macro_derive]` must be `pub`"
200200
};
201-
self.handler.span_err(self.source_map.def_span(item.span), msg);
201+
self.handler.span_err(self.source_map.guess_head_span(item.span), msg);
202202
}
203203
}
204204

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

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

@@ -298,7 +298,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
298298

299299
let attr = match found_attr {
300300
None => {
301-
self.check_not_pub_in_root(&item.vis, self.source_map.def_span(item.span));
301+
self.check_not_pub_in_root(&item.vis, self.source_map.guess_head_span(item.span));
302302
let prev_in_root = mem::replace(&mut self.in_root, false);
303303
visit::walk_item(self, item);
304304
self.in_root = prev_in_root;

src/librustc_expand/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn generic_extension<'cx>(
326326
let mut err = cx.struct_span_err(span, &parse_failure_msg(&token));
327327
err.span_label(span, label);
328328
if !def_span.is_dummy() && !cx.source_map().is_imported(def_span) {
329-
err.span_label(cx.source_map().def_span(def_span), "when calling this macro");
329+
err.span_label(cx.source_map().guess_head_span(def_span), "when calling this macro");
330330
}
331331

332332
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`

src/librustc_infer/infer/error_reporting/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn msg_span_from_early_bound_and_free_regions(
200200
};
201201
let (prefix, span) = match *region {
202202
ty::ReEarlyBound(ref br) => {
203-
let mut sp = sm.def_span(tcx.hir().span(node));
203+
let mut sp = sm.guess_head_span(tcx.hir().span(node));
204204
if let Some(param) =
205205
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
206206
{
@@ -209,7 +209,7 @@ fn msg_span_from_early_bound_and_free_regions(
209209
(format!("the lifetime `{}` as defined on", br.name), sp)
210210
}
211211
ty::ReFree(ty::FreeRegion { bound_region: ty::BoundRegion::BrNamed(_, name), .. }) => {
212-
let mut sp = sm.def_span(tcx.hir().span(node));
212+
let mut sp = sm.guess_head_span(tcx.hir().span(node));
213213
if let Some(param) =
214214
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
215215
{
@@ -223,7 +223,7 @@ fn msg_span_from_early_bound_and_free_regions(
223223
}
224224
_ => (
225225
format!("the lifetime `{}` as defined on", region),
226-
sm.def_span(tcx.hir().span(node)),
226+
sm.guess_head_span(tcx.hir().span(node)),
227227
),
228228
},
229229
_ => bug!(),

src/librustc_infer/traits/error_reporting/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2020
requirement: &dyn fmt::Display,
2121
) -> DiagnosticBuilder<'tcx> {
2222
let msg = "impl has stricter requirements than trait";
23-
let sp = self.tcx.sess.source_map().def_span(error_span);
23+
let sp = self.tcx.sess.source_map().guess_head_span(error_span);
2424

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

2727
if let Some(trait_item_span) = self.tcx.hir().span_if_local(trait_item_def_id) {
28-
let span = self.tcx.sess.source_map().def_span(trait_item_span);
28+
let span = self.tcx.sess.source_map().guess_head_span(trait_item_span);
2929
err.span_label(span, format!("definition of `{}` from trait", item_name));
3030
}
3131

@@ -46,7 +46,7 @@ pub fn report_object_safety_error(
4646
hir::Node::Item(item) => Some(item.ident.span),
4747
_ => None,
4848
});
49-
let span = tcx.sess.source_map().def_span(span);
49+
let span = tcx.sess.source_map().guess_head_span(span);
5050
let mut err = struct_span_err!(
5151
tcx.sess,
5252
span,

src/librustc_lint/builtin.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl EarlyLintPass for WhileTrue {
7676
if let ast::LitKind::Bool(true) = lit.kind {
7777
if !lit.span.from_expansion() {
7878
let msg = "denote infinite loops with `loop { ... }`";
79-
let condition_span = cx.sess.source_map().def_span(e.span);
79+
let condition_span = cx.sess.source_map().guess_head_span(e.span);
8080
cx.struct_span_lint(WHILE_TRUE, condition_span, |lint| {
8181
lint.build(msg)
8282
.span_suggestion_short(
@@ -374,9 +374,13 @@ impl MissingDoc {
374374

375375
let has_doc = attrs.iter().any(|a| has_doc(a));
376376
if !has_doc {
377-
cx.struct_span_lint(MISSING_DOCS, cx.tcx.sess.source_map().def_span(sp), |lint| {
378-
lint.build(&format!("missing documentation for {} {}", article, desc)).emit()
379-
});
377+
cx.struct_span_lint(
378+
MISSING_DOCS,
379+
cx.tcx.sess.source_map().guess_head_span(sp),
380+
|lint| {
381+
lint.build(&format!("missing documentation for {} {}", article, desc)).emit()
382+
},
383+
);
380384
}
381385
}
382386
}
@@ -406,7 +410,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
406410
if !has_doc {
407411
cx.struct_span_lint(
408412
MISSING_DOCS,
409-
cx.tcx.sess.source_map().def_span(macro_def.span),
413+
cx.tcx.sess.source_map().guess_head_span(macro_def.span),
410414
|lint| lint.build("missing documentation for macro").emit(),
411415
);
412416
}
@@ -978,7 +982,7 @@ impl UnreachablePub {
978982
if span.from_expansion() {
979983
applicability = Applicability::MaybeIncorrect;
980984
}
981-
let def_span = cx.tcx.sess.source_map().def_span(span);
985+
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
982986
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
983987
let mut err = lint.build(&format!("unreachable `pub` {}", what));
984988
let replacement = if cx.tcx.features().crate_visibility_modifier {

src/librustc_mir/transform/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -565,14 +565,14 @@ fn is_enclosed(
565565
}
566566

567567
fn report_unused_unsafe(tcx: TyCtxt<'_>, used_unsafe: &FxHashSet<hir::HirId>, id: hir::HirId) {
568-
let span = tcx.sess.source_map().def_span(tcx.hir().span(id));
568+
let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id));
569569
tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
570570
let msg = "unnecessary `unsafe` block";
571571
let mut db = lint.build(msg);
572572
db.span_label(span, msg);
573573
if let Some((kind, id)) = is_enclosed(tcx, used_unsafe, id) {
574574
db.span_label(
575-
tcx.sess.source_map().def_span(tcx.hir().span(id)),
575+
tcx.sess.source_map().guess_head_span(tcx.hir().span(id)),
576576
format!("because it's nested under this `unsafe` {}", kind),
577577
);
578578
}

src/librustc_mir_build/lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn check_fn_for_unconditional_recursion<'tcx>(
123123
// recurs.
124124
if !reached_exit_without_self_call && !self_call_locations.is_empty() {
125125
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
126-
let sp = tcx.sess.source_map().def_span(tcx.hir().span(hir_id));
126+
let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span(hir_id));
127127
tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| {
128128
let mut db = lint.build("function cannot return without recursing");
129129
db.span_label(sp, "cannot return without recursing");

src/librustc_parse/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ impl<'a> Parser<'a> {
907907
}
908908

909909
fn error_bad_item_kind<T>(&self, span: Span, kind: &ItemKind, ctx: &str) -> Option<T> {
910-
let span = self.sess.source_map().def_span(span);
910+
let span = self.sess.source_map().guess_head_span(span);
911911
let msg = format!("{} is not supported in {}", kind.descr(), ctx);
912912
self.struct_span_err(span, &msg).emit();
913913
None

src/librustc_passes/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
590590
// We should probably annotate ident.span with the macro
591591
// context, but that's a larger change.
592592
if item.span.source_callee().is_some() {
593-
self.tcx.sess.source_map().def_span(item.span)
593+
self.tcx.sess.source_map().guess_head_span(item.span)
594594
} else {
595595
item.ident.span
596596
}
@@ -663,7 +663,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
663663
}
664664
hir::ImplItemKind::Fn(_, body_id) => {
665665
if !self.symbol_is_live(impl_item.hir_id) {
666-
let span = self.tcx.sess.source_map().def_span(impl_item.span);
666+
let span = self.tcx.sess.source_map().guess_head_span(impl_item.span);
667667
self.warn_dead_code(
668668
impl_item.hir_id,
669669
span,

src/librustc_resolve/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,12 @@ impl<'a> Resolver<'a> {
791791
_ => Some(
792792
self.session
793793
.source_map()
794-
.def_span(self.cstore().get_span_untracked(def_id, self.session)),
794+
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
795795
),
796796
});
797797
if let Some(span) = def_span {
798798
err.span_label(
799-
self.session.source_map().def_span(span),
799+
self.session.source_map().guess_head_span(span),
800800
&format!(
801801
"similarly named {} `{}` defined here",
802802
suggestion.res.descr(),
@@ -986,7 +986,7 @@ impl<'a> Resolver<'a> {
986986
which = if first { "" } else { " which" },
987987
dots = if next_binding.is_some() { "..." } else { "" },
988988
);
989-
let def_span = self.session.source_map().def_span(binding.span);
989+
let def_span = self.session.source_map().guess_head_span(binding.span);
990990
let mut note_span = MultiSpan::from_span(def_span);
991991
if !first && binding.vis == ty::Visibility::Public {
992992
note_span.push_span_label(def_span, "consider importing it directly".into());

src/librustc_resolve/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
14411441
let enum_resolution = resolutions.get(&key).expect("resolution should exist");
14421442
let enum_span =
14431443
enum_resolution.borrow().binding.expect("binding should exist").span;
1444-
let enum_def_span = this.session.source_map().def_span(enum_span);
1444+
let enum_def_span = this.session.source_map().guess_head_span(enum_span);
14451445
let enum_def_snippet = this
14461446
.session
14471447
.source_map()

src/librustc_resolve/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,8 @@ impl<'a> Resolver<'a> {
25172517
false => "defined",
25182518
};
25192519

2520-
let (name, span) = (ident.name, self.session.source_map().def_span(new_binding.span));
2520+
let (name, span) =
2521+
(ident.name, self.session.source_map().guess_head_span(new_binding.span));
25212522

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

25592560
err.span_label(span, format!("`{}` re{} here", name, new_participle));
25602561
err.span_label(
2561-
self.session.source_map().def_span(old_binding.span),
2562+
self.session.source_map().guess_head_span(old_binding.span),
25622563
format!("previous {} of the {} `{}` here", old_noun, old_kind, name),
25632564
);
25642565

src/librustc_span/source_map.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,14 @@ impl SourceMap {
733733
}
734734
}
735735

736-
pub fn def_span(&self, sp: Span) -> Span {
736+
/// Given a `Span`, return a span ending in the closest `{`. This is useful when you have a
737+
/// `Span` enclosing a whole item but we need to point at only the head (usually the first
738+
/// line) of that item.
739+
///
740+
/// *Only suitable for diagnostics.*
741+
pub fn guess_head_span(&self, sp: Span) -> Span {
742+
// FIXME: extend the AST items to have a head span, or replace callers with pointing at
743+
// the item's ident when appropriate.
737744
self.span_until_char(sp, '{')
738745
}
739746

src/librustc_trait_selection/traits/error_reporting/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
482482

483483
ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => {
484484
let found_kind = self.closure_kind(closure_substs).unwrap();
485-
let closure_span = self
486-
.tcx
487-
.sess
488-
.source_map()
489-
.def_span(self.tcx.hir().span_if_local(closure_def_id).unwrap());
485+
let closure_span =
486+
self.tcx.sess.source_map().guess_head_span(
487+
self.tcx.hir().span_if_local(closure_def_id).unwrap(),
488+
);
490489
let hir_id = self.tcx.hir().as_local_hir_id(closure_def_id).unwrap();
491490
let mut err = struct_span_err!(
492491
self.tcx.sess,
@@ -580,7 +579,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
580579

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

585584
if self.reported_closure_mismatch.borrow().contains(&(span, found_span)) {
586585
// We check closures twice, with obligations flowing in different directions,
@@ -680,7 +679,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
680679
kind: hir::ExprKind::Closure(_, ref _decl, id, span, _),
681680
..
682681
}) => (
683-
self.tcx.sess.source_map().def_span(span),
682+
self.tcx.sess.source_map().guess_head_span(span),
684683
self.tcx
685684
.hir()
686685
.body(id)
@@ -723,7 +722,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
723722
kind: hir::TraitItemKind::Fn(ref sig, _),
724723
..
725724
}) => (
726-
self.tcx.sess.source_map().def_span(span),
725+
self.tcx.sess.source_map().guess_head_span(span),
727726
sig.decl
728727
.inputs
729728
.iter()
@@ -741,7 +740,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
741740
.ctor_hir_id()
742741
.map(|hir_id| self.tcx.hir().span(hir_id))
743742
.unwrap_or(DUMMY_SP);
744-
let span = self.tcx.sess.source_map().def_span(span);
743+
let span = self.tcx.sess.source_map().guess_head_span(span);
745744

746745
(span, vec![ArgKind::empty(); variant_data.fields().len()])
747746
}
@@ -1624,7 +1623,7 @@ pub fn recursive_type_with_infinite_size_error(
16241623
) -> DiagnosticBuilder<'tcx> {
16251624
assert!(type_def_id.is_local());
16261625
let span = tcx.hir().span_if_local(type_def_id).unwrap();
1627-
let span = tcx.sess.source_map().def_span(span);
1626+
let span = tcx.sess.source_map().guess_head_span(span);
16281627
let mut err = struct_span_err!(
16291628
tcx.sess,
16301629
span,

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13811381
let msg = format!("required by `{}`", item_name);
13821382

13831383
if let Some(sp) = tcx.hir().span_if_local(item_def_id) {
1384-
let sp = tcx.sess.source_map().def_span(sp);
1384+
let sp = tcx.sess.source_map().guess_head_span(sp);
13851385
err.span_label(sp, &msg);
13861386
} else {
13871387
err.note(&msg);

0 commit comments

Comments
 (0)