1
Fork 0

Use ObligationCtxt intead of dyn TraitEngine

This commit is contained in:
Santiago Pastorino 2022-11-16 15:58:48 -03:00
parent 3f2b2eee8f
commit ad094cdceb
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
5 changed files with 30 additions and 47 deletions

View file

@ -46,7 +46,7 @@ use rustc_hir::Expr;
use rustc_hir_analysis::astconv::AstConv; use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::{Coercion, InferOk, InferResult}; use rustc_infer::infer::{Coercion, InferOk, InferResult};
use rustc_infer::traits::{Obligation, TraitEngine, TraitEngineExt}; use rustc_infer::traits::Obligation;
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::adjustment::{ use rustc_middle::ty::adjustment::{
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast, Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCast,
@ -62,8 +62,7 @@ use rustc_span::{self, BytePos, DesugaringKind, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use rustc_trait_selection::infer::InferCtxtExt as _; use rustc_trait_selection::infer::InferCtxtExt as _;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::TraitEngineExt as _; use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt};
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use std::ops::Deref; use std::ops::Deref;
@ -1055,9 +1054,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let Ok(ok) = coerce.coerce(source, target) else { let Ok(ok) = coerce.coerce(source, target) else {
return false; return false;
}; };
let mut fcx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.tcx); let ocx = ObligationCtxt::new_in_snapshot(self);
fcx.register_predicate_obligations(self, ok.obligations); ocx.register_obligations(ok.obligations);
fcx.select_where_possible(&self).is_empty() ocx.select_where_possible().is_empty()
}) })
} }

View file

@ -4,13 +4,12 @@ pub mod suggestions;
use super::{ use super::{
FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes, Obligation, ObligationCause, FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes, Obligation, ObligationCause,
ObligationCauseCode, OutputTypeParameterMismatch, Overflow, PredicateObligation, ObligationCauseCode, ObligationCtxt, OutputTypeParameterMismatch, Overflow,
SelectionContext, SelectionError, TraitNotObjectSafe, PredicateObligation, SelectionContext, SelectionError, TraitNotObjectSafe,
}; };
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode}; use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::{self, InferCtxt, TyCtxtInferExt}; use crate::infer::{self, InferCtxt, TyCtxtInferExt};
use crate::traits::engine::TraitEngineExt as _;
use crate::traits::query::evaluate_obligation::InferCtxtExt as _; use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::query::normalize::AtExt as _; use crate::traits::query::normalize::AtExt as _;
use crate::traits::specialize::to_pretty_impl_header; use crate::traits::specialize::to_pretty_impl_header;
@ -30,7 +29,6 @@ use rustc_hir::Item;
use rustc_hir::Node; use rustc_hir::Node;
use rustc_infer::infer::error_reporting::TypeErrCtxt; use rustc_infer::infer::error_reporting::TypeErrCtxt;
use rustc_infer::infer::TypeTrace; use rustc_infer::infer::TypeTrace;
use rustc_infer::traits::TraitEngine;
use rustc_middle::traits::select::OverflowError; use rustc_middle::traits::select::OverflowError;
use rustc_middle::ty::abstract_const::NotConstEvaluatable; use rustc_middle::ty::abstract_const::NotConstEvaluatable;
use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::error::ExpectedFound;
@ -354,9 +352,9 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
param_env, param_env,
ty.rebind(ty::TraitPredicate { trait_ref, constness, polarity }), ty.rebind(ty::TraitPredicate { trait_ref, constness, polarity }),
); );
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new_in_snapshot(self.tcx); let ocx = ObligationCtxt::new_in_snapshot(self);
fulfill_cx.register_predicate_obligation(self, obligation); ocx.register_obligation(obligation);
if fulfill_cx.select_all_or_error(self).is_empty() { if ocx.select_all_or_error().is_empty() {
return Ok(( return Ok((
ty::ClosureKind::from_def_id(self.tcx, trait_def_id) ty::ClosureKind::from_def_id(self.tcx, trait_def_id)
.expect("expected to map DefId to ClosureKind"), .expect("expected to map DefId to ClosureKind"),

View file

@ -31,7 +31,6 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_infer::traits::TraitEngineExt as _;
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{ use rustc_middle::ty::{
@ -403,9 +402,9 @@ pub fn fully_solve_obligation<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
obligation: PredicateObligation<'tcx>, obligation: PredicateObligation<'tcx>,
) -> Vec<FulfillmentError<'tcx>> { ) -> Vec<FulfillmentError<'tcx>> {
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx); let ocx = ObligationCtxt::new(infcx);
engine.register_predicate_obligation(infcx, obligation); ocx.register_obligation(obligation);
engine.select_all_or_error(infcx) ocx.select_all_or_error()
} }
/// Process a set of obligations (and any nested obligations that come from them) /// Process a set of obligations (and any nested obligations that come from them)
@ -414,9 +413,9 @@ pub fn fully_solve_obligations<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>, obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
) -> Vec<FulfillmentError<'tcx>> { ) -> Vec<FulfillmentError<'tcx>> {
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx); let ocx = ObligationCtxt::new(infcx);
engine.register_predicate_obligations(infcx, obligations); ocx.register_obligations(obligations);
engine.select_all_or_error(infcx) ocx.select_all_or_error()
} }
/// Process a bound (and any nested obligations that come from it) to completion. /// Process a bound (and any nested obligations that come from it) to completion.
@ -429,9 +428,9 @@ pub fn fully_solve_bound<'tcx>(
ty: Ty<'tcx>, ty: Ty<'tcx>,
bound: DefId, bound: DefId,
) -> Vec<FulfillmentError<'tcx>> { ) -> Vec<FulfillmentError<'tcx>> {
let mut engine = <dyn TraitEngine<'tcx>>::new(infcx.tcx); let ocx = ObligationCtxt::new(infcx);
engine.register_bound(infcx, param_env, ty, bound, cause); ocx.register_bound(cause, param_env, ty, bound);
engine.select_all_or_error(infcx) ocx.select_all_or_error()
} }
/// Normalizes the predicates and checks whether they hold in an empty environment. If this /// Normalizes the predicates and checks whether they hold in an empty environment. If this

