Skip to content

Commit b719f28

Browse files
committed
HIR analysis: Remove unnecessary abstraction over list of clauses
1 parent d8810e3 commit b719f28

File tree

8 files changed

+76
-174
lines changed

8 files changed

+76
-174
lines changed

compiler/rustc_hir_analysis/src/bounds.rs

-100
This file was deleted.

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use tracing::{debug, instrument};
1313

1414
use super::ItemCtxt;
1515
use super::predicates_of::assert_only_contains_predicates_from;
16-
use crate::bounds::Bounds;
1716
use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter};
1817

1918
/// For associated types we include both bounds written on the type
@@ -38,7 +37,7 @@ fn associated_type_bounds<'tcx>(
3837
);
3938

4039
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
41-
let mut bounds = Bounds::default();
40+
let mut bounds = Vec::new();
4241
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter);
4342
// Associated types are implicitly sized unless a `?Sized` bound is found
4443
match filter {
@@ -68,7 +67,7 @@ fn associated_type_bounds<'tcx>(
6867
)
6968
});
7069

71-
let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses().chain(bounds_from_parent));
70+
let all_bounds = tcx.arena.alloc_from_iter(bounds.into_iter().chain(bounds_from_parent));
7271
debug!(
7372
"associated_type_bounds({}) = {:?}",
7473
tcx.def_path_str(assoc_item_def_id.to_def_id()),
@@ -327,7 +326,7 @@ fn opaque_type_bounds<'tcx>(
327326
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
328327
ty::print::with_reduced_queries!({
329328
let icx = ItemCtxt::new(tcx, opaque_def_id);
330-
let mut bounds = Bounds::default();
329+
let mut bounds = Vec::new();
331330
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter);
332331
// Opaque types are implicitly sized unless a `?Sized` bound is found
333332
match filter {
@@ -343,7 +342,7 @@ fn opaque_type_bounds<'tcx>(
343342
}
344343
debug!(?bounds);
345344

346-
tcx.arena.alloc_from_iter(bounds.clauses())
345+
tcx.arena.alloc_slice(&bounds)
347346
})
348347
}
349348

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use rustc_span::{DUMMY_SP, Ident, Span};
1313
use tracing::{debug, instrument, trace};
1414

1515
use super::item_bounds::explicit_item_bounds_with_filter;
16-
use crate::bounds::Bounds;
1716
use crate::collect::ItemCtxt;
1817
use crate::constrained_generic_params as cgp;
1918
use crate::delegation::inherit_predicates_for_delegation_item;
@@ -178,15 +177,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
178177
// on a trait we must also consider the bounds that follow the trait's name,
179178
// like `trait Foo: A + B + C`.
180179
if let Some(self_bounds) = is_trait {
181-
let mut bounds = Bounds::default();
180+
let mut bounds = Vec::new();
182181
icx.lowerer().lower_bounds(
183182
tcx.types.self_param,
184183
self_bounds,
185184
&mut bounds,
186185
ty::List::empty(),
187186
PredicateFilter::All,
188187
);
189-
predicates.extend(bounds.clauses());
188+
predicates.extend(bounds);
190189
}
191190

192191
// In default impls, we can assume that the self type implements
@@ -209,7 +208,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
209208
GenericParamKind::Lifetime { .. } => (),
210209
GenericParamKind::Type { .. } => {
211210
let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
212-
let mut bounds = Bounds::default();
211+
let mut bounds = Vec::new();
213212
// Params are implicitly sized unless a `?Sized` bound is found
214213
icx.lowerer().add_sized_bound(
215214
&mut bounds,
@@ -219,7 +218,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
219218
param.span,
220219
);
221220
trace!(?bounds);
222-
predicates.extend(bounds.clauses());
221+
predicates.extend(bounds);
223222
trace!(?predicates);
224223
}
225224
hir::GenericParamKind::Const { .. } => {
@@ -264,15 +263,15 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
264263
}
265264
}
266265

267-
let mut bounds = Bounds::default();
266+
let mut bounds = Vec::new();
268267
icx.lowerer().lower_bounds(
269268
ty,
270269
bound_pred.bounds,
271270
&mut bounds,
272271
bound_vars,
273272
PredicateFilter::All,
274273
);
275-
predicates.extend(bounds.clauses());
274+
predicates.extend(bounds);
276275
}
277276

278277
hir::WherePredicateKind::RegionPredicate(region_pred) => {
@@ -627,15 +626,15 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
627626
let icx = ItemCtxt::new(tcx, trait_def_id);
628627

629628
let self_param_ty = tcx.types.self_param;
630-
let mut bounds = Bounds::default();
629+
let mut bounds = Vec::new();
631630
icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter);
632631

633632
let where_bounds_that_match =
634633
icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
635634

636635
// Combine the two lists to form the complete set of superbounds:
637636
let implied_bounds =
638-
&*tcx.arena.alloc_from_iter(bounds.clauses().chain(where_bounds_that_match));
637+
&*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
639638
debug!(?implied_bounds);
640639

641640
// Now require that immediate supertraits are lowered, which will, in
@@ -904,7 +903,7 @@ impl<'tcx> ItemCtxt<'tcx> {
904903
param_def_id: LocalDefId,
905904
filter: PredicateFilter,
906905
) -> Vec<(ty::Clause<'tcx>, Span)> {
907-
let mut bounds = Bounds::default();
906+
let mut bounds = Vec::new();
908907

909908
for predicate in hir_generics.predicates {
910909
let hir_id = predicate.hir_id;
@@ -938,7 +937,7 @@ impl<'tcx> ItemCtxt<'tcx> {
938937
);
939938
}
940939

941-
bounds.clauses().collect()
940+
bounds
942941
}
943942
}
944943

