Skip to content

Commit 8db3d7c

Browse files
authored
Rollup merge of #99911 - cjgillot:no-guess, r=davidtwco
Remove some uses of `guess_head_span` That function cuts a span at the first occurrence of `{`. Using `def_span` is almost always more precise.
2 parents 1530ed8 + 2134dd3 commit 8db3d7c

File tree

20 files changed

+97
-114
lines changed

20 files changed

+97
-114
lines changed

compiler/rustc_lint/src/builtin.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,6 @@ impl UnreachablePub {
13641364
cx: &LateContext<'_>,
13651365
what: &str,
13661366
def_id: LocalDefId,
1367-
span: Span,
13681367
vis_span: Span,
13691368
exportable: bool,
13701369
) {
@@ -1373,7 +1372,7 @@ impl UnreachablePub {
13731372
if vis_span.from_expansion() {
13741373
applicability = Applicability::MaybeIncorrect;
13751374
}
1376-
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
1375+
let def_span = cx.tcx.def_span(def_id);
13771376
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
13781377
let mut err = lint.build(fluent::lint::builtin_unreachable_pub);
13791378
err.set_arg("what", what);
@@ -1399,36 +1398,22 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
13991398
if let hir::ItemKind::Use(_, hir::UseKind::ListStem) = &item.kind {
14001399
return;
14011400
}
1402-
self.perform_lint(cx, "item", item.def_id, item.span, item.vis_span, true);
1401+
self.perform_lint(cx, "item", item.def_id, item.vis_span, true);
14031402
}
14041403

14051404
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
1406-
self.perform_lint(
1407-
cx,
1408-
"item",
1409-
foreign_item.def_id,
1410-
foreign_item.span,
1411-
foreign_item.vis_span,
1412-
true,
1413-
);
1405+
self.perform_lint(cx, "item", foreign_item.def_id, foreign_item.vis_span, true);
14141406
}
14151407

14161408
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
14171409
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
1418-
self.perform_lint(cx, "field", def_id, field.span, field.vis_span, false);
1410+
self.perform_lint(cx, "field", def_id, field.vis_span, false);
14191411
}
14201412

14211413
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
14221414
// Only lint inherent impl items.
14231415
if cx.tcx.associated_item(impl_item.def_id).trait_item_def_id.is_none() {
1424-
self.perform_lint(
1425-
cx,
1426-
"item",
1427-
impl_item.def_id,
1428-
impl_item.span,
1429-
impl_item.vis_span,
1430-
false,
1431-
);
1416+
self.perform_lint(cx, "item", impl_item.def_id, impl_item.vis_span, false);
14321417
}
14331418
}
14341419
}

compiler/rustc_mir_build/src/lints.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::graph::iterate::{
22
NodeStatus, TriColorDepthFirstSearch, TriColorVisitor,
33
};
4-
use rustc_hir::intravisit::FnKind;
4+
use rustc_hir::def::DefKind;
55
use rustc_middle::mir::{BasicBlock, BasicBlocks, Body, Operand, TerminatorKind};
66
use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
77
use rustc_middle::ty::{self, AssocItem, AssocItemContainer, Instance, TyCtxt};
@@ -12,12 +12,7 @@ use std::ops::ControlFlow;
1212
pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1313
let def_id = body.source.def_id().expect_local();
1414

