Skip to content

Commit de0e7d3

Browse files
pass PredicateFilter to compute_bounds
1 parent 858a861 commit de0e7d3

File tree

4 files changed

+51
-72
lines changed

4 files changed

+51
-72
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use rustc_span::symbol::Ident;
99
use rustc_span::{ErrorGuaranteed, Span};
1010
use rustc_trait_selection::traits;
1111

12-
use crate::astconv::{AstConv, ConvertedBinding, ConvertedBindingKind};
12+
use crate::astconv::{
13+
AstConv, ConvertedBinding, ConvertedBindingKind, OnlySelfBounds, PredicateFilter,
14+
};
1315
use crate::bounds::Bounds;
1416
use crate::errors::{MultipleRelaxedDefaultBounds, ValueOfAssociatedStructAlreadySpecified};
1517

16-
use super::OnlySelfBounds;
17-
1818
impl<'tcx> dyn AstConv<'tcx> + '_ {
1919
/// Sets `implicitly_sized` to true on `Bounds` if necessary
2020
pub(crate) fn add_implicitly_sized(
@@ -176,47 +176,39 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
176176
&self,
177177
param_ty: Ty<'tcx>,
178178
ast_bounds: &[hir::GenericBound<'_>],
179-
only_self_bounds: OnlySelfBounds,
179+
filter: PredicateFilter,
180180
) -> Bounds<'tcx> {
181181
let mut bounds = Bounds::default();
182-
self.add_bounds(
183-
param_ty,
184-
ast_bounds.iter(),
185-
&mut bounds,
186-
ty::List::empty(),
187-
only_self_bounds,
188-
);
189-
debug!(?bounds);
190182

191-
bounds
192-
}
193-
194-
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
195-
/// named `assoc_name` into ty::Bounds. Ignore the rest.
196-
pub(crate) fn compute_bounds_that_match_assoc_item(
197-
&self,
198-
param_ty: Ty<'tcx>,
199-
ast_bounds: &[hir::GenericBound<'_>],
200-
assoc_name: Ident,
201-
) -> Bounds<'tcx> {
202-
let mut result = Vec::new();
203-
204-
for ast_bound in ast_bounds {
205-
if let Some(trait_ref) = ast_bound.trait_ref()
206-
&& let Some(trait_did) = trait_ref.trait_def_id()
207-
&& self.tcx().trait_may_define_assoc_item(trait_did, assoc_name)
208-
{
209-
result.push(ast_bound.clone());
183+
let only_self_bounds = match filter {
184+
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
185+
OnlySelfBounds(false)
210186
}
211-
}
187+
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => OnlySelfBounds(true),
188+
};
212189

213-
let mut bounds = Bounds::default();
214190
self.add_bounds(
215191
param_ty,
216-
result.iter(),
192+
ast_bounds.iter().filter(|bound| {
193+
match filter {
194+
PredicateFilter::All
195+
| PredicateFilter::SelfOnly
196+
| PredicateFilter::SelfAndAssociatedTypeBounds => true,
197+
PredicateFilter::SelfThatDefines(assoc_name) => {
198+
if let Some(trait_ref) = bound.trait_ref()
199+
&& let Some(trait_did) = trait_ref.trait_def_id()
200+
&& self.tcx().trait_may_define_assoc_item(trait_did, assoc_name)
201+
{
202+
true
203+
} else {
204+
false
205+
}
206+
}
207+
}
208+
}),
217209
&mut bounds,
218210
ty::List::empty(),
219-
OnlySelfBounds(true),
211+
only_self_bounds,
220212
);
221213
debug!(?bounds);
222214

compiler/rustc_hir_analysis/src/astconv/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ pub struct PathSeg(pub DefId, pub usize);
5858
#[derive(Copy, Clone, Debug)]
5959
pub struct OnlySelfBounds(pub bool);
6060

61+
#[derive(Copy, Clone, Debug)]
62+
pub enum PredicateFilter {
63+
/// All predicates may be implied by the trait.
64+
All,
65+
66+
/// Only traits that reference `Self: ..` are implied by the trait.
67+
SelfOnly,
68+
69+
/// Only traits that reference `Self: ..` and define an associated type
70+
/// with the given ident are implied by the trait.
71+
SelfThatDefines(Ident),
72+
73+
/// Only traits that reference `Self: ..` and their associated type bounds.
74+
/// For example, given `Self: Tr<A: B>`, this would expand to `Self: Tr`
75+
/// and `<Self as Tr>::A: B`.
76+
SelfAndAssociatedTypeBounds,
77+
}
78+
6179
pub trait AstConv<'tcx> {
6280
fn tcx(&self) -> TyCtxt<'tcx>;
6381

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::ItemCtxt;
2-
use crate::astconv::{AstConv, OnlySelfBounds};
2+
use crate::astconv::{AstConv, PredicateFilter};
33
use rustc_hir as hir;
44
use rustc_infer::traits::util;
55
use rustc_middle::ty::subst::InternalSubsts;
@@ -26,7 +26,7 @@ fn associated_type_bounds<'tcx>(
2626
);
2727

