Skip to content

Commit

Permalink
Rollup merge of rust-lang#104483 - oli-obk:santa-clauses-make-goals, …
Browse files Browse the repository at this point in the history
…r=compiler-errors

Convert predicates into Predicate in the Obligation constructor

instead of having almost all callers do that.

This reduces a bit of boilerplate, and also paves the way for my work towards rust-lang/compiler-team#531 (as it makes it easier to accept both goals and clauses where right now it only accepts predicates).
  • Loading branch information
matthiaskrgr committed Nov 17, 2022
2 parents 1c52c24 + 4f11f3b commit ed97f24
Show file tree
Hide file tree
Showing 49 changed files with 259 additions and 235 deletions.
8 changes: 3 additions & 5 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use rustc_infer::infer::{DefiningAnchor, InferCtxt};
use rustc_infer::traits::{Obligation, ObligationCause};
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{
self, OpaqueHiddenType, OpaqueTypeKey, ToPredicate, Ty, TyCtxt, TypeFoldable,
};
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
use rustc_span::Span;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::ObligationCtxt;
Expand Down Expand Up @@ -256,8 +254,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
// 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
// hidden type is well formed even without those bounds.
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()))
.to_predicate(infcx.tcx);
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()));

let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());

Expand All @@ -282,6 +279,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
}

