Skip to content

Commit 3436069

Browse files
authored
Rollup merge of #112734 - dswij:bounds-predicates-clause, r=compiler-errors
Make `Bound::predicates` use `Clause` Part of #107250 `Bound::predicates` returns an iterator over `Binder<_, Clause>` instead of `Predicate`. I tried updating `explicit_predicates_of` as well, but it seems that it needs a lot more change than I thought. Will do it in a separate PR instead.
2 parents a066e1b + f874345 commit 3436069

File tree

6 files changed

+57
-24
lines changed

6 files changed

+57
-24
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
3131
use rustc_infer::traits::ObligationCause;
3232
use rustc_middle::middle::stability::AllowUnstable;
3333
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef};
34+
use rustc_middle::ty::DynKind;
3435
use rustc_middle::ty::GenericParamDefKind;
36+
use rustc_middle::ty::ToPredicate;
3537
use rustc_middle::ty::{self, Const, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
36-
use rustc_middle::ty::{DynKind, ToPredicate};
3738
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
3839
use rustc_span::edit_distance::find_best_match_for_name;
3940
use rustc_span::symbol::{kw, Ident, Symbol};
@@ -944,7 +945,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
944945

945946
let mut trait_bounds = vec![];
946947
let mut projection_bounds = vec![];
947-
for (pred, span) in bounds.predicates() {
948+
for (clause, span) in bounds.predicates() {
949+
let pred: ty::Predicate<'tcx> = clause.to_predicate(tcx);
948950
let bound_pred = pred.kind();
949951
match bound_pred.skip_binder() {
950952
ty::PredicateKind::Clause(clause) => match clause {

compiler/rustc_hir_analysis/src/bounds.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! `ty` form from the HIR.
33
44
use rustc_hir::LangItem;
5+
use rustc_middle::ty::Binder;
56
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
67
use rustc_span::Span;
78

@@ -23,52 +24,58 @@ use rustc_span::Span;
2324
/// include the self type (e.g., `trait_bounds`) but in others we do not
2425
#[derive(Default, PartialEq, Eq, Clone, Debug)]
2526
pub struct Bounds<'tcx> {
26-
pub predicates: Vec<(ty::Predicate<'tcx>, Span)>,
27+
pub predicates: Vec<(Binder<'tcx, ty::Clause<'tcx>>, Span)>,
2728
}
2829

2930
impl<'tcx> Bounds<'tcx> {
3031
pub fn push_region_bound(
3132
&mut self,
32-
tcx: TyCtxt<'tcx>,
33+
_tcx: TyCtxt<'tcx>,
3334
region: ty::PolyTypeOutlivesPredicate<'tcx>,
3435
span: Span,
3536
) {
36-
self.predicates.push((region.to_predicate(tcx), span));
37+
self.predicates.push((region.map_bound(|p| ty::Clause::TypeOutlives(p)), span));
3738
}
3839

3940
pub fn push_trait_bound(
4041
&mut self,
41-
tcx: TyCtxt<'tcx>,
42+
_tcx: TyCtxt<'tcx>,
4243
trait_ref: ty::PolyTraitRef<'tcx>,
4344
span: Span,
4445
constness: ty::BoundConstness,
4546
polarity: ty::ImplPolarity,
4647
) {
4748
self.predicates.push((
48-
trait_ref
49-
.map_bound(|trait_ref| ty::TraitPredicate { trait_ref, constness, polarity })
50-
.to_predicate(tcx),
49+
trait_ref.map_bound(|trait_ref| {
50+
ty::Clause::Trait(ty::TraitPredicate { trait_ref, constness, polarity })
51+
}),
5152
span,
5253
));
5354
}
5455

5556
pub fn push_projection_bound(
5657
&mut self,
57-
tcx: TyCtxt<'tcx>,
58+
_tcx: TyCtxt<'tcx>,
5859
projection: ty::PolyProjectionPredicate<'tcx>,
5960
span: Span,
6061
) {
61-
self.predicates.push((projection.to_predicate(tcx), span));
62+
self.predicates.push((projection.map_bound(|proj| ty::Clause::Projection(proj)), span));
6263
}
6364

6465
pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) {
6566
let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span));
6667
let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [ty]);
6768
// Preferable to put this obligation first, since we report better errors for sized ambiguity.
68-
self.predicates.insert(0, (trait_ref.without_const().to_predicate(tcx), span));
69+
self.predicates.insert(
70+
0,
71+
(
72+
ty::Binder::dummy(ty::Clause::Trait(trait_ref.without_const().to_predicate(tcx))),
73+
span,
74+
),
75+
);
6976
}
7077

71-
pub fn predicates(&self) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> + '_ {
78+
pub fn predicates(&self) -> impl Iterator<Item = (Binder<'tcx, ty::Clause<'tcx>>, Span)> + '_ {
7279
self.predicates.iter().cloned()
7380
}
7481
}

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::astconv::{AstConv, OnlySelfBounds};
33
use rustc_hir as hir;
44
use rustc_infer::traits::util;
55
use rustc_middle::ty::subst::InternalSubsts;
6+
use rustc_middle::ty::ToPredicate;
67
use rustc_middle::ty::{self, Ty, TyCtxt};
78
use rustc_span::def_id::{DefId, LocalDefId};
89
use rustc_span::Span;
@@ -44,7 +45,12 @@ fn associated_type_bounds<'tcx>(
4445
}
4546
});
4647

