Skip to content

Commit fa5acdc

Browse files
authored
Rollup merge of rust-lang#134984 - compiler-errors:obligation-tweaks, r=lqd
`ObligationCause` construction tweaks in typeck Mostly just consolidating the way we construct obligations in `FnCtxt`.
2 parents 92f56fc + 1638948 commit fa5acdc

File tree

8 files changed

+28
-42
lines changed

8 files changed

+28
-42
lines changed

Diff for: compiler/rustc_hir_typeck/src/callee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId;
77
use rustc_hir::{self as hir, HirId, LangItem};
88
use rustc_hir_analysis::autoderef::Autoderef;
99
use rustc_infer::infer;
10-
use rustc_infer::traits::{self, Obligation, ObligationCause, ObligationCauseCode};
10+
use rustc_infer::traits::{Obligation, ObligationCause, ObligationCauseCode};
1111
use rustc_middle::ty::adjustment::{
1212
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability,
1313
};
@@ -512,7 +512,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
512512
self.register_bound(
513513
ty,
514514
self.tcx.require_lang_item(hir::LangItem::Tuple, Some(sp)),
515-
traits::ObligationCause::new(sp, self.body_id, ObligationCauseCode::RustCall),
515+
self.cause(sp, ObligationCauseCode::RustCall),
516516
);
517517
self.require_type_is_sized(ty, sp, ObligationCauseCode::RustCall);
518518
} else {

Diff for: compiler/rustc_hir_typeck/src/coercion.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
580580
let mut selcx = traits::SelectionContext::new(self);
581581

582582
// Create an obligation for `Source: CoerceUnsized<Target>`.
583-
let cause =
584-
ObligationCause::new(self.cause.span, self.body_id, ObligationCauseCode::Coercion {
585-
source,
586-
target,
587-
});
583+
let cause = self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target });
588584

589585
// Use a FIFO queue for this custom fulfillment procedure.
590586
//

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_hir::{ExprKind, HirId, QPath};
2222
use rustc_hir_analysis::hir_ty_lowering::{FeedConstTy, HirTyLowerer as _};
2323
use rustc_infer::infer;
2424
use rustc_infer::infer::{DefineOpaqueTypes, InferOk};
25-
use rustc_infer::traits::ObligationCause;
2625
use rustc_infer::traits::query::NoSolution;
2726
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
2827
use rustc_middle::ty::error::{ExpectedFound, TypeError};
@@ -1174,9 +1173,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11741173
for err in errors {
11751174
let cause = &mut err.obligation.cause;
11761175
if let ObligationCauseCode::OpaqueReturnType(None) = cause.code() {
1177-
let new_cause = ObligationCause::new(
1176+
let new_cause = self.cause(
11781177
cause.span,
1179-
cause.body_id,
11801178
ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, hir_id))),
11811179
);
11821180
*cause = new_cause;
@@ -3856,7 +3854,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38563854

38573855
// Enums are anyway always sized. But just to safeguard against future
38583856
// language extensions, let's double-check.
3859-
self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc);
3857+
self.require_type_is_sized(
3858+
field_ty,
3859+
expr.span,
3860+
ObligationCauseCode::FieldSized {
3861+
adt_kind: AdtKind::Enum,
3862+
span: self.tcx.def_span(field.did),
3863+
last: false,
3864+
},
3865+
);
38603866

38613867
if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
38623868
self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
@@ -3884,11 +3890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38843890
let field_ty = self.field_ty(expr.span, field, args);
38853891

