Skip to content

Commit fb29d65

Browse files
committed
Move ty::InstantiatedPredicates to its own little module (cute)
1 parent 05c8728 commit fb29d65

File tree

3 files changed

+72
-66
lines changed

3 files changed

+72
-66
lines changed

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ mod visibility;
155155

156156
pub use bound_constness::BoundConstness;
157157
pub use impl_polarity::ImplPolarity;
158-
pub use predicate::{Predicate, PredicateKind};
158+
pub use predicate::{InstantiatedPredicates, Predicate, PredicateKind};
159159
pub use visibility::Visibility;
160160

161161
pub struct ResolverOutputs {
@@ -872,71 +872,6 @@ impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> {
872872
PredicateKind::Clause(Clause::Trait(self)).to_predicate(tcx)
873873
}
874874
}
875-
876-
/// Represents the bounds declared on a particular set of type
877-
/// parameters. Should eventually be generalized into a flag list of
878-
/// where-clauses. You can obtain an `InstantiatedPredicates` list from a
879-
/// `GenericPredicates` by using the `instantiate` method. Note that this method
880-
/// reflects an important semantic invariant of `InstantiatedPredicates`: while
881-
/// the `GenericPredicates` are expressed in terms of the bound type
882-
/// parameters of the impl/trait/whatever, an `InstantiatedPredicates` instance
883-
/// represented a set of bounds for some particular instantiation,
884-
/// meaning that the generic parameters have been substituted with
885-
/// their values.
886-
///
887-
/// Example:
888-
/// ```ignore (illustrative)
889-
/// struct Foo<T, U: Bar<T>> { ... }
890-
/// ```
891-
/// Here, the `GenericPredicates` for `Foo` would contain a list of bounds like
892-
/// `[[], [U:Bar<T>]]`. Now if there were some particular reference
893-
/// like `Foo<isize,usize>`, then the `InstantiatedPredicates` would be `[[],
894-
/// [usize:Bar<isize>]]`.
895-
#[derive(Clone, Debug, TypeFoldable, TypeVisitable)]
896-
pub struct InstantiatedPredicates<'tcx> {
897-
pub predicates: Vec<Predicate<'tcx>>,
898-
pub spans: Vec<Span>,
899-
}
900-
901-
impl<'tcx> InstantiatedPredicates<'tcx> {
902-
pub fn empty() -> InstantiatedPredicates<'tcx> {
903-
InstantiatedPredicates { predicates: vec![], spans: vec![] }
904-
}
905-
906-
pub fn is_empty(&self) -> bool {
907-
self.predicates.is_empty()
908-
}
909-
910-
pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
911-
(&self).into_iter()
912-
}
913-
}
914-
915-
impl<'tcx> IntoIterator for InstantiatedPredicates<'tcx> {
916-
type Item = (Predicate<'tcx>, Span);
917-
918-
type IntoIter = std::iter::Zip<std::vec::IntoIter<Predicate<'tcx>>, std::vec::IntoIter<Span>>;
919-
920-
fn into_iter(self) -> Self::IntoIter {
921-
debug_assert_eq!(self.predicates.len(), self.spans.len());
922-
std::iter::zip(self.predicates, self.spans)
923-
}
924-
}
925-
926-
impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> {
927-
type Item = (Predicate<'tcx>, Span);
928-
929-
type IntoIter = std::iter::Zip<
930-
std::iter::Copied<std::slice::Iter<'a, Predicate<'tcx>>>,
931-
std::iter::Copied<std::slice::Iter<'a, Span>>,
932-
>;
933-
934-
fn into_iter(self) -> Self::IntoIter {
935-
debug_assert_eq!(self.predicates.len(), self.spans.len());
936-
std::iter::zip(self.predicates.iter().copied(), self.spans.iter().copied())
937-
}
938-
}
939-
940875
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable, Lift)]
941876
#[derive(TypeFoldable, TypeVisitable)]
942877
pub struct OpaqueTypeKey<'tcx> {

compiler/rustc_middle/src/ty/predicate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use crate::ty::{
1111
PolyTypeOutlivesPredicate, SubstsRef, SubtypePredicate, Term, TraitPredicate, Ty, TypeFlags,
1212
};
1313

14+
mod instantiated_predicates;
15+
16+
pub use instantiated_predicates::InstantiatedPredicates;
17+
1418
/// Use this rather than `PredicateKind`, whenever possible.
1519
#[derive(Clone, Copy, PartialEq, Eq, Hash, HashStable)]
1620
#[rustc_pass_by_value]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use rustc_span::Span;
2+
3+
use crate::ty::Predicate;
4+
5+
/// Represents the bounds declared on a particular set of type
6+
/// parameters. Should eventually be generalized into a flag list of
7+
/// where-clauses. You can obtain an `InstantiatedPredicates` list from a
8+
/// `GenericPredicates` by using the `instantiate` method. Note that this method
9+
/// reflects an important semantic invariant of `InstantiatedPredicates`: while
10+
/// the `GenericPredicates` are expressed in terms of the bound type
11+
/// parameters of the impl/trait/whatever, an `InstantiatedPredicates` instance
12+
/// represented a set of bounds for some particular instantiation,
13+
/// meaning that the generic parameters have been substituted with
14+
/// their values.
15+
///
16+
/// Example:
17+
/// ```ignore (illustrative)
18+
/// struct Foo<T, U: Bar<T>> { ... }
19+
/// ```
20+
/// Here, the `GenericPredicates` for `Foo` would contain a list of bounds like
21+
/// `[[], [U:Bar<T>]]`. Now if there were some particular reference
22+
/// like `Foo<isize,usize>`, then the `InstantiatedPredicates` would be `[[],
23+
/// [usize:Bar<isize>]]`.
24+
#[derive(Clone, Debug, TypeFoldable, TypeVisitable)]
25+
pub struct InstantiatedPredicates<'tcx> {
26+
pub predicates: Vec<Predicate<'tcx>>,
27+
pub spans: Vec<Span>,
28+
}
29+
30+
impl<'tcx> InstantiatedPredicates<'tcx> {
31+
pub fn empty() -> InstantiatedPredicates<'tcx> {
32+
InstantiatedPredicates { predicates: vec![], spans: vec![] }
33+
}
34+
35+
pub fn is_empty(&self) -> bool {
36+
self.predicates.is_empty()
37+
}
38+
39+
pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
40+
(&self).into_iter()
41+
}
42+
}
43+
44+
impl<'tcx> IntoIterator for InstantiatedPredicates<'tcx> {
45+
type Item = (Predicate<'tcx>, Span);
46+
47+
type IntoIter = std::iter::Zip<std::vec::IntoIter<Predicate<'tcx>>, std::vec::IntoIter<Span>>;
48+
49+
fn into_iter(self) -> Self::IntoIter {
50+
debug_assert_eq!(self.predicates.len(), self.spans.len());
51+
std::iter::zip(self.predicates, self.spans)
52+
}
53+
}
54+
55+
impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> {
56+
type Item = (Predicate<'tcx>, Span);
57+
58+
type IntoIter = std::iter::Zip<
59+
std::iter::Copied<std::slice::Iter<'a, Predicate<'tcx>>>,
60+
std::iter::Copied<std::slice::Iter<'a, Span>>,
61+
>;
62+
63+
fn into_iter(self) -> Self::IntoIter {
64+
debug_assert_eq!(self.predicates.len(), self.spans.len());
65+
std::iter::zip(self.predicates.iter().copied(), self.spans.iter().copied())
66+
}
67+
}

0 commit comments

Comments
 (0)