Skip to content

Commit 3e3feac

Browse files
authored
Rollup merge of #132243 - compiler-errors:no-span, r=jieyouxu
Remove `ObligationCause::span()` method I think it's an incredibly confusing footgun to expose both `obligation_cause.span` and `obligation_cause.span()`. Especially because `ObligationCause::span()` (the method) seems to just be hacking around a single quirk in the way we set up obligation causes for match arms. First commit removes the need for that hack, with only one diagnostic span changing (but IMO not really getting worse -- I'd argue that it was already confusing).
2 parents f14637b + 7f54b9e commit 3e3feac

File tree

12 files changed

+38
-46
lines changed

12 files changed

+38
-46
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
611611
Err(terr) => {
612612
let mut diag = struct_span_code_err!(
613613
tcx.dcx(),
614-
cause.span(),
614+
cause.span,
615615
E0053,
616616
"method `{}` has an incompatible return type for trait",
617617
trait_m.name
@@ -1169,7 +1169,7 @@ fn extract_spans_for_error_reporting<'tcx>(
11691169
TypeError::ArgumentMutability(i) | TypeError::ArgumentSorts(ExpectedFound { .. }, i) => {
11701170
(impl_args.nth(i).unwrap(), trait_args.and_then(|mut args| args.nth(i)))
11711171
}
1172-
_ => (cause.span(), tcx.hir().span_if_local(trait_m.def_id)),
1172+
_ => (cause.span, tcx.hir().span_if_local(trait_m.def_id)),
11731173
}
11741174
}
11751175

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ pub fn check_function_signature<'tcx>(
612612
match err {
613613
TypeError::ArgumentMutability(i)
614614
| TypeError::ArgumentSorts(ExpectedFound { .. }, i) => args.nth(i).unwrap(),
615-
_ => cause.span(),
615+
_ => cause.span,
616616
}
617617
}
618618

compiler/rustc_hir_typeck/src/_match.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9494
(None, arm.body.span)
9595
};
9696

97-
let (span, code) = match prior_arm {
97+
let code = match prior_arm {
9898
// The reason for the first arm to fail is not that the match arms diverge,
9999
// but rather that there's a prior obligation that doesn't hold.
100-
None => {
101-
(arm_span, ObligationCauseCode::BlockTailExpression(arm.body.hir_id, match_src))
102-
}
103-
Some((prior_arm_block_id, prior_arm_ty, prior_arm_span)) => (
104-
expr.span,
100+
None => ObligationCauseCode::BlockTailExpression(arm.body.hir_id, match_src),
101+
Some((prior_arm_block_id, prior_arm_ty, prior_arm_span)) => {
105102
ObligationCauseCode::MatchExpressionArm(Box::new(MatchExpressionArmCause {
106103
arm_block_id,
107104
arm_span,
@@ -110,13 +107,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
110107
prior_arm_ty,
111108
prior_arm_span,
112109
scrut_span: scrut.span,
110+
expr_span: expr.span,
113111
source: match_src,
114112
prior_non_diverging_arms: prior_non_diverging_arms.clone(),
115113
tail_defines_return_position_impl_trait,
116-
})),
117-
),
114+
}))
115+
}
118116
};
119-
let cause = self.cause(span, code);
117+
let cause = self.cause(arm_span, code);
120118

