@@ -294,6 +294,18 @@ pub enum DuplicateCheckingMode {
294
294
OverwriteDuplicates
295
295
}
296
296
297
+ // Returns the namespace associated with the given duplicate checking mode,
298
+ // or fails for OverwriteDuplicates. This is used for error messages.
299
+ pub fn namespace_for_duplicate_checking_mode ( mode : DuplicateCheckingMode )
300
+ -> Namespace {
301
+ match mode {
302
+ ForbidDuplicateModules | ForbidDuplicateTypes |
303
+ ForbidDuplicateTypesAndValues => TypeNS ,
304
+ ForbidDuplicateValues => ValueNS ,
305
+ OverwriteDuplicates => fail ! ( "OverwriteDuplicates has no namespace" )
306
+ }
307
+ }
308
+
297
309
/// One local scope.
298
310
pub struct Rib {
299
311
bindings : @mut HashMap < ident , def_like > ,
@@ -995,43 +1007,37 @@ impl Resolver {
995
1007
// nothing.
996
1008
997
1009
let mut is_duplicate = false ;
998
- let ns = match duplicate_checking_mode {
1010
+ match duplicate_checking_mode {
999
1011
ForbidDuplicateModules => {
1000
- is_duplicate = child . get_module_if_available ( ) . is_some ( ) ;
1001
- Some ( TypeNS )
1012
+ is_duplicate =
1013
+ child . get_module_if_available ( ) . is_some ( ) ;
1002
1014
}
1003
1015
ForbidDuplicateTypes => {
1004
1016
match child. def_for_namespace ( TypeNS ) {
1005
1017
Some ( def_mod( _) ) | None => { }
1006
1018
Some ( _) => is_duplicate = true
1007
1019
}
1008
- Some ( TypeNS )
1009
1020
}
1010
1021
ForbidDuplicateValues => {
1011
1022
is_duplicate = child. defined_in_namespace ( ValueNS ) ;
1012
- Some ( ValueNS )
1013
1023
}
1014
1024
ForbidDuplicateTypesAndValues => {
1015
- let mut n = None ;
1016
1025
match child. def_for_namespace ( TypeNS ) {
1017
1026
Some ( def_mod( _) ) | None => { }
1018
- Some ( _) => {
1019
- n = Some ( TypeNS ) ;
1020
- is_duplicate = true ;
1021
- }
1027
+ Some ( _) => is_duplicate = true
1022
1028
} ;
1023
1029
if child. defined_in_namespace ( ValueNS ) {
1024
1030
is_duplicate = true ;
1025
- n = Some ( ValueNS ) ;
1026
1031
}
1027
- n
1028
1032
}
1029
- OverwriteDuplicates => None
1030
- } ;
1031
- if is_duplicate {
1033
+ OverwriteDuplicates => { }
1034
+ }
1035
+ if duplicate_checking_mode != OverwriteDuplicates &&
1036
+ is_duplicate {
1032
1037
// Return an error here by looking up the namespace that
1033
1038
// had the duplicate.
1034
- let ns = ns. unwrap ( ) ;
1039
+ let ns = namespace_for_duplicate_checking_mode (
1040
+ duplicate_checking_mode) ;
1035
1041
self . session . span_err ( sp,
1036
1042
fmt ! ( "duplicate definition of %s `%s`" ,
1037
1043
namespace_to_str( ns) ,
@@ -1189,22 +1195,22 @@ impl Resolver {
1189
1195
1190
1196
// These items live in both the type and value namespaces.
1191
1197
item_struct( struct_def, _) => {
1192
- // Adding to both Type and Value namespaces or just Type?
1193
- let ( forbid, ctor_id) = match struct_def. ctor_id {
1194
- Some ( ctor_id) => ( ForbidDuplicateTypesAndValues , Some ( ctor_id) ) ,
1195
- None => ( ForbidDuplicateTypes , None )
1196
- } ;
1197
-
1198
- let ( name_bindings, new_parent) = self . add_child ( ident, parent, forbid, sp) ;
1198
+ let ( name_bindings, new_parent) =
1199
+ self . add_child ( ident, parent, ForbidDuplicateTypes , sp) ;
1199
1200
1200
- // Define a name in the type namespace.
1201
- name_bindings . define_type ( privacy, def_ty ( local_def ( item. id ) ) , sp) ;
1201
+ name_bindings . define_type (
1202
+ privacy, def_ty ( local_def ( item. id ) ) , sp) ;
1202
1203
1203
- // If this is a newtype or unit-like struct, define a name
1204
- // in the value namespace as well
1205
- do ctor_id. while_some |cid| {
1206
- name_bindings. define_value ( privacy, def_struct ( local_def ( cid) ) , sp) ;
1207
- None
1204
+ // If this struct is tuple-like or enum-like, define a name
1205
+ // in the value namespace.
1206
+ match struct_def. ctor_id {
1207
+ None => { }
1208
+ Some ( ctor_id) => {
1209
+ name_bindings. define_value (
1210
+ privacy,
1211
+ def_struct ( local_def ( ctor_id) ) ,
1212
+ sp) ;
1213
+ }
1208
1214
}
1209
1215
1210
1216
// Record the def ID of this struct.
0 commit comments