2828
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
29-
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, OnlySelfBounds(false));
29+
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, PredicateFilter::All);
3030
// Associated types are implicitly sized unless a `?Sized` bound is found
3131
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
3232

@@ -68,7 +68,7 @@ fn opaque_type_bounds<'tcx>(
6868
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
6969
ty::print::with_no_queries!({
7070
let icx = ItemCtxt::new(tcx, opaque_def_id);
71-
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, OnlySelfBounds(false));
71+
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, PredicateFilter::All);
7272
// Opaque types are implicitly sized unless a `?Sized` bound is found
7373
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
7474
debug!(?bounds);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::astconv::{AstConv, OnlySelfBounds};
1+
use crate::astconv::{AstConv, OnlySelfBounds, PredicateFilter};
22
use crate::bounds::Bounds;
33
use crate::collect::ItemCtxt;
44
use crate::constrained_generic_params as cgp;
@@ -125,7 +125,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
125125
if let Some(self_bounds) = is_trait {
126126
predicates.extend(
127127
icx.astconv()
128-
.compute_bounds(tcx.types.self_param, self_bounds, OnlySelfBounds(false))
128+
.compute_bounds(tcx.types.self_param, self_bounds, PredicateFilter::All)
129129
.clauses(),
130130
);
131131
}
@@ -530,24 +530,6 @@ pub(super) fn explicit_predicates_of<'tcx>(
530530
}
531531
}
532532

533-
#[derive(Copy, Clone, Debug)]
534-
pub enum PredicateFilter {
535-
/// All predicates may be implied by the trait.
536-
All,
537-
538-
/// Only traits that reference `Self: ..` are implied by the trait.
539-
SelfOnly,
540-
541-
/// Only traits that reference `Self: ..` and define an associated type
542-
/// with the given ident are implied by the trait.
543-
SelfThatDefines(Ident),
544-
545-
/// Only traits that reference `Self: ..` and their associated type bounds.
546-
/// For example, given `Self: Tr<A: B>`, this would expand to `Self: Tr`
547-
/// and `<Self as Tr>::A: B`.
548-
SelfAndAssociatedTypeBounds,
549-
}
550-
551533
/// Ensures that the super-predicates of the trait with a `DefId`
552534
/// of `trait_def_id` are converted and stored. This also ensures that
553535
/// the transitive super-predicates are converted.
@@ -610,20 +592,7 @@ pub(super) fn implied_predicates_with_filter(
610592
let icx = ItemCtxt::new(tcx, trait_def_id);
611593

612594
let self_param_ty = tcx.types.self_param;
613-
let superbounds = match filter {
614-
// Should imply both "real" supertraits, and also associated type bounds
615-
// from the supertraits position.
616-
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
617-
icx.astconv().compute_bounds(self_param_ty, bounds, OnlySelfBounds(false))
618-
}
619-
// Should only imply "real" supertraits, i.e. predicates with the self type `Self`.
620-
PredicateFilter::SelfOnly => {
621-
icx.astconv().compute_bounds(self_param_ty, bounds, OnlySelfBounds(true))
622-
}
623-
PredicateFilter::SelfThatDefines(assoc_name) => {
624-
icx.astconv().compute_bounds_that_match_assoc_item(self_param_ty, bounds, assoc_name)
625-
}
626-
};
595+
let superbounds = icx.astconv().compute_bounds(self_param_ty, bounds, filter);
627596

628597
let where_bounds_that_match = icx.type_parameter_bounds_in_generics(
629598
generics,

0 commit comments

Comments
 (0)