Skip to content

Commit 3be7786

Browse files
committed
Avoid storing the ImplPolarity and Constness next to a TraitRef and use TraitPredicate instead
1 parent 63ce5c1 commit 3be7786

File tree

11 files changed

+61
-54
lines changed

11 files changed

+61
-54
lines changed

compiler/rustc_infer/src/traits/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ pub fn transitive_bounds_that_define_assoc_type<'tcx>(
311311
));
312312
for (super_predicate, _) in super_predicates.predicates {
313313
let subst_predicate = super_predicate.subst_supertrait(tcx, &trait_ref);
314-
if let Some(binder) = subst_predicate.to_opt_poly_trait_ref() {
315-
stack.push(binder.value);
314+
if let Some(binder) = subst_predicate.to_opt_poly_trait_pred() {
315+
stack.push(binder.map_bound(|t| t.trait_ref));
316316
}
317317
}
318318

@@ -345,8 +345,8 @@ impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToT
345345

346346
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
347347
while let Some(obligation) = self.base_iterator.next() {
348-
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
349-
return Some(data.value);
348+
if let Some(data) = obligation.predicate.to_opt_poly_trait_pred() {
349+
return Some(data.map_bound(|t| t.trait_ref));
350350
}
351351
}
352352
None

compiler/rustc_middle/src/traits/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub enum SelectionCandidate<'tcx> {
101101
/// `false` if there are no *further* obligations.
102102
has_nested: bool,
103103
},
104-
ParamCandidate((ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, ty::ImplPolarity)),
104+
ParamCandidate(ty::PolyTraitPredicate<'tcx>),
105105
ImplCandidate(DefId),
106106
AutoImplCandidate(DefId),
107107

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,12 +880,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
880880
}
881881