ocx.register_obligation(Obligation::misc(
infcx.tcx,
instantiated_ty.span,
body_id,
param_env,
Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
trait_ref,
constness: ty::BoundConstness::NotConst,
polarity: ty::ImplPolarity::Positive,
}))
.to_predicate(self.tcx()),
})),
locations,
category,
);
Expand Down Expand Up @@ -122,26 +121,26 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {

pub(super) fn prove_predicates(
&mut self,
predicates: impl IntoIterator<Item = impl ToPredicate<'tcx>>,
predicates: impl IntoIterator<
Item = impl ToPredicate<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug,
>,
locations: Locations,
category: ConstraintCategory<'tcx>,
) {
for predicate in predicates {
let predicate = predicate.to_predicate(self.tcx());
debug!("prove_predicates(predicate={:?}, locations={:?})", predicate, locations,);

self.prove_predicate(predicate, locations, category);
}
}

#[instrument(skip(self), level = "debug")]
pub(super) fn prove_predicate(
&mut self,
predicate: ty::Predicate<'tcx>,
predicate: impl ToPredicate<'tcx, ty::Predicate<'tcx>> + std::fmt::Debug,
locations: Locations,
category: ConstraintCategory<'tcx>,
) {
let param_env = self.param_env;
let predicate = predicate.to_predicate(self.tcx());
self.fully_perform_op(
locations,
category,
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ use rustc_middle::ty::subst::{SubstsRef, UserSubsts};
use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{
self, Binder, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, Dynamic,
OpaqueHiddenType, OpaqueTypeKey, RegionVid, ToPredicate, Ty, TyCtxt, UserType,
UserTypeAnnotationIndex,
OpaqueHiddenType, OpaqueTypeKey, RegionVid, Ty, TyCtxt, UserType, UserTypeAnnotationIndex,
};
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -1069,8 +1068,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

self.prove_predicate(
ty::Binder::dummy(ty::PredicateKind::WellFormed(inferred_ty.into()))
.to_predicate(self.tcx()),
ty::Binder::dummy(ty::PredicateKind::WellFormed(inferred_ty.into())),
Locations::All(span),
ConstraintCategory::TypeAnnotation,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
polarity: ty::ImplPolarity::Positive,
});
let obligation =
Obligation::new(ObligationCause::dummy(), param_env, poly_trait_pred);
Obligation::new(tcx, ObligationCause::dummy(), param_env, poly_trait_pred);

let implsrc = {
let infcx = tcx.infer_ctxt().build();
Expand Down Expand Up @@ -816,6 +816,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {

if !nonconst_call_permission {
let obligation = Obligation::new(
tcx,
ObligationCause::dummy_with_span(*fn_span),
param_env,
tcx.mk_predicate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
}
Adt(..) => {
let obligation = Obligation::new(
tcx,
ObligationCause::dummy(),
param_env,
Binder::dummy(TraitPredicate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl Qualif for NeedsNonConstDrop {
let destruct = cx.tcx.require_lang_item(LangItem::Destruct, None);

let obligation = Obligation::new(
cx.tcx,
ObligationCause::dummy(),
cx.param_env,
ty::Binder::dummy(ty::TraitPredicate {
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use rustc_middle::middle::stability::EvalResult;
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::util::{Discr, IntTypeExt};
use rustc_middle::ty::{
self, ParamEnv, ToPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
};
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
use rustc_span::symbol::sym;
use rustc_span::{self, Span};
Expand Down Expand Up @@ -464,9 +462,8 @@ fn check_opaque_meets_bounds<'tcx>(
// Additionally 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
// hidden type is well formed even without those bounds.
let predicate =
ty::Binder::dummy(ty::PredicateKind::WellFormed(hidden_ty.into())).to_predicate(tcx);
ocx.register_obligation(Obligation::new(misc_cause, param_env, predicate));
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(hidden_ty.into()));
ocx.register_obligation(Obligation::new(tcx, misc_cause, param_env, predicate));

// Check that all obligations are satisfied by the implementation's
// version.
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_hir_analysis/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn compare_predicate_entailment<'tcx>(
kind: impl_m.kind,
},
);
ocx.register_obligation(traits::Obligation::new(cause, param_env, predicate));
ocx.register_obligation(traits::Obligation::new(tcx, cause, param_env, predicate));
}

// We now need to check that the signature of the impl method is
Expand Down Expand Up @@ -611,6 +611,7 @@ impl<'tcx> TypeFolder<'tcx> for ImplTraitInTraitCollector<'_, 'tcx> {
);

self.ocx.register_obligation(traits::Obligation::new(
self.tcx(),
ObligationCause::new(
self.span,
self.body_id,
Expand Down Expand Up @@ -1585,7 +1586,7 @@ fn compare_type_predicate_entailment<'tcx>(
},
);
ocx.register_obligations(obligations);
ocx.register_obligation(traits::Obligation::new(cause, param_env, predicate));
ocx.register_obligation(traits::Obligation::new(tcx, cause, param_env, predicate));
}

// Check that all obligations are satisfied by the implementation's
Expand Down Expand Up @@ -1790,7 +1791,7 @@ pub fn check_type_bounds<'tcx>(
.subst_iter_copied(tcx, rebased_substs)
.map(|(concrete_ty_bound, span)| {
debug!("check_type_bounds: concrete_ty_bound = {:?}", concrete_ty_bound);
traits::Obligation::new(mk_cause(span), param_env, concrete_ty_bound)
traits::Obligation::new(tcx, mk_cause(span), param_env, concrete_ty_bound)
})
.collect();
debug!("check_type_bounds: item_bounds={:?}", obligations);
Expand Down
17 changes: 9 additions & 8 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use rustc_middle::mir::ConstraintCategory;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::trait_def::TraitSpecializationKind;
use rustc_middle::ty::{
self, AdtKind, DefIdTree, GenericParamDefKind, ToPredicate, Ty, TyCtxt, TypeFoldable,
TypeSuperVisitable, TypeVisitable, TypeVisitor,
self, AdtKind, DefIdTree, GenericParamDefKind, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
TypeVisitable, TypeVisitor,
};
use rustc_middle::ty::{GenericArgKind, InternalSubsts};
use rustc_session::parse::feature_err;
Expand Down Expand Up @@ -75,9 +75,10 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
// for a type to be WF, we do not need to check if const trait predicates satisfy.
let param_env = self.param_env.without_const();
self.ocx.register_obligation(traits::Obligation::new(
self.tcx(),
cause,
param_env,
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)).to_predicate(self.tcx()),
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)),
));
}
}
Expand Down Expand Up @@ -1111,12 +1112,12 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
traits::MiscObligation,
);
wfcx.register_obligation(traits::Obligation::new(
tcx,
cause,
wfcx.param_env,
ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(
ty::Const::from_anon_const(tcx, discr_def_id.expect_local()),
))
.to_predicate(tcx),
)),
));
}
}
Expand Down Expand Up @@ -1453,7 +1454,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
wfcx.body_id,
traits::ItemObligation(def_id.to_def_id()),
);
traits::Obligation::new(cause, wfcx.param_env, pred)
traits::Obligation::new(tcx, cause, wfcx.param_env, pred)
});

let predicates = predicates.0.instantiate_identity(tcx);
Expand Down Expand Up @@ -1783,8 +1784,7 @@ fn receiver_is_implemented<'tcx>(
substs: tcx.mk_substs_trait(receiver_ty, &[]),
});

let obligation =
traits::Obligation::new(cause, wfcx.param_env, trait_ref.without_const().to_predicate(tcx));
let obligation = traits::Obligation::new(tcx, cause, wfcx.param_env, trait_ref.without_const());

if wfcx.infcx.predicate_must_hold_modulo_regions(&obligation) {
true
Expand Down Expand Up @@ -1931,6 +1931,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
}

let obligation = traits::Obligation::new(
tcx,
traits::ObligationCause::new(span, self.body_id, traits::TrivialBound),
empty_env,
pred,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hir::{ForeignItem, ForeignItemKind, HirId};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::{ObligationCause, WellFormedLoc};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Region, ToPredicate, TyCtxt, TypeFoldable, TypeFolder};
use rustc_middle::ty::{self, Region, TyCtxt, TypeFoldable, TypeFolder};
use rustc_trait_selection::traits;

