Skip to content

Commit ad09210

Browse files
authored
Unrolled build for rust-lang#116261
Rollup merge of rust-lang#116261 - lcnr:wf-only-clause, r=davidtwco a small wf and clause cleanup - remove `Clause::from_projection_clause`, instead use `ToPredicate` - change `predicate_obligations` to directly take a `Clause` - remove some unnecessary `&` - use clause in `min_specialization` checks where easily applicable
2 parents e3c631b + aac29a0 commit ad09210

File tree

9 files changed

+81
-101
lines changed

9 files changed

+81
-101
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
@@ -1130,11 +1130,11 @@ fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocIt
11301130
let wf_obligations =
11311131
bounds.instantiate_identity_iter_copied().flat_map(|(bound, bound_span)| {
11321132
let normalized_bound = wfcx.normalize(span, None, bound);
1133-
traits::wf::predicate_obligations(
1133+
traits::wf::clause_obligations(
11341134
wfcx.infcx,
11351135
wfcx.param_env,
11361136
wfcx.body_def_id,
1137-
normalized_bound.as_predicate(),
1137+
normalized_bound,
11381138
bound_span,
11391139
)
11401140
});
@@ -1234,7 +1234,7 @@ fn check_impl<'tcx>(
12341234
wfcx.infcx,
12351235
wfcx.param_env,
12361236
wfcx.body_def_id,
1237-
&trait_pred,
1237+
trait_pred,
12381238
ast_trait_ref.path.span,
12391239
item,
12401240
);
@@ -1443,13 +1443,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14431443
debug!(?predicates.predicates);
14441444
assert_eq!(predicates.predicates.len(), predicates.spans.len());
14451445
let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| {
1446-
traits::wf::predicate_obligations(
1447-
infcx,
1448-
wfcx.param_env,
1449-
wfcx.body_def_id,
1450-
p.as_predicate(),
1451-
sp,
1452-
)
1446+
traits::wf::clause_obligations(infcx, wfcx.param_env, wfcx.body_def_id, p, sp)
14531447
});
14541448
let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect();
14551449
wfcx.register_obligations(obligations);

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+23-37
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ fn check_predicates<'tcx>(
376376
let always_applicable_traits = impl1_predicates
377377
.iter()
378378
.copied()
379-
.filter(|(clause, _span)| {
379+
.filter(|&(clause, _span)| {
380380
matches!(
381-
trait_predicate_kind(tcx, clause.as_predicate()),
381+
trait_specialization_kind(tcx, clause),
382382
Some(TraitSpecializationKind::AlwaysApplicable)
383383
)
384384
})
@@ -402,7 +402,7 @@ fn check_predicates<'tcx>(
402402
.iter()
403403
.any(|pred2| trait_predicates_eq(tcx, clause.as_predicate(), *pred2, span))
404404
{
405-
check_specialization_on(tcx, clause.as_predicate(), span)
405+
check_specialization_on(tcx, clause, span)
406406
}
407407
}
408408
}
@@ -441,19 +441,16 @@ fn trait_predicates_eq<'tcx>(
441441
}
442442

443443
#[instrument(level = "debug", skip(tcx))]
444-
fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, span: Span) {
445-
match predicate.kind().skip_binder() {
444+
fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, clause: ty::Clause<'tcx>, span: Span) {
445+
match clause.kind().skip_binder() {
446446
// Global predicates are either always true or always false, so we
447447
// are fine to specialize on.
448-
_ if predicate.is_global() => (),
448+
_ if clause.is_global() => (),
449449
// We allow specializing on explicitly marked traits with no associated
450450
// items.
451-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
452-
trait_ref,
453-
polarity: _,
454-
})) => {
451+
ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity: _ }) => {
455452
if !matches!(
456-
trait_predicate_kind(tcx, predicate),
453+
trait_specialization_kind(tcx, clause),
457454
Some(TraitSpecializationKind::Marker)
458455
) {
459456
tcx.sess
@@ -467,18 +464,15 @@ fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
467464
.emit();
468465
}
469466
}
470-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(ty::ProjectionPredicate {
471-
projection_ty,
472-
term,
473-
})) => {
467+
ty::ClauseKind::Projection(ty::ProjectionPredicate { projection_ty, term }) => {
474468
tcx.sess
475469
.struct_span_err(
476470
span,
477471
format!("cannot specialize on associated type `{projection_ty} == {term}`",),
478472
)
479473
.emit();
480474
}
481-
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..)) => {
475+
ty::ClauseKind::ConstArgHasType(..) => {
482476
// FIXME(min_specialization), FIXME(const_generics):
483477
// It probably isn't right to allow _every_ `ConstArgHasType` but I am somewhat unsure
484478
// about the actual rules that would be sound. Can't just always error here because otherwise
@@ -490,33 +484,25 @@ fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
490484
}
491485
_ => {
492486
tcx.sess
493-
.struct_span_err(span, format!("cannot specialize on predicate `{predicate}`"))
487+
.struct_span_err(span, format!("cannot specialize on predicate `{clause}`"))
494488
.emit();
495489
}
496490
}
497491
}
498492

499-
fn trait_predicate_kind<'tcx>(
493+
fn trait_specialization_kind<'tcx>(
500494
tcx: TyCtxt<'tcx>,
501-
predicate: ty::Predicate<'tcx>,
495+
clause: ty::Clause<'tcx>,
502496
) -> Option<TraitSpecializationKind> {
503-
match predicate.kind().skip_binder() {
504-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
505-
trait_ref,
506-
polarity: _,
507-
})) => Some(tcx.trait_def(trait_ref.def_id).specialization_kind),
508-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(_))
509-
| ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(_))
510-
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(_))
511-
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
512-
| ty::PredicateKind::AliasRelate(..)
513-
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_))
514-
| ty::PredicateKind::Subtype(_)
515-
| ty::PredicateKind::Coerce(_)
516-
| ty::PredicateKind::ObjectSafe(_)
517-
| ty::PredicateKind::ClosureKind(..)
518-
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
519-
| ty::PredicateKind::ConstEquate(..)
520-
| ty::PredicateKind::Ambiguous => None,
497+
match clause.kind().skip_binder() {
498+
ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity: _ }) => {
499+
Some(tcx.trait_def(trait_ref.def_id).specialization_kind)
500+
}
501+
ty::ClauseKind::RegionOutlives(_)
502+
| ty::ClauseKind::TypeOutlives(_)
503+
| ty::ClauseKind::Projection(_)
504+
| ty::ClauseKind::ConstArgHasType(..)
505+
| ty::ClauseKind::WellFormed(_)
506+
| ty::ClauseKind::ConstEvaluatable(..) => None,
521507
}
522508
}

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
}

0 commit comments

Comments
 (0)