Skip to content

Commit e920c5d

Browse files
committed
Add at methods to FnCtxt and Coerce to simplify the "inherited" method calls
1 parent 01aacd4 commit e920c5d

File tree

11 files changed

+50
-70
lines changed

11 files changed

+50
-70
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
564564
// Check that E' = S'.
565565
let cause = self.misc(hir_ty.span);
566566
let InferOk { value: (), obligations } = self
567-
.at(&cause, self.param_env)
567+
.at(&cause)
568568
.define_opaque_types(self.defining_use_anchor())
569569
.eq(*expected_ty, supplied_ty)?;
570570
all_obligations.extend(obligations);
@@ -577,7 +577,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
577577
);
578578
let cause = &self.misc(decl.output.span());
579579
let InferOk { value: (), obligations } = self
580-
.at(cause, self.param_env)
580+
.at(cause)
581581
.define_opaque_types(self.defining_use_anchor())
582582
.eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
583583
all_obligations.extend(obligations);

compiler/rustc_hir_typeck/src/coercion.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use rustc_hir::def_id::DefId;
4444
use rustc_hir::intravisit::{self, Visitor};
4545
use rustc_hir::Expr;
4646
use rustc_hir_analysis::astconv::AstConv;
47+
use rustc_infer::infer::at::At;
4748
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
4849
use rustc_infer::infer::{Coercion, DefiningAnchor, InferOk, InferResult};
4950
use rustc_infer::traits::Obligation;
@@ -140,12 +141,14 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
140141
Coerce { fcx, cause, allow_two_phase, use_lub: false }
141142
}
142143