pub fn provide(providers: &mut Providers) {
Expand Down Expand Up @@ -74,10 +74,10 @@ fn diagnostic_hir_wf_check<'tcx>(
let errors = traits::fully_solve_obligation(
&infcx,
traits::Obligation::new(
self.tcx,
cause,
self.param_env,
ty::Binder::dummy(ty::PredicateKind::WellFormed(tcx_ty.into()))
.to_predicate(self.tcx),
ty::Binder::dummy(ty::PredicateKind::WellFormed(tcx_ty.into())),
),
);
if !errors.is_empty() {
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_errors::{Applicability, Diagnostic, MultiSpan};
use rustc_hir::{self as hir, ExprKind};
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::traits::Obligation;
use rustc_middle::ty::{self, ToPredicate, Ty};
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use rustc_trait_selection::traits::{
Expand Down Expand Up @@ -538,23 +538,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.bound_explicit_item_bounds(rpit_def_id)
.subst_iter_copied(self.tcx, substs)
{
let pred = match pred.kind().skip_binder() {
let pred = pred.kind().rebind(match pred.kind().skip_binder() {
ty::PredicateKind::Trait(mut trait_pred) => {
assert_eq!(trait_pred.trait_ref.self_ty(), opaque_ty);
trait_pred.trait_ref.substs =
self.tcx.mk_substs_trait(ty, &trait_pred.trait_ref.substs[1..]);
pred.kind().rebind(trait_pred).to_predicate(self.tcx)
ty::PredicateKind::Trait(trait_pred)
}
ty::PredicateKind::Projection(mut proj_pred) => {
assert_eq!(proj_pred.projection_ty.self_ty(), opaque_ty);
proj_pred.projection_ty.substs = self
.tcx
.mk_substs_trait(ty, &proj_pred.projection_ty.substs[1..]);
pred.kind().rebind(proj_pred).to_predicate(self.tcx)
ty::PredicateKind::Projection(proj_pred)
}
_ => continue,
};
});
if !self.predicate_must_hold_modulo_regions(&Obligation::new(
self.tcx,
ObligationCause::misc(span, self.body_id),
self.param_env,
pred,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
predicates.predicates.iter().zip(&predicates.spans)
{
let obligation = Obligation::new(
self.tcx,
ObligationCause::dummy_with_span(callee_expr.span),
self.param_env,
*predicate,
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::relate::RelateResult;
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{self, ToPredicate, Ty, TypeAndMut};
use rustc_middle::ty::{self, Ty, TypeAndMut};
use rustc_session::parse::feature_err;
use rustc_span::symbol::sym;
use rustc_span::{self, BytePos, DesugaringKind, Span};
Expand Down Expand Up @@ -278,13 +278,13 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
for &source_ty in &[a, b] {
if source_ty != target_ty {
obligations.push(Obligation::new(
self.tcx(),
self.cause.clone(),
self.param_env,
ty::Binder::dummy(ty::PredicateKind::Coerce(ty::CoercePredicate {
a: source_ty,
b: target_ty,
}))
.to_predicate(self.tcx()),
})),
));
}
}
Expand Down Expand Up @@ -669,7 +669,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
continue;
}
};
match selcx.select(&obligation.with(trait_pred)) {
match selcx.select(&obligation.with(selcx.tcx(), trait_pred)) {
// Uncertain or unimplemented.
Ok(None) => {
if trait_pred.def_id() == unsize_did {
Expand Down Expand Up @@ -783,10 +783,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// and then require that the resulting predicate (e.g., `usize: Clone`)
// holds (it does).
let predicate = predicate.with_self_ty(self.tcx, a);
Obligation::new(self.cause.clone(), self.param_env, predicate)
Obligation::new(self.tcx, self.cause.clone(), self.param_env, predicate)
})
// Enforce the region bound (e.g., `usize: 'static`, in our example).
.chain([Obligation::new(
self.tcx,
self.cause.clone(),
self.param_env,
self.tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::TypeOutlives(
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{
self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, ToPredicate, Ty,
UserType,
self, AdtKind, CanonicalUserType, DefIdTree, EarlyBinder, GenericParamDefKind, Ty, UserType,
};
use rustc_middle::ty::{GenericArgKind, InternalSubsts, SubstsRef, UserSelfTy, UserSubsts};
use rustc_session::lint;
Expand Down Expand Up @@ -559,9 +558,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// WF obligations never themselves fail, so no real need to give a detailed cause:
let cause = traits::ObligationCause::new(span, self.body_id, code);
self.register_predicate(traits::Obligation::new(
self.tcx,
cause,
self.param_env,
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)).to_predicate(self.tcx),
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)),
));
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
);
let obligation = traits::Obligation::new(
self.tcx,
traits::ObligationCause::dummy(),
self.param_env,
ty::Binder::dummy(ty::TraitPredicate {
Expand Down
Loading

0 comments on commit ed97f24

Please sign in to comment.