13
13
//! Here we build the "reduced graph": the graph of the module tree without
14
14
//! any imports resolved.
15
15
16
- use DefModifiers ;
17
16
use resolve_imports:: ImportDirectiveSubclass :: { self , GlobImport } ;
18
17
use Module ;
19
18
use Namespace :: { self , TypeNS , ValueNS } ;
@@ -28,9 +27,9 @@ use rustc::hir::def::*;
28
27
use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , DefId } ;
29
28
use rustc:: ty:: { self , VariantKind } ;
30
29
31
- use syntax:: ast:: Name ;
30
+ use syntax:: ast:: { Name , NodeId } ;
32
31
use syntax:: attr:: AttrMetaMethods ;
33
- use syntax:: parse:: token:: { special_idents , SELF_KEYWORD_NAME , SUPER_KEYWORD_NAME } ;
32
+ use syntax:: parse:: token:: keywords ;
34
33
use syntax:: codemap:: { Span , DUMMY_SP } ;
35
34
36
35
use rustc:: hir;
@@ -53,10 +52,9 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
53
52
}
54
53
}
55
54
56
- impl < ' a > ToNameBinding < ' a > for ( Def , Span , DefModifiers , ty:: Visibility ) {
55
+ impl < ' a > ToNameBinding < ' a > for ( Def , Span , ty:: Visibility ) {
57
56
fn to_name_binding ( self ) -> NameBinding < ' a > {
58
- let kind = NameBindingKind :: Def ( self . 0 ) ;
59
- NameBinding { modifiers : self . 2 , kind : kind, span : Some ( self . 1 ) , vis : self . 3 }
57
+ NameBinding { kind : NameBindingKind :: Def ( self . 0 ) , span : Some ( self . 1 ) , vis : self . 2 }
60
58
}
61
59
}
62
60
@@ -100,12 +98,42 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
100
98
block. stmts . iter ( ) . any ( is_item)
101
99
}
102
100
101
+ fn sanity_check_import ( & self , view_path : & hir:: ViewPath , id : NodeId ) {
102
+ let path = match view_path. node {
103
+ ViewPathSimple ( _, ref path) |
104
+ ViewPathGlob ( ref path) |
105
+ ViewPathList ( ref path, _) => path
106
+ } ;
107
+
108
+ // Check for type parameters
109
+ let found_param = path. segments . iter ( ) . any ( |segment| {
110
+ !segment. parameters . types ( ) . is_empty ( ) ||
111
+ !segment. parameters . lifetimes ( ) . is_empty ( ) ||
112
+ !segment. parameters . bindings ( ) . is_empty ( )
113
+ } ) ;
114
+ if found_param {
115
+ self . session . span_err ( path. span ,
116
+ "type or lifetime parameter is found in import path" ) ;
117
+ }
118
+
119
+ // Checking for special identifiers in path
120
+ // prevent `self` or `super` at beginning of global path
121
+ if path. global && path. segments . len ( ) > 0 {
122
+ let first = path. segments [ 0 ] . identifier . name ;
123
+ if first == keywords:: Super . to_name ( ) || first == keywords:: SelfValue . to_name ( ) {
124
+ self . session . add_lint (
125
+ lint:: builtin:: SUPER_OR_SELF_IN_GLOBAL_PATH , id, path. span ,
126
+ format ! ( "expected identifier, found keyword `{}`" , first)
127
+ ) ;
128
+ }
129
+ }
130
+ }
131
+
103
132
/// Constructs the reduced graph for one item.
104
133
fn build_reduced_graph_for_item ( & mut self , item : & Item , parent_ref : & mut Module < ' b > ) {
105
134
let parent = * parent_ref;
106
135
let name = item. name ;
107
136
let sp = item. span ;
108
- let modifiers = DefModifiers :: IMPORTABLE ;
109
137
self . current_module = parent;
110
138
let vis = self . resolve_visibility ( & item. vis ) ;
111
139
@@ -114,10 +142,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
114
142
// Extract and intern the module part of the path. For
115
143
// globs and lists, the path is found directly in the AST;
116
144
// for simple paths we have to munge the path a little.
117
- let is_global;
118
145
let module_path: Vec < Name > = match view_path. node {
119
146
ViewPathSimple ( _, ref full_path) => {
120
- is_global = full_path. global ;
121
147
full_path. segments
122
148
. split_last ( )
123
149
. unwrap ( )
@@ -129,30 +155,17 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
129
155
130
156
ViewPathGlob ( ref module_ident_path) |
131
157
ViewPathList ( ref module_ident_path, _) => {
132
- is_global = module_ident_path. global ;
133
158
module_ident_path. segments
134
159
. iter ( )
135
160
. map ( |seg| seg. identifier . name )
136
161
. collect ( )
137
162
}
138
163
} ;
139
164
140
- // Checking for special identifiers in path
141
- // prevent `self` or `super` at beginning of global path
142
- if is_global && ( module_path. first ( ) == Some ( & SELF_KEYWORD_NAME ) ||
143
- module_path. first ( ) == Some ( & SUPER_KEYWORD_NAME ) ) {
144
- self . session . add_lint (
145
- lint:: builtin:: SUPER_OR_SELF_IN_GLOBAL_PATH ,
146
- item. id ,
147
- item. span ,
148
- format ! ( "expected identifier, found keyword `{}`" ,
149
- module_path. first( ) . unwrap( ) . as_str( ) ) ) ;
150
- }
165
+ self . sanity_check_import ( view_path, item. id ) ;
151
166
152
167
// Build up the import directives.
153
- let is_prelude = item. attrs . iter ( ) . any ( |attr| {
154
- attr. name ( ) == special_idents:: prelude_import. name . as_str ( )
155
- } ) ;
168
+ let is_prelude = item. attrs . iter ( ) . any ( |attr| attr. name ( ) == "prelude_import" ) ;
156
169
157
170
match view_path. node {
158
171
ViewPathSimple ( binding, ref full_path) => {
@@ -268,21 +281,21 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
268
281
ItemStatic ( _, m, _) => {
269
282
let mutbl = m == hir:: MutMutable ;
270
283
let def = Def :: Static ( self . ast_map . local_def_id ( item. id ) , mutbl) ;
271
- self . define ( parent, name, ValueNS , ( def, sp, modifiers , vis) ) ;
284
+ self . define ( parent, name, ValueNS , ( def, sp, vis) ) ;
272
285
}
273
286
ItemConst ( _, _) => {
274
287
let def = Def :: Const ( self . ast_map . local_def_id ( item. id ) ) ;
275
- self . define ( parent, name, ValueNS , ( def, sp, modifiers , vis) ) ;
288
+ self . define ( parent, name, ValueNS , ( def, sp, vis) ) ;
276
289
}
277
290
ItemFn ( _, _, _, _, _, _) => {
278
291
let def = Def :: Fn ( self . ast_map . local_def_id ( item. id ) ) ;
279
- self . define ( parent, name, ValueNS , ( def, sp, modifiers , vis) ) ;
292
+ self . define ( parent, name, ValueNS , ( def, sp, vis) ) ;
280
293
}
281
294
282
295
// These items live in the type namespace.
283
296
ItemTy ( ..) => {
284
297
let def = Def :: TyAlias ( self . ast_map . local_def_id ( item. id ) ) ;
285
- self . define ( parent, name, TypeNS , ( def, sp, modifiers , vis) ) ;
298
+ self . define ( parent, name, TypeNS , ( def, sp, vis) ) ;
286
299
}
287
300
288
301
ItemEnum ( ref enum_definition, _) => {
@@ -301,13 +314,13 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
301
314
ItemStruct ( ref struct_def, _) => {
302
315
// Define a name in the type namespace.
303
316
let def = Def :: Struct ( self . ast_map . local_def_id ( item. id ) ) ;
304
- self . define ( parent, name, TypeNS , ( def, sp, modifiers , vis) ) ;
317
+ self . define ( parent, name, TypeNS , ( def, sp, vis) ) ;
305
318
306
319
// If this is a newtype or unit-like struct, define a name
307
320
// in the value namespace as well
308
321
if !struct_def. is_struct ( ) {
309
322
let def = Def :: Struct ( self . ast_map . local_def_id ( struct_def. id ( ) ) ) ;
310
- self . define ( parent, name, ValueNS , ( def, sp, modifiers , vis) ) ;
323
+ self . define ( parent, name, ValueNS , ( def, sp, vis) ) ;
311
324
}
312
325
313
326
// Record the def ID and fields of this struct.
@@ -339,8 +352,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
339
352
hir:: TypeTraitItem ( ..) => ( Def :: AssociatedTy ( def_id, item_def_id) , TypeNS ) ,
340
353
} ;
341
354
342
- let modifiers = DefModifiers :: empty ( ) ; // NB: not DefModifiers::IMPORTABLE
343
- self . define ( module_parent, item. name , ns, ( def, item. span , modifiers, vis) ) ;
355
+ self . define ( module_parent, item. name , ns, ( def, item. span , vis) ) ;
344
356
345
357
self . trait_item_map . insert ( ( item. name , def_id) , item_def_id) ;
346
358
}
@@ -363,19 +375,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
363
375
364
376
// Variants are always treated as importable to allow them to be glob used.
365
377
// All variants are defined in both type and value namespaces as future-proofing.
366
- let modifiers = DefModifiers :: IMPORTABLE ;
367
378
let def = Def :: Variant ( item_id, self . ast_map . local_def_id ( variant. node . data . id ( ) ) ) ;
368
-
369
- self . define ( parent, name, ValueNS , ( def, variant. span , modifiers, parent. vis ) ) ;
370
- self . define ( parent, name, TypeNS , ( def, variant. span , modifiers, parent. vis ) ) ;
379
+ self . define ( parent, name, ValueNS , ( def, variant. span , parent. vis ) ) ;
380
+ self . define ( parent, name, TypeNS , ( def, variant. span , parent. vis ) ) ;
371
381
}
372
382
373
383
/// Constructs the reduced graph for one foreign item.
374
384
fn build_reduced_graph_for_foreign_item ( & mut self ,
375
385
foreign_item : & ForeignItem ,
376
386
parent : Module < ' b > ) {
377
387
let name = foreign_item. name ;
378
- let modifiers = DefModifiers :: IMPORTABLE ;
379
388
380
389
let def = match foreign_item. node {
381
390
ForeignItemFn ( ..) => {
@@ -387,7 +396,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
387
396
} ;
388
397
self . current_module = parent;
389
398
let vis = self . resolve_visibility ( & foreign_item. vis ) ;
390
- self . define ( parent, name, ValueNS , ( def, foreign_item. span , modifiers , vis) ) ;
399
+ self . define ( parent, name, ValueNS , ( def, foreign_item. span , vis) ) ;
391
400
}
392
401
393
402
fn build_reduced_graph_for_block ( & mut self , block : & Block , parent : & mut Module < ' b > ) {
@@ -422,10 +431,6 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
422
431
423
432
let name = xcdef. name ;
424
433
let vis = if parent. is_trait ( ) { ty:: Visibility :: Public } else { xcdef. vis } ;
425
- let modifiers = match parent. is_normal ( ) {
426
- true => DefModifiers :: IMPORTABLE ,
427
- false => DefModifiers :: empty ( ) ,
428
- } ;
429
434
430
435
match def {
431
436
Def :: Mod ( _) | Def :: ForeignMod ( _) | Def :: Enum ( ..) => {
@@ -439,9 +444,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
439
444
debug ! ( "(building reduced graph for external crate) building variant {}" , name) ;
440
445
// Variants are always treated as importable to allow them to be glob used.
441
446
// All variants are defined in both type and value namespaces as future-proofing.
442
- let modifiers = DefModifiers :: IMPORTABLE ;
443
- self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , modifiers, vis) ) ;
444
- self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , modifiers, vis) ) ;
447
+ self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
448
+ self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
445
449
if self . session . cstore . variant_kind ( variant_id) == Some ( VariantKind :: Struct ) {
446
450
// Not adding fields for variants as they are not accessed with a self receiver
447
451
self . structs . insert ( variant_id, Vec :: new ( ) ) ;
@@ -454,7 +458,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
454
458
Def :: Method ( ..) => {
455
459
debug ! ( "(building reduced graph for external crate) building value (fn/static) {}" ,
456
460
name) ;
457
- self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , modifiers , vis) ) ;
461
+ self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
458
462
}
459
463
Def :: Trait ( def_id) => {
460
464
debug ! ( "(building reduced graph for external crate) building type {}" , name) ;
@@ -480,16 +484,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
480
484
}
481
485
Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) => {
482
486
debug ! ( "(building reduced graph for external crate) building type {}" , name) ;
483
- self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , modifiers , vis) ) ;
487
+ self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
484
488
}
485
489
Def :: Struct ( def_id)
486
490
if self . session . cstore . tuple_struct_definition_if_ctor ( def_id) . is_none ( ) => {
487
491
debug ! ( "(building reduced graph for external crate) building type and value for {}" ,
488
492
name) ;
489
- self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , modifiers , vis) ) ;
493
+ self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
490
494
if let Some ( ctor_def_id) = self . session . cstore . struct_ctor_def_id ( def_id) {
491
495
let def = Def :: Struct ( ctor_def_id) ;
492
- self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , modifiers , vis) ) ;
496
+ self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
493
497
}
494
498
495
499
// Record the def ID and fields of this struct.
0 commit comments