882882
impl<'tcx> Predicate<'tcx> {
883-
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
883+
pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> {
884884
let predicate = self.kind();
885885
match predicate.skip_binder() {
886-
PredicateKind::Trait(t) => {
887-
Some(ConstnessAnd { constness: t.constness, value: predicate.rebind(t.trait_ref) })
888-
}
886+
PredicateKind::Trait(t) => Some(predicate.rebind(t)),
889887
PredicateKind::Projection(..)
890888
| PredicateKind::Subtype(..)
891889
| PredicateKind::Coerce(..)

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,14 +572,17 @@ fn prepare_vtable_segments<'tcx, T>(
572572
.predicates
573573
.into_iter()
574574
.filter_map(move |(pred, _)| {
575-
pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_ref()
575+
pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_pred()
576576
});
577577

578578
'diving_in_skip_visited_traits: loop {
579579
if let Some(next_super_trait) = direct_super_traits_iter.next() {
580580
if visited.insert(next_super_trait.to_predicate(tcx)) {
581+
// We're throwing away potential constness of super traits here.
582+
// FIXME: handle ~const super traits
583+
let next_super_trait = next_super_trait.map_bound(|t| t.trait_ref);
581584
stack.push((
582-
next_super_trait.value,
585+
next_super_trait,
583586
emit_vptr_on_new_entry,
584587
Some(direct_super_traits_iter),
585588
));
@@ -611,7 +614,11 @@ fn prepare_vtable_segments<'tcx, T>(
611614
if let Some(siblings) = siblings_opt {
612615
if let Some(next_inner_most_trait_ref) = siblings.next() {
613616
if visited.insert(next_inner_most_trait_ref.to_predicate(tcx)) {
614-
*inner_most_trait_ref = next_inner_most_trait_ref.value;
617+
// We're throwing away potential constness of super traits here.
618+
// FIXME: handle ~const super traits
619+
let next_inner_most_trait_ref =
620+
next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
621+
*inner_most_trait_ref = next_inner_most_trait_ref;
615622
*emit_vptr = emit_vptr_on_new_entry;
616623
break 'exiting_out;
617624
} else {

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,17 +383,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
383383
.param_env
384384
.caller_bounds()
385385
.iter()
386-
.filter_map(|o| o.to_opt_poly_trait_ref());
386+
.filter_map(|o| o.to_opt_poly_trait_pred());
387387

388388
// Micro-optimization: filter out predicates relating to different traits.
389389
let matching_bounds =
390-
all_bounds.filter(|p| p.value.def_id() == stack.obligation.predicate.def_id());
390+
all_bounds.filter(|p| p.def_id() == stack.obligation.predicate.def_id());
391391

392392
// Keep only those bounds which may apply, and propagate overflow if it occurs.
393393
for bound in matching_bounds {
394-
let wc = self.evaluate_where_clause(stack, bound.value)?;
394+
// FIXME(oli-obk): it is suspicious that we are dropping the constness and
395+
// polarity here.
396+
let wc = self.evaluate_where_clause(stack, bound.map_bound(|t| t.trait_ref))?;
395397
if wc.may_apply() {
396-
candidates.vec.push(ParamCandidate((bound, stack.obligation.polarity())));
398+
candidates.vec.push(ParamCandidate(bound));
397399
}
398400
}
399401

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
5858
}
5959

6060
ParamCandidate(param) => {
61-
let obligations = self.confirm_param_candidate(obligation, param.0.value);
62-
Ok(ImplSource::Param(obligations, param.0.constness))
61+
let obligations =
62+
self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
63+
Ok(ImplSource::Param(obligations, param.skip_binder().constness))
6364
}
6465

6566
ImplCandidate(impl_def_id) => {
@@ -139,7 +140,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
139140

140141
let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
141142
let placeholder_trait_predicate =
142-
self.infcx().replace_bound_vars_with_placeholders(trait_predicate);
143+
self.infcx().replace_bound_vars_with_placeholders(trait_predicate).trait_ref;
143144
let placeholder_self_ty = placeholder_trait_predicate.self_ty();
144145
let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate);
145146
let (def_id, substs) = match *placeholder_self_ty.kind() {
@@ -150,8 +151,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
150151

151152
let candidate_predicate = tcx.item_bounds(def_id)[idx].subst(tcx, substs);
152153
let candidate = candidate_predicate
153-
.to_opt_poly_trait_ref()
154-
.expect("projection candidate is not a trait predicate");
154+
.to_opt_poly_trait_pred()
155+
.expect("projection candidate is not a trait predicate")
156+
.map_bound(|t| t.trait_ref);
155157
let mut obligations = Vec::new();
156158
let candidate = normalize_with_depth_to(
157159
self,
@@ -165,7 +167,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
165167
obligations.extend(self.infcx.commit_if_ok(|_| {
166168
self.infcx
167169
.at(&obligation.cause, obligation.param_env)
168-
.sup(placeholder_trait_predicate.to_poly_trait_ref(), candidate.value)
170+
.sup(placeholder_trait_predicate, candidate)
169171
.map(|InferOk { obligations, .. }| obligations)
170172
.map_err(|_| Unimplemented)
171173
})?);

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,10 +1089,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10891089
ImplCandidate(def_id)
10901090
if tcx.impl_constness(def_id) == hir::Constness::Const => {}
10911091
// const param
1092-
ParamCandidate((
1093-
ty::ConstnessAnd { constness: ty::BoundConstness::ConstIfConst, .. },
1094-
_,
1095-
)) => {}
1092+
ParamCandidate(trait_pred)
1093+
if trait_pred.skip_binder().constness
1094+
== ty::BoundConstness::ConstIfConst => {}
10961095
// auto trait impl
10971096
AutoImplCandidate(..) => {}
10981097
// generator, this will raise error in other places
@@ -1474,8 +1473,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14741473
// Check if a bound would previously have been removed when normalizing
14751474
// the param_env so that it can be given the lowest priority. See
14761475
// #50825 for the motivation for this.
1477-
let is_global =
1478-
|cand: &ty::PolyTraitRef<'_>| cand.is_known_global() && !cand.has_late_bound_regions();
1476+
let is_global = |cand: &ty::PolyTraitPredicate<'_>| {
1477+
cand.is_known_global() && !cand.has_late_bound_regions()
1478+
};
14791479

14801480
// (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
14811481
// and `DiscriminantKindCandidate` to anything else.
@@ -1506,25 +1506,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15061506
| ConstDropCandidate,
15071507
) => false,
15081508

1509-
(
1510-
ParamCandidate((other, other_polarity)),
1511-
ParamCandidate((victim, victim_polarity)),
1512-
) => {
1513-
let same_except_bound_vars = other.value.skip_binder()
1514-
== victim.value.skip_binder()
1515-
&& other.constness == victim.constness
1516-
&& other_polarity == victim_polarity
1517-
&& !other.value.skip_binder().has_escaping_bound_vars();
1509+
(ParamCandidate(other), ParamCandidate(victim)) => {
1510+
let same_except_bound_vars = other.skip_binder().trait_ref
1511+
== victim.skip_binder().trait_ref
1512+
&& other.skip_binder().constness == victim.skip_binder().constness
1513+
&& other.skip_binder().polarity == victim.skip_binder().polarity
1514+
&& !other.skip_binder().trait_ref.has_escaping_bound_vars();
15181515
if same_except_bound_vars {
15191516
// See issue #84398. In short, we can generate multiple ParamCandidates which are
15201517
// the same except for unused bound vars. Just pick the one with the fewest bound vars
15211518
// or the current one if tied (they should both evaluate to the same answer). This is
15221519
// probably best characterized as a "hack", since we might prefer to just do our
15231520
// best to *not* create essentially duplicate candidates in the first place.
1524-
other.value.bound_vars().len() <= victim.value.bound_vars().len()
1525-
} else if other.value == victim.value
1526-
&& victim.constness == ty::BoundConstness::NotConst
1527-
&& other_polarity == victim_polarity
1521+
other.bound_vars().len() <= victim.bound_vars().len()
1522+
} else if other.skip_binder().trait_ref == victim.skip_binder().trait_ref
1523+
&& victim.skip_binder().constness == ty::BoundConstness::NotConst
1524+
&& other.skip_binder().polarity == victim.skip_binder().polarity
15281525
{
15291526
// Drop otherwise equivalent non-const candidates in favor of const candidates.
15301527
true
@@ -1554,11 +1551,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15541551
| TraitAliasCandidate(..)
15551552
| ObjectCandidate(_)
15561553
| ProjectionCandidate(_),
1557-
) => !is_global(&cand.0.value),
1554+
) => !is_global(cand),
15581555
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
15591556
// Prefer these to a global where-clause bound
15601557
// (see issue #50825).
1561-
is_global(&cand.0.value)
1558+
is_global(cand)
15621559
}
15631560
(
15641561
ImplCandidate(_)
@@ -1574,7 +1571,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15741571
) => {
15751572
// Prefer these to a global where-clause bound
15761573
// (see issue #50825).
1577-
is_global(&cand.0.value) && other.evaluation.must_apply_modulo_regions()
1574+
is_global(cand) && other.evaluation.must_apply_modulo_regions()
15781575
}
15791576

15801577
(ProjectionCandidate(i), ProjectionCandidate(j))

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,9 @@ crate fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<St
500500
Vec::with_capacity(predicates.len() + types_without_default_bounds.len());
501501

502502
for (p, _) in predicates {
503-
if let Some(poly_trait_ref) = p.to_opt_poly_trait_ref() {
504-
if Some(poly_trait_ref.value.def_id()) == sized_trait {
505-
types_without_default_bounds.remove(poly_trait_ref.value.self_ty().skip_binder());
503+
if let Some(poly_trait_ref) = p.to_opt_poly_trait_pred() {
504+
if Some(poly_trait_ref.def_id()) == sized_trait {
505+
types_without_default_bounds.remove(poly_trait_ref.self_ty().skip_binder());
506506
continue;
507507
}
508508
}

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ impl<'tcx> TraitAliasExpander<'tcx> {
124124

125125
let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| {
126126
pred.subst_supertrait(tcx, &trait_ref)
127-
.to_opt_poly_trait_ref()
128-
.map(|trait_ref| item.clone_and_push(trait_ref.value, *span))
127+
.to_opt_poly_trait_pred()
128+
.map(|trait_ref| item.clone_and_push(trait_ref.map_bound(|t| t.trait_ref), *span))
129129
});
130130
debug!("expand_trait_aliases: items={:?}", items.clone());
131131

@@ -181,8 +181,8 @@ impl Iterator for SupertraitDefIds<'tcx> {
181181
predicates
182182
.predicates
183183
.iter()
184-
.filter_map(|(pred, _)| pred.to_opt_poly_trait_ref())
185-
.map(|trait_ref| trait_ref.value.def_id())
184+
.filter_map(|(pred, _)| pred.to_opt_poly_trait_pred())
185+
.map(|trait_ref| trait_ref.def_id())
186186
.filter(|&super_def_id| visited.insert(super_def_id)),
187187
);
188188
Some(def_id)

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,10 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
298298

299299
let extend = |obligation: traits::PredicateObligation<'tcx>| {
300300
let mut cause = cause.clone();
301-
if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_ref() {
301+
if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_pred() {
302302
let derived_cause = traits::DerivedObligationCause {
303-
parent_trait_ref: parent_trait_ref.value,
303+
// TODO: sus
304+
parent_trait_ref: parent_trait_ref.map_bound(|t| t.trait_ref),
304305
parent_code: Lrc::new(obligation.cause.code.clone()),
305306
};
306307
cause.make_mut().code =

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15881588
traits::transitive_bounds_that_define_assoc_type(
15891589
tcx,
15901590
predicates.iter().filter_map(|(p, _)| {
1591-
p.to_opt_poly_trait_ref().map(|trait_ref| trait_ref.value)
1591+
Some(p.to_opt_poly_trait_pred()?.map_bound(|t| t.trait_ref))
15921592
}),
15931593
assoc_name,
15941594
)

0 commit comments

Comments
 (0)