From 218373897d07124357fc947f04349b29b9b72e2c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 2 Oct 2024 12:55:35 +1000 Subject: [PATCH 1/2] Remove `At::eq_structurally_relating_aliases`. It's unused. --- compiler/rustc_infer/src/infer/at.rs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index 6ce47db8b9bd2..64e0218b56933 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -187,28 +187,6 @@ impl<'a, 'tcx> At<'a, 'tcx> { }) } - /// Equates `expected` and `found` while structurally relating aliases. - /// This should only be used inside of the next generation trait solver - /// when relating rigid aliases. - pub fn eq_structurally_relating_aliases( - self, - expected: T, - actual: T, - ) -> InferResult<'tcx, ()> - where - T: ToTrace<'tcx>, - { - assert!(self.infcx.next_trait_solver()); - let mut fields = CombineFields::new( - self.infcx, - ToTrace::to_trace(self.cause, expected, actual), - self.param_env, - DefineOpaqueTypes::Yes, - ); - fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?; - Ok(InferOk { value: (), obligations: fields.into_obligations() }) - } - pub fn relate( self, define_opaque_types: DefineOpaqueTypes, From 4f42b282c0c0891eb3e6dbd55e010f88fe429ebb Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 2 Oct 2024 09:45:39 +1000 Subject: [PATCH 2/2] Avoid `InferOk<'tcx, ()>`. `InferOk<'tcx, T>` wraps a `T` and a `Vec>`. A lot of the instances are `InferOk<'tcx, ()>`, which is just a clumsy wrapper for a `Vec>`. This commit removes the `InferOk` in those cases, avoiding a lot of boilerplate wrapping/unwrapping code. To help with this, the unused `UnitResult` type is replaced with `UnitInferResult`, which is like `InferResult<'tcx, ()>` but without the useless `InferOk<'tcx, ()>` wrapper. --- compiler/rustc_hir_typeck/src/callee.rs | 2 +- compiler/rustc_hir_typeck/src/cast.rs | 2 +- compiler/rustc_hir_typeck/src/closure.rs | 4 +- compiler/rustc_hir_typeck/src/coercion.rs | 6 +- compiler/rustc_hir_typeck/src/demand.rs | 4 +- compiler/rustc_hir_typeck/src/expr.rs | 4 +- compiler/rustc_hir_typeck/src/fallback.rs | 4 +- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 5 +- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 4 +- .../rustc_hir_typeck/src/method/confirm.rs | 4 +- compiler/rustc_infer/src/infer/at.rs | 42 +++++---- .../src/infer/canonical/query_response.rs | 86 +++++++++---------- compiler/rustc_infer/src/infer/mod.rs | 15 ++-- .../rustc_infer/src/infer/opaque_types/mod.rs | 1 - .../src/solve/inspect/analyse.rs | 4 +- .../src/traits/coherence.rs | 8 +- .../src/traits/engine.rs | 10 +-- .../src/traits/fulfill.rs | 22 ++--- .../src/traits/project.rs | 6 +- .../src/traits/select/confirmation.rs | 14 ++- .../src/traits/select/mod.rs | 37 ++++---- .../src/traits/specialize/mod.rs | 4 +- src/librustdoc/clean/blanket_impl.rs | 5 +- 23 files changed, 133 insertions(+), 160 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 13ba615d4c927..2299d32f30266 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -890,7 +890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let cause = self.misc(span); // We know the type of `effect` to be `bool`, there will be no opaque type inference. match self.at(&cause, self.param_env).eq(infer::DefineOpaqueTypes::Yes, effect, param) { - Ok(infer::InferOk { obligations, value: () }) => { + Ok(obligations) => { self.register_predicates(obligations); } Err(e) => { diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs index fcd2940b83ae3..0a5ababc6bd4b 100644 --- a/compiler/rustc_hir_typeck/src/cast.rs +++ b/compiler/rustc_hir_typeck/src/cast.rs @@ -918,7 +918,7 @@ impl<'a, 'tcx> CastCheck<'tcx> { if fcx .at(&cause, fcx.param_env) .eq(DefineOpaqueTypes::Yes, src_obj, dst_obj) - .map(|infer_ok| fcx.register_infer_ok_obligations(infer_ok)) + .map(|obligations| fcx.register_predicates(obligations)) .is_err() { return Err(CastError::DifferingKinds { src_kind, dst_kind }); diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index 3e7ce2955fc05..2cc78ef77ba6e 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -820,7 +820,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).eq( + let obligations = self.at(&cause, self.param_env).eq( DefineOpaqueTypes::Yes, *expected_ty, supplied_ty, @@ -830,7 +830,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let supplied_output_ty = supplied_sig.output(); let cause = &self.misc(decl.output.span()); - let InferOk { value: (), obligations } = self.at(cause, self.param_env).eq( + let obligations = self.at(cause, self.param_env).eq( DefineOpaqueTypes::Yes, expected_sigs.liberated_sig.output(), supplied_output_ty, diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index fb462eec1b9aa..80412129e15f2 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -138,7 +138,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { at.lub(DefineOpaqueTypes::Yes, b, a) } else { at.sup(DefineOpaqueTypes::Yes, b, a) - .map(|InferOk { value: (), obligations }| InferOk { value: b, obligations }) + .map(|obligations| InferOk { value: b, obligations }) }; // In the new solver, lazy norm may allow us to shallowly equate @@ -1599,8 +1599,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { expected, found, ) - .map(|infer_ok| { - fcx.register_infer_ok_obligations(infer_ok); + .map(|obligations| { + fcx.register_predicates(obligations); expression_ty }) }; diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index cfa8fc4bbf2e6..14f93e060e3e2 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -190,7 +190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) -> Result<(), Diag<'a>> { self.at(cause, self.param_env) .sup(DefineOpaqueTypes::Yes, expected, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|obligations| self.register_predicates(obligations)) .map_err(|e| self.err_ctxt().report_mismatched_types(cause, expected, actual, e)) } @@ -217,7 +217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) -> Result<(), Diag<'a>> { self.at(cause, self.param_env) .eq(DefineOpaqueTypes::Yes, expected, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|obligations| self.register_predicates(obligations)) .map_err(|e| self.err_ctxt().report_mismatched_types(cause, expected, actual, e)) } diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index b34ed4640db12..40049eceac72e 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -16,7 +16,7 @@ use rustc_hir::lang_items::LangItem; use rustc_hir::{ExprKind, HirId, QPath}; use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _; use rustc_infer::infer; -use rustc_infer::infer::{DefineOpaqueTypes, InferOk}; +use rustc_infer::infer::DefineOpaqueTypes; use rustc_infer::traits::ObligationCause; use rustc_infer::traits::query::NoSolution; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase}; @@ -1832,7 +1832,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { target_ty, fru_ty, ) { - Ok(InferOk { obligations, value: () }) => { + Ok(obligations) => { self.register_predicates(obligations) } Err(_) => { diff --git a/compiler/rustc_hir_typeck/src/fallback.rs b/compiler/rustc_hir_typeck/src/fallback.rs index 4fd508ab896e7..cb6e765fa3325 100644 --- a/compiler/rustc_hir_typeck/src/fallback.rs +++ b/compiler/rustc_hir_typeck/src/fallback.rs @@ -7,7 +7,7 @@ use rustc_data_structures::unord::{UnordBag, UnordMap, UnordSet}; use rustc_hir as hir; use rustc_hir::HirId; use rustc_hir::intravisit::Visitor; -use rustc_infer::infer::{DefineOpaqueTypes, InferOk}; +use rustc_infer::infer::DefineOpaqueTypes; use rustc_middle::bug; use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable}; use rustc_session::lint; @@ -116,7 +116,7 @@ impl<'tcx> FnCtxt<'_, 'tcx> { let expected = self.tcx.consts.true_; let cause = self.misc(DUMMY_SP); match self.at(&cause, self.param_env).eq(DefineOpaqueTypes::Yes, expected, effect) { - Ok(InferOk { obligations, value: () }) => { + Ok(obligations) => { self.register_predicates(obligations); } Err(e) => { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 62107877283a0..4dc60b9cd5cc0 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -613,12 +613,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let ty::Infer(ty::InferTy::TyVar(_)) = interior.kind() else { span_bug!(span, "coroutine interior witness not infer: {:?}", interior.kind()) }; - let ok = self + let mut obligations = self .at(&self.misc(span), self.param_env) // Will never define opaque types, as all we do is instantiate a type variable. .eq(DefineOpaqueTypes::Yes, interior, witness) .expect("Failed to unify coroutine interior type"); - let mut obligations = ok.obligations; // Also collect the obligations that were unstalled by this unification. obligations @@ -1386,7 +1385,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { impl_ty, self_ty, ) { - Ok(ok) => self.register_infer_ok_obligations(ok), + Ok(obligations) => self.register_predicates(obligations), Err(_) => { self.dcx().span_bug( span, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 550c58b5a17c6..c3bae98f57f1f 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -15,7 +15,7 @@ use rustc_hir_analysis::check::intrinsicck::InlineAsmCtxt; use rustc_hir_analysis::check::potentially_plural_count; use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer; use rustc_index::IndexVec; -use rustc_infer::infer::{DefineOpaqueTypes, InferOk, TypeTrace}; +use rustc_infer::infer::{DefineOpaqueTypes, TypeTrace}; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::error::TypeError; use rustc_middle::ty::visit::TypeVisitableExt; @@ -338,7 +338,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // If neither check failed, the types are compatible match formal_ty_error { - Ok(InferOk { obligations, value: () }) => { + Ok(obligations) => { self.register_predicates(obligations); Compatibility::Compatible } diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 72842075fec0a..1ae709ff8c73b 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -9,7 +9,7 @@ use rustc_hir_analysis::hir_ty_lowering::generics::{ use rustc_hir_analysis::hir_ty_lowering::{ GenericArgsLowerer, HirTyLowerer, IsMethodCall, RegionInferReason, }; -use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk}; +use rustc_infer::infer::{self, DefineOpaqueTypes}; use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext}; use rustc_middle::ty::adjustment::{ Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion, @@ -512,7 +512,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { })), ); match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) { - Ok(InferOk { obligations, value: () }) => { + Ok(obligations) => { self.register_predicates(obligations); } Err(terr) => { diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index 64e0218b56933..68573a9b67af8 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -29,6 +29,7 @@ use rustc_middle::bug; use rustc_middle::ty::{Const, ImplSubject}; use super::*; +use crate::infer::UnitInferResult; use crate::infer::relate::{Relate, StructurallyRelateAliases, TypeRelation}; use crate::traits::Obligation; @@ -105,7 +106,7 @@ impl<'a, 'tcx> At<'a, 'tcx> { define_opaque_types: DefineOpaqueTypes, expected: T, actual: T, - ) -> InferResult<'tcx, ()> + ) -> UnitInferResult<'tcx> where T: ToTrace<'tcx>, { @@ -116,7 +117,7 @@ impl<'a, 'tcx> At<'a, 'tcx> { define_opaque_types, ); fields.sup().relate(expected, actual)?; - Ok(InferOk { value: (), obligations: fields.into_obligations() }) + Ok(fields.into_obligations()) } /// Makes `expected <: actual`. @@ -125,7 +126,7 @@ impl<'a, 'tcx> At<'a, 'tcx> { define_opaque_types: DefineOpaqueTypes, expected: T, actual: T, - ) -> InferResult<'tcx, ()> + ) -> UnitInferResult<'tcx> where T: ToTrace<'tcx>, { @@ -136,7 +137,7 @@ impl<'a, 'tcx> At<'a, 'tcx> { define_opaque_types, ); fields.sub().relate(expected, actual)?; - Ok(InferOk { value: (), obligations: fields.into_obligations() }) + Ok(fields.into_obligations()) } /// Makes `expected == actual`. @@ -145,7 +146,7 @@ impl<'a, 'tcx> At<'a, 'tcx> { define_opaque_types: DefineOpaqueTypes, expected: T, actual: T, - ) -> InferResult<'tcx, ()> + ) -> UnitInferResult<'tcx> where T: ToTrace<'tcx>, { @@ -164,27 +165,24 @@ impl<'a, 'tcx> At<'a, 'tcx> { trace: TypeTrace<'tcx>, expected: T, actual: T, - ) -> InferResult<'tcx, ()> + ) -> UnitInferResult<'tcx> where T: Relate>, { let mut fields = CombineFields::new(self.infcx, trace, self.param_env, define_opaque_types); fields.equate(StructurallyRelateAliases::No).relate(expected, actual)?; - Ok(InferOk { - value: (), - obligations: fields - .goals - .into_iter() - .map(|goal| { - Obligation::new( - self.infcx.tcx, - fields.trace.cause.clone(), - goal.param_env, - goal.predicate, - ) - }) - .collect(), - }) + Ok(fields + .goals + .into_iter() + .map(|goal| { + Obligation::new( + self.infcx.tcx, + fields.trace.cause.clone(), + goal.param_env, + goal.predicate, + ) + }) + .collect()) } pub fn relate( @@ -193,7 +191,7 @@ impl<'a, 'tcx> At<'a, 'tcx> { expected: T, variance: ty::Variance, actual: T, - ) -> InferResult<'tcx, ()> + ) -> UnitInferResult<'tcx> where T: ToTrace<'tcx>, { diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 7791bd736281c..03f290760260b 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -25,7 +25,7 @@ use crate::infer::canonical::{ QueryOutlivesConstraint, QueryRegionConstraints, QueryResponse, }; use crate::infer::region_constraints::{Constraint, RegionConstraintData}; -use crate::infer::{DefineOpaqueTypes, InferCtxt, InferOk, InferResult}; +use crate::infer::{DefineOpaqueTypes, InferCtxt, InferOk, InferResult, UnitInferResult}; use crate::traits::query::NoSolution; use crate::traits::{ Obligation, ObligationCause, PredicateObligation, ScrubbedTraitError, TraitEngine, @@ -283,19 +283,19 @@ impl<'tcx> InferCtxt<'tcx> { } (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => { - obligations.extend( - self.at(&cause, param_env) - .eq(DefineOpaqueTypes::Yes, v1, v2)? - .into_obligations(), - ); + obligations.extend(self.at(&cause, param_env).eq( + DefineOpaqueTypes::Yes, + v1, + v2, + )?); } (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => { - obligations.extend( - self.at(&cause, param_env) - .eq(DefineOpaqueTypes::Yes, v1, v2)? - .into_obligations(), - ); + obligations.extend(self.at(&cause, param_env).eq( + DefineOpaqueTypes::Yes, + v1, + v2, + )?); } _ => { @@ -363,16 +363,13 @@ impl<'tcx> InferCtxt<'tcx> { query_response, )?; - value.obligations.extend( - self.unify_query_response_instantiation_guess( - cause, - param_env, - original_values, - &value.value, - query_response, - )? - .into_obligations(), - ); + value.obligations.extend(self.unify_query_response_instantiation_guess( + cause, + param_env, + original_values, + &value.value, + query_response, + )?); Ok(value) } @@ -504,15 +501,11 @@ impl<'tcx> InferCtxt<'tcx> { // opaque type's hidden value directly, because the hidden type may have been an inference // variable that got constrained to the opaque type itself. In that case we want to equate // the generic args of the opaque with the generic params of its hidden type version. - obligations.extend( - self.at(cause, param_env) - .eq( - DefineOpaqueTypes::Yes, - Ty::new_opaque(self.tcx, a.def_id.to_def_id(), a.args), - b, - )? - .obligations, - ); + obligations.extend(self.at(cause, param_env).eq( + DefineOpaqueTypes::Yes, + Ty::new_opaque(self.tcx, a.def_id.to_def_id(), a.args), + b, + )?); } Ok(InferOk { value: result_args, obligations }) @@ -531,7 +524,7 @@ impl<'tcx> InferCtxt<'tcx> { original_values: &OriginalQueryValues<'tcx>, result_args: &CanonicalVarValues<'tcx>, query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>, - ) -> InferResult<'tcx, ()> + ) -> UnitInferResult<'tcx> where R: Debug + TypeFoldable>, { @@ -597,18 +590,18 @@ impl<'tcx> InferCtxt<'tcx> { param_env: ty::ParamEnv<'tcx>, variables1: &OriginalQueryValues<'tcx>, variables2: impl Fn(BoundVar) -> GenericArg<'tcx>, - ) -> InferResult<'tcx, ()> { + ) -> UnitInferResult<'tcx> { let mut obligations = vec![]; for (index, value1) in variables1.var_values.iter().enumerate() { let value2 = variables2(BoundVar::new(index)); match (value1.unpack(), value2.unpack()) { (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => { - obligations.extend( - self.at(cause, param_env) - .eq(DefineOpaqueTypes::Yes, v1, v2)? - .into_obligations(), - ); + obligations.extend(self.at(cause, param_env).eq( + DefineOpaqueTypes::Yes, + v1, + v2, + )?); } (GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2)) if re1.is_erased() && re2.is_erased() => @@ -616,22 +609,25 @@ impl<'tcx> InferCtxt<'tcx> { // no action needed } (GenericArgKind::Lifetime(v1), GenericArgKind::Lifetime(v2)) => { - obligations.extend( - self.at(cause, param_env) - .eq(DefineOpaqueTypes::Yes, v1, v2)? - .into_obligations(), - ); + obligations.extend(self.at(cause, param_env).eq( + DefineOpaqueTypes::Yes, + v1, + v2, + )?); } (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => { - let ok = self.at(cause, param_env).eq(DefineOpaqueTypes::Yes, v1, v2)?; - obligations.extend(ok.into_obligations()); + obligations.extend(self.at(cause, param_env).eq( + DefineOpaqueTypes::Yes, + v1, + v2, + )?); } _ => { bug!("kind mismatch, cannot unify {:?} and {:?}", value1, value2,); } } } - Ok(InferOk { value: (), obligations }) + Ok(obligations) } } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 5fa1bf51634a3..0c01580ac575a 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -50,7 +50,6 @@ use snapshot::undo_log::InferCtxtUndoLogs; use tracing::{debug, instrument}; use type_variable::TypeVariableOrigin; -use crate::infer::relate::RelateResult; use crate::traits::{self, ObligationCause, ObligationInspector, PredicateObligation, TraitEngine}; pub mod at; @@ -76,7 +75,9 @@ pub struct InferOk<'tcx, T> { } pub type InferResult<'tcx, T> = Result, TypeError<'tcx>>; -pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result" +// Like `InferResult<'tcx, ()>`, but without the useless `InferOk<'tcx, ()>` wrapper. +pub type UnitInferResult<'tcx> = Result>, TypeError<'tcx>>; + pub type FixupResult = Result; // "fixup result" pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable< @@ -679,12 +680,6 @@ impl<'tcx, T> InferOk<'tcx, T> { } } -impl<'tcx> InferOk<'tcx, ()> { - pub fn into_obligations(self) -> Vec> { - self.obligations - } -} - impl<'tcx> InferCtxt<'tcx> { pub fn dcx(&self) -> DiagCtxtHandle<'_> { self.tcx.dcx().taintable_handle(&self.tainted_by_errors) @@ -807,7 +802,7 @@ impl<'tcx> InferCtxt<'tcx> { cause: &ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, predicate: ty::PolyCoercePredicate<'tcx>, - ) -> Result, (TyVid, TyVid)> { + ) -> Result, (TyVid, TyVid)> { let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate { a_is_expected: false, // when coercing from `a` to `b`, `b` is expected a: p.a, @@ -821,7 +816,7 @@ impl<'tcx> InferCtxt<'tcx> { cause: &ObligationCause<'tcx>, param_env: ty::ParamEnv<'tcx>, predicate: ty::PolySubtypePredicate<'tcx>, - ) -> Result, (TyVid, TyVid)> { + ) -> Result, (TyVid, TyVid)> { // Check for two unresolved inference variables, in which case we can // make no progress. This is partly a micro-optimization, but it's // also an opportunity to "sub-unify" the variables. This isn't diff --git a/compiler/rustc_infer/src/infer/opaque_types/mod.rs b/compiler/rustc_infer/src/infer/opaque_types/mod.rs index 55c51bc856f8e..6135128af5e57 100644 --- a/compiler/rustc_infer/src/infer/opaque_types/mod.rs +++ b/compiler/rustc_infer/src/infer/opaque_types/mod.rs @@ -541,7 +541,6 @@ impl<'tcx> InferCtxt<'tcx> { goals.extend( self.at(&ObligationCause::dummy_with_span(span), param_env) .eq(DefineOpaqueTypes::Yes, prev, hidden_ty)? - .obligations .into_iter() // FIXME: Shuttling between obligations and goals is awkward. .map(Goal::from), diff --git a/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs b/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs index 254620e0b597f..ebd8fecf502fb 100644 --- a/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs +++ b/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs @@ -13,7 +13,7 @@ use std::assert_matches::assert_matches; use rustc_ast_ir::try_visit; use rustc_ast_ir::visit::VisitorResult; -use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk}; +use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt}; use rustc_macros::extension; use rustc_middle::traits::ObligationCause; use rustc_middle::traits::solve::{Certainty, Goal, GoalSource, NoSolution, QueryResult}; @@ -72,7 +72,7 @@ impl<'tcx> NormalizesToTermHack<'tcx> { .at(&ObligationCause::dummy_with_span(span), param_env) .eq(DefineOpaqueTypes::Yes, self.term, self.unconstrained_term) .map_err(|_| NoSolution) - .and_then(|InferOk { value: (), obligations }| { + .and_then(|obligations| { let ocx = ObligationCtxt::new(infcx); ocx.register_obligations(obligations); let errors = ocx.select_all_or_error(); diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 27d2a3c15b9ac..d90a430c45852 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -292,7 +292,7 @@ fn equate_impl_headers<'tcx>( _ => bug!("equate_impl_headers given mismatched impl kinds"), }; - result.map(|infer_ok| infer_ok.obligations).ok() + result.map(|obligations| obligations).ok() } /// The result of [fn impl_intersection_has_impossible_obligation]. @@ -464,7 +464,7 @@ fn plug_infer_with_placeholders<'tcx>( fn visit_ty(&mut self, ty: Ty<'tcx>) { let ty = self.infcx.shallow_resolve(ty); if ty.is_ty_var() { - let Ok(InferOk { value: (), obligations }) = + let Ok(obligations) = self.infcx.at(&ObligationCause::dummy(), ty::ParamEnv::empty()).eq( // Comparing against a type variable never registers hidden types anyway DefineOpaqueTypes::Yes, @@ -489,7 +489,7 @@ fn plug_infer_with_placeholders<'tcx>( fn visit_const(&mut self, ct: ty::Const<'tcx>) { let ct = self.infcx.shallow_resolve_const(ct); if ct.is_ct_infer() { - let Ok(InferOk { value: (), obligations }) = + let Ok(obligations) = self.infcx.at(&ObligationCause::dummy(), ty::ParamEnv::empty()).eq( // The types of the constants are the same, so there is no hidden type // registration happening anyway. @@ -518,7 +518,7 @@ fn plug_infer_with_placeholders<'tcx>( .unwrap_region_constraints() .opportunistic_resolve_var(self.infcx.tcx, vid); if r.is_var() { - let Ok(InferOk { value: (), obligations }) = + let Ok(obligations) = self.infcx.at(&ObligationCause::dummy(), ty::ParamEnv::empty()).eq( // Lifetimes don't contain opaque types (or any types for that matter). DefineOpaqueTypes::Yes, diff --git a/compiler/rustc_trait_selection/src/traits/engine.rs b/compiler/rustc_trait_selection/src/traits/engine.rs index d562692c1a865..6eed755e9df8b 100644 --- a/compiler/rustc_trait_selection/src/traits/engine.rs +++ b/compiler/rustc_trait_selection/src/traits/engine.rs @@ -131,7 +131,7 @@ where self.infcx .at(cause, param_env) .eq(DefineOpaqueTypes::Yes, expected, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|obligations| self.register_obligations(obligations)) } pub fn eq_trace>>( @@ -145,7 +145,7 @@ where self.infcx .at(cause, param_env) .eq_trace(DefineOpaqueTypes::Yes, trace, expected, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|obligations| self.register_obligations(obligations)) } /// Checks whether `expected` is a subtype of `actual`: `expected <: actual`. @@ -159,7 +159,7 @@ where self.infcx .at(cause, param_env) .sub(DefineOpaqueTypes::Yes, expected, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|obligations| self.register_obligations(obligations)) } pub fn relate>( @@ -173,7 +173,7 @@ where self.infcx .at(cause, param_env) .relate(DefineOpaqueTypes::Yes, expected, variance, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|obligations| self.register_obligations(obligations)) } /// Checks whether `expected` is a supertype of `actual`: `expected :> actual`. @@ -187,7 +187,7 @@ where self.infcx .at(cause, param_env) .sup(DefineOpaqueTypes::Yes, expected, actual) - .map(|infer_ok| self.register_infer_ok_obligations(infer_ok)) + .map(|infer_ok| self.register_obligations(infer_ok)) } #[must_use] diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index 33b8cf037017e..a3c883ae29ab1 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -477,7 +477,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { ct_ty, ty, ) { - Ok(inf_ok) => ProcessResult::Changed(mk_pending(inf_ok.into_obligations())), + Ok(obligations) => ProcessResult::Changed(mk_pending(obligations)), Err(_) => ProcessResult::Error(FulfillmentErrorCode::Select( SelectionError::ConstArgHasWrongType { ct, ct_ty, expected_ty: ty }, )), @@ -527,11 +527,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)]; ProcessResult::Unchanged } - Ok(Ok(mut ok)) => { - for subobligation in &mut ok.obligations { + Ok(Ok(mut obligations)) => { + for subobligation in &mut obligations { subobligation.set_depth_from_parent(obligation.recursion_depth); } - ProcessResult::Changed(mk_pending(ok.obligations)) + ProcessResult::Changed(mk_pending(obligations)) } Ok(Err(err)) => { let expected_found = @@ -553,7 +553,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { vec![TyOrConstInferVar::Ty(a), TyOrConstInferVar::Ty(b)]; ProcessResult::Unchanged } - Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)), + Ok(Ok(obligations)) => ProcessResult::Changed(mk_pending(obligations)), Ok(Err(err)) => { let expected_found = ExpectedFound::new(false, coerce.a, coerce.b); ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err)) @@ -616,9 +616,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { ty::AliasTerm::from(b), ) { - return ProcessResult::Changed(mk_pending( - new_obligations.into_obligations(), - )); + return ProcessResult::Changed(mk_pending(new_obligations)); } } (_, Unevaluated(_)) | (Unevaluated(_), _) => (), @@ -629,9 +627,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { // `generic_const_exprs` .eq(DefineOpaqueTypes::Yes, c1, c2) { - return ProcessResult::Changed(mk_pending( - new_obligations.into_obligations(), - )); + return ProcessResult::Changed(mk_pending(new_obligations)); } } } @@ -673,9 +669,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { c1, c2, ) { - Ok(inf_ok) => { - ProcessResult::Changed(mk_pending(inf_ok.into_obligations())) - } + Ok(obligations) => ProcessResult::Changed(mk_pending(obligations)), Err(err) => { ProcessResult::Error(FulfillmentErrorCode::ConstEquate( ExpectedFound::new(true, c1, c2), diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 9cd99d99fc3c3..51d974b8d9f78 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -266,7 +266,7 @@ fn project_and_unify_term<'cx, 'tcx>( normalized, actual, ) { - Ok(InferOk { obligations: inferred_obligations, value: () }) => { + Ok(inferred_obligations) => { obligations.extend(inferred_obligations); ProjectAndUnifyResult::Holds(obligations) } @@ -638,7 +638,7 @@ pub fn compute_inherent_assoc_ty_args<'a, 'b, 'tcx>( } match selcx.infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, impl_ty, self_ty) { - Ok(mut ok) => obligations.append(&mut ok.obligations), + Ok(mut more_obligations) => obligations.append(&mut more_obligations), Err(_) => { tcx.dcx().span_bug( cause.span, @@ -2021,7 +2021,7 @@ fn confirm_param_env_candidate<'cx, 'tcx>( cache_projection, obligation_projection, ) { - Ok(InferOk { value: _, obligations }) => { + Ok(obligations) => { nested_obligations.extend(obligations); assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations); // FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 5141e969608e9..523cc70eae91e 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -13,7 +13,7 @@ use std::ops::ControlFlow; use rustc_ast::Mutability; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_hir::lang_items::LangItem; -use rustc_infer::infer::{DefineOpaqueTypes, HigherRankedType, InferOk}; +use rustc_infer::infer::{DefineOpaqueTypes, HigherRankedType}; use rustc_infer::traits::ObligationCauseCode; use rustc_middle::traits::{BuiltinImplSource, SignatureMismatchData}; use rustc_middle::ty::{ @@ -193,7 +193,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::No, placeholder_trait_predicate, candidate) - .map(|InferOk { obligations, .. }| obligations) .map_err(|_| Unimplemented)?, ); @@ -590,7 +589,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::No, trait_predicate.trait_ref, upcast_trait_ref) - .map(|InferOk { obligations, .. }| obligations) .map_err(|_| Unimplemented)?, ); @@ -1077,7 +1075,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::Yes, obligation_trait_ref, found_trait_ref) - .map(|InferOk { mut obligations, .. }| { + .map(|mut obligations| { obligations.extend(nested); obligations }) @@ -1168,7 +1166,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // Require that the traits involved in this upcast are **equal**; // only the **lifetime bound** is changed. - let InferOk { mut obligations, .. } = self + let mut obligations = self .infcx .at(&obligation.cause, obligation.param_env) .sup(DefineOpaqueTypes::Yes, target, source_trait) @@ -1235,7 +1233,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // `[T; n]` -> `[T]` (&ty::Array(a, _), &ty::Slice(b)) => { - let InferOk { obligations, .. } = self + let obligations = self .infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::Yes, b, a) @@ -1283,7 +1281,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { if unsizing_params.contains(i as u32) { args_b[i] } else { k } })); let new_struct = Ty::new_adt(tcx, def, args); - let InferOk { obligations, .. } = self + let obligations = self .infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::Yes, target, new_struct) @@ -1315,7 +1313,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // last element is equal to the target. let new_tuple = Ty::new_tup_from_iter(tcx, a_mid.iter().copied().chain(iter::once(b_last))); - let InferOk { mut obligations, .. } = self + let mut obligations = self .infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::Yes, target, new_tuple) diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index fba1d1025ca66..0c7b2875be7c8 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -45,7 +45,7 @@ use super::{ TraitQueryMode, const_evaluatable, project, util, wf, }; use crate::error_reporting::InferCtxtErrorExt; -use crate::infer::{InferCtxt, InferOk, TypeFreshener}; +use crate::infer::{InferCtxt, TypeFreshener}; use crate::solve::InferCtxtSelectExt as _; use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to}; use crate::traits::project::{ProjectAndUnifyResult, ProjectionCacheKeyExt}; @@ -649,7 +649,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let p = bound_predicate.rebind(p); // Does this code ever run? match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) { - Ok(Ok(InferOk { obligations, .. })) => { + Ok(Ok(obligations)) => { self.evaluate_predicates_recursively(previous_stack, obligations) } Ok(Err(_)) => Ok(EvaluatedToErr), @@ -661,7 +661,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let p = bound_predicate.rebind(p); // Does this code ever run? match self.infcx.coerce_predicate(&obligation.cause, obligation.param_env, p) { - Ok(Ok(InferOk { obligations, .. })) => { + Ok(Ok(obligations)) => { self.evaluate_predicates_recursively(previous_stack, obligations) } Ok(Err(_)) => Ok(EvaluatedToErr), @@ -883,7 +883,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { (Unevaluated(a), Unevaluated(b)) if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst => { - if let Ok(InferOk { obligations, value: () }) = self + if let Ok(obligations) = self .infcx .at(&obligation.cause, obligation.param_env) // Can define opaque types as this is only reachable with @@ -902,7 +902,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } (_, Unevaluated(_)) | (Unevaluated(_), _) => (), (_, _) => { - if let Ok(InferOk { obligations, value: () }) = self + if let Ok(obligations) = self .infcx .at(&obligation.cause, obligation.param_env) // Can define opaque types as this is only reachable with @@ -942,10 +942,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { c1, c2, ) { - Ok(inf_ok) => self.evaluate_predicates_recursively( - previous_stack, - inf_ok.into_obligations(), - ), + Ok(obligations) => self + .evaluate_predicates_recursively(previous_stack, obligations), Err(_) => Ok(EvaluatedToErr), } } @@ -997,10 +995,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ct_ty, ty, ) { - Ok(inf_ok) => self.evaluate_predicates_recursively( - previous_stack, - inf_ok.into_obligations(), - ), + Ok(obligations) => { + self.evaluate_predicates_recursively(previous_stack, obligations) + } Err(_) => Ok(EvaluatedToErr), } } @@ -1676,7 +1673,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { self.infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::No, placeholder_trait_ref, trait_bound) - .map(|InferOk { obligations: _, value: () }| { + .map(|_| { // This method is called within a probe, so we can't have // inference variables and placeholders escape. if !trait_bound.has_infer() && !trait_bound.has_placeholders() { @@ -1740,7 +1737,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::No, obligation.predicate, infer_projection) - .is_ok_and(|InferOk { obligations, value: () }| { + .is_ok_and(|obligations| { self.evaluate_predicates_recursively( TraitObligationStackList::empty(&ProvisionalEvaluationCache::default()), nested_obligations.into_iter().chain(obligations), @@ -2527,7 +2524,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { ObligationCauseCode::MatchImpl(obligation.cause.clone(), impl_def_id), ); - let InferOk { obligations, .. } = self + let obligations = self .infcx .at(&cause, obligation.param_env) .eq(DefineOpaqueTypes::No, placeholder_obligation_trait_ref, impl_trait_ref) @@ -2605,8 +2602,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { source_principal, ) }) - .map_err(|_| SelectionError::Unimplemented)? - .into_obligations(), + .map_err(|_| SelectionError::Unimplemented)?, ); } // Check that b_ty's projection is satisfied by exactly one of @@ -2674,8 +2670,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { source_projection, ) }) - .map_err(|_| SelectionError::Unimplemented)? - .into_obligations(), + .map_err(|_| SelectionError::Unimplemented)?, ); } // Check that b_ty's auto traits are present in a_ty's bounds. @@ -2726,7 +2721,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { self.infcx .at(&obligation.cause, obligation.param_env) .eq(DefineOpaqueTypes::No, predicate.trait_ref, trait_ref) - .map(|InferOk { obligations, .. }| obligations) + .map(|obligations| obligations) .map_err(|_| ()) } diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index b82a343364581..274db89794798 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -28,7 +28,7 @@ use tracing::{debug, instrument}; use super::{SelectionContext, util}; use crate::error_reporting::traits::to_pretty_impl_header; use crate::errors::NegativePositiveConflict; -use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt}; +use crate::infer::{InferCtxt, TyCtxtInferExt}; use crate::traits::select::IntercrateAmbiguityCause; use crate::traits::{FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt, coherence}; @@ -235,7 +235,7 @@ fn fulfill_implication<'tcx>( util::impl_subject_and_oblig(&selcx, param_env, target_impl, target_args, error_cause); // do the impls unify? If not, no specialization. - let Ok(InferOk { obligations: more_obligations, .. }) = infcx + let Ok(more_obligations) = infcx .at(&ObligationCause::dummy(), param_env) // Ok to use `Yes`, as all the generic params are already replaced by inference variables, // which will match the opaque type no matter if it is defining or not. diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 1f3cb4a61b895..3436c0a223c5a 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -1,5 +1,5 @@ use rustc_hir as hir; -use rustc_infer::infer::{DefineOpaqueTypes, InferOk, TyCtxtInferExt}; +use rustc_infer::infer::{DefineOpaqueTypes, TyCtxtInferExt}; use rustc_infer::traits; use rustc_middle::ty::{self, Upcast}; use rustc_span::DUMMY_SP; @@ -48,14 +48,13 @@ pub(crate) fn synthesize_blanket_impls( // Require the type the impl is implemented on to match // our type, and ignore the impl if there was a mismatch. - let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq( + let Ok(obligations) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq( DefineOpaqueTypes::Yes, impl_trait_ref.self_ty(), impl_ty, ) else { continue; }; - let InferOk { value: (), obligations } = eq_result; // FIXME(eddyb) ignoring `obligations` might cause false positives. drop(obligations);