View file

@ -10,14 +10,14 @@
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/specialization.html
pub mod specialization_graph; pub mod specialization_graph;
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _};
use specialization_graph::GraphExt; use specialization_graph::GraphExt;
use crate::errors::NegativePositiveConflict; use crate::errors::NegativePositiveConflict;
use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt}; use crate::infer::{InferCtxt, InferOk, TyCtxtInferExt};
use crate::traits::engine::TraitEngineExt as _;
use crate::traits::select::IntercrateAmbiguityCause; use crate::traits::select::IntercrateAmbiguityCause;
use crate::traits::{self, coherence, FutureCompatOverlapErrorKind, ObligationCause}; use crate::traits::{
self, coherence, FutureCompatOverlapErrorKind, ObligationCause, ObligationCtxt,
};
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{error_code, DelayDm, Diagnostic}; use rustc_errors::{error_code, DelayDm, Diagnostic};
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
@ -204,12 +204,12 @@ fn fulfill_implication<'tcx>(
// Needs to be `in_snapshot` because this function is used to rebase // Needs to be `in_snapshot` because this function is used to rebase
// substitutions, which may happen inside of a select within a probe. // substitutions, which may happen inside of a select within a probe.
let mut engine = <dyn TraitEngine<'tcx>>::new_in_snapshot(infcx.tcx); let ocx = ObligationCtxt::new_in_snapshot(infcx);
// attempt to prove all of the predicates for impl2 given those for impl1 // attempt to prove all of the predicates for impl2 given those for impl1
// (which are packed up in penv) // (which are packed up in penv)
engine.register_predicate_obligations(infcx, obligations.chain(more_obligations)); ocx.register_obligations(obligations.chain(more_obligations));
let errors = engine.select_all_or_error(infcx); let errors = ocx.select_all_or_error();
if !errors.is_empty() { if !errors.is_empty() {
// no dice! // no dice!
debug!( debug!(

View file

@ -1,6 +1,5 @@
use crate::infer::{InferCtxt, TyCtxtInferExt}; use crate::infer::{InferCtxt, TyCtxtInferExt};
use crate::traits::ObligationCause; use crate::traits::{ObligationCause, ObligationCtxt};
use crate::traits::{TraitEngine, TraitEngineExt};
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir; use rustc_hir as hir;
@ -72,28 +71,16 @@ fn type_marked_structural<'tcx>(
adt_ty: Ty<'tcx>, adt_ty: Ty<'tcx>,
cause: ObligationCause<'tcx>, cause: ObligationCause<'tcx>,
) -> bool { ) -> bool {
let mut fulfillment_cx = <dyn TraitEngine<'tcx>>::new(infcx.tcx); let ocx = ObligationCtxt::new(infcx);
// require `#[derive(PartialEq)]` // require `#[derive(PartialEq)]`
let structural_peq_def_id = let structural_peq_def_id =
infcx.tcx.require_lang_item(LangItem::StructuralPeq, Some(cause.span)); infcx.tcx.require_lang_item(LangItem::StructuralPeq, Some(cause.span));
fulfillment_cx.register_bound( ocx.register_bound(cause.clone(), ty::ParamEnv::empty(), adt_ty, structural_peq_def_id);
infcx,
ty::ParamEnv::empty(),
adt_ty,
structural_peq_def_id,
cause.clone(),
);
// for now, require `#[derive(Eq)]`. (Doing so is a hack to work around // for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
// the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.) // the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
let structural_teq_def_id = let structural_teq_def_id =
infcx.tcx.require_lang_item(LangItem::StructuralTeq, Some(cause.span)); infcx.tcx.require_lang_item(LangItem::StructuralTeq, Some(cause.span));
fulfillment_cx.register_bound( ocx.register_bound(cause, ty::ParamEnv::empty(), adt_ty, structural_teq_def_id);
infcx,
ty::ParamEnv::empty(),
adt_ty,
structural_teq_def_id,
cause,
);
// We deliberately skip *reporting* fulfillment errors (via // We deliberately skip *reporting* fulfillment errors (via
// `report_fulfillment_errors`), for two reasons: // `report_fulfillment_errors`), for two reasons:
@ -104,7 +91,7 @@ fn type_marked_structural<'tcx>(
// //
// 2. We are sometimes doing future-incompatibility lints for // 2. We are sometimes doing future-incompatibility lints for
// now, so we do not want unconditional errors here. // now, so we do not want unconditional errors here.
fulfillment_cx.select_all_or_error(infcx).is_empty() ocx.select_all_or_error().is_empty()
} }
/// This implements the traversal over the structure of a given type to try to /// This implements the traversal over the structure of a given type to try to