Skip to content

Commit

Permalink
Add at methods to FnCtxt and Coerce to simplify the "inherited"…
Browse files Browse the repository at this point in the history
… method calls
  • Loading branch information
oli-obk committed Mar 1, 2023
1 parent 01aacd4 commit e920c5d
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 70 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Check that E' = S'.
let cause = self.misc(hir_ty.span);
let InferOk { value: (), obligations } = self
.at(&cause, self.param_env)
.at(&cause)
.define_opaque_types(self.defining_use_anchor())
.eq(*expected_ty, supplied_ty)?;
all_obligations.extend(obligations);
Expand All @@ -577,7 +577,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
let cause = &self.misc(decl.output.span());
let InferOk { value: (), obligations } = self
.at(cause, self.param_env)
.at(cause)
.define_opaque_types(self.defining_use_anchor())
.eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
all_obligations.extend(obligations);
Expand Down
31 changes: 14 additions & 17 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::Expr;
use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer::at::At;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::{Coercion, DefiningAnchor, InferOk, InferResult};
use rustc_infer::traits::Obligation;
Expand Down Expand Up @@ -140,12 +141,14 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
Coerce { fcx, cause, allow_two_phase, use_lub: false }
}

pub fn at(&self) -> At<'_, 'tcx> {
self.infcx.at(&self.cause, self.param_env)
}

fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
self.commit_if_ok(|_| {
let at = self
.at(&self.cause, self.fcx.param_env)
.define_opaque_types(self.defining_use_anchor());
let at = self.at().define_opaque_types(self.defining_use_anchor());
if self.use_lub {
at.lub(b, a)
} else {
Expand Down Expand Up @@ -177,9 +180,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// so this will have the side-effect of making sure we have no ambiguities
// due to `[type error]` and `_` not coercing together.
let _ = self.commit_if_ok(|_| {
self.at(&self.cause, self.param_env)
.define_opaque_types(self.defining_use_anchor())
.eq(a, b)
self.at().define_opaque_types(self.defining_use_anchor()).eq(a, b)
});
return success(vec![], self.fcx.tcx.ty_error(guar), vec![]);
}
Expand Down Expand Up @@ -843,8 +844,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
//! into a closure or a `proc`.

let b = self.shallow_resolve(b);
let InferOk { value: b, mut obligations } =
self.at(&self.cause, self.param_env).normalize(b);
let InferOk { value: b, mut obligations } = self.at().normalize(b);
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);

match b.kind() {
Expand All @@ -865,8 +865,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
}

let InferOk { value: a_sig, obligations: o1 } =
self.at(&self.cause, self.param_env).normalize(a_sig);
let InferOk { value: a_sig, obligations: o1 } = self.at().normalize(a_sig);
obligations.extend(o1);

let a_fn_pointer = self.tcx.mk_fn_ptr(a_sig);
Expand Down Expand Up @@ -1105,9 +1104,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(ty::FnDef(..), ty::FnDef(..)) => {
// Don't reify if the function types have a LUB, i.e., they
// are the same function and their parameters have a LUB.
match self
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
{
match self.commit_if_ok(|_| self.at(cause).lub(prev_ty, new_ty)) {
// We have a LUB of prev_ty and new_ty, just return it.
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
Err(_) => {
Expand Down Expand Up @@ -1155,7 +1152,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// The signature must match.
let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
let sig = self
.at(cause, self.param_env)
.at(cause)
.trace(prev_ty, new_ty)
.lub(a_sig, b_sig)
.map(|ok| self.register_infer_ok_obligations(ok))?;
Expand Down Expand Up @@ -1241,7 +1238,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);

return self
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
.commit_if_ok(|_| self.at(cause).lub(prev_ty, new_ty))
.map(|ok| self.register_infer_ok_obligations(ok));
}
}
Expand All @@ -1252,7 +1249,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(e) = first_error {
Err(e)
} else {
self.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
self.commit_if_ok(|_| self.at(cause).lub(prev_ty, new_ty))
.map(|ok| self.register_infer_ok_obligations(ok))
}
}
Expand Down Expand Up @@ -1489,7 +1486,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
//
// Another example is `break` with no argument expression.
assert!(expression_ty.is_unit(), "if let hack without unit type");
fcx.at(cause, fcx.param_env)
fcx.at(cause)
// needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
.define_opaque_types(fcx.defining_use_anchor())
.eq_exp(label_expression_as_expected, expression_ty, self.merged_ty())
Expand Down
12 changes: 2 additions & 10 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Ty<'tcx>,
actual: Ty<'tcx>,
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
match self
.at(cause, self.param_env)
.define_opaque_types(self.defining_use_anchor())
.sup(expected, actual)
{
match self.at(cause).define_opaque_types(self.defining_use_anchor()).sup(expected, actual) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
None
Expand Down Expand Up @@ -147,11 +143,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Ty<'tcx>,
actual: Ty<'tcx>,
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
match self
.at(cause, self.param_env)
.define_opaque_types(self.defining_use_anchor())
.eq(expected, actual)
{
match self.at(cause).define_opaque_types(self.defining_use_anchor()).eq(expected, actual) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
None
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(_) = remaining_fields.remove(&ident) {
let target_ty = self.field_ty(base_expr.span, f, substs);
let cause = self.misc(base_expr.span);
match self.at(&cause, self.param_env).sup(target_ty, fru_ty) {
match self.at(&cause).sup(target_ty, fru_ty) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations)
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
self.register_infer_ok_obligations(
self.at(&self.misc(span), self.param_env).normalize(value),
)
self.register_infer_ok_obligations(self.at(&self.misc(span)).normalize(value))
}

pub fn require_type_meets(
Expand Down Expand Up @@ -561,7 +559,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Unify `interior` with `witness` and collect all the resulting obligations.
let span = self.tcx.hir().body(body_id).value.span;
let ok = self
.at(&self.misc(span), self.param_env)
.at(&self.misc(span))
.eq(interior, witness)
.expect("Failed to unify generator interior type");
let mut obligations = ok.obligations;
Expand Down Expand Up @@ -1319,7 +1317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// This also occurs for an enum variant on a type alias.
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).subst(tcx, substs));
let self_ty = self.normalize(span, self_ty);
match self.at(&self.misc(span), self.param_env).eq(impl_ty, self_ty) {
match self.at(&self.misc(span)).eq(impl_ty, self_ty) {
Ok(ok) => self.register_infer_ok_obligations(ok),
Err(_) => {
self.tcx.sess.delay_span_bug(
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

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

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

// Same as above: if either the coerce type or the checked type is an error type,
// consider them *not* compatible.
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod suggestions;

pub use _impl::*;
use rustc_errors::ErrorGuaranteed;
use rustc_infer::infer::at::At;
pub use suggestions::*;

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

pub fn at(&'a self, cause: &'a ObligationCause<'tcx>) -> At<'a, 'tcx> {
self.infcx.at(cause, self.param_env)
}

pub fn sess(&self) -> &Session {
&self.tcx.sess
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/generator_interior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub fn resolve_interior<'a, 'tcx>(
);

// Unify the type variable inside the generator with the new witness
match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
match fcx.at(&fcx.misc(body.value.span)).eq(interior, witness) {
Ok(ok) => fcx.register_infer_ok_obligations(ok),
_ => bug!("failed to relate {interior} and {witness}"),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
substs,
})),
);
match self.at(&cause, self.param_env).sup(method_self_ty, self_ty) {
match self.at(&cause).sup(method_self_ty, self_ty) {
Ok(InferOk { obligations, value: () }) => {
self.register_predicates(obligations);
}
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let fn_sig =
self.instantiate_binder_with_fresh_vars(obligation.cause.span, infer::FnCall, fn_sig);

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

let InferOk { value, obligations: o } =
self.at(&obligation.cause, self.param_env).normalize(bounds);
let InferOk { value, obligations: o } = self.at(&obligation.cause).normalize(bounds);
let bounds = {
obligations.extend(o);
value
Expand Down
38 changes: 15 additions & 23 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
// see issue #89650
let cause = traits::ObligationCause::misc(self.span, self.body_id);
let InferOk { value: xform_self_ty, obligations } =
self.fcx.at(&cause, self.param_env).normalize(xform_self_ty);
self.fcx.at(&cause).normalize(xform_self_ty);

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

if let Some(self_ty) = self_ty {
if self
.at(&ObligationCause::dummy(), self.param_env)
.sup(fty.inputs()[0], self_ty)
.is_err()
{
if self.at(&ObligationCause::dummy()).sup(fty.inputs()[0], self_ty).is_err() {
return false;
}
}
Expand Down Expand Up @@ -1440,9 +1436,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
CandidateSource::Trait(candidate.item.container_id(self.tcx))
}
TraitCandidate(trait_ref) => self.probe(|_| {
let _ = self
.at(&ObligationCause::dummy(), self.param_env)
.sup(candidate.xform_self_ty, self_ty);
let _ = self.at(&ObligationCause::dummy()).sup(candidate.xform_self_ty, self_ty);
match self.select_trait_candidate(trait_ref) {
Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
// If only a single impl matches, make the error message point
Expand All @@ -1469,16 +1463,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {

self.probe(|_| {
// First check that the self type can be related.
let sub_obligations = match self
.at(&ObligationCause::dummy(), self.param_env)
.sup(probe.xform_self_ty, self_ty)
{
Ok(InferOk { obligations, value: () }) => obligations,
Err(err) => {
debug!("--> cannot relate self-types {:?}", err);
return ProbeResult::NoMatch;
}
};
let sub_obligations =
match self.at(&ObligationCause::dummy()).sup(probe.xform_self_ty, self_ty) {
Ok(InferOk { obligations, value: () }) => obligations,
Err(err) => {
debug!("--> cannot relate self-types {:?}", err);
return ProbeResult::NoMatch;
}
};

let mut result = ProbeResult::Match;
let mut xform_ret_ty = probe.xform_ret_ty;
Expand All @@ -1500,7 +1492,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
let InferOk {
value: normalized_xform_ret_ty,
obligations: normalization_obligations,
} = self.fcx.at(&cause, self.param_env).normalize(xform_ret_ty);
} = self.fcx.at(&cause).normalize(xform_ret_ty);
xform_ret_ty = normalized_xform_ret_ty;
debug!("xform_ret_ty after normalization: {:?}", xform_ret_ty);

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

let InferOk { value: impl_bounds, obligations: norm_obligations } =
self.fcx.at(&cause, self.param_env).normalize(impl_bounds);
self.fcx.at(&cause).normalize(impl_bounds);

// Convert the bounds into obligations.
let impl_obligations = traits::predicates_for_generics(
Expand Down Expand Up @@ -1663,7 +1655,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
let InferOk {
value: normalized_xform_ret_ty,
obligations: normalization_obligations,
} = self.fcx.at(&cause, self.param_env).normalize(xform_ret_ty);
} = self.fcx.at(&cause).normalize(xform_ret_ty);
xform_ret_ty = normalized_xform_ret_ty;
debug!("xform_ret_ty after normalization: {:?}", xform_ret_ty);
// Evaluate those obligations to see if they might possibly hold.
Expand All @@ -1686,7 +1678,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
);
if let ProbeResult::Match = result
&& self
.at(&ObligationCause::dummy(), self.param_env)
.at(&ObligationCause::dummy())
.sup(return_ty, xform_ret_ty)
.is_err()
{
Expand Down

0 comments on commit e920c5d

Please sign in to comment.