47-
let all_bounds = tcx.arena.alloc_from_iter(bounds.predicates().chain(bounds_from_parent));
48+
let all_bounds = tcx.arena.alloc_from_iter(
49+
bounds
50+
.predicates()
51+
.map(|(clause, span)| (clause.to_predicate(tcx), span))
52+
.chain(bounds_from_parent),
53+
);
4854
debug!(
4955
"associated_type_bounds({}) = {:?}",
5056
tcx.def_path_str(assoc_item_def_id.to_def_id()),
@@ -72,7 +78,9 @@ fn opaque_type_bounds<'tcx>(
7278
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
7379
debug!(?bounds);
7480

75-
tcx.arena.alloc_from_iter(bounds.predicates())
81+
tcx.arena.alloc_from_iter(
82+
bounds.predicates().map(|(clause, span)| (clause.to_predicate(tcx), span)),
83+
)
7684
})
7785
}
7886

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
126126
predicates.extend(
127127
icx.astconv()
128128
.compute_bounds(tcx.types.self_param, self_bounds, OnlySelfBounds(false))
129-
.predicates(),
129+
.predicates()
130+
.map(|(clause, span)| (clause.to_predicate(tcx), span)),
130131
);
131132
}
132133

@@ -175,7 +176,9 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
175176
param.span,
176177
);
177178
trace!(?bounds);
178-
predicates.extend(bounds.predicates());
179+
predicates.extend(
180+
bounds.predicates().map(|(clause, span)| (clause.to_predicate(tcx), span)),
181+
);
179182
trace!(?predicates);
180183
}
181184
GenericParamKind::Const { .. } => {
@@ -234,7 +237,9 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
234237
bound_vars,
235238
OnlySelfBounds(false),
236239
);
237-
predicates.extend(bounds.predicates());
240+
predicates.extend(
241+
bounds.predicates().map(|(clause, span)| (clause.to_predicate(tcx), span)),
242+
);
238243
}
239244

240245
hir::WherePredicate::RegionPredicate(region_pred) => {
@@ -658,8 +663,12 @@ pub(super) fn implied_predicates_with_filter(
658663
};
659664

660665
// Combine the two lists to form the complete set of superbounds:
661-
let implied_bounds =
662-
&*tcx.arena.alloc_from_iter(superbounds.predicates().chain(where_bounds_that_match));
666+
let implied_bounds = &*tcx.arena.alloc_from_iter(
667+
superbounds
668+
.predicates()
669+
.map(|(clause, span)| (clause.to_predicate(tcx), span))
670+
.chain(where_bounds_that_match),
671+
);
663672
debug!(?implied_bounds);
664673

665674
// Now require that immediate supertraits are converted, which will, in
@@ -816,7 +825,7 @@ impl<'tcx> ItemCtxt<'tcx> {
816825
);
817826
}
818827

819-
bounds.predicates().collect()
828+
bounds.predicates().map(|(clause, span)| (clause.to_predicate(self.tcx), span)).collect()
820829
}
821830

822831
#[instrument(level = "trace", skip(self))]

compiler/rustc_middle/src/ty/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,13 @@ impl<'tcx> ToPredicate<'tcx> for Clause<'tcx> {
12141214
}
12151215
}
12161216

1217+
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, Clause<'tcx>> {
1218+
#[inline(always)]
1219+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1220+
tcx.mk_predicate(self.map_bound(|clause| ty::PredicateKind::Clause(clause)))
1221+
}
1222+
}
1223+
12171224
impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
12181225
#[inline(always)]
12191226
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {

compiler/rustc_privacy/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1270,13 +1270,13 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12701270
);
12711271

12721272
for (pred, _) in bounds.predicates() {
1273-
match pred.kind().skip_binder() {
1274-
ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => {
1273+
match pred.skip_binder() {
1274+
ty::Clause::Trait(trait_predicate) => {
12751275
if self.visit_trait(trait_predicate.trait_ref).is_break() {
12761276
return;
12771277
}
12781278
}
1279-
ty::PredicateKind::Clause(ty::Clause::Projection(proj_predicate)) => {
1279+
ty::Clause::Projection(proj_predicate) => {
12801280
let term = self.visit(proj_predicate.term);
12811281
if term.is_break()
12821282
|| self.visit_projection_ty(proj_predicate.projection_ty).is_break()

0 commit comments

Comments
 (0)