Skip to content

Commit ffcf7e8

Browse files
Moved all Assoc* types to assoc.rs.
1 parent 11e41b0 commit ffcf7e8

File tree

2 files changed

+173
-162
lines changed

2 files changed

+173
-162
lines changed

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
pub use self::AssocItemContainer::*;
2+
3+
use crate::ty;
4+
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
5+
use rustc_hir as hir;
6+
use rustc_hir::def::{DefKind, Namespace};
7+
use rustc_hir::def_id::DefId;
8+
use rustc_span::symbol::{Ident, Symbol};
9+
10+
use super::{TyCtxt, Visibility};
11+
12+
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Hash)]
13+
pub enum AssocItemContainer {
14+
TraitContainer(DefId),
15+
ImplContainer(DefId),
16+
}
17+
18+
impl AssocItemContainer {
19+
/// Asserts that this is the `DefId` of an associated item declared
20+
/// in a trait, and returns the trait `DefId`.
21+
pub fn assert_trait(&self) -> DefId {
22+
match *self {
23+
TraitContainer(id) => id,
24+
_ => bug!("associated item has wrong container type: {:?}", self),
25+
}
26+
}
27+
28+
pub fn id(&self) -> DefId {
29+
match *self {
30+
TraitContainer(id) => id,
31+
ImplContainer(id) => id,
32+
}
33+
}
34+
}
35+
36+
#[derive(Copy, Clone, Debug, PartialEq, HashStable, Eq, Hash)]
37+
pub struct AssocItem {
38+
pub def_id: DefId,
39+
#[stable_hasher(project(name))]
40+
pub ident: Ident,
41+
pub kind: AssocKind,
42+
pub vis: Visibility,
43+
pub defaultness: hir::Defaultness,
44+
pub container: AssocItemContainer,
45+
46+
/// Whether this is a method with an explicit self
47+
/// as its first parameter, allowing method calls.
48+
pub fn_has_self_parameter: bool,
49+
}
50+
51+
impl AssocItem {
52+
pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
53+
match self.kind {
54+
ty::AssocKind::Fn => {
55+
// We skip the binder here because the binder would deanonymize all
56+
// late-bound regions, and we don't want method signatures to show up
57+
// `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
58+
// regions just fine, showing `fn(&MyType)`.
59+
tcx.fn_sig(self.def_id).skip_binder().to_string()
60+
}
61+
ty::AssocKind::Type => format!("type {};", self.ident),
62+
ty::AssocKind::Const => {
63+
format!("const {}: {:?};", self.ident, tcx.type_of(self.def_id))
64+
}
65+
}
66+
}
67+
}
68+
69+
#[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash)]
70+
pub enum AssocKind {
71+
Const,
72+
Fn,
73+
Type,
74+
}
75+
76+
impl AssocKind {
77+
pub fn namespace(&self) -> Namespace {
78+
match *self {
79+
ty::AssocKind::Type => Namespace::TypeNS,
80+
ty::AssocKind::Const | ty::AssocKind::Fn => Namespace::ValueNS,
81+
}
82+
}
83+
84+
pub fn as_def_kind(&self) -> DefKind {
85+
match self {
86+
AssocKind::Const => DefKind::AssocConst,
87+
AssocKind::Fn => DefKind::AssocFn,
88+
AssocKind::Type => DefKind::AssocTy,
89+
}
90+
}
91+
}
92+
93+
/// A list of `ty::AssocItem`s in definition order that allows for efficient lookup by name.
94+
///
95+
/// When doing lookup by name, we try to postpone hygienic comparison for as long as possible since
96+
/// it is relatively expensive. Instead, items are indexed by `Symbol` and hygienic comparison is
97+
/// done only on items with the same name.
98+
#[derive(Debug, Clone, PartialEq, HashStable)]
99+
pub struct AssociatedItems<'tcx> {
100+
pub(super) items: SortedIndexMultiMap<u32, Symbol, &'tcx ty::AssocItem>,
101+
}
102+
103+
impl<'tcx> AssociatedItems<'tcx> {
104+
/// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order.
105+
pub fn new(items_in_def_order: impl IntoIterator<Item = &'tcx ty::AssocItem>) -> Self {
106+
let items = items_in_def_order.into_iter().map(|item| (item.ident.name, item)).collect();
107+
AssociatedItems { items }
108+
}
109+
110+
/// Returns a slice of associated items in the order they were defined.
111+
///
112+
/// New code should avoid relying on definition order. If you need a particular associated item
113+
/// for a known trait, make that trait a lang item instead of indexing this array.
114+
pub fn in_definition_order(&self) -> impl '_ + Iterator<Item = &ty::AssocItem> {
115+
self.items.iter().map(|(_, v)| *v)
116+
}
117+
118+
pub fn len(&self) -> usize {
119+
self.items.len()
120+
}
121+
122+
/// Returns an iterator over all associated items with the given name, ignoring hygiene.
123+
pub fn filter_by_name_unhygienic(
124+
&self,
125+
name: Symbol,
126+
) -> impl '_ + Iterator<Item = &ty::AssocItem> {
127+
self.items.get_by_key(&name).copied()
128+
}
129+
130+
/// Returns an iterator over all associated items with the given name.
131+
///
132+
/// Multiple items may have the same name if they are in different `Namespace`s. For example,
133+
/// an associated type can have the same name as a method. Use one of the `find_by_name_and_*`
134+
/// methods below if you know which item you are looking for.
135+
pub fn filter_by_name(
136+
&'a self,
137+
tcx: TyCtxt<'a>,
138+
ident: Ident,
139+
parent_def_id: DefId,
140+
) -> impl 'a + Iterator<Item = &'a ty::AssocItem> {
141+
self.filter_by_name_unhygienic(ident.name)
142+
.filter(move |item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
143+
}
144+
145+
/// Returns the associated item with the given name and `AssocKind`, if one exists.
146+
pub fn find_by_name_and_kind(
147+
&self,
148+
tcx: TyCtxt<'_>,
149+
ident: Ident,
150+
kind: AssocKind,
151+
parent_def_id: DefId,
152+
) -> Option<&ty::AssocItem> {
153+
self.filter_by_name_unhygienic(ident.name)
154+
.filter(|item| item.kind == kind)
155+
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
156+
}
157+
158+
/// Returns the associated item with the given name in the given `Namespace`, if one exists.
159+
pub fn find_by_name_and_namespace(
160+
&self,
161+
tcx: TyCtxt<'_>,
162+
ident: Ident,
163+
ns: Namespace,
164+
parent_def_id: DefId,
165+
) -> Option<&ty::AssocItem> {
166+
self.filter_by_name_unhygienic(ident.name)
167+
.filter(|item| item.kind.namespace() == ns)
168+
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
169+
}
170+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use self::AssocItemContainer::*;
1414
pub use self::BorrowKind::*;
1515
pub use self::IntVarValue::*;
1616
pub use self::Variance::*;
17+
pub use assoc::*;
1718
pub use generics::*;
1819
pub use upvar::*;
1920

@@ -34,13 +35,12 @@ use rustc_attr as attr;
3435
use rustc_data_structures::captures::Captures;
3536
use rustc_data_structures::fingerprint::Fingerprint;
3637
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
37-
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
3838
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3939
use rustc_data_structures::sync::{self, par_iter, ParallelIterator};
4040
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
4141
use rustc_errors::ErrorReported;
4242
use rustc_hir as hir;
43-
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Namespace, Res};
43+
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
4444
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
4545
use rustc_hir::lang_items::LangItem;
4646
use rustc_hir::{Constness, Node};
@@ -107,6 +107,7 @@ pub mod trait_def;
107107
pub mod util;
108108
pub mod walk;
109109

