Skip to content

Commit e0e6f1d

Browse files
authored
Rollup merge of #98869 - compiler-errors:stop_guessing_head_span, r=cjgillot
Remove some usages of `guess_head_span` No need to pass things through `guess_head_span` if they already point to the head span. Only major change is that we point to the head span of `enum`s on some errors now, which I prefer. r? `@cjgillot`
2 parents a6d3ee3 + fcfb3e9 commit e0e6f1d

File tree

35 files changed

+162
-223
lines changed

35 files changed

+162
-223
lines changed

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
714714
}
715715

716716
fn const_equate(&mut self, _a: Const<'tcx>, _b: Const<'tcx>) {
717-
span_bug!(
718-
self.cause.span(self.infcx.tcx),
719-
"generic_const_exprs: unreachable `const_equate`"
720-
);
717+
span_bug!(self.cause.span(), "generic_const_exprs: unreachable `const_equate`");
721718
}
722719

723720
fn normalization() -> NormalizationStrategy {

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14351435
swap_secondary_and_primary: bool,
14361436
force_label: bool,
14371437
) {
1438-
let span = cause.span(self.tcx);
1438+
let span = cause.span();
14391439

14401440
// For some types of errors, expected-found does not make
14411441
// sense, so just ignore the values we were given.
@@ -2085,7 +2085,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20852085

20862086
debug!("report_and_explain_type_error(trace={:?}, terr={:?})", trace, terr);
20872087

2088-
let span = trace.cause.span(self.tcx);
2088+
let span = trace.cause.span();
20892089
let failure_code = trace.cause.as_failure_code(terr);
20902090
let mut diag = match failure_code {
20912091
FailureCode::Error0038(did) => {

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
204204
expected_substs: SubstsRef<'tcx>,
205205
actual_substs: SubstsRef<'tcx>,
206206
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
207-
let span = cause.span(self.tcx());
207+
let span = cause.span();
208208
let msg = format!(
209209
"implementation of `{}` is not general enough",
210210
self.tcx().def_path_str(trait_def_id),

compiler/rustc_infer/src/traits/error_reporting/mod.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
1818
trait_item_def_id: DefId,
1919
requirement: &dyn fmt::Display,
2020
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
21-
let msg = "impl has stricter requirements than trait";
22-
let sp = self.tcx.sess.source_map().guess_head_span(error_span);
21+
let mut err = struct_span_err!(
22+
self.tcx.sess,
23+
error_span,
24+
E0276,
25+
"impl has stricter requirements than trait"
26+
);
2327

24-
let mut err = struct_span_err!(self.tcx.sess, sp, E0276, "{}", msg);
25-
26-
if trait_item_def_id.is_local() {
28+
if let Some(span) = self.tcx.hir().span_if_local(trait_item_def_id) {
2729
let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
28-
err.span_label(
29-
self.tcx.def_span(trait_item_def_id),
30-
format!("definition of `{}` from trait", item_name),
31-
);
30+
err.span_label(span, format!("definition of `{}` from trait", item_name));
3231
}
3332

34-
err.span_label(sp, format!("impl has extra requirement {}", requirement));
33+
err.span_label(error_span, format!("impl has extra requirement {}", requirement));
3534

3635
err
3736
}
@@ -48,7 +47,6 @@ pub fn report_object_safety_error<'tcx>(
4847
hir::Node::Item(item) => Some(item.ident.span),
4948
_ => None,
5049
});
51-
let span = tcx.sess.source_map().guess_head_span(span);
5250
let mut err = struct_span_err!(
5351
tcx.sess,
5452
span,

compiler/rustc_lint/src/context.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,8 @@ pub trait LintContext: Sized {
694694
}
695695

696696
if let Some(span) = in_test_module {
697-
let def_span = self.sess().source_map().guess_head_span(span);
698697
db.span_help(
699-
span.shrink_to_lo().to(def_span),
698+
self.sess().source_map().guess_head_span(span),
700699
"consider adding a `#[cfg(test)]` to the containing module",
701700
);
702701
}

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,8 @@ impl<'tcx> ObligationCause<'tcx> {
139139
ObligationCause { span, body_id: hir::CRATE_HIR_ID, code: Default::default() }
140140
}
141141

142-
pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span {
142+
pub fn span(&self) -> Span {
143143
match *self.code() {
144-
ObligationCauseCode::CompareImplMethodObligation { .. }
145-
| ObligationCauseCode::MainFunctionType
146-
| ObligationCauseCode::StartFunctionType => {
147-
tcx.sess.source_map().guess_head_span(self.span)
148-
}
149144
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
150145
arm_span,
151146
..

compiler/rustc_privacy/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,8 +1759,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17591759
|| self.tcx.resolutions(()).has_pub_restricted
17601760
{
17611761
let descr = descr.to_string();
1762-
let vis_span =
1763-
self.tcx.sess.source_map().guess_head_span(self.tcx.def_span(def_id));
1762+
let vis_span = self.tcx.def_span(def_id);
17641763
if kind == "trait" {
17651764
self.tcx.sess.emit_err(InPublicInterfaceTraits {
17661765
span,

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
823823

824824
ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
825825
let found_kind = self.closure_kind(closure_substs).unwrap();
826-
let closure_span =
827-
self.tcx.sess.source_map().guess_head_span(
828-
self.tcx.hir().span_if_local(closure_def_id).unwrap(),
829-
);
826+
let closure_span = self.tcx.def_span(closure_def_id);
830827
let mut err = struct_span_err!(
831828
self.tcx.sess,
832829
closure_span,
@@ -951,9 +948,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
951948
_ => None,
952949
};
953950

954-
let found_span = found_did
955-
.and_then(|did| self.tcx.hir().span_if_local(did))
956-
.map(|sp| self.tcx.sess.source_map().guess_head_span(sp)); // the sp could be an fn def
951+
let found_span = found_did.and_then(|did| self.tcx.hir().span_if_local(did));
957952

958953
if self.reported_closure_mismatch.borrow().contains(&(span, found_span)) {
959954
// We check closures twice, with obligations flowing in different directions,
@@ -1089,7 +1084,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10891084
kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, .. }),
10901085
..
10911086
}) => (
1092-
sm.guess_head_span(fn_decl_span),
1087+
fn_decl_span,
10931088
hir.body(body)
10941089
.params
10951090
.iter()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
15431543
ty::Generator(..) => "generator",
15441544
_ => "function",
15451545
};
1546-
let span = self.tcx.sess.source_map().guess_head_span(span);
15471546
let mut err = struct_span_err!(
15481547
self.tcx.sess,
15491548
span,

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
603603
),
604604
(Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
605605
span_bug!(
606-
obligation.cause.span(self.selcx.tcx()),
606+
obligation.cause.span(),
607607
"ConstEquate: const_eval_resolve returned an unexpected error"
608608
)
609609
}

0 commit comments

Comments
 (0)