121119
// This is the moral equivalent of `coercion.coerce(self, cause, arm.body, arm_ty)`.
122120
// We use it this way to be able to expand on the potential error and detect when a

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2027,7 +2027,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20272027
}
20282028
Err(_) => {
20292029
span_bug!(
2030-
cause.span(),
2030+
cause.span,
20312031
"subtyping remaining fields of type changing FRU failed: {target_ty} != {fru_ty}: {}::{}",
20322032
variant.name,
20332033
ident.name,

compiler/rustc_hir_typeck/src/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
550550
// This has/will have errored in wfcheck, which we cannot depend on from here, as typeck on functions
551551
// may run before wfcheck if the function is used in const eval.
552552
self.dcx().span_delayed_bug(
553-
cause.span(),
553+
cause.span,
554554
format!("{self_ty} was a subtype of {method_self_ty} but now is not?"),
555555
);
556556
}

compiler/rustc_middle/src/traits/mod.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@ impl<'tcx> ObligationCause<'tcx> {
9292
ObligationCause { span, body_id: CRATE_DEF_ID, code: Default::default() }
9393
}
9494

95-
pub fn span(&self) -> Span {
96-
match *self.code() {
97-
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
98-
arm_span,
99-
..
100-
}) => arm_span,
101-
_ => self.span,
102-
}
103-
}
104-
10595
#[inline]
10696
pub fn code(&self) -> &ObligationCauseCode<'tcx> {
10797
&self.code
@@ -517,10 +507,17 @@ pub struct MatchExpressionArmCause<'tcx> {
517507
pub prior_arm_block_id: Option<HirId>,
518508
pub prior_arm_ty: Ty<'tcx>,
519509
pub prior_arm_span: Span,
510+
/// Span of the scrutinee of the match (the matched value).
520511
pub scrut_span: Span,
512+
/// Source of the match, i.e. `match` or a desugaring.
521513
pub source: hir::MatchSource,
514+
/// Span of the *whole* match expr.
515+
pub expr_span: Span,
516+
/// Spans of the previous arms except for those that diverge (i.e. evaluate to `!`).
517+
///
518+
/// These are used for pointing out errors that may affect several arms.
522519
pub prior_non_diverging_arms: Vec<Span>,
523-
// Is the expectation of this match expression an RPIT?
520+
/// Is the expectation of this match expression an RPIT?
524521
pub tail_defines_return_position_impl_trait: Option<LocalDefId>,
525522
}
526523

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
392392
Some(ty) if expected == ty => {
393393
let source_map = self.tcx.sess.source_map();
394394
err.span_suggestion(
395-
source_map.end_point(cause.span()),
395+
source_map.end_point(cause.span),
396396
"try removing this `?`",
397397
"",
398398
Applicability::MachineApplicable,
@@ -412,6 +412,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
412412
source,
413413
ref prior_non_diverging_arms,
414414
scrut_span,
415+
expr_span,
415416
..
416417
}) => match source {
417418
hir::MatchSource::TryDesugar(scrut_hir_id) => {
@@ -430,7 +431,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
430431
Some(ty) if expected == ty => {
431432
let source_map = self.tcx.sess.source_map();
432433
err.span_suggestion(
433-
source_map.end_point(cause.span()),
434+
source_map.end_point(cause.span),
434435
"try removing this `?`",
435436
"",
436437
Applicability::MachineApplicable,
@@ -460,12 +461,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
460461
format!("this and all prior arms are found to be of type `{t}`"),
461462
);
462463
}
463-
let outer = if any_multiline_arm || !source_map.is_multiline(cause.span) {
464+
let outer = if any_multiline_arm || !source_map.is_multiline(expr_span) {
464465
// Cover just `match` and the scrutinee expression, not
465466
// the entire match body, to reduce diagram noise.
466-
cause.span.shrink_to_lo().to(scrut_span)
467+
expr_span.shrink_to_lo().to(scrut_span)
467468
} else {
468-
cause.span
469+
expr_span
469470
};
470471
let msg = "`match` arms have incompatible types";
471472
err.span_label(outer, msg);
@@ -1148,7 +1149,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
11481149
terr: TypeError<'tcx>,
11491150
prefer_label: bool,
11501151
) {
1151-
let span = cause.span();
1152+
let span = cause.span;
11521153

11531154
// For some types of errors, expected-found does not make
11541155
// sense, so just ignore the values we were given.
@@ -1642,7 +1643,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
16421643
terr: TypeError<'tcx>,
16431644
) -> Vec<TypeErrorAdditionalDiags> {
16441645
let mut suggestions = Vec::new();
1645-
let span = trace.cause.span();
1646+
let span = trace.cause.span;
16461647
let values = self.resolve_vars_if_possible(trace.values);
16471648
if let Some((expected, found)) = values.ty() {
16481649
match (expected.kind(), found.kind()) {
@@ -1792,7 +1793,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17921793
) -> Diag<'a> {
17931794
debug!("report_and_explain_type_error(trace={:?}, terr={:?})", trace, terr);
17941795

1795-
let span = trace.cause.span();
1796+
let span = trace.cause.span;
17961797
let failure_code = trace.cause.as_failure_code_diag(
17971798
terr,
17981799
span,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
237237
expected_args: GenericArgsRef<'tcx>,
238238
actual_args: GenericArgsRef<'tcx>,
239239
) -> Diag<'tcx> {
240-
let span = cause.span();
240+
let span = cause.span;
241241

242242
let (leading_ellipsis, satisfy_span, where_span, dup_span, def_id) =
243243
if let ObligationCauseCode::WhereClause(def_id, span)

compiler/rustc_trait_selection/src/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
11801180
selcx.tcx(),
11811181
selcx.tcx().require_lang_item(
11821182
LangItem::Sized,
1183-
Some(obligation.cause.span()),
1183+
Some(obligation.cause.span),
11841184
),
11851185
[self_ty],
11861186
),
@@ -1600,7 +1600,7 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
16001600
// exist. Instead, `Pointee<Metadata = ()>` should be a supertrait of `Sized`.
16011601
let sized_predicate = ty::TraitRef::new(
16021602
tcx,
1603-
tcx.require_lang_item(LangItem::Sized, Some(obligation.cause.span())),
1603+
tcx.require_lang_item(LangItem::Sized, Some(obligation.cause.span)),
16041604
[self_ty],
16051605
);
16061606
obligations.push(obligation.with(tcx, sized_predicate));

compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'tcx> InferCtxt<'tcx> {
9090
assert!(!self.intercrate);
9191
let c_pred =
9292
self.canonicalize_query(param_env.and(obligation.predicate), &mut _orig_values);
93-
self.tcx.at(obligation.cause.span()).evaluate_obligation(c_pred)
93+
self.tcx.at(obligation.cause.span).evaluate_obligation(c_pred)
9494
}
9595
}
9696

tests/ui/wf/wf-dyn-incompat-trait-obj-match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ fn main() {
2222
Some(()) => &S,
2323
None => &R, //~ ERROR E0308
2424
}
25-
let t: &dyn Trait = match opt() { //~ ERROR E0038
25+
let t: &dyn Trait = match opt() {
2626
Some(()) => &S, //~ ERROR E0038
27-
None => &R,
27+
None => &R, //~ ERROR E0038
2828
};
2929
}

tests/ui/wf/wf-dyn-incompat-trait-obj-match.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ LL | trait Trait: Sized {}
3131
= note: required for the cast from `&S` to `&dyn Trait`
3232

3333
error[E0038]: the trait `Trait` cannot be made into an object
34-
--> $DIR/wf-dyn-incompat-trait-obj-match.rs:25:25
34+
--> $DIR/wf-dyn-incompat-trait-obj-match.rs:27:17
3535
|
36-
LL | let t: &dyn Trait = match opt() {
37-
| _________________________^
38-
LL | | Some(()) => &S,
39-
LL | | None => &R,
40-
LL | | };
41-
| |_____^ `Trait` cannot be made into an object
36+
LL | None => &R,
37+
| ^^ `Trait` cannot be made into an object
4238
|
4339
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
4440
--> $DIR/wf-dyn-incompat-trait-obj-match.rs:6:14

0 commit comments

Comments
 (0)