Skip to content

Remove some uses of guess_head_span #99911

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

Merged
merged 3 commits into from
Aug 1, 2022
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
25 changes: 5 additions & 20 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,6 @@ impl UnreachablePub {
cx: &LateContext<'_>,
what: &str,
def_id: LocalDefId,
span: Span,
vis_span: Span,
exportable: bool,
) {
Expand All @@ -1373,7 +1372,7 @@ impl UnreachablePub {
if vis_span.from_expansion() {
applicability = Applicability::MaybeIncorrect;
}
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
let def_span = cx.tcx.def_span(def_id);
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
let mut err = lint.build(fluent::lint::builtin_unreachable_pub);
err.set_arg("what", what);
Expand All @@ -1399,36 +1398,22 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
if let hir::ItemKind::Use(_, hir::UseKind::ListStem) = &item.kind {
return;
}
self.perform_lint(cx, "item", item.def_id, item.span, item.vis_span, true);
self.perform_lint(cx, "item", item.def_id, item.vis_span, true);
}

fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
self.perform_lint(
cx,
"item",
foreign_item.def_id,
foreign_item.span,
foreign_item.vis_span,
true,
);
self.perform_lint(cx, "item", foreign_item.def_id, foreign_item.vis_span, true);
}

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

fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
// Only lint inherent impl items.
if cx.tcx.associated_item(impl_item.def_id).trait_item_def_id.is_none() {
self.perform_lint(
cx,
"item",
impl_item.def_id,
impl_item.span,
impl_item.vis_span,
false,
);
self.perform_lint(cx, "item", impl_item.def_id, impl_item.vis_span, false);
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions compiler/rustc_mir_build/src/lints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_data_structures::graph::iterate::{
NodeStatus, TriColorDepthFirstSearch, TriColorVisitor,
};
use rustc_hir::intravisit::FnKind;
use rustc_hir::def::DefKind;
use rustc_middle::mir::{BasicBlock, BasicBlocks, Body, Operand, TerminatorKind};
use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
use rustc_middle::ty::{self, AssocItem, AssocItemContainer, Instance, TyCtxt};
Expand All @@ -12,12 +12,7 @@ use std::ops::ControlFlow;
pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
let def_id = body.source.def_id().expect_local();

if let Some(fn_kind) = tcx.hir().get_by_def_id(def_id).fn_kind() {
if let FnKind::Closure = fn_kind {
// closures can't recur, so they don't matter.
return;
}

if let DefKind::Fn | DefKind::AssocFn = tcx.def_kind(def_id) {
// If this is trait/impl method, extract the trait's substs.
let trait_substs = match tcx.opt_associated_item(def_id.to_def_id()) {
Some(AssocItem {
Expand All @@ -41,8 +36,8 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {

vis.reachable_recursive_calls.sort();

let sp = tcx.def_span(def_id);
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span_with_body(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
3 changes: 1 addition & 2 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,8 +1754,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
|| self.in_assoc_ty
|| self.tcx.resolutions(()).has_pub_restricted
{
let vis_span =
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
let vis_span = self.tcx.def_span(def_id);
if kind == "trait" {
self.tcx.sess.emit_err(InPublicInterfaceTraits {
span,
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_query_system/src/query/job.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::dep_graph::DepContext;
use crate::query::plumbing::CycleError;
use crate::query::{QueryContext, QueryStackFrame};
use rustc_hir::def::DefKind;
Expand Down Expand Up @@ -536,17 +535,13 @@ pub(crate) fn report_cycle<'a>(
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
assert!(!stack.is_empty());

let fix_span = |span: Span, query: &QueryStackFrame| {
sess.source_map().guess_head_span(query.default_span(span))
};

let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
let span = stack[0].query.default_span(stack[1 % stack.len()].span);
let mut err =
struct_span_err!(sess, span, E0391, "cycle detected when {}", stack[0].query.description);

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

Expand Down Expand Up @@ -577,7 +572,7 @@ pub(crate) fn report_cycle<'a>(
}

if let Some((span, query)) = usage {
err.span_note(fix_span(span, &query), &format!("cycle used when {}", query.description));
err.span_note(query.default_span(span), &format!("cycle used when {}", query.description));
}

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

current_query = query_info.job.parent;
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,11 +1587,7 @@ impl<'a> Resolver<'a> {
};
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
LOCAL_CRATE => self.opt_span(def_id),
_ => Some(
self.session
.source_map()
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
),
_ => Some(self.cstore().get_span_untracked(def_id, self.session)),
});
if let Some(def_span) = def_span {
if span.overlaps(def_span) {
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
fn def_span(&self, def_id: DefId) -> Option<Span> {
match def_id.krate {
LOCAL_CRATE => self.r.opt_span(def_id),
_ => Some(
self.r
.session
.source_map()
.guess_head_span(self.r.cstore().get_span_untracked(def_id, self.r.session)),
),
_ => Some(self.r.cstore().get_span_untracked(def_id, self.r.session)),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
));

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

let message = outer_generator
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub(crate) fn if_cause(
&self,
span: Span,
cond_span: Span,
then_expr: &'tcx hir::Expr<'tcx>,
else_expr: &'tcx hir::Expr<'tcx>,
then_ty: Ty<'tcx>,
Expand Down Expand Up @@ -354,10 +355,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// 6 | | };
// | |_____^ expected integer, found `()`
// ```
if block.expr.is_none() && block.stmts.is_empty()
&& let Some(outer_span) = &mut outer_span
{
*outer_span = self.tcx.sess.source_map().guess_head_span(*outer_span);
if block.expr.is_none() && block.stmts.is_empty() && outer_span.is_some() {
let sp = if let Some(cs) = cond_span.find_ancestor_inside(span) {
span.with_hi(cs.hi())
} else {
span
};
outer_span = Some(sp);
}

(self.find_block_span(block), block.hir_id)
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let else_diverges = self.diverges.get();

let opt_suggest_box_span = self.opt_suggest_box_span(else_ty, orig_expected);
let if_cause =
self.if_cause(sp, then_expr, else_expr, then_ty, else_ty, opt_suggest_box_span);
let if_cause = self.if_cause(
sp,
cond_expr.span,
then_expr,
else_expr,
then_ty,
else_ty,
opt_suggest_box_span,
);

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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/async-await/no-const-async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ note: cycle used when checking item types in top-level module
--> $DIR/no-const-async.rs:4:1
|
LL | pub const async fn x() {}
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/consts/issue-44415.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ error[E0391]: cycle detected when evaluating type-level constant
--> $DIR/issue-44415.rs:6:17
|
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
--> $DIR/issue-44415.rs:6:17
|
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`...
--> $DIR/issue-44415.rs:6:17
|
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires computing layout of `[u8; _]`...
= note: ...which requires normalizing `[u8; _]`...
Expand Down
18 changes: 14 additions & 4 deletions src/test/ui/cycle-trait/cycle-trait-default-type-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ LL | trait Foo<X = Box<dyn Foo>> {
note: cycle used when collecting item types in top-level module
--> $DIR/cycle-trait-default-type-trait.rs:4:1
|
LL | trait Foo<X = Box<dyn Foo>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / trait Foo<X = Box<dyn Foo>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This span was wrong, as it's supposed to point to the module.

LL | |
LL | |
LL | | }
LL | |
LL | | fn main() { }
| |_____________^

error[E0391]: cycle detected when computing type of `Foo::X`
--> $DIR/cycle-trait-default-type-trait.rs:4:23
Expand All @@ -21,8 +26,13 @@ LL | trait Foo<X = Box<dyn Foo>> {
note: cycle used when collecting item types in top-level module
--> $DIR/cycle-trait-default-type-trait.rs:4:1
|
LL | trait Foo<X = Box<dyn Foo>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / trait Foo<X = Box<dyn Foo>> {
LL | |
LL | |
LL | | }
LL | |
LL | | fn main() { }
| |_____________^

error: aborting due to 2 previous errors

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/cycle-trait/cycle-trait-supertrait-direct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ LL | trait Chromosome: Chromosome {
note: cycle used when collecting item types in top-level module
--> $DIR/cycle-trait-supertrait-direct.rs:3:1
|
LL | trait Chromosome: Chromosome {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / trait Chromosome: Chromosome {
LL | |
LL | | }
| |_^

error: aborting due to previous error

Expand Down
42 changes: 16 additions & 26 deletions src/test/ui/hrtb/hrtb-perfect-forwarding.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ warning: function cannot return without recursing
LL | / fn no_hrtb<'b, T>(mut t: T)
LL | | where
LL | | T: Bar<&'b isize>,
LL | | {
... |
LL | | no_hrtb(&mut t);
| | --------------- recursive call site
LL | | }
| |_^ cannot return without recursing
| |______________________^ cannot return without recursing
...
LL | no_hrtb(&mut t);
| --------------- recursive call site
|
= note: `#[warn(unconditional_recursion)]` on by default
= help: a `loop` may express intention better if this is on purpose
Expand All @@ -20,12 +18,10 @@ warning: function cannot return without recursing
LL | / fn bar_hrtb<T>(mut t: T)
LL | | where
LL | | T: for<'b> Bar<&'b isize>,
LL | | {
... |
LL | | bar_hrtb(&mut t);
| | ---------------- recursive call site
LL | | }
| |_^ cannot return without recursing
| |______________________________^ cannot return without recursing
...
LL | bar_hrtb(&mut t);
| ---------------- recursive call site
|
= help: a `loop` may express intention better if this is on purpose

Expand All @@ -35,14 +31,10 @@ warning: function cannot return without recursing
LL | / fn foo_hrtb_bar_not<'b, T>(mut t: T)
LL | | where
LL | | T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
LL | | {
... |
LL | | foo_hrtb_bar_not(&mut t);
| | ------------------------ recursive call site
LL | |
LL | |
LL | | }
| |_^ cannot return without recursing
| |_______________________________________________^ cannot return without recursing
...
LL | foo_hrtb_bar_not(&mut t);
| ------------------------ recursive call site
|
= help: a `loop` may express intention better if this is on purpose

Expand Down Expand Up @@ -70,12 +62,10 @@ warning: function cannot return without recursing
LL | / fn foo_hrtb_bar_hrtb<T>(mut t: T)
LL | | where
LL | | T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
LL | | {
LL | | // OK -- now we have `T : for<'b> Bar<&'b isize>`.
LL | | foo_hrtb_bar_hrtb(&mut t);
| | ------------------------- recursive call site
LL | | }
| |_^ cannot return without recursing
| |_______________________________________________________^ cannot return without recursing
...
LL | foo_hrtb_bar_hrtb(&mut t);
| ------------------------- recursive call site
|
= help: a `loop` may express intention better if this is on purpose

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/issues/issue-12511.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ LL | trait T2 : T1 {
note: cycle used when collecting item types in top-level module
--> $DIR/issue-12511.rs:1:1
|
LL | trait T1 : T2 {
| ^^^^^^^^^^^^^
LL | / trait T1 : T2 {
LL | |
LL | | }
| |_^

error: aborting due to previous error

Expand Down
Loading