38863892
if self.tcx.features().offset_of_slice() {
3887-
self.require_type_has_static_alignment(
3888-
field_ty,
3889-
expr.span,
3890-
ObligationCauseCode::Misc,
3891-
);
3893+
self.require_type_has_static_alignment(field_ty, expr.span);
38923894
} else {
38933895
self.require_type_is_sized(
38943896
field_ty,
@@ -3917,11 +3919,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39173919
{
39183920
if let Some(&field_ty) = tys.get(index) {
39193921
if self.tcx.features().offset_of_slice() {
3920-
self.require_type_has_static_alignment(
3921-
field_ty,
3922-
expr.span,
3923-
ObligationCauseCode::Misc,
3924-
);
3922+
self.require_type_has_static_alignment(field_ty, expr.span);
39253923
} else {
39263924
self.require_type_is_sized(
39273925
field_ty,

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
384384
code: traits::ObligationCauseCode<'tcx>,
385385
def_id: DefId,
386386
) {
387-
self.register_bound(ty, def_id, traits::ObligationCause::new(span, self.body_id, code));
387+
self.register_bound(ty, def_id, self.cause(span, code));
388388
}
389389

390390
pub(crate) fn require_type_is_sized(
@@ -410,12 +410,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
410410
}
411411
}
412412

413-
pub(crate) fn require_type_has_static_alignment(
414-
&self,
415-
ty: Ty<'tcx>,
416-
span: Span,
417-
code: traits::ObligationCauseCode<'tcx>,
418-
) {
413+
pub(crate) fn require_type_has_static_alignment(&self, ty: Ty<'tcx>, span: Span) {
419414
if !ty.references_error() {
420415
let tail = self.tcx.struct_tail_raw(
421416
ty,
@@ -434,7 +429,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
434429
} else {
435430
// We can't be sure, let's required full `Sized`.
436431
let lang_item = self.tcx.require_lang_item(LangItem::Sized, None);
437-
self.require_type_meets(ty, span, code, lang_item);
432+
self.require_type_meets(ty, span, ObligationCauseCode::Misc, lang_item);
438433
}
439434
}
440435
}
@@ -572,7 +567,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
572567
code: traits::ObligationCauseCode<'tcx>,
573568
) {
574569
// WF obligations never themselves fail, so no real need to give a detailed cause:
575-
let cause = traits::ObligationCause::new(span, self.body_id, code);
570+
let cause = self.cause(span, code);
576571
self.register_predicate(traits::Obligation::new(
577572
self.tcx,
578573
cause,
@@ -1426,9 +1421,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14261421
let bounds = self.instantiate_bounds(span, def_id, args);
14271422

14281423
for obligation in traits::predicates_for_generics(
1429-
|idx, predicate_span| {
1430-
traits::ObligationCause::new(span, self.body_id, code(idx, predicate_span))
1431-
},
1424+
|idx, predicate_span| self.cause(span, code(idx, predicate_span)),
14321425
param_env,
14331426
bounds,
14341427
) {
@@ -1561,7 +1554,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15611554
query_result: &Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
15621555
) -> InferResult<'tcx, Ty<'tcx>> {
15631556
self.instantiate_query_response_and_region_obligations(
1564-
&traits::ObligationCause::misc(span, self.body_id),
1557+
&self.misc(span),
15651558
self.param_env,
15661559
original_values,
15671560
query_result,

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207207
self.register_wf_obligation(
208208
fn_input_ty.into(),
209209
arg_expr.span,
210-
ObligationCauseCode::Misc,
210+
ObligationCauseCode::WellFormed(None),
211211
);
212212
}
213213

Diff for: compiler/rustc_hir_typeck/src/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
601601
self.call_expr.hir_id,
602602
idx,
603603
);
604-
traits::ObligationCause::new(self.span, self.body_id, code)
604+
self.cause(self.span, code)
605605
},
606606
self.param_env,
607607
method_predicates,

Diff for: compiler/rustc_hir_typeck/src/method/probe.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1739,8 +1739,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
17391739
&self,
17401740
trait_ref: ty::TraitRef<'tcx>,
17411741
) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
1742-
let cause = traits::ObligationCause::misc(self.span, self.body_id);
1743-
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, trait_ref);
1742+
let obligation =
1743+
traits::Obligation::new(self.tcx, self.misc(self.span), self.param_env, trait_ref);
17441744
traits::SelectionContext::new(self).select(&obligation)
17451745
}
17461746

@@ -1841,7 +1841,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18411841
self.scope_expr_id,
18421842
idx,
18431843
);
1844-
ObligationCause::new(self.span, self.body_id, code)
1844+
self.cause(self.span, code)
18451845
},
18461846
self.param_env,
18471847
impl_bounds,

Diff for: compiler/rustc_hir_typeck/src/method/suggest.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
105105
return false;
106106
};
107107
let trait_ref = ty::TraitRef::new(self.tcx, into_iterator_trait, [ty]);
108-
let cause = ObligationCause::new(span, self.body_id, ObligationCauseCode::Misc);
109-
let obligation = Obligation::new(self.tcx, cause, self.param_env, trait_ref);
108+
let obligation = Obligation::new(self.tcx, self.misc(span), self.param_env, trait_ref);
110109
if !self.predicate_must_hold_modulo_regions(&obligation) {
111110
return false;
112111
}
@@ -3489,7 +3488,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34893488
let pred = ty::TraitRef::new(self.tcx, unpin_trait, [*rcvr_ty]);
34903489
let unpin = self.predicate_must_hold_considering_regions(&Obligation::new(
34913490
self.tcx,
3492-
ObligationCause::misc(rcvr.span, self.body_id),
3491+
self.misc(rcvr.span),
34933492
self.param_env,
34943493
pred,
34953494
));

0 commit comments

Comments
 (0)