144+
pub fn at(&self) -> At<'_, 'tcx> {
145+
self.infcx.at(&self.cause, self.param_env)
146+
}
147+
143148
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
144149
debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
145150
self.commit_if_ok(|_| {
146-
let at = self
147-
.at(&self.cause, self.fcx.param_env)
148-
.define_opaque_types(self.defining_use_anchor());
151+
let at = self.at().define_opaque_types(self.defining_use_anchor());
149152
if self.use_lub {
150153
at.lub(b, a)
151154
} else {
@@ -177,9 +180,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
177180
// so this will have the side-effect of making sure we have no ambiguities
178181
// due to `[type error]` and `_` not coercing together.
179182
let _ = self.commit_if_ok(|_| {
180-
self.at(&self.cause, self.param_env)
181-
.define_opaque_types(self.defining_use_anchor())
182-
.eq(a, b)
183+
self.at().define_opaque_types(self.defining_use_anchor()).eq(a, b)
183184
});
184185
return success(vec![], self.fcx.tcx.ty_error(guar), vec![]);
185186
}
@@ -843,8 +844,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
843844
//! into a closure or a `proc`.
844845
845846
let b = self.shallow_resolve(b);
846-
let InferOk { value: b, mut obligations } =
847-
self.at(&self.cause, self.param_env).normalize(b);
847+
let InferOk { value: b, mut obligations } = self.at().normalize(b);
848848
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
849849

850850
match b.kind() {
@@ -865,8 +865,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
865865
}
866866
}
867867

868-
let InferOk { value: a_sig, obligations: o1 } =
869-
self.at(&self.cause, self.param_env).normalize(a_sig);
868+
let InferOk { value: a_sig, obligations: o1 } = self.at().normalize(a_sig);
870869
obligations.extend(o1);
871870

872871
let a_fn_pointer = self.tcx.mk_fn_ptr(a_sig);
@@ -1105,9 +1104,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11051104
(ty::FnDef(..), ty::FnDef(..)) => {
11061105
// Don't reify if the function types have a LUB, i.e., they
11071106
// are the same function and their parameters have a LUB.
1108-
match self
1109-
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
1110-
{
1107+
match self.commit_if_ok(|_| self.at(cause).lub(prev_ty, new_ty)) {
11111108
// We have a LUB of prev_ty and new_ty, just return it.
11121109
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
11131110
Err(_) => {
@@ -1155,7 +1152,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11551152
// The signature must match.
11561153
let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
11571154
let sig = self
1158-
.at(cause, self.param_env)
1155+
.at(cause)
11591156
.trace(prev_ty, new_ty)
11601157
.lub(a_sig, b_sig)
11611158
.map(|ok| self.register_infer_ok_obligations(ok))?;
@@ -1241,7 +1238,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12411238
);
12421239

12431240
return self
1244-
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
1241+
.commit_if_ok(|_| self.at(cause).lub(prev_ty, new_ty))
12451242
.map(|ok| self.register_infer_ok_obligations(ok));
12461243
}
12471244
}
@@ -1252,7 +1249,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12521249
if let Some(e) = first_error {
12531250
Err(e)
12541251
} else {
1255-
self.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
1252+
self.commit_if_ok(|_| self.at(cause).lub(prev_ty, new_ty))
12561253
.map(|ok| self.register_infer_ok_obligations(ok))
12571254
}
12581255
}
@@ -1489,7 +1486,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14891486
//
14901487
// Another example is `break` with no argument expression.
14911488
assert!(expression_ty.is_unit(), "if let hack without unit type");
1492-
fcx.at(cause, fcx.param_env)
1489+
fcx.at(cause)
14931490
// needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
14941491
.define_opaque_types(fcx.defining_use_anchor())
14951492
.eq_exp(label_expression_as_expected, expression_ty, self.merged_ty())

compiler/rustc_hir_typeck/src/demand.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
113113
expected: Ty<'tcx>,
114114
actual: Ty<'tcx>,
115115
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
116-
match self
117-
.at(cause, self.param_env)
118-
.define_opaque_types(self.defining_use_anchor())
119-
.sup(expected, actual)
120-
{
116+
match self.at(cause).define_opaque_types(self.defining_use_anchor()).sup(expected, actual) {
121117
Ok(InferOk { obligations, value: () }) => {
122118
self.register_predicates(obligations);
123119
None
@@ -147,11 +143,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
147143
expected: Ty<'tcx>,
148144
actual: Ty<'tcx>,
149145
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
150-
match self
151-
.at(cause, self.param_env)
152-
.define_opaque_types(self.defining_use_anchor())
153-
.eq(expected, actual)
154-
{
146+
match self.at(cause).define_opaque_types(self.defining_use_anchor()).eq(expected, actual) {
155147
Ok(InferOk { obligations, value: () }) => {
156148
self.register_predicates(obligations);
157149
None

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16831683
if let Some(_) = remaining_fields.remove(&ident) {
16841684
let target_ty = self.field_ty(base_expr.span, f, substs);
16851685
let cause = self.misc(base_expr.span);
1686-
match self.at(&cause, self.param_env).sup(target_ty, fru_ty) {
1686+
match self.at(&cause).sup(target_ty, fru_ty) {
16871687
Ok(InferOk { obligations, value: () }) => {
16881688
self.register_predicates(obligations)
16891689
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
317317
where
318318
T: TypeFoldable<TyCtxt<'tcx>>,
319319
{
320-
self.register_infer_ok_obligations(
321-
self.at(&self.misc(span), self.param_env).normalize(value),
322-
)
320+
self.register_infer_ok_obligations(self.at(&self.misc(span)).normalize(value))
323321
}
324322

325323
pub fn require_type_meets(
@@ -561,7 +559,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
561559
// Unify `interior` with `witness` and collect all the resulting obligations.
562560
let span = self.tcx.hir().body(body_id).value.span;
563561
let ok = self
564-
.at(&self.misc(span), self.param_env)
562+
.at(&self.misc(span))
565563
.eq(interior, witness)
566564
.expect("Failed to unify generator interior type");
567565
let mut obligations = ok.obligations;
@@ -1319,7 +1317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13191317
// This also occurs for an enum variant on a type alias.
13201318
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).subst(tcx, substs));
13211319
let self_ty = self.normalize(span, self_ty);
1322-
match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) {
1320+
match self.at(&self.misc(span)).eq(impl_ty, self_ty) {
13231321
Ok(ok) => self.register_infer_ok_obligations(ok),
13241322
Err(_) => {
13251323
self.tcx.sess.delay_span_bug(

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
302302

303303
// 3. Check if the formal type is a supertype of the checked one
304304
// and register any such obligations for future type checks
305-
let supertype_error = self
306-
.at(&self.misc(provided_arg.span), self.param_env)
307-
.sup(formal_input_ty, coerced_ty);
305+
let supertype_error =
306+
self.at(&self.misc(provided_arg.span)).sup(formal_input_ty, coerced_ty);
308307
let subtyping_error = match supertype_error {
309308
Ok(InferOk { obligations, value: () }) => {
310309
self.register_predicates(obligations);
@@ -585,9 +584,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
585584
}
586585

587586
// Using probe here, since we don't want this subtyping to affect inference.
588-
let subtyping_error = self.probe(|_| {
589-
self.at(&self.misc(arg_span), self.param_env).sup(formal_input_ty, coerced_ty).err()
590-
});
587+
let subtyping_error = self
588+
.probe(|_| self.at(&self.misc(arg_span)).sup(formal_input_ty, coerced_ty).err());
591589

592590
// Same as above: if either the coerce type or the checked type is an error type,
593591
// consider them *not* compatible.

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod suggestions;
66

77
pub use _impl::*;
88
use rustc_errors::ErrorGuaranteed;
9+
use rustc_infer::infer::at::At;
910
pub use suggestions::*;
1011

1112
use crate::coercion::DynamicCoerceMany;
@@ -145,6 +146,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
145146
self.cause(span, ObligationCauseCode::MiscObligation)
146147
}
147148

149+
pub fn at(&'a self, cause: &'a ObligationCause<'tcx>) -> At<'a, 'tcx> {
150+
self.infcx.at(cause, self.param_env)
151+
}
152+
148153
pub fn sess(&self) -> &Session {
149154
&self.tcx.sess
150155
}

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub fn resolve_interior<'a, 'tcx>(
327327
);
328328

329329
// Unify the type variable inside the generator with the new witness
330-
match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
330+
match fcx.at(&fcx.misc(body.value.span)).eq(interior, witness) {
331331
Ok(ok) => fcx.register_infer_ok_obligations(ok),
332332
_ => bug!("failed to relate {interior} and {witness}"),
333333
}

compiler/rustc_hir_typeck/src/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
478478
substs,
479479
})),
480480
);
481-
match self.at(&cause, self.param_env).sup(method_self_ty, self_ty) {
481+
match self.at(&cause).sup(method_self_ty, self_ty) {
482482
Ok(InferOk { obligations, value: () }) => {
483483
self.register_predicates(obligations);
484484
}

compiler/rustc_hir_typeck/src/method/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
409409
let fn_sig =
410410
self.instantiate_binder_with_fresh_vars(obligation.cause.span, infer::FnCall, fn_sig);
411411

412-
let InferOk { value, obligations: o } =
413-
self.at(&obligation.cause, self.param_env).normalize(fn_sig);
412+
let InferOk { value, obligations: o } = self.at(&obligation.cause).normalize(fn_sig);
414413
let fn_sig = {
415414
obligations.extend(o);
416415
value
@@ -426,8 +425,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
426425
// any late-bound regions appearing in its bounds.
427426
let bounds = self.tcx.predicates_of(def_id).instantiate(self.tcx, substs);
428427

429-
let InferOk { value, obligations: o } =
430-
self.at(&obligation.cause, self.param_env).normalize(bounds);
428+
let InferOk { value, obligations: o } = self.at(&obligation.cause).normalize(bounds);
431429
let bounds = {
432430
obligations.extend(o);
433431
value

compiler/rustc_hir_typeck/src/method/probe.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
754754
// see issue #89650
755755
let cause = traits::ObligationCause::misc(self.span, self.body_id);
756756
let InferOk { value: xform_self_ty, obligations } =
757-
self.fcx.at(&cause, self.param_env).normalize(xform_self_ty);
757+
self.fcx.at(&cause).normalize(xform_self_ty);
758758

759759
debug!(
760760
"assemble_inherent_impl_probe after normalization: xform_self_ty = {:?}/{:?}",
@@ -933,11 +933,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
933933
let fty = self.instantiate_binder_with_fresh_vars(self.span, infer::FnCall, fty);
934934

935935
if let Some(self_ty) = self_ty {
936-
if self
937-
.at(&ObligationCause::dummy(), self.param_env)
938-
.sup(fty.inputs()[0], self_ty)
939-
.is_err()
940-
{
936+
if self.at(&ObligationCause::dummy()).sup(fty.inputs()[0], self_ty).is_err() {
941937
return false;
942938
}
943939
}
@@ -1440,9 +1436,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14401436
CandidateSource::Trait(candidate.item.container_id(self.tcx))
14411437
}
14421438
TraitCandidate(trait_ref) => self.probe(|_| {
1443-
let _ = self
1444-
.at(&ObligationCause::dummy(), self.param_env)
1445-
.sup(candidate.xform_self_ty, self_ty);
1439+
let _ = self.at(&ObligationCause::dummy()).sup(candidate.xform_self_ty, self_ty);
14461440
match self.select_trait_candidate(trait_ref) {
14471441
Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
14481442
// If only a single impl matches, make the error message point
@@ -1469,16 +1463,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14691463

14701464
self.probe(|_| {
14711465
// First check that the self type can be related.
1472-
let sub_obligations = match self
1473-
.at(&ObligationCause::dummy(), self.param_env)
1474-
.sup(probe.xform_self_ty, self_ty)
1475-
{
1476-
Ok(InferOk { obligations, value: () }) => obligations,
1477-
Err(err) => {
1478-
debug!("--> cannot relate self-types {:?}", err);
1479-
return ProbeResult::NoMatch;
1480-
}
1481-
};
1466+
let sub_obligations =
1467+
match self.at(&ObligationCause::dummy()).sup(probe.xform_self_ty, self_ty) {
1468+
Ok(InferOk { obligations, value: () }) => obligations,
1469+
Err(err) => {
1470+
debug!("--> cannot relate self-types {:?}", err);
1471+
return ProbeResult::NoMatch;
1472+
}
1473+
};
14821474

14831475
let mut result = ProbeResult::Match;
14841476
let mut xform_ret_ty = probe.xform_ret_ty;
@@ -1500,7 +1492,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15001492
let InferOk {
15011493
value: normalized_xform_ret_ty,
15021494
obligations: normalization_obligations,
1503-
} = self.fcx.at(&cause, self.param_env).normalize(xform_ret_ty);
1495+
} = self.fcx.at(&cause).normalize(xform_ret_ty);
15041496
xform_ret_ty = normalized_xform_ret_ty;
15051497
debug!("xform_ret_ty after normalization: {:?}", xform_ret_ty);
15061498

@@ -1510,7 +1502,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15101502
let impl_bounds = impl_bounds.instantiate(self.tcx, substs);
15111503

15121504
let InferOk { value: impl_bounds, obligations: norm_obligations } =
1513-
self.fcx.at(&cause, self.param_env).normalize(impl_bounds);
1505+
self.fcx.at(&cause).normalize(impl_bounds);
15141506

15151507
// Convert the bounds into obligations.
15161508
let impl_obligations = traits::predicates_for_generics(
@@ -1663,7 +1655,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16631655
let InferOk {
16641656
value: normalized_xform_ret_ty,
16651657
obligations: normalization_obligations,
1666-
} = self.fcx.at(&cause, self.param_env).normalize(xform_ret_ty);
1658+
} = self.fcx.at(&cause).normalize(xform_ret_ty);
16671659
xform_ret_ty = normalized_xform_ret_ty;
16681660
debug!("xform_ret_ty after normalization: {:?}", xform_ret_ty);
16691661
// Evaluate those obligations to see if they might possibly hold.
@@ -1686,7 +1678,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16861678
);
16871679
if let ProbeResult::Match = result
16881680
&& self
1689-
.at(&ObligationCause::dummy(), self.param_env)
1681+
.at(&ObligationCause::dummy())
16901682
.sup(return_ty, xform_ret_ty)
16911683
.is_err()
16921684
{

0 commit comments

Comments
 (0)