@@ -1007,7 +1006,7 @@ pub(super) fn const_conditions<'tcx>(
10071006
};
10081007

10091008
let icx = ItemCtxt::new(tcx, def_id);
1010-
let mut bounds = Bounds::default();
1009+
let mut bounds = Vec::new();
10111010

10121011
for pred in generics.predicates {
10131012
match pred.kind {
@@ -1027,12 +1026,12 @@ pub(super) fn const_conditions<'tcx>(
10271026
}
10281027

10291028
if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1030-
bounds.push_const_bound(
1031-
tcx,
1032-
ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id())),
1033-
ty::BoundConstness::Maybe,
1029+
// We've checked above that the trait is conditionally const.
1030+
bounds.push((
1031+
ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1032+
.to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
10341033
DUMMY_SP,
1035-
);
1034+
));
10361035

10371036
icx.lowerer().lower_bounds(
10381037
tcx.types.self_param,
@@ -1045,7 +1044,7 @@ pub(super) fn const_conditions<'tcx>(
10451044

10461045
ty::ConstConditions {
10471046
parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1048-
predicates: tcx.arena.alloc_from_iter(bounds.clauses().map(|(clause, span)| {
1047+
predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
10491048
(
10501049
clause.kind().map_bound(|clause| match clause {
10511050
ty::ClauseKind::HostEffect(ty::HostEffectPredicate {

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ use rustc_hir::HirId;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::{DefId, LocalDefId};
1010
use rustc_middle::bug;
11-
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TyCtxt};
11+
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TyCtxt, Upcast};
1212
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
1313
use rustc_trait_selection::traits;
1414
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
1515
use smallvec::SmallVec;
1616
use tracing::{debug, instrument};
1717

1818
use super::errors::GenericsArgsErrExtend;
19-
use crate::bounds::Bounds;
2019
use crate::errors;
2120
use crate::hir_ty_lowering::{
2221
AssocItemQSelf, FeedConstTy, HirTyLowerer, PredicateFilter, RegionInferReason,
@@ -28,7 +27,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2827
/// Doesn't add the bound if the HIR bounds contain any of `Sized`, `?Sized` or `!Sized`.
2928
pub(crate) fn add_sized_bound(
3029
&self,
31-
bounds: &mut Bounds<'tcx>,
30+
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
3231
self_ty: Ty<'tcx>,
3332
hir_bounds: &'tcx [hir::GenericBound<'tcx>],
3433
self_ty_where_predicates: Option<(LocalDefId, &'tcx [hir::WherePredicate<'tcx>])>,
@@ -113,10 +112,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
113112
if seen_sized_unbound || seen_negative_sized_bound || seen_positive_sized_bound {
114113
// There was in fact a `?Sized`, `!Sized` or explicit `Sized` bound;
115114
// we don't need to do anything.
116-
} else if sized_def_id.is_some() {
115+
} else if let Some(sized_def_id) = sized_def_id {
117116
// There was no `?Sized`, `!Sized` or explicit `Sized` bound;
118117
// add `Sized` if it's available.
119-
bounds.push_sized(tcx, self_ty, span);
118+
let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [self_ty]);
119+
// Preferable to put this obligation first, since we report better errors for sized ambiguity.
120+
bounds.insert(0, (trait_ref.upcast(tcx), span));
120121
}
121122
}
122123

@@ -146,7 +147,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
146147
&self,
147148
param_ty: Ty<'tcx>,
148149
hir_bounds: I,
149-
bounds: &mut Bounds<'tcx>,
150+
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
150151
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
151152
predicate_filter: PredicateFilter,
152153
) where
@@ -189,14 +190,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
189190
}
190191

191192
let region = self.lower_lifetime(lifetime, RegionInferReason::OutlivesBound);
192-
bounds.push_region_bound(
193-
self.tcx(),
194-
ty::Binder::bind_with_vars(
195-
ty::OutlivesPredicate(param_ty, region),
196-
bound_vars,
197-
),
198-
lifetime.ident.span,
193+
let bound = ty::Binder::bind_with_vars(
194+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(param_ty, region)),
195+
bound_vars,
199196
);
197+
bounds.push((bound.upcast(self.tcx()), lifetime.ident.span));
200198
}
201199
hir::GenericBound::Use(..) => {
202200
// We don't actually lower `use` into the type layer.
@@ -219,7 +217,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
219217
hir_ref_id: hir::HirId,
220218
trait_ref: ty::PolyTraitRef<'tcx>,
221219
constraint: &hir::AssocItemConstraint<'tcx>,
222-
bounds: &mut Bounds<'tcx>,
220+
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
223221
duplicates: &mut FxIndexMap<DefId, Span>,
224222
path_span: Span,
225223
predicate_filter: PredicateFilter,
@@ -389,14 +387,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
389387
PredicateFilter::All
390388
| PredicateFilter::SelfOnly
391389
| PredicateFilter::SelfAndAssociatedTypeBounds => {
392-
bounds.push_projection_bound(
393-
tcx,
394-
projection_term.map_bound(|projection_term| ty::ProjectionPredicate {
390+
let bound = projection_term.map_bound(|projection_term| {
391+
ty::ClauseKind::Projection(ty::ProjectionPredicate {
395392
projection_term,
396393
term,
397-
}),
398-
constraint.span,
399-
);
394+
})
395+
});
396+
bounds.push((bound.upcast(tcx), constraint.span));
400397
}
401398
// SelfTraitThatDefines is only interested in trait predicates.
402399
PredicateFilter::SelfTraitThatDefines(_) => {}

0 commit comments

Comments
 (0)