@@ -27,7 +27,6 @@ use resolve_imports::Shadowable;
27
27
use { resolve_error, ResolutionError } ;
28
28
29
29
use self :: DuplicateCheckingMode :: * ;
30
- use self :: NamespaceError :: * ;
31
30
32
31
use rustc:: metadata:: csearch;
33
32
use rustc:: metadata:: decoder:: { DefLike , DlDef , DlField , DlImpl } ;
@@ -60,29 +59,12 @@ use std::rc::Rc;
60
59
// another item exists with the same name in some namespace.
61
60
#[ derive( Copy , Clone , PartialEq ) ]
62
61
enum DuplicateCheckingMode {
63
- ForbidDuplicateModules ,
64
- ForbidDuplicateTypesAndModules ,
62
+ ForbidDuplicateTypes ,
65
63
ForbidDuplicateValues ,
66
64
ForbidDuplicateTypesAndValues ,
67
65
OverwriteDuplicates ,
68
66
}
69
67
70
- #[ derive( Copy , Clone , PartialEq ) ]
71
- enum NamespaceError {
72
- NoError ,
73
- ModuleError ,
74
- TypeError ,
75
- ValueError ,
76
- }
77
-
78
- fn namespace_error_to_string ( ns : NamespaceError ) -> & ' static str {
79
- match ns {
80
- NoError => "" ,
81
- ModuleError | TypeError => "type or module" ,
82
- ValueError => "value" ,
83
- }
84
- }
85
-
86
68
struct GraphBuilder < ' a , ' b : ' a , ' tcx : ' b > {
87
69
resolver : & ' a mut Resolver < ' b , ' tcx > ,
88
70
}
@@ -112,27 +94,16 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
112
94
visit:: walk_crate ( & mut visitor, krate) ;
113
95
}
114
96
115
- /// Adds a new child item to the module definition of the parent node and
116
- /// returns its corresponding name bindings as well as the current parent.
117
- /// Or, if we're inside a block, creates (or reuses) an anonymous module
118
- /// corresponding to the innermost block ID and returns the name bindings
119
- /// as well as the newly-created parent.
120
- ///
121
- /// # Panics
122
- ///
123
- /// Panics if this node does not have a module definition and we are not inside
124
- /// a block.
97
+ /// Adds a new child item to the module definition of the parent node,
98
+ /// or if there is already a child, does duplicate checking on the child.
99
+ /// Returns the child's corresponding name bindings.
125
100
fn add_child ( & self ,
126
101
name : Name ,
127
102
parent : & Rc < Module > ,
128
103
duplicate_checking_mode : DuplicateCheckingMode ,
129
104
// For printing errors
130
105
sp : Span )
131
106
-> NameBindings {
132
- // If this is the immediate descendant of a module, then we add the
133
- // child name directly. Otherwise, we create or reuse an anonymous
134
- // module and add the child to that.
135
-
136
107
self . check_for_conflicts_between_external_crates_and_items ( & * * parent, name, sp) ;
137
108
138
109
// Add or reuse the child.
@@ -146,79 +117,33 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
146
117
Some ( child) => {
147
118
// Enforce the duplicate checking mode:
148
119
//
149
- // * If we're requesting duplicate module checking, check that
150
- // there isn't a module in the module with the same name.
151
- //
152
120
// * If we're requesting duplicate type checking, check that
153
- // there isn't a type in the module with the same name .
121
+ // the name isn't defined in the type namespace .
154
122
//
155
123
// * If we're requesting duplicate value checking, check that
156
- // there isn't a value in the module with the same name .
124
+ // the name isn't defined in the value namespace .
157
125
//
158
- // * If we're requesting duplicate type checking and duplicate
159
- // value checking, check that there isn't a duplicate type
160
- // and a duplicate value with the same name.
126
+ // * If we're requesting duplicate type and value checking,
127
+ // check that the name isn't defined in either namespace.
161
128
//
162
129
// * If no duplicate checking was requested at all, do
163
130
// nothing.
164
131
165
- let mut duplicate_type = NoError ;
166
132
let ns = match duplicate_checking_mode {
167
- ForbidDuplicateModules => {
168
- if child. get_module_if_available ( ) . is_some ( ) {
169
- duplicate_type = ModuleError ;
170
- }
171
- Some ( TypeNS )
172
- }
173
- ForbidDuplicateTypesAndModules => {
174
- if child. type_ns . defined ( ) {
175
- duplicate_type = TypeError ;
176
- }
177
- Some ( TypeNS )
178
- }
179
- ForbidDuplicateValues => {
180
- if child. value_ns . defined ( ) {
181
- duplicate_type = ValueError ;
182
- }
183
- Some ( ValueNS )
184
- }
185
- ForbidDuplicateTypesAndValues => {
186
- let mut n = None ;
187
- match child. type_ns . def ( ) {
188
- Some ( DefMod ( _) ) | None => { }
189
- Some ( _) => {
190
- n = Some ( TypeNS ) ;
191
- duplicate_type = TypeError ;
192
- }
193
- }
194
- if child. value_ns . defined ( ) {
195
- duplicate_type = ValueError ;
196
- n = Some ( ValueNS ) ;
197
- }
198
- n
199
- }
200
- OverwriteDuplicates => None ,
133
+ ForbidDuplicateTypes if child. type_ns . defined ( ) => TypeNS ,
134
+ ForbidDuplicateValues if child. value_ns . defined ( ) => ValueNS ,
135
+ ForbidDuplicateTypesAndValues if child. type_ns . defined ( ) => TypeNS ,
136
+ ForbidDuplicateTypesAndValues if child. value_ns . defined ( ) => ValueNS ,
137
+ _ => return child,
201
138
} ;
202
- if duplicate_type != NoError {
203
- // Return an error here by looking up the namespace that
204
- // had the duplicate.
205
- let ns = ns. unwrap ( ) ;
206
- resolve_error (
207
- self ,
208
- sp,
209
- ResolutionError :: DuplicateDefinition (
210
- namespace_error_to_string ( duplicate_type) ,
211
- name)
212
- ) ;
213
- {
214
- let r = child[ ns] . span ( ) ;
215
- if let Some ( sp) = r {
216
- self . session . span_note ( sp,
217
- & format ! ( "first definition of {} `{}` here" ,
218
- namespace_error_to_string( duplicate_type) ,
219
- name) ) ;
220
- }
221
- }
139
+
140
+ // Record an error here by looking up the namespace that had the duplicate
141
+ let ns_str = match ns { TypeNS => "type or module" , ValueNS => "value" } ;
142
+ resolve_error ( self , sp, ResolutionError :: DuplicateDefinition ( ns_str, name) ) ;
143
+
144
+ if let Some ( sp) = child[ ns] . span ( ) {
145
+ let note = format ! ( "first definition of {} `{}` here" , ns_str, name) ;
146
+ self . session . span_note ( sp, & note) ;
222
147
}
223
148
child
224
149
}
@@ -409,29 +334,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
409
334
}
410
335
411
336
ItemMod ( ..) => {
412
- let child = parent. children . borrow ( ) . get ( & name) . cloned ( ) ;
413
- if let Some ( child) = child {
414
- // check if there's struct of the same name already defined
415
- if child. type_ns . defined ( ) &&
416
- child. get_module_if_available ( ) . is_none ( ) {
417
- self . session . span_warn ( sp,
418
- & format ! ( "duplicate definition of {} `{}`. \
419
- Defining a module and a struct with \
420
- the same name will be disallowed soon.",
421
- namespace_error_to_string( TypeError ) ,
422
- name) ) ;
423
- {
424
- let r = child. type_ns . span ( ) ;
425
- if let Some ( sp) = r {
426
- self . session . span_note ( sp,
427
- & format ! ( "first definition of {} `{}` here" ,
428
- namespace_error_to_string( TypeError ) ,
429
- name) ) ;
430
- }
431
- }
432
- }
433
- }
434
- let name_bindings = self . add_child ( name, parent, ForbidDuplicateModules , sp) ;
337
+ let name_bindings = self . add_child ( name, parent, ForbidDuplicateTypes , sp) ;
435
338
436
339
let parent_link = self . get_parent_link ( parent, name) ;
437
340
let def = DefMod ( self . ast_map . local_def_id ( item. id ) ) ;
@@ -469,7 +372,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
469
372
ItemTy ( ..) => {
470
373
let name_bindings = self . add_child ( name,
471
374
parent,
472
- ForbidDuplicateTypesAndModules ,
375
+ ForbidDuplicateTypes ,
473
376
sp) ;
474
377
475
378
let parent_link = self . get_parent_link ( parent, name) ;
@@ -481,7 +384,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
481
384
ItemEnum ( ref enum_definition, _) => {
482
385
let name_bindings = self . add_child ( name,
483
386
parent,
484
- ForbidDuplicateTypesAndModules ,
387
+ ForbidDuplicateTypes ,
485
388
sp) ;
486
389
487
390
let parent_link = self . get_parent_link ( parent, name) ;
@@ -501,31 +404,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
501
404
ItemStruct ( ref struct_def, _) => {
502
405
// Adding to both Type and Value namespaces or just Type?
503
406
let ( forbid, ctor_id) = if struct_def. is_struct ( ) {
504
- ( ForbidDuplicateTypesAndModules , None )
407
+ ( ForbidDuplicateTypes , None )
505
408
} else {
506
- let child = parent. children . borrow ( ) . get ( & name) . cloned ( ) ;
507
- if let Some ( child) = child {
508
- // check if theres a DefMod
509
- if let Some ( DefMod ( _) ) = child. type_ns . def ( ) {
510
- self . session . span_warn ( sp,
511
- & format ! ( "duplicate definition of {} `{}`. \
512
- Defining a module and a struct \
513
- with the same name will be \
514
- disallowed soon.",
515
- namespace_error_to_string( TypeError ) ,
516
- name) ) ;
517
- {
518
- let r = child. type_ns . span ( ) ;
519
- if let Some ( sp) = r {
520
- self . session
521
- . span_note ( sp,
522
- & format ! ( "first definition of {} `{}` here" ,
523
- namespace_error_to_string( TypeError ) ,
524
- name) ) ;
525
- }
526
- }
527
- }
528
- }
529
409
( ForbidDuplicateTypesAndValues , Some ( struct_def. id ( ) ) )
530
410
} ;
531
411
@@ -566,7 +446,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
566
446
ItemTrait ( _, _, _, ref items) => {
567
447
let name_bindings = self . add_child ( name,
568
448
parent,
569
- ForbidDuplicateTypesAndModules ,
449
+ ForbidDuplicateTypes ,
570
450
sp) ;
571
451
572
452
let def_id = self . ast_map . local_def_id ( item. id ) ;
0 commit comments