Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a848c4b

Browse files
oli-obkfee1-dead
authored andcommitted
Avoid storing the ImplPolarity and Constness next to a TraitRef and use TraitPredicate instead
1 parent d161cc2 commit a848c4b

File tree

11 files changed

+59
-53
lines changed

11 files changed

+59
-53
lines changed

compiler/rustc_infer/src/traits/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ pub fn transitive_bounds_that_define_assoc_type<'tcx>(
328328
));
329329
for (super_predicate, _) in super_predicates.predicates {
330330
let subst_predicate = super_predicate.subst_supertrait(tcx, &trait_ref);
331-
if let Some(binder) = subst_predicate.to_opt_poly_trait_ref() {
332-
stack.push(binder.value);
331+
if let Some(binder) = subst_predicate.to_opt_poly_trait_pred() {
332+
stack.push(binder.map_bound(|t| t.trait_ref));
333333
}
334334
}
335335

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

363363
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
364364
while let Some(obligation) = self.base_iterator.next() {
365-
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
366-
return Some(data.value);
365+
if let Some(data) = obligation.predicate.to_opt_poly_trait_pred() {
366+
return Some(data.map_bound(|t| t.trait_ref));
367367
}
368368
}
369369
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
@@ -885,12 +885,10 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
885885
}
886886

887887
impl<'tcx> Predicate<'tcx> {
888-
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
888+
pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> {
889889
let predicate = self.kind();
890890
match predicate.skip_binder() {
891-
PredicateKind::Trait(t) => {
892-
Some(ConstnessAnd { constness: t.constness, value: predicate.rebind(t.trait_ref) })
893-
}
891+
PredicateKind::Trait(t) => Some(predicate.rebind(t)),
894892
PredicateKind::Projection(..)
895893
| PredicateKind::Subtype(..)
896894
| PredicateKind::Coerce(..)

compiler/rustc_trait_selection/src/traits/mod.rs

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

580580
'diving_in_skip_visited_traits: loop {
581581
if let Some(next_super_trait) = direct_super_traits_iter.next() {
582582
if visited.insert(next_super_trait.to_predicate(tcx)) {
583+
// We're throwing away potential constness of super traits here.
584+
// FIXME: handle ~const super traits
585+
let next_super_trait = next_super_trait.map_bound(|t| t.trait_ref);
583586
stack.push((
584-
next_super_trait.value,
587+
next_super_trait,
585588
emit_vptr_on_new_entry,
586589
Some(direct_super_traits_iter),
587590
));
@@ -613,7 +616,11 @@ fn prepare_vtable_segments<'tcx, T>(
613616
if let Some(siblings) = siblings_opt {
614617
if let Some(next_inner_most_trait_ref) = siblings.next() {
615618
if visited.insert(next_inner_most_trait_ref.to_predicate(tcx)) {
616-
*inner_most_trait_ref = next_inner_most_trait_ref.value;
619+
// We're throwing away potential constness of super traits here.
620+
// FIXME: handle ~const super traits
621+
let next_inner_most_trait_ref =
622+
next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
623+
*inner_most_trait_ref = next_inner_most_trait_ref;
617624
*emit_vptr = emit_vptr_on_new_entry;
618625
break 'exiting_out;
619626
} 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: 17 additions & 21 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,7 +1473,7 @@ 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 = |cand: &ty::PolyTraitRef<'tcx>| {
1476+
let is_global = |cand: &ty::PolyTraitPredicate<'tcx>| {
14781477
cand.is_global(self.infcx.tcx) && !cand.has_late_bound_regions()
14791478
};
14801479

@@ -1507,25 +1506,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15071506
| ConstDropCandidate,
15081507
) => false,
15091508

1510-
(
1511-
ParamCandidate((other, other_polarity)),
1512-
ParamCandidate((victim, victim_polarity)),
1513-
) => {
1514-
let same_except_bound_vars = other.value.skip_binder()
1515-
== victim.value.skip_binder()
1516-
&& other.constness == victim.constness
1517-
&& other_polarity == victim_polarity
1518-
&& !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();
15191515
if same_except_bound_vars {
15201516
// See issue #84398. In short, we can generate multiple ParamCandidates which are
15211517
// the same except for unused bound vars. Just pick the one with the fewest bound vars
15221518
// or the current one if tied (they should both evaluate to the same answer). This is
15231519
// probably best characterized as a "hack", since we might prefer to just do our
15241520
// best to *not* create essentially duplicate candidates in the first place.
1525-
other.value.bound_vars().len() <= victim.value.bound_vars().len()
1526-
} else if other.value == victim.value
1527-
&& victim.constness == ty::BoundConstness::NotConst
1528-
&& 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
15291525
{
15301526
// Drop otherwise equivalent non-const candidates in favor of const candidates.
15311527
true
@@ -1555,11 +1551,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15551551
| TraitAliasCandidate(..)
15561552
| ObjectCandidate(_)
15571553
| ProjectionCandidate(_),
1558-
) => !is_global(&cand.0.value),
1554+
) => !is_global(cand),
15591555
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
15601556
// Prefer these to a global where-clause bound
15611557
// (see issue #50825).
1562-
is_global(&cand.0.value)
1558+
is_global(cand)
15631559
}
15641560
(
15651561
ImplCandidate(_)
@@ -1575,7 +1571,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15751571
) => {
15761572
// Prefer these to a global where-clause bound
15771573
// (see issue #50825).
1578-
is_global(&cand.0.value) && other.evaluation.must_apply_modulo_regions()
1574+
is_global(cand) && other.evaluation.must_apply_modulo_regions()
15791575
}
15801576

15811577
(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
@@ -508,9 +508,9 @@ crate fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<St
508508
Vec::with_capacity(predicates.len() + types_without_default_bounds.len());
509509

510510
for (p, _) in predicates {
511-
if let Some(poly_trait_ref) = p.to_opt_poly_trait_ref() {
512-
if Some(poly_trait_ref.value.def_id()) == sized_trait {
513-
types_without_default_bounds.remove(poly_trait_ref.value.self_ty().skip_binder());
511+
if let Some(poly_trait_ref) = p.to_opt_poly_trait_pred() {
512+
if Some(poly_trait_ref.def_id()) == sized_trait {
513+
types_without_default_bounds.remove(poly_trait_ref.self_ty().skip_binder());
514514
continue;
515515
}
516516
}

compiler/rustc_trait_selection/src/traits/util.rs

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

127127
let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| {
128128
pred.subst_supertrait(tcx, &trait_ref)
129-
.to_opt_poly_trait_ref()
130-
.map(|trait_ref| item.clone_and_push(trait_ref.value, *span))
129+
.to_opt_poly_trait_pred()
130+
.map(|trait_ref| item.clone_and_push(trait_ref.map_bound(|t| t.trait_ref), *span))
131131
});
132132
debug!("expand_trait_aliases: items={:?}", items.clone());
133133

@@ -183,8 +183,8 @@ impl Iterator for SupertraitDefIds<'tcx> {
183183
predicates
184184
.predicates
185185
.iter()
186-
.filter_map(|(pred, _)| pred.to_opt_poly_trait_ref())
187-
.map(|trait_ref| trait_ref.value.def_id())
186+
.filter_map(|(pred, _)| pred.to_opt_poly_trait_pred())
187+
.map(|trait_ref| trait_ref.def_id())
188188
.filter(|&super_def_id| visited.insert(super_def_id)),
189189
);
190190
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)