110+
mod assoc;
110111
mod consts;
111112
mod context;
112113
mod diagnostics;
@@ -134,30 +135,6 @@ pub struct ResolverOutputs {
134135
pub extern_prelude: FxHashMap<Symbol, bool>,
135136
}
136137

137-
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Hash)]
138-
pub enum AssocItemContainer {
139-
TraitContainer(DefId),
140-
ImplContainer(DefId),
141-
}
142-
143-
impl AssocItemContainer {
144-
/// Asserts that this is the `DefId` of an associated item declared
145-
/// in a trait, and returns the trait `DefId`.
146-
pub fn assert_trait(&self) -> DefId {
147-
match *self {
148-
TraitContainer(id) => id,
149-
_ => bug!("associated item has wrong container type: {:?}", self),
150-
}
151-
}
152-
153-
pub fn id(&self) -> DefId {
154-
match *self {
155-
TraitContainer(id) => id,
156-
ImplContainer(id) => id,
157-
}
158-
}
159-
}
160-
161138
/// The "header" of an impl is everything outside the body: a Self type, a trait
162139
/// ref (in the case of a trait impl), and a set of predicates (from the
163140
/// bounds / where-clauses).
@@ -182,142 +159,6 @@ pub enum ImplPolarity {
182159
Reservation,
183160
}
184161