15-
if let Some(fn_kind) = tcx.hir().get_by_def_id(def_id).fn_kind() {
16-
if let FnKind::Closure = fn_kind {
17-
// closures can't recur, so they don't matter.
18-
return;
19-
}
20-
15+
if let DefKind::Fn | DefKind::AssocFn = tcx.def_kind(def_id) {
2116
// If this is trait/impl method, extract the trait's substs.
2217
let trait_substs = match tcx.opt_associated_item(def_id.to_def_id()) {
2318
Some(AssocItem {
@@ -41,8 +36,8 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
4136

4237
vis.reachable_recursive_calls.sort();
4338

39+
let sp = tcx.def_span(def_id);
4440
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
45-
let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span_with_body(hir_id));
4641
tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| {
4742
let mut db = lint.build("function cannot return without recursing");
4843
db.span_label(sp, "cannot return without recursing");

compiler/rustc_privacy/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1754,8 +1754,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17541754
|| self.in_assoc_ty
17551755
|| self.tcx.resolutions(()).has_pub_restricted
17561756
{
1757-
let vis_span =
1758-
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
1757+
let vis_span = self.tcx.def_span(def_id);
17591758
if kind == "trait" {
17601759
self.tcx.sess.emit_err(InPublicInterfaceTraits {
17611760
span,

compiler/rustc_query_system/src/query/job.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::dep_graph::DepContext;
21
use crate::query::plumbing::CycleError;
32
use crate::query::{QueryContext, QueryStackFrame};
43
use rustc_hir::def::DefKind;
@@ -536,17 +535,13 @@ pub(crate) fn report_cycle<'a>(
536535
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
537536
assert!(!stack.is_empty());
538537

539-
let fix_span = |span: Span, query: &QueryStackFrame| {
540-
sess.source_map().guess_head_span(query.default_span(span))
541-
};
542-
543-
let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
538+
let span = stack[0].query.default_span(stack[1 % stack.len()].span);
544539
let mut err =
545540
struct_span_err!(sess, span, E0391, "cycle detected when {}", stack[0].query.description);
546541

547542
for i in 1..stack.len() {
548543
let query = &stack[i].query;
549-
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
544+
let span = query.default_span(stack[(i + 1) % stack.len()].span);
550545
err.span_note(span, &format!("...which requires {}...", query.description));
551546
}
552547

@@ -577,7 +572,7 @@ pub(crate) fn report_cycle<'a>(
577572
}
578573

579574
if let Some((span, query)) = usage {
580-
err.span_note(fix_span(span, &query), &format!("cycle used when {}", query.description));
575+
err.span_note(query.default_span(span), &format!("cycle used when {}", query.description));
581576
}
582577

583578
err
@@ -606,8 +601,7 @@ pub fn print_query_stack<CTX: QueryContext>(
606601
Level::FailureNote,
607602
&format!("#{} [{}] {}", i, query_info.query.name, query_info.query.description),
608603
);
609-
diag.span =
610-
tcx.dep_context().sess().source_map().guess_head_span(query_info.job.span).into();
604+
diag.span = query_info.job.span.into();
611605
handler.force_print_diagnostic(diag);
612606

613607
current_query = query_info.job.parent;

compiler/rustc_resolve/src/diagnostics.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1587,11 +1587,7 @@ impl<'a> Resolver<'a> {
15871587
};
15881588
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
15891589
LOCAL_CRATE => self.opt_span(def_id),
1590-
_ => Some(
1591-
self.session
1592-
.source_map()
1593-
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
1594-
),
1590+
_ => Some(self.cstore().get_span_untracked(def_id, self.session)),
15951591
});
15961592
if let Some(def_span) = def_span {
15971593
if span.overlaps(def_span) {

compiler/rustc_resolve/src/late/diagnostics.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
136136
fn def_span(&self, def_id: DefId) -> Option<Span> {
137137
match def_id.krate {
138138
LOCAL_CRATE => self.r.opt_span(def_id),
139-
_ => Some(
140-
self.r
141-
.session
142-
.source_map()
143-
.guess_head_span(self.r.cstore().get_span_untracked(def_id, self.r.session)),
144-
),
139+
_ => Some(self.r.cstore().get_span_untracked(def_id, self.r.session)),
145140
}
146141
}
147142

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

-1
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19461946
));
19471947

19481948
let original_span = err.span.primary_span().unwrap();
1949-
let original_span = self.tcx.sess.source_map().guess_head_span(original_span);
19501949
let mut span = MultiSpan::from_span(original_span);
19511950

19521951
let message = outer_generator

compiler/rustc_typeck/src/check/_match.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
293293
pub(crate) fn if_cause(
294294
&self,
295295
span: Span,
296+
cond_span: Span,
296297
then_expr: &'tcx hir::Expr<'tcx>,
297298
else_expr: &'tcx hir::Expr<'tcx>,
298299
then_ty: Ty<'tcx>,
@@ -355,10 +356,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
355356
// 6 | | };
356357
// | |_____^ expected integer, found `()`
357358
// ```
358-
if block.expr.is_none() && block.stmts.is_empty()
359-
&& let Some(outer_span) = &mut outer_span
360-
{
361-
*outer_span = self.tcx.sess.source_map().guess_head_span(*outer_span);
359+
if block.expr.is_none() && block.stmts.is_empty() && outer_span.is_some() {
360+
let sp = if let Some(cs) = cond_span.find_ancestor_inside(span) {
361+
span.with_hi(cs.hi())
362+
} else {
363+
span
364+
};
365+
outer_span = Some(sp);
362366
}
363367

364368
(self.find_block_span(block), block.hir_id)

compiler/rustc_typeck/src/check/expr.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10031003
let else_diverges = self.diverges.get();
10041004

10051005
let opt_suggest_box_span = self.opt_suggest_box_span(else_ty, orig_expected);
1006-
let if_cause =
1007-
self.if_cause(sp, then_expr, else_expr, then_ty, else_ty, opt_suggest_box_span);
1006+
let if_cause = self.if_cause(
1007+
sp,
1008+
cond_expr.span,
1009+
then_expr,
1010+
else_expr,
1011+
then_ty,
1012+
else_ty,
1013+
opt_suggest_box_span,
1014+
);
10081015

10091016
coerce.coerce(self, &if_cause, else_expr, else_ty);
10101017

src/test/ui/async-await/no-const-async.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ note: cycle used when checking item types in top-level module
3535
--> $DIR/no-const-async.rs:4:1
3636
|
3737
LL | pub const async fn x() {}
38-
| ^^^^^^^^^^^^^^^^^^^^^^
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: aborting due to 2 previous errors
4141

src/test/ui/consts/issue-44415.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ error[E0391]: cycle detected when evaluating type-level constant
22
--> $DIR/issue-44415.rs:6:17
33
|
44
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
5-
| ^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
88
--> $DIR/issue-44415.rs:6:17
99
|
1010
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
11-
| ^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
1313
--> $DIR/issue-44415.rs:6:17
1414
|
1515
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
16-
| ^^^^^^
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717
= note: ...which requires computing layout of `Foo`...
1818
= note: ...which requires computing layout of `[u8; _]`...
1919
= note: ...which requires normalizing `[u8; _]`...

src/test/ui/cycle-trait/cycle-trait-default-type-trait.stderr

+14-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ LL | trait Foo<X = Box<dyn Foo>> {
88
note: cycle used when collecting item types in top-level module
99
--> $DIR/cycle-trait-default-type-trait.rs:4:1
1010
|
11-
LL | trait Foo<X = Box<dyn Foo>> {
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
LL | / trait Foo<X = Box<dyn Foo>> {
12+
LL | |
13+
LL | |
14+
LL | | }
15+
LL | |
16+
LL | | fn main() { }
17+
| |_____________^
1318

1419
error[E0391]: cycle detected when computing type of `Foo::X`
1520
--> $DIR/cycle-trait-default-type-trait.rs:4:23
@@ -21,8 +26,13 @@ LL | trait Foo<X = Box<dyn Foo>> {
2126
note: cycle used when collecting item types in top-level module
2227
--> $DIR/cycle-trait-default-type-trait.rs:4:1
2328
|
24-
LL | trait Foo<X = Box<dyn Foo>> {
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
LL | / trait Foo<X = Box<dyn Foo>> {
30+
LL | |
31+
LL | |
32+
LL | | }
33+
LL | |
34+
LL | | fn main() { }
35+
| |_____________^
2636

2737
error: aborting due to 2 previous errors
2838

src/test/ui/cycle-trait/cycle-trait-supertrait-direct.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ LL | trait Chromosome: Chromosome {
1313
note: cycle used when collecting item types in top-level module
1414
--> $DIR/cycle-trait-supertrait-direct.rs:3:1
1515
|
16-
LL | trait Chromosome: Chromosome {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | / trait Chromosome: Chromosome {
17+
LL | |
18+
LL | | }
19+
| |_^
1820

1921
error: aborting due to previous error
2022

src/test/ui/hrtb/hrtb-perfect-forwarding.stderr

+16-26
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ warning: function cannot return without recursing
44
LL | / fn no_hrtb<'b, T>(mut t: T)
55
LL | | where
66
LL | | T: Bar<&'b isize>,
7-
LL | | {
8-
... |
9-
LL | | no_hrtb(&mut t);
10-
| | --------------- recursive call site
11-
LL | | }
12-
| |_^ cannot return without recursing
7+
| |______________________^ cannot return without recursing
8+
...
9+
LL | no_hrtb(&mut t);
10+
| --------------- recursive call site
1311
|
1412
= note: `#[warn(unconditional_recursion)]` on by default
1513
= help: a `loop` may express intention better if this is on purpose
@@ -20,12 +18,10 @@ warning: function cannot return without recursing
2018
LL | / fn bar_hrtb<T>(mut t: T)
2119
LL | | where
2220
LL | | T: for<'b> Bar<&'b isize>,
23-
LL | | {
24-
... |
25-
LL | | bar_hrtb(&mut t);
26-
| | ---------------- recursive call site
27-
LL | | }
28-
| |_^ cannot return without recursing
21+
| |______________________________^ cannot return without recursing
22+
...
23+
LL | bar_hrtb(&mut t);
24+
| ---------------- recursive call site
2925
|
3026
= help: a `loop` may express intention better if this is on purpose
3127

@@ -35,14 +31,10 @@ warning: function cannot return without recursing
3531
LL | / fn foo_hrtb_bar_not<'b, T>(mut t: T)
3632
LL | | where
3733
LL | | T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
38-
LL | | {
39-
... |
40-
LL | | foo_hrtb_bar_not(&mut t);
41-
| | ------------------------ recursive call site
42-
LL | |
43-
LL | |
44-
LL | | }
45-
| |_^ cannot return without recursing
34+
| |_______________________________________________^ cannot return without recursing
35+
...
36+
LL | foo_hrtb_bar_not(&mut t);
37+
| ------------------------ recursive call site
4638
|
4739
= help: a `loop` may express intention better if this is on purpose
4840

@@ -70,12 +62,10 @@ warning: function cannot return without recursing
7062
LL | / fn foo_hrtb_bar_hrtb<T>(mut t: T)
7163
LL | | where
7264
LL | | T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
73-
LL | | {
74-
LL | | // OK -- now we have `T : for<'b> Bar<&'b isize>`.
75-
LL | | foo_hrtb_bar_hrtb(&mut t);
76-
| | ------------------------- recursive call site
77-
LL | | }
78-
| |_^ cannot return without recursing
65+
| |_______________________________________________________^ cannot return without recursing
66+
...
67+
LL | foo_hrtb_bar_hrtb(&mut t);
68+
| ------------------------- recursive call site
7969
|
8070
= help: a `loop` may express intention better if this is on purpose
8171

src/test/ui/issues/issue-12511.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ LL | trait T2 : T1 {
2323
note: cycle used when collecting item types in top-level module
2424
--> $DIR/issue-12511.rs:1:1
2525
|
26-
LL | trait T1 : T2 {
27-
| ^^^^^^^^^^^^^
26+
LL | / trait T1 : T2 {
27+
LL | |
28+
LL | | }
29+
| |_^
2830

2931
error: aborting due to previous error
3032

0 commit comments

Comments
 (0)