@@ -14,6 +14,7 @@ pub use self::AssocItemContainer::*;
14
14
pub use self :: BorrowKind :: * ;
15
15
pub use self :: IntVarValue :: * ;
16
16
pub use self :: Variance :: * ;
17
+ pub use assoc:: * ;
17
18
pub use generics:: * ;
18
19
pub use upvar:: * ;
19
20
@@ -34,13 +35,12 @@ use rustc_attr as attr;
34
35
use rustc_data_structures:: captures:: Captures ;
35
36
use rustc_data_structures:: fingerprint:: Fingerprint ;
36
37
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet , FxIndexMap } ;
37
- use rustc_data_structures:: sorted_map:: SortedIndexMultiMap ;
38
38
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
39
39
use rustc_data_structures:: sync:: { self , par_iter, ParallelIterator } ;
40
40
use rustc_data_structures:: tagged_ptr:: CopyTaggedPtr ;
41
41
use rustc_errors:: ErrorReported ;
42
42
use rustc_hir as hir;
43
- use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Namespace , Res } ;
43
+ use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Res } ;
44
44
use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , LocalDefId , CRATE_DEF_INDEX } ;
45
45
use rustc_hir:: lang_items:: LangItem ;
46
46
use rustc_hir:: { Constness , Node } ;
@@ -107,6 +107,7 @@ pub mod trait_def;
107
107
pub mod util;
108
108
pub mod walk;
109
109
110
+ mod assoc;
110
111
mod consts;
111
112
mod context;
112
113
mod diagnostics;
@@ -134,30 +135,6 @@ pub struct ResolverOutputs {
134
135
pub extern_prelude : FxHashMap < Symbol , bool > ,
135
136
}
136
137
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
-
161
138
/// The "header" of an impl is everything outside the body: a Self type, a trait
162
139
/// ref (in the case of a trait impl), and a set of predicates (from the
163
140
/// bounds / where-clauses).
@@ -182,142 +159,6 @@ pub enum ImplPolarity {
182
159
Reservation ,
183
160
}
184
161
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
-
321
162
#[ derive( Clone , Debug , PartialEq , Eq , Copy , Hash , TyEncodable , TyDecodable , HashStable ) ]
322
163
pub enum Visibility {
323
164
/// Visible everywhere (including in other crates).
0 commit comments