Skip to content

Commit ec7d364

Browse files
Rollup merge of rust-lang#113144 - compiler-errors:elaborate-clauses, r=oli-obk
Make the `Elaboratable` trait take clauses We only ever elaborate clauses, so make this explicit in the trait's definition rather than having a bunch of `.expect_clause()` calls everywhere.
2 parents 860bb9e + aafc801 commit ec7d364

File tree

1 file changed

+42
-40
lines changed
  • compiler/rustc_infer/src/traits

1 file changed

+42
-40
lines changed

compiler/rustc_infer/src/traits/util.rs

+42-40
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ pub struct Elaborator<'tcx, O> {
8080
pub trait Elaboratable<'tcx> {
8181
fn predicate(&self) -> ty::Predicate<'tcx>;
8282

83-
// Makes a new `Self` but with a different predicate.
84-
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self;
83+
// Makes a new `Self` but with a different clause that comes from elaboration.
84+
fn child(&self, clause: ty::Clause<'tcx>) -> Self;
8585

86-
// Makes a new `Self` but with a different predicate and a different cause
87-
// code (if `Self` has one).
86+
// Makes a new `Self` but with a different clause and a different cause
87+
// code (if `Self` has one, such as [`PredicateObligation`]).
8888
fn child_with_derived_cause(
8989
&self,
90-
predicate: ty::Predicate<'tcx>,
90+
clause: ty::Clause<'tcx>,
9191
span: Span,
9292
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
9393
index: usize,
@@ -99,18 +99,18 @@ impl<'tcx> Elaboratable<'tcx> for PredicateObligation<'tcx> {
9999
self.predicate
100100
}
101101

102-
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
102+
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
103103
Obligation {
104104
cause: self.cause.clone(),
105105
param_env: self.param_env,
106106
recursion_depth: 0,
107-
predicate,
107+
predicate: clause.as_predicate(),
108108
}
109109
}
110110

111111
fn child_with_derived_cause(
112112
&self,
113-
predicate: ty::Predicate<'tcx>,
113+
clause: ty::Clause<'tcx>,
114114
span: Span,
115115
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
116116
index: usize,
@@ -123,7 +123,12 @@ impl<'tcx> Elaboratable<'tcx> for PredicateObligation<'tcx> {
123123
span,
124124
}))
125125
});
126-
Obligation { cause, param_env: self.param_env, recursion_depth: 0, predicate }
126+
Obligation {
127+
cause,
128+
param_env: self.param_env,
129+
recursion_depth: 0,
130+
predicate: clause.as_predicate(),
131+
}
127132
}
128133
}
129134

@@ -132,18 +137,18 @@ impl<'tcx> Elaboratable<'tcx> for ty::Predicate<'tcx> {
132137
*self
133138
}
134139

135-
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
136-
predicate
140+
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
141+
clause.as_predicate()
137142
}
138143

139144
fn child_with_derived_cause(
140145
&self,
141-
predicate: ty::Predicate<'tcx>,
146+
clause: ty::Clause<'tcx>,
142147
_span: Span,
143148
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
144149
_index: usize,
145150
) -> Self {
146-
predicate
151+
clause.as_predicate()
147152
}
148153
}
149154

@@ -152,18 +157,18 @@ impl<'tcx> Elaboratable<'tcx> for (ty::Predicate<'tcx>, Span) {
152157
self.0
153158
}
154159

155-
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
156-
(predicate, self.1)
160+
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
161+
(clause.as_predicate(), self.1)
157162
}
158163

159164
fn child_with_derived_cause(
160165
&self,
161-
predicate: ty::Predicate<'tcx>,
166+
clause: ty::Clause<'tcx>,
162167
_span: Span,
163168
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
164169
_index: usize,
165170
) -> Self {
166-
(predicate, self.1)
171+
(clause.as_predicate(), self.1)
167172
}
168173
}
169174

@@ -172,18 +177,18 @@ impl<'tcx> Elaboratable<'tcx> for (ty::Clause<'tcx>, Span) {
172177
self.0.as_predicate()
173178
}
174179

175-
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
176-
(predicate.expect_clause(), self.1)
180+
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
181+
(clause, self.1)
177182
}
178183

179184
fn child_with_derived_cause(
180185
&self,
181-
predicate: ty::Predicate<'tcx>,
186+
clause: ty::Clause<'tcx>,
182187
_span: Span,
183188
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
184189
_index: usize,
185190
) -> Self {
186-
(predicate.expect_clause(), self.1)
191+
(clause, self.1)
187192
}
188193
}
189194

@@ -192,18 +197,18 @@ impl<'tcx> Elaboratable<'tcx> for ty::Clause<'tcx> {
192197
self.as_predicate()
193198
}
194199

195-
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
196-
predicate.expect_clause()
200+
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
201+
clause
197202
}
198203

199204
fn child_with_derived_cause(
200205
&self,
201-
predicate: ty::Predicate<'tcx>,
206+
clause: ty::Clause<'tcx>,
202207
_span: Span,
203208
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
204209
_index: usize,
205210
) -> Self {
206-
predicate.expect_clause()
211+
clause
207212
}
208213
}
209214

@@ -252,14 +257,13 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
252257
};
253258

254259
let obligations =
255-
predicates.predicates.iter().enumerate().map(|(index, &(mut pred, span))| {
260+
predicates.predicates.iter().enumerate().map(|(index, &(mut clause, span))| {
256261
// when parent predicate is non-const, elaborate it to non-const predicates.
257262
if data.constness == ty::BoundConstness::NotConst {
258-
pred = pred.without_const(tcx);
263+
clause = clause.without_const(tcx);
259264
}
260265
elaboratable.child_with_derived_cause(
261-
pred.subst_supertrait(tcx, &bound_predicate.rebind(data.trait_ref))
262-
.as_predicate(),
266+
clause.subst_supertrait(tcx, &bound_predicate.rebind(data.trait_ref)),
263267
span,
264268
bound_predicate.rebind(data),
265269
index,
@@ -333,26 +337,25 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
333337
if r.is_late_bound() {
334338
None
335339
} else {
336-
Some(ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
337-
ty::OutlivesPredicate(r, r_min),
340+
Some(ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(
341+
r, r_min,
338342
)))
339343
}
340344
}
341345

342346
Component::Param(p) => {
343347
let ty = tcx.mk_ty_param(p.index, p.name);
344-
Some(ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
345-
ty::OutlivesPredicate(ty, r_min),
346-
)))
348+
Some(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, r_min)))
347349
}
348350

349351
Component::UnresolvedInferenceVariable(_) => None,
350352

351353
Component::Alias(alias_ty) => {
352354
// We might end up here if we have `Foo<<Bar as Baz>::Assoc>: 'a`.
353355
// With this, we can deduce that `<Bar as Baz>::Assoc: 'a`.
354-
Some(ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
355-
ty::OutlivesPredicate(alias_ty.to_ty(tcx), r_min),
356+
Some(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
357+
alias_ty.to_ty(tcx),
358+
r_min,
356359
)))
357360
}
358361

@@ -362,10 +365,9 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
362365
None
363366
}
364367
})
365-
.map(|predicate_kind| {
366-
bound_predicate.rebind(predicate_kind).to_predicate(tcx)
367-
})
368-
.map(|predicate| elaboratable.child(predicate)),
368+
.map(|clause| {
369+
elaboratable.child(bound_predicate.rebind(clause).to_predicate(tcx))
370+
}),
369371
);
370372
}
371373
ty::PredicateKind::Clause(ty::ClauseKind::TypeWellFormedFromEnv(..)) => {

0 commit comments

Comments
 (0)