Rollup merge of #103868 - compiler-errors:trait-engine-less, r=jackh726
Use `TraitEngine` (by itself) less Replace `TraitEngine` in favor of `ObligationCtxt` or `fully_solve_*`, improving code readability.
This commit is contained in:
commit
ad01a37ca9
6 changed files with 57 additions and 86 deletions
|
@ -23,7 +23,6 @@ use rustc_span::hygiene::DesugaringKind;
|
||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::{BytePos, Span, Symbol};
|
use rustc_span::{BytePos, Span, Symbol};
|
||||||
use rustc_trait_selection::infer::InferCtxtExt;
|
use rustc_trait_selection::infer::InferCtxtExt;
|
||||||
use rustc_trait_selection::traits::TraitEngineExt as _;
|
|
||||||
|
|
||||||
use crate::borrow_set::TwoPhaseActivation;
|
use crate::borrow_set::TwoPhaseActivation;
|
||||||
use crate::borrowck_errors;
|
use crate::borrowck_errors;
|
||||||
|
@ -613,24 +612,20 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
||||||
else { return; };
|
else { return; };
|
||||||
// Try to find predicates on *generic params* that would allow copying `ty`
|
// Try to find predicates on *generic params* that would allow copying `ty`
|
||||||
let infcx = tcx.infer_ctxt().build();
|
let infcx = tcx.infer_ctxt().build();
|
||||||
let mut fulfill_cx = <dyn rustc_infer::traits::TraitEngine<'_>>::new(infcx.tcx);
|
|
||||||
|
|
||||||
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
|
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
|
||||||
let cause = ObligationCause::new(
|
let cause = ObligationCause::new(
|
||||||
span,
|
span,
|
||||||
self.mir_hir_id(),
|
self.mir_hir_id(),
|
||||||
rustc_infer::traits::ObligationCauseCode::MiscObligation,
|
rustc_infer::traits::ObligationCauseCode::MiscObligation,
|
||||||
);
|
);
|
||||||
fulfill_cx.register_bound(
|
let errors = rustc_trait_selection::traits::fully_solve_bound(
|
||||||
&infcx,
|
&infcx,
|
||||||
|
cause,
|
||||||
self.param_env,
|
self.param_env,
|
||||||
// Erase any region vids from the type, which may not be resolved
|
// Erase any region vids from the type, which may not be resolved
|
||||||
infcx.tcx.erase_regions(ty),
|
infcx.tcx.erase_regions(ty),
|
||||||
copy_did,
|
copy_did,
|
||||||
cause,
|
|
||||||
);
|
);
|
||||||
// Select all, including ambiguous predicates
|
|
||||||
let errors = fulfill_cx.select_all_or_error(&infcx);
|
|
||||||
|
|
||||||
// Only emit suggestion if all required predicates are on generic
|
// Only emit suggestion if all required predicates are on generic
|
||||||
let predicates: Result<Vec<_>, _> = errors
|
let predicates: Result<Vec<_>, _> = errors
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rustc_hir::def_id::LocalDefId;
|
||||||
use rustc_hir::OpaqueTyOrigin;
|
use rustc_hir::OpaqueTyOrigin;
|
||||||
use rustc_infer::infer::TyCtxtInferExt as _;
|
use rustc_infer::infer::TyCtxtInferExt as _;
|
||||||
use rustc_infer::infer::{DefiningAnchor, InferCtxt};
|
use rustc_infer::infer::{DefiningAnchor, InferCtxt};
|
||||||
use rustc_infer::traits::{Obligation, ObligationCause, TraitEngine};
|
use rustc_infer::traits::{Obligation, ObligationCause};
|
||||||
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
|
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
|
||||||
use rustc_middle::ty::visit::TypeVisitable;
|
use rustc_middle::ty::visit::TypeVisitable;
|
||||||
use rustc_middle::ty::{
|
use rustc_middle::ty::{
|
||||||
|
@ -12,7 +12,7 @@ use rustc_middle::ty::{
|
||||||
};
|
};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
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::ObligationCtxt;
|
||||||
|
|
||||||
use super::RegionInferenceContext;
|
use super::RegionInferenceContext;
|
||||||
|
|
||||||
|
@ -252,28 +252,24 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
|
||||||
// type-alias-impl-trait/issue-67844-nested-opaque.rs
|
// type-alias-impl-trait/issue-67844-nested-opaque.rs
|
||||||
let infcx =
|
let infcx =
|
||||||
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
|
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
|
||||||
|
let ocx = ObligationCtxt::new(&infcx);
|
||||||
// Require the hidden type to be well-formed with only the generics of the opaque type.
|
// Require the hidden type to be well-formed with only the generics of the opaque type.
|
||||||
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
|
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
|
||||||
// hidden type is well formed even without those bounds.
|
// hidden type is well formed even without those bounds.
|
||||||
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()))
|
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()))
|
||||||
.to_predicate(infcx.tcx);
|
.to_predicate(infcx.tcx);
|
||||||
let mut fulfillment_cx = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
|
|
||||||
|
|
||||||
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());
|
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());
|
||||||
|
|
||||||
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
|
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
|
||||||
// the bounds that the function supplies.
|
// the bounds that the function supplies.
|
||||||
let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs);
|
let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs);
|
||||||
match infcx
|
if let Err(err) = ocx.eq(
|
||||||
.at(&ObligationCause::misc(instantiated_ty.span, body_id), param_env)
|
&ObligationCause::misc(instantiated_ty.span, body_id),
|
||||||
.eq(opaque_ty, definition_ty)
|
param_env,
|
||||||
{
|
opaque_ty,
|
||||||
Ok(infer_ok) => {
|
definition_ty,
|
||||||
for obligation in infer_ok.obligations {
|
) {
|
||||||
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
infcx
|
infcx
|
||||||
.err_ctxt()
|
.err_ctxt()
|
||||||
.report_mismatched_types(
|
.report_mismatched_types(
|
||||||
|
@ -284,16 +280,17 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
|
||||||
)
|
)
|
||||||
.emit();
|
.emit();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fulfillment_cx.register_predicate_obligation(
|
ocx.register_obligation(Obligation::misc(
|
||||||
&infcx,
|
instantiated_ty.span,
|
||||||
Obligation::misc(instantiated_ty.span, body_id, param_env, predicate),
|
body_id,
|
||||||
);
|
param_env,
|
||||||
|
predicate,
|
||||||
|
));
|
||||||
|
|
||||||
// Check that all obligations are satisfied by the implementation's
|
// Check that all obligations are satisfied by the implementation's
|
||||||
// version.
|
// version.
|
||||||
let errors = fulfillment_cx.select_all_or_error(&infcx);
|
let errors = ocx.select_all_or_error();
|
||||||
|
|
||||||
// This is still required for many(half of the tests in ui/type-alias-impl-trait)
|
// This is still required for many(half of the tests in ui/type-alias-impl-trait)
|
||||||
// tests to pass
|
// tests to pass
|
||||||
|
|
|
@ -13,11 +13,8 @@ use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty,
|
||||||
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef, TypeVisitable};
|
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef, TypeVisitable};
|
||||||
use rustc_mir_dataflow::{self, Analysis};
|
use rustc_mir_dataflow::{self, Analysis};
|
||||||
use rustc_span::{sym, Span, Symbol};
|
use rustc_span::{sym, Span, Symbol};
|
||||||
use rustc_trait_selection::infer::InferCtxtExt;
|
|
||||||
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
|
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
|
||||||
use rustc_trait_selection::traits::{
|
use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt, SelectionContext};
|
||||||
self, ObligationCauseCode, SelectionContext, TraitEngine, TraitEngineExt,
|
|
||||||
};
|
|
||||||
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
@ -747,35 +744,26 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
|
||||||
// "non-const" check. This is required for correctness here.
|
// "non-const" check. This is required for correctness here.
|
||||||
{
|
{
|
||||||
let infcx = tcx.infer_ctxt().build();
|
let infcx = tcx.infer_ctxt().build();
|
||||||
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
|
let ocx = ObligationCtxt::new(&infcx);
|
||||||
|
|
||||||
let predicates = tcx.predicates_of(callee).instantiate(tcx, substs);
|
let predicates = tcx.predicates_of(callee).instantiate(tcx, substs);
|
||||||
let hir_id = tcx
|
let hir_id = tcx
|
||||||
.hir()
|
.hir()
|
||||||
.local_def_id_to_hir_id(self.body.source.def_id().expect_local());
|
.local_def_id_to_hir_id(self.body.source.def_id().expect_local());
|
||||||
let cause = || {
|
let cause = ObligationCause::new(
|
||||||
ObligationCause::new(
|
|
||||||
terminator.source_info.span,
|
terminator.source_info.span,
|
||||||
hir_id,
|
hir_id,
|
||||||
ObligationCauseCode::ItemObligation(callee),
|
ObligationCauseCode::ItemObligation(callee),
|
||||||
)
|
|
||||||
};
|
|
||||||
let normalized = infcx.partially_normalize_associated_types_in(
|
|
||||||
cause(),
|
|
||||||
param_env,
|
|
||||||
predicates,
|
|
||||||
);
|
);
|
||||||
|
let normalized_predicates =
|
||||||
for p in normalized.obligations {
|
ocx.normalize(cause.clone(), param_env, predicates);
|
||||||
fulfill_cx.register_predicate_obligation(&infcx, p);
|
ocx.register_obligations(traits::predicates_for_generics(
|
||||||
}
|
|_, _| cause.clone(),
|
||||||
for obligation in traits::predicates_for_generics(
|
|
||||||
|_, _| cause(),
|
|
||||||
self.param_env,
|
self.param_env,
|
||||||
normalized.value,
|
normalized_predicates,
|
||||||
) {
|
));
|
||||||
fulfill_cx.register_predicate_obligation(&infcx, obligation);
|
|
||||||
}
|
let errors = ocx.select_all_or_error();
|
||||||
let errors = fulfill_cx.select_all_or_error(&infcx);
|
|
||||||
if !errors.is_empty() {
|
if !errors.is_empty() {
|
||||||
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
|
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ use rustc_span::symbol::{sym, Ident};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use rustc_trait_selection::infer::InferCtxtExt;
|
use rustc_trait_selection::infer::InferCtxtExt;
|
||||||
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt as _;
|
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt as _;
|
||||||
use rustc_trait_selection::traits::{FulfillmentError, TraitEngine, TraitEngineExt};
|
use rustc_trait_selection::traits::FulfillmentError;
|
||||||
use rustc_type_ir::sty::TyKind::*;
|
use rustc_type_ir::sty::TyKind::*;
|
||||||
|
|
||||||
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
|
@ -785,9 +785,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
other_ty_expr,
|
other_ty_expr,
|
||||||
expected,
|
expected,
|
||||||
);
|
);
|
||||||
let mut fulfill = <dyn TraitEngine<'_>>::new(self.tcx);
|
Err(rustc_trait_selection::traits::fully_solve_obligation(self, obligation))
|
||||||
fulfill.register_predicate_obligation(self, obligation);
|
|
||||||
Err(fulfill.select_where_possible(&self.infcx))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,9 @@ use crate::{LateContext, LateLintPass, LintContext};
|
||||||
use hir::{Expr, Pat};
|
use hir::{Expr, Pat};
|
||||||
use rustc_errors::{Applicability, DelayDm};
|
use rustc_errors::{Applicability, DelayDm};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_infer::traits::TraitEngine;
|
|
||||||
use rustc_infer::{infer::TyCtxtInferExt, traits::ObligationCause};
|
use rustc_infer::{infer::TyCtxtInferExt, traits::ObligationCause};
|
||||||
use rustc_middle::ty::{self, List};
|
use rustc_middle::ty::{self, List};
|
||||||
use rustc_span::{sym, Span};
|
use rustc_span::{sym, Span};
|
||||||
use rustc_trait_selection::traits::TraitEngineExt;
|
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
/// The `for_loops_over_fallibles` lint checks for `for` loops over `Option` or `Result` values.
|
/// The `for_loops_over_fallibles` lint checks for `for` loops over `Option` or `Result` values.
|
||||||
|
@ -160,24 +158,19 @@ fn suggest_question_mark<'tcx>(
|
||||||
|
|
||||||
let ty = substs.type_at(0);
|
let ty = substs.type_at(0);
|
||||||
let infcx = cx.tcx.infer_ctxt().build();
|
let infcx = cx.tcx.infer_ctxt().build();
|
||||||
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
|
|
||||||
|
|
||||||
let cause = ObligationCause::new(
|
let cause = ObligationCause::new(
|
||||||
span,
|
span,
|
||||||
body_id.hir_id,
|
body_id.hir_id,
|
||||||
rustc_infer::traits::ObligationCauseCode::MiscObligation,
|
rustc_infer::traits::ObligationCauseCode::MiscObligation,
|
||||||
);
|
);
|
||||||
fulfill_cx.register_bound(
|
let errors = rustc_trait_selection::traits::fully_solve_bound(
|
||||||
&infcx,
|
&infcx,
|
||||||
|
cause,
|
||||||
ty::ParamEnv::empty(),
|
ty::ParamEnv::empty(),
|
||||||
// Erase any region vids from the type, which may not be resolved
|
// Erase any region vids from the type, which may not be resolved
|
||||||
infcx.tcx.erase_regions(ty),
|
infcx.tcx.erase_regions(ty),
|
||||||
into_iterator_did,
|
into_iterator_did,
|
||||||
cause,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Select all, including ambiguous predicates
|
|
||||||
let errors = fulfill_cx.select_all_or_error(&infcx);
|
|
||||||
|
|
||||||
errors.is_empty()
|
errors.is_empty()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::infer::InferCtxt;
|
use crate::infer::InferCtxt;
|
||||||
use crate::traits::query::type_op::{self, TypeOp, TypeOpOutput};
|
use crate::traits::query::type_op::{self, TypeOp, TypeOpOutput};
|
||||||
use crate::traits::query::NoSolution;
|
use crate::traits::query::NoSolution;
|
||||||
use crate::traits::{ObligationCause, TraitEngine, TraitEngineExt};
|
use crate::traits::ObligationCause;
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::HirId;
|
use rustc_hir::HirId;
|
||||||
|
@ -74,20 +74,20 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
|
||||||
debug!(?constraints);
|
debug!(?constraints);
|
||||||
// Instantiation may have produced new inference variables and constraints on those
|
// Instantiation may have produced new inference variables and constraints on those
|
||||||
// variables. Process these constraints.
|
// variables. Process these constraints.
|
||||||
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(self.tcx);
|
|
||||||
let cause = ObligationCause::misc(span, body_id);
|
let cause = ObligationCause::misc(span, body_id);
|
||||||
for &constraint in &constraints.outlives {
|
let errors = super::fully_solve_obligations(
|
||||||
let obligation = self.query_outlives_constraint_to_obligation(
|
self,
|
||||||
constraint,
|
constraints.outlives.iter().map(|constraint| {
|
||||||
|
self.query_outlives_constraint_to_obligation(
|
||||||
|
*constraint,
|
||||||
cause.clone(),
|
cause.clone(),
|
||||||
param_env,
|
param_env,
|
||||||
|
)
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
fulfill_cx.register_predicate_obligation(self, obligation);
|
|
||||||
}
|
|
||||||
if !constraints.member_constraints.is_empty() {
|
if !constraints.member_constraints.is_empty() {
|
||||||
span_bug!(span, "{:#?}", constraints.member_constraints);
|
span_bug!(span, "{:#?}", constraints.member_constraints);
|
||||||
}
|
}
|
||||||
let errors = fulfill_cx.select_all_or_error(self);
|
|
||||||
if !errors.is_empty() {
|
if !errors.is_empty() {
|
||||||
self.tcx.sess.delay_span_bug(
|
self.tcx.sess.delay_span_bug(
|
||||||
span,
|
span,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue