Skip to content

Commit a4f6770

Browse files
committed
a small wf and clause cleanup
1 parent c1f86f0 commit a4f6770

File tree

8 files changed

+58
-64
lines changed

8 files changed

+58
-64
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_infer::traits::util;
1616
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1717
use rustc_middle::ty::fold::BottomUpFolder;
1818
use rustc_middle::ty::util::ExplicitSelf;
19+
use rustc_middle::ty::ToPredicate;
1920
use rustc_middle::ty::{
2021
self, GenericArgs, Ty, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
2122
};
@@ -2279,16 +2280,16 @@ pub(super) fn check_type_bounds<'tcx>(
22792280
//
22802281
// impl<T> X for T where T: X { type Y = <T as X>::Y; }
22812282
}
2282-
_ => predicates.push(ty::Clause::from_projection_clause(
2283-
tcx,
2283+
_ => predicates.push(
22842284
ty::Binder::bind_with_vars(
22852285
ty::ProjectionPredicate {
22862286
projection_ty: tcx.mk_alias_ty(trait_ty.def_id, rebased_args),
22872287
term: normalize_impl_ty.into(),
22882288
},
22892289
bound_vars,
2290-
),
2291-
)),
2290+
)
2291+
.to_predicate(tcx),
2292+
),
22922293
};
22932294
ty::ParamEnv::new(tcx.mk_clauses(&predicates), Reveal::UserFacing)
22942295
};

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1105,11 +1105,11 @@ fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocIt
11051105
let wf_obligations =
11061106
bounds.instantiate_identity_iter_copied().flat_map(|(bound, bound_span)| {
11071107
let normalized_bound = wfcx.normalize(span, None, bound);
1108-
traits::wf::predicate_obligations(
1108+
traits::wf::clause_obligations(
11091109
wfcx.infcx,
11101110
wfcx.param_env,
11111111
wfcx.body_def_id,
1112-
normalized_bound.as_predicate(),
1112+
normalized_bound,
11131113
bound_span,
11141114
)
11151115
});
@@ -1209,7 +1209,7 @@ fn check_impl<'tcx>(
12091209
wfcx.infcx,
12101210
wfcx.param_env,
12111211
wfcx.body_def_id,
1212-
&trait_pred,
1212+
trait_pred,
12131213
ast_trait_ref.path.span,
12141214
item,
12151215
);
@@ -1418,13 +1418,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14181418
debug!(?predicates.predicates);
14191419
assert_eq!(predicates.predicates.len(), predicates.spans.len());
14201420
let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| {
1421-
traits::wf::predicate_obligations(
1422-
infcx,
1423-
wfcx.param_env,
1424-
wfcx.body_def_id,
1425-
p.as_predicate(),
1426-
sp,
1427-
)
1421+
traits::wf::clause_obligations(infcx, wfcx.param_env, wfcx.body_def_id, p, sp)
14281422
});
14291423
let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect();
14301424
wfcx.register_obligations(obligations);

compiler/rustc_middle/src/ty/mod.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,6 @@ impl rustc_errors::IntoDiagnosticArg for Clause<'_> {
578578
pub struct Clause<'tcx>(Interned<'tcx, WithCachedTypeInfo<ty::Binder<'tcx, PredicateKind<'tcx>>>>);
579579

580580
impl<'tcx> Clause<'tcx> {
581-
pub fn from_projection_clause(tcx: TyCtxt<'tcx>, pred: PolyProjectionPredicate<'tcx>) -> Self {
582-
let pred: Predicate<'tcx> = pred.to_predicate(tcx);
583-
pred.expect_clause()
584-
}
585-
586581
pub fn as_predicate(self) -> Predicate<'tcx> {
587582
Predicate(self.0)
588583
}
@@ -1296,12 +1291,25 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
12961291
}
12971292
}
12981293

1294+
impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> {
1295+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1296+
PredicateKind::Clause(ClauseKind::Trait(self)).to_predicate(tcx)
1297+
}
1298+
}
1299+
12991300
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
13001301
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
13011302
self.map_bound(|p| PredicateKind::Clause(ClauseKind::Trait(p))).to_predicate(tcx)
13021303
}
13031304
}
13041305

1306+
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for TraitPredicate<'tcx> {
1307+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
1308+
let p: Predicate<'tcx> = self.to_predicate(tcx);
1309+
p.expect_clause()
1310+
}
1311+
}
1312+
13051313
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyTraitPredicate<'tcx> {
13061314
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
13071315
let p: Predicate<'tcx> = self.to_predicate(tcx);
@@ -1340,9 +1348,10 @@ impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for ProjectionPredicate<'tcx> {
13401348
}
13411349
}
13421350

1343-
impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> {
1344-
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1345-
PredicateKind::Clause(ClauseKind::Trait(self)).to_predicate(tcx)
1351+
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyProjectionPredicate<'tcx> {
1352+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
1353+
let p: Predicate<'tcx> = self.to_predicate(tcx);
1354+
p.expect_clause()
13461355
}
13471356
}
13481357

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'tcx> PolyExistentialPredicate<'tcx> {
725725
self.rebind(tr).with_self_ty(tcx, self_ty).to_predicate(tcx)
726726
}
727727
ExistentialPredicate::Projection(p) => {
728-
ty::Clause::from_projection_clause(tcx, self.rebind(p.with_self_ty(tcx, self_ty)))
728+
self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx)
729729
}
730730
ExistentialPredicate::AutoTrait(did) => {
731731
let generics = tcx.generics_of(did);

compiler/rustc_trait_selection/src/solve/project_goals.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
346346
ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output])
347347
});
348348

349-
let pred = ty::Clause::from_projection_clause(
350-
tcx,
351-
tupled_inputs_and_output.map_bound(|(inputs, output)| ty::ProjectionPredicate {
349+
let pred = tupled_inputs_and_output
350+
.map_bound(|(inputs, output)| ty::ProjectionPredicate {
352351
projection_ty: tcx
353352
.mk_alias_ty(goal.predicate.def_id(), [goal.predicate.self_ty(), inputs]),
354353
term: output.into(),
355-
}),
356-
);
354+
})
355+
.to_predicate(tcx);
357356

358357
// A built-in `Fn` impl only holds if the output is sized.
359358
// (FIXME: technically we only need to check this if the type is a fn ptr...)

compiler/rustc_trait_selection/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ fn assemble_candidates_from_object_ty<'cx, 'tcx>(
16441644
let env_predicates = data
16451645
.projection_bounds()
16461646
.filter(|bound| bound.item_def_id() == obligation.predicate.def_id)
1647-
.map(|p| ty::Clause::from_projection_clause(tcx, p.with_self_ty(tcx, object_ty)));
1647+
.map(|p| p.with_self_ty(tcx, object_ty).to_predicate(tcx));
16481648

16491649
assemble_candidates_from_predicates(
16501650
selcx,

compiler/rustc_trait_selection/src/traits/wf.rs

+22-30
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ pub fn unnormalized_obligations<'tcx>(
105105

106106
/// Returns the obligations that make this trait reference
107107
/// well-formed. For example, if there is a trait `Set` defined like
108-
/// `trait Set<K:Eq>`, then the trait reference `Foo: Set<Bar>` is WF
108+
/// `trait Set<K: Eq>`, then the trait bound `Foo: Set<Bar>` is WF
109109
/// if `Bar: Eq`.
110110
pub fn trait_obligations<'tcx>(
111111
infcx: &InferCtxt<'tcx>,
112112
param_env: ty::ParamEnv<'tcx>,
113113
body_id: LocalDefId,
114-
trait_pred: &ty::TraitPredicate<'tcx>,
114+
trait_pred: ty::TraitPredicate<'tcx>,
115115
span: Span,
116116
item: &'tcx hir::Item<'tcx>,
117117
) -> Vec<traits::PredicateObligation<'tcx>> {
@@ -129,12 +129,17 @@ pub fn trait_obligations<'tcx>(
129129
wf.normalize(infcx)
130130
}
131131

132+
/// Returns the requirements for `clause` to be well-formed.
133+
///
134+
/// For example, if there is a trait `Set` defined like
135+
/// `trait Set<K: Eq>`, then the trait bound `Foo: Set<Bar>` is WF
136+
/// if `Bar: Eq`.
132137
#[instrument(skip(infcx), ret)]
133-
pub fn predicate_obligations<'tcx>(
138+
pub fn clause_obligations<'tcx>(
134139
infcx: &InferCtxt<'tcx>,
135140
param_env: ty::ParamEnv<'tcx>,
136141
body_id: LocalDefId,
137-
predicate: ty::Predicate<'tcx>,
142+
clause: ty::Clause<'tcx>,
138143
span: Span,
139144
) -> Vec<traits::PredicateObligation<'tcx>> {
140145
let mut wf = WfPredicates {
@@ -148,45 +153,32 @@ pub fn predicate_obligations<'tcx>(
148153
};
149154

