@@ -31,7 +31,6 @@ use std::rc::Rc;
31
31
32
32
use syntax:: ast:: Name ;
33
33
use syntax:: attr;
34
- use syntax:: parse:: token;
35
34
36
35
use syntax:: ast:: { self , Block , ForeignItem , ForeignItemKind , Item , ItemKind } ;
37
36
use syntax:: ast:: { Mutability , StmtKind , TraitItem , TraitItemKind } ;
@@ -77,6 +76,12 @@ impl<'b> Resolver<'b> {
77
76
} )
78
77
}
79
78
79
+ fn insert_field_names ( & mut self , def_id : DefId , field_names : Vec < Name > ) {
80
+ if !field_names. is_empty ( ) {
81
+ self . field_names . insert ( def_id, field_names) ;
82
+ }
83
+ }
84
+
80
85
/// Constructs the reduced graph for one item.
81
86
fn build_reduced_graph_for_item ( & mut self , item : & Item , expansion : Mark ) {
82
87
let parent = self . current_module ;
@@ -301,28 +306,26 @@ impl<'b> Resolver<'b> {
301
306
self . define ( parent, name, ValueNS , ( ctor_def, sp, vis) ) ;
302
307
}
303
308
304
- // Record the def ID and fields of this struct .
305
- let field_names = struct_def. fields ( ) . iter ( ) . enumerate ( ) . map ( | ( index , field) | {
309
+ // Record field names for error reporting .
310
+ let field_names = struct_def. fields ( ) . iter ( ) . filter_map ( | field| {
306
311
self . resolve_visibility ( & field. vis ) ;
307
312
field. ident . map ( |ident| ident. name )
308
- . unwrap_or_else ( || token:: intern ( & index. to_string ( ) ) )
309
313
} ) . collect ( ) ;
310
314
let item_def_id = self . definitions . local_def_id ( item. id ) ;
311
- self . structs . insert ( item_def_id, field_names) ;
315
+ self . insert_field_names ( item_def_id, field_names) ;
312
316
}
313
317
314
318
ItemKind :: Union ( ref vdata, _) => {
315
319
let def = Def :: Union ( self . definitions . local_def_id ( item. id ) ) ;
316
320
self . define ( parent, name, TypeNS , ( def, sp, vis) ) ;
317
321
318
- // Record the def ID and fields of this union .
319
- let field_names = vdata. fields ( ) . iter ( ) . enumerate ( ) . map ( | ( index , field) | {
322
+ // Record field names for error reporting .
323
+ let field_names = vdata. fields ( ) . iter ( ) . filter_map ( | field| {
320
324
self . resolve_visibility ( & field. vis ) ;
321
325
field. ident . map ( |ident| ident. name )
322
- . unwrap_or_else ( || token:: intern ( & index. to_string ( ) ) )
323
326
} ) . collect ( ) ;
324
327
let item_def_id = self . definitions . local_def_id ( item. id ) ;
325
- self . structs . insert ( item_def_id, field_names) ;
328
+ self . insert_field_names ( item_def_id, field_names) ;
326
329
}
327
330
328
331
ItemKind :: DefaultImpl ( ..) | ItemKind :: Impl ( ..) => { }
@@ -347,18 +350,17 @@ impl<'b> Resolver<'b> {
347
350
parent : Module < ' b > ,
348
351
vis : ty:: Visibility ) {
349
352
let name = variant. node . name . name ;
350
- let ctor_kind = CtorKind :: from_vdata ( & variant. node . data ) ;
351
- if variant. node . data . is_struct ( ) {
352
- // Not adding fields for variants as they are not accessed with a self receiver
353
- let variant_def_id = self . definitions . local_def_id ( variant. node . data . id ( ) ) ;
354
- self . structs . insert ( variant_def_id, Vec :: new ( ) ) ;
355
- }
353
+ let def_id = self . definitions . local_def_id ( variant. node . data . id ( ) ) ;
356
354
357
- // All variants are defined in both type and value namespaces as future-proofing.
358
- let def = Def :: Variant ( self . definitions . local_def_id ( variant. node . data . id ( ) ) ) ;
359
- let ctor_def = Def :: VariantCtor ( self . definitions . local_def_id ( variant. node . data . id ( ) ) ,
360
- ctor_kind) ;
355
+ // Define a name in the type namespace.
356
+ let def = Def :: Variant ( def_id) ;
361
357
self . define ( parent, name, TypeNS , ( def, variant. span , vis) ) ;
358
+
359
+ // Define a constructor name in the value namespace.
360
+ // Braced variants, unlike structs, generate unusable names in
361
+ // value namespace, they are reserved for possible future use.
362
+ let ctor_kind = CtorKind :: from_vdata ( & variant. node . data ) ;
363
+ let ctor_def = Def :: VariantCtor ( def_id, ctor_kind) ;
362
364
self . define ( parent, name, ValueNS , ( ctor_def, variant. span , vis) ) ;
363
365
}
364
366
@@ -407,79 +409,55 @@ impl<'b> Resolver<'b> {
407
409
} ;
408
410
409
411
match def {
410
- Def :: Mod ( _) | Def :: Enum ( ..) => {
411
- debug ! ( "(building reduced graph for external crate) building module {} {:?}" ,
412
- name, vis) ;
412
+ Def :: Mod ( ..) | Def :: Enum ( ..) => {
413
413
let module = self . new_module ( parent, ModuleKind :: Def ( def, name) , false ) ;
414
- let _ = self . try_define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
414
+ self . define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
415
415
}
416
416
Def :: Variant ( ..) => {
417
- debug ! ( "(building reduced graph for external crate) building variant {}" , name) ;
418
- // All variants are defined in both type and value namespaces as future-proofing.
419
- let vkind = self . session . cstore . variant_kind ( def_id) . unwrap ( ) ;
420
- let _ = self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
421
- if vkind == ty:: VariantKind :: Struct {
422
- // Not adding fields for variants as they are not accessed with a self receiver
423
- self . structs . insert ( def_id, Vec :: new ( ) ) ;
424
- }
417
+ self . define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
425
418
}
426
419
Def :: VariantCtor ( ..) => {
427
- let _ = self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
420
+ self . define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
428
421
}
429
422
Def :: Fn ( ..) |
430
423
Def :: Static ( ..) |
431
424
Def :: Const ( ..) |
432
425
Def :: AssociatedConst ( ..) |
433
426
Def :: Method ( ..) => {
434
- debug ! ( "(building reduced graph for external crate) building value (fn/static) {}" ,
435
- name) ;
436
- let _ = self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
427
+ self . define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
437
428
}
438
- Def :: Trait ( _) => {
439
- debug ! ( "(building reduced graph for external crate) building type {}" , name) ;
440
-
441
- // If this is a trait, add all the trait item names to the trait
442
- // info.
429
+ Def :: Trait ( ..) => {
430
+ let module = self . new_module ( parent, ModuleKind :: Def ( def, name) , false ) ;
431
+ self . define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
443
432
433
+ // If this is a trait, add all the trait item names to the trait info.
444
434
let trait_item_def_ids = self . session . cstore . impl_or_trait_items ( def_id) ;
445
- for & trait_item_def in & trait_item_def_ids {
446
- let trait_item_name =
447
- self . session . cstore . def_key ( trait_item_def)
448
- . disambiguated_data . data . get_opt_name ( )
449
- . expect ( "opt_item_name returned None for trait" ) ;
450
-
451
- debug ! ( "(building reduced graph for external crate) ... adding trait item \
452
- '{}'",
453
- trait_item_name) ;
454
-
435
+ for trait_item_def_id in trait_item_def_ids {
436
+ let trait_item_name = self . session . cstore . def_key ( trait_item_def_id)
437
+ . disambiguated_data . data . get_opt_name ( )
438
+ . expect ( "opt_item_name returned None for trait" ) ;
455
439
self . trait_item_map . insert ( ( trait_item_name, def_id) , false ) ;
456
440
}
457
-
458
- let module = self . new_module ( parent, ModuleKind :: Def ( def, name) , false ) ;
459
- let _ = self . try_define ( parent, name, TypeNS , ( module, DUMMY_SP , vis) ) ;
460
441
}
461
442
Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) => {
462
- debug ! ( "(building reduced graph for external crate) building type {}" , name) ;
463
- let _ = self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
443
+ self . define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
464
444
}
465
445
Def :: Struct ( ..) => {
466
- debug ! ( "(building reduced graph for external crate) building type and value for {}" ,
467
- name) ;
468
- let _ = self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
446
+ self . define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
469
447
470
- // Record the def ID and fields of this struct .
471
- let fields = self . session . cstore . struct_field_names ( def_id) ;
472
- self . structs . insert ( def_id, fields ) ;
448
+ // Record field names for error reporting .
449
+ let field_names = self . session . cstore . struct_field_names ( def_id) ;
450
+ self . insert_field_names ( def_id, field_names ) ;
473
451
}
474
452
Def :: StructCtor ( ..) => {
475
- let _ = self . try_define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
453
+ self . define ( parent, name, ValueNS , ( def, DUMMY_SP , vis) ) ;
476
454
}
477
- Def :: Union ( _ ) => {
478
- let _ = self . try_define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
455
+ Def :: Union ( .. ) => {
456
+ self . define ( parent, name, TypeNS , ( def, DUMMY_SP , vis) ) ;
479
457
480
- // Record the def ID and fields of this union .
481
- let fields = self . session . cstore . struct_field_names ( def_id) ;
482
- self . structs . insert ( def_id, fields ) ;
458
+ // Record field names for error reporting .
459
+ let field_names = self . session . cstore . struct_field_names ( def_id) ;
460
+ self . insert_field_names ( def_id, field_names ) ;
483
461
}
484
462
Def :: Local ( ..) |
485
463
Def :: PrimTy ( ..) |
@@ -488,7 +466,7 @@ impl<'b> Resolver<'b> {
488
466
Def :: Label ( ..) |
489
467
Def :: SelfTy ( ..) |
490
468
Def :: Err => {
491
- bug ! ( "didn't expect ` {:?}` " , def) ;
469
+ bug ! ( "unexpected definition: {:?}" , def) ;
492
470
}
493
471
}
494
472
}
0 commit comments