185-
#[derive(Copy, Clone, Debug, PartialEq, HashStable, Eq, Hash)]
186-
pub struct AssocItem {
187-
pub def_id: DefId,
188-
#[stable_hasher(project(name))]
189-
pub ident: Ident,
190-
pub kind: AssocKind,
191-
pub vis: Visibility,
192-
pub defaultness: hir::Defaultness,
193-
pub container: AssocItemContainer,
194-
195-
/// Whether this is a method with an explicit self
196-
/// as its first parameter, allowing method calls.
197-
pub fn_has_self_parameter: bool,
198-
}
199-
200-
#[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash)]
201-
pub enum AssocKind {
202-
Const,
203-
Fn,
204-
Type,
205-
}
206-
207-
impl AssocKind {
208-
pub fn namespace(&self) -> Namespace {
209-
match *self {
210-
ty::AssocKind::Type => Namespace::TypeNS,
211-
ty::AssocKind::Const | ty::AssocKind::Fn => Namespace::ValueNS,
212-
}
213-
}
214-
215-
pub fn as_def_kind(&self) -> DefKind {
216-
match self {
217-
AssocKind::Const => DefKind::AssocConst,
218-
AssocKind::Fn => DefKind::AssocFn,
219-
AssocKind::Type => DefKind::AssocTy,
220-
}
221-
}
222-
}
223-
224-
impl AssocItem {
225-
pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
226-
match self.kind {
227-
ty::AssocKind::Fn => {
228-
// We skip the binder here because the binder would deanonymize all
229-
// late-bound regions, and we don't want method signatures to show up
230-
// `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
231-
// regions just fine, showing `fn(&MyType)`.
232-
tcx.fn_sig(self.def_id).skip_binder().to_string()
233-
}
234-
ty::AssocKind::Type => format!("type {};", self.ident),
235-
ty::AssocKind::Const => {
236-
format!("const {}: {:?};", self.ident, tcx.type_of(self.def_id))
237-
}
238-
}
239-
}
240-
}
241-
242-
/// A list of `ty::AssocItem`s in definition order that allows for efficient lookup by name.
243-
///
244-
/// When doing lookup by name, we try to postpone hygienic comparison for as long as possible since
245-
/// it is relatively expensive. Instead, items are indexed by `Symbol` and hygienic comparison is
246-
/// done only on items with the same name.
247-
#[derive(Debug, Clone, PartialEq, HashStable)]
248-
pub struct AssociatedItems<'tcx> {
249-
items: SortedIndexMultiMap<u32, Symbol, &'tcx ty::AssocItem>,
250-
}
251-
252-
impl<'tcx> AssociatedItems<'tcx> {
253-
/// Constructs an `AssociatedItems` map from a series of `ty::AssocItem`s in definition order.
254-
pub fn new(items_in_def_order: impl IntoIterator<Item = &'tcx ty::AssocItem>) -> Self {
255-
let items = items_in_def_order.into_iter().map(|item| (item.ident.name, item)).collect();
256-
AssociatedItems { items }
257-
}
258-
259-
/// Returns a slice of associated items in the order they were defined.
260-
///
261-
/// New code should avoid relying on definition order. If you need a particular associated item
262-
/// for a known trait, make that trait a lang item instead of indexing this array.
263-
pub fn in_definition_order(&self) -> impl '_ + Iterator<Item = &ty::AssocItem> {
264-
self.items.iter().map(|(_, v)| *v)
265-
}
266-
267-
pub fn len(&self) -> usize {
268-
self.items.len()
269-
}
270-
271-
/// Returns an iterator over all associated items with the given name, ignoring hygiene.
272-
pub fn filter_by_name_unhygienic(
273-
&self,
274-
name: Symbol,
275-
) -> impl '_ + Iterator<Item = &ty::AssocItem> {
276-
self.items.get_by_key(&name).copied()
277-
}
278-
279-
/// Returns an iterator over all associated items with the given name.
280-
///
281-
/// Multiple items may have the same name if they are in different `Namespace`s. For example,
282-
/// an associated type can have the same name as a method. Use one of the `find_by_name_and_*`
283-
/// methods below if you know which item you are looking for.
284-
pub fn filter_by_name(
285-
&'a self,
286-
tcx: TyCtxt<'a>,
287-
ident: Ident,
288-
parent_def_id: DefId,
289-
) -> impl 'a + Iterator<Item = &'a ty::AssocItem> {
290-
self.filter_by_name_unhygienic(ident.name)
291-
.filter(move |item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
292-
}
293-
294-
/// Returns the associated item with the given name and `AssocKind`, if one exists.
295-
pub fn find_by_name_and_kind(
296-
&self,
297-
tcx: TyCtxt<'_>,
298-
ident: Ident,
299-
kind: AssocKind,
300-
parent_def_id: DefId,
301-
) -> Option<&ty::AssocItem> {
302-
self.filter_by_name_unhygienic(ident.name)
303-
.filter(|item| item.kind == kind)
304-
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
305-
}
306-
307-
/// Returns the associated item with the given name in the given `Namespace`, if one exists.
308-
pub fn find_by_name_and_namespace(
309-
&self,
310-
tcx: TyCtxt<'_>,
311-
ident: Ident,
312-
ns: Namespace,
313-
parent_def_id: DefId,
314-
) -> Option<&ty::AssocItem> {
315-
self.filter_by_name_unhygienic(ident.name)
316-
.filter(|item| item.kind.namespace() == ns)
317-
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
318-
}
319-
}
320-
321162
#[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, TyEncodable, TyDecodable, HashStable)]
322163
pub enum Visibility {
323164
/// Visible everywhere (including in other crates).

0 commit comments

Comments
 (0)