150155
// It's ok to skip the binder here because wf code is prepared for it
151-
match predicate.kind().skip_binder() {
152-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(t)) => {
153-
wf.compute_trait_pred(&t, Elaborate::None);
156+
match clause.kind().skip_binder() {
157+
ty::ClauseKind::Trait(t) => {
158+
wf.compute_trait_pred(t, Elaborate::None);
154159
}
155-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..)) => {}
156-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
157-
ty,
158-
_reg,
159-
))) => {
160+
ty::ClauseKind::RegionOutlives(..) => {}
161+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => {
160162
wf.compute(ty.into());
161163
}
162-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(t)) => {
164+
ty::ClauseKind::Projection(t) => {
163165
wf.compute_projection(t.projection_ty);
164166
wf.compute(match t.term.unpack() {
165167
ty::TermKind::Ty(ty) => ty.into(),
166168
ty::TermKind::Const(c) => c.into(),
167169
})
168170
}
169-
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
171+
ty::ClauseKind::ConstArgHasType(ct, ty) => {
170172
wf.compute(ct.into());
171173
wf.compute(ty.into());
172174
}
173-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
175+
ty::ClauseKind::WellFormed(arg) => {
174176
wf.compute(arg);
175177
}
176178

177-
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
179+
ty::ClauseKind::ConstEvaluatable(ct) => {
178180
wf.compute(ct.into());
179181
}
180-
181-
ty::PredicateKind::ObjectSafe(_)
182-
| ty::PredicateKind::ClosureKind(..)
183-
| ty::PredicateKind::Subtype(..)
184-
| ty::PredicateKind::Coerce(..)
185-
| ty::PredicateKind::ConstEquate(..)
186-
| ty::PredicateKind::Ambiguous
187-
| ty::PredicateKind::AliasRelate(..) => {
188-
bug!("We should only wf check where clauses, unexpected predicate: {predicate:?}")
189-
}
190182
}
191183

192184
wf.normalize(infcx)
@@ -233,7 +225,7 @@ enum Elaborate {
233225

234226
fn extend_cause_with_original_assoc_item_obligation<'tcx>(
235227
tcx: TyCtxt<'tcx>,
236-
trait_ref: &ty::TraitRef<'tcx>,
228+
trait_ref: ty::TraitRef<'tcx>,
237229
item: Option<&hir::Item<'tcx>>,
238230
cause: &mut traits::ObligationCause<'tcx>,
239231
pred: ty::Predicate<'tcx>,
@@ -336,9 +328,9 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
336328
}
337329

338330
/// Pushes the obligations required for `trait_ref` to be WF into `self.out`.
339-
fn compute_trait_pred(&mut self, trait_pred: &ty::TraitPredicate<'tcx>, elaborate: Elaborate) {
331+
fn compute_trait_pred(&mut self, trait_pred: ty::TraitPredicate<'tcx>, elaborate: Elaborate) {
340332
let tcx = self.tcx();
341-
let trait_ref = &trait_pred.trait_ref;
333+
let trait_ref = trait_pred.trait_ref;
342334

343335
// Negative trait predicates don't require supertraits to hold, just
344336
// that their args are WF.
@@ -411,7 +403,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
411403

412404
// Compute the obligations that are required for `trait_ref` to be WF,
413405
// given that it is a *negative* trait predicate.
414-
fn compute_negative_trait_pred(&mut self, trait_ref: &ty::TraitRef<'tcx>) {
406+
fn compute_negative_trait_pred(&mut self, trait_ref: ty::TraitRef<'tcx>) {
415407
for arg in trait_ref.args {
416408
self.compute(arg);
417409
}

compiler/rustc_ty_utils/src/ty.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ use rustc_hir as hir;
33
use rustc_hir::def::DefKind;
44
use rustc_index::bit_set::BitSet;
55
use rustc_middle::query::Providers;
6-
use rustc_middle::ty::{
7-
self, EarlyBinder, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
8-
};
6+
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt, TypeVisitor};
7+
use rustc_middle::ty::{ToPredicate, TypeSuperVisitable, TypeVisitable};
98
use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
109
use rustc_span::DUMMY_SP;
1110
use rustc_trait_selection::traits;
@@ -214,10 +213,10 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
214213
// strategy, then just reinterpret the associated type like an opaque :^)
215214
let default_ty = self.tcx.type_of(shifted_alias_ty.def_id).instantiate(self.tcx, shifted_alias_ty.args);
216215

217-
self.predicates.push(ty::Clause::from_projection_clause(self.tcx, ty::Binder::bind_with_vars(
216+
self.predicates.push(ty::Binder::bind_with_vars(
218217
ty::ProjectionPredicate { projection_ty: shifted_alias_ty, term: default_ty.into() },
219218
self.bound_vars,
220-
)));
219+
).to_predicate(self.tcx));
221220

222221
// We walk the *un-shifted* alias ty, because we're tracking the de bruijn
223222
// binder depth, and if we were to walk `shifted_alias_ty` instead, we'd

0 commit comments

Comments
 (0)