1313//! Here we build the "reduced graph": the graph of the module tree without
1414//! any imports resolved.
1515
16- use DefModifiers ;
1716use resolve_imports:: ImportDirectiveSubclass :: { self , GlobImport } ;
1817use Module ;
1918use Namespace :: { self , TypeNS , ValueNS } ;
@@ -28,9 +27,9 @@ use rustc::hir::def::*;
2827use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , DefId } ;
2928use rustc:: ty:: { self , VariantKind } ;
3029
31- use syntax:: ast:: Name ;
30+ use syntax:: ast:: { Name , NodeId } ;
3231use syntax:: attr:: AttrMetaMethods ;
33- use syntax:: parse:: token:: { special_idents , SELF_KEYWORD_NAME , SUPER_KEYWORD_NAME } ;
32+ use syntax:: parse:: token:: keywords ;
3433use syntax:: codemap:: { Span , DUMMY_SP } ;
3534
3635use rustc:: hir;
@@ -53,10 +52,9 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
5352 }
5453}
5554
56- impl < ' a > ToNameBinding < ' a > for ( Def , Span , DefModifiers , ty:: Visibility ) {
55+ impl < ' a > ToNameBinding < ' a > for ( Def , Span , ty:: Visibility ) {
5756 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 }
6058 }
6159}
6260
@@ -100,12 +98,42 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
10098 block. stmts . iter ( ) . any ( is_item)
10199 }
102100
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+
103132 /// Constructs the reduced graph for one item.
104133 fn build_reduced_graph_for_item ( & mut self , item : & Item , parent_ref : & mut Module < ' b > ) {
105134 let parent = * parent_ref;
106135 let name = item. name ;
107136 let sp = item. span ;
108- let modifiers = DefModifiers :: IMPORTABLE ;
109137 self . current_module = parent;
110138 let vis = self . resolve_visibility ( & item. vis ) ;
111139
@@ -114,10 +142,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
114142 // Extract and intern the module part of the path. For
115143 // globs and lists, the path is found directly in the AST;
116144 // for simple paths we have to munge the path a little.
117- let is_global;
118145 let module_path: Vec < Name > = match view_path. node {
119146 ViewPathSimple ( _, ref full_path) => {
120- is_global = full_path. global ;
121147 full_path. segments
122148 . split_last ( )
123149 . unwrap ( )
@@ -129,30 +155,17 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
129155
130156 ViewPathGlob ( ref module_ident_path) |
131157 ViewPathList ( ref module_ident_path, _) => {
132- is_global = module_ident_path. global ;
133158 module_ident_path. segments
134159 . iter ( )
135160 . map ( |seg| seg. identifier . name )
136161 . collect ( )
137162 }
138163 } ;
139164
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 ) ;
151166
152167 // 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" ) ;
156169
157170 match view_path. node {
158171 ViewPathSimple ( binding, ref full_path) => {
@@ -268,21 +281,21 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
268281 ItemStatic ( _, m, _) => {
269282 let mutbl = m == hir:: MutMutable ;
270283 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) ) ;
272285 }
273286 ItemConst ( _, _) => {
274287 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) ) ;
276289 }
277290 ItemFn ( _, _, _, _, _, _) => {
278291 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) ) ;
280293 }
281294
282295 // These items live in the type namespace.
283296 ItemTy ( ..) => {
284297 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) ) ;
286299 }
287300
288301 ItemEnum ( ref enum_definition, _) => {
@@ -301,13 +314,13 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
301314 ItemStruct ( ref struct_def, _) => {
302315 // Define a name in the type namespace.
303316 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) ) ;
305318
306319 // If this is a newtype or unit-like struct, define a name
307320 // in the value namespace as well
308321 if !struct_def. is_struct ( ) {
309322 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) ) ;
311324 }
312325
313326 // Record the def ID and fields of this struct.
@@ -339,8 +352,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
339352 hir:: TypeTraitItem ( ..) => ( Def :: AssociatedTy ( def_id, item_def_id) , TypeNS ) ,
340353 } ;
341354
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) ) ;
344356
345357 self . trait_item_map . insert ( ( item. name , def_id) , item_def_id) ;
346358 }
@@ -363,19 +375,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
363375
364376 // Variants are always treated as importable to allow them to be glob used.
365377 // All variants are defined in both type and value namespaces as future-proofing.
366- let modifiers = DefModifiers :: IMPORTABLE ;
367378 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 ) ) ;
371381 }
372382
373383 /// Constructs the reduced graph for one foreign item.
374384 fn build_reduced_graph_for_foreign_item ( & mut self ,
375385 foreign_item : & ForeignItem ,
376386 parent : Module < ' b > ) {
377387 let name = foreign_item. name ;
378- let modifiers = DefModifiers :: IMPORTABLE ;
379388
380389 let def = match foreign_item. node {
381390 ForeignItemFn ( ..) => {
@@ -387,7 +396,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
387396 } ;
388397 self . current_module = parent;
389398 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) ) ;
391400 }
392401
393402 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> {
422431
423432 let name = xcdef. name ;
424433 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- } ;
429434
430435 match def {
431436 Def :: Mod ( _) | Def :: ForeignMod ( _) | Def :: Enum ( ..) => {
@@ -439,9 +444,8 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
439444 debug ! ( "(building reduced graph for external crate) building variant {}" , name) ;
440445 // Variants are always treated as importable to allow them to be glob used.
441446 // 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) ) ;
445449 if self . session . cstore . variant_kind ( variant_id) == Some ( VariantKind :: Struct ) {
446450 // Not adding fields for variants as they are not accessed with a self receiver
447451 self . structs . insert ( variant_id, Vec :: new ( ) ) ;
@@ -454,7 +458,7 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
454458 Def :: Method ( ..) => {
455459 debug ! ( "(building reduced graph for external crate) building value (fn/static) {}" ,
456460 name) ;
457- self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , modifiers , vis) ) ;
461+ self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
458462 }
459463 Def :: Trait ( def_id) => {
460464 debug ! ( "(building reduced graph for external crate) building type {}" , name) ;
@@ -480,16 +484,16 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
480484 }
481485 Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) => {
482486 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) ) ;
484488 }
485489 Def :: Struct ( def_id)
486490 if self . session . cstore . tuple_struct_definition_if_ctor ( def_id) . is_none ( ) => {
487491 debug ! ( "(building reduced graph for external crate) building type and value for {}" ,
488492 name) ;
489- self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , modifiers , vis) ) ;
493+ self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
490494 if let Some ( ctor_def_id) = self . session . cstore . struct_ctor_def_id ( def_id) {
491495 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) ) ;
493497 }
494498
495499 // Record the def ID and fields of this struct.
0 commit comments