@@ -64,30 +64,51 @@ impl Module<'hir> {
64
64
}
65
65
66
66
pub ( crate ) fn push_item ( & mut self , new_item : Item < ' hir > ) {
67
- if let Some ( existing_item) =
68
- self . items . iter_mut ( ) . find ( |item| item. name ( ) == new_item. name ( ) )
69
- {
70
- if existing_item. from_glob {
71
- debug ! ( "push_item: {:?} shadowed by {:?}" , * existing_item, new_item) ;
72
- * existing_item = new_item;
73
- return ;
74
- } else if new_item. from_glob {
75
- return ;
67
+ if !new_item. name ( ) . is_empty ( ) {
68
+ // todo: also check namespace
69
+ if let Some ( existing_item) =
70
+ self . items . iter_mut ( ) . find ( |item| item. name ( ) == new_item. name ( ) )
71
+ {
72
+ match ( existing_item. from_glob , new_item. from_glob ) {
73
+ ( true , _) => {
74
+ // `existing_item` is from glob, no matter whether `new_item` is from glob
75
+ // `new_item` should always shadow `existing_item`
76
+ debug ! ( "push_item: {:?} shadowed by {:?}" , existing_item, new_item) ;
77
+ * existing_item = new_item;
78
+ return ;
79
+ }
80
+ ( false , true ) => {
81
+ // `existing_item` is not from glob but `new_item` is
82
+ // just keep `existing_item` and return at once
83
+ return ;
84
+ }
85
+ ( false , false ) => unreachable ! ( ) // todo: how to handle this?
86
+ }
76
87
}
77
88
}
89
+ // no item with same name and namespace exists, just collect `new_item`
78
90
self . items . push ( new_item) ;
79
91
}
80
92
81
- pub ( crate ) fn push_mod ( & mut self , new_item : Module < ' hir > ) {
82
- if let Some ( existing_mod) = self . mods . iter_mut ( ) . find ( |mod_| mod_. name == new_item. name ) {
83
- if existing_mod. from_glob {
84
- debug ! ( "push_mod: {:?} shadowed by {:?}" , existing_mod. name, new_item. name) ;
85
- * existing_mod = new_item;
86
- return ;
87
- } else if new_item. from_glob {
88
- return ;
93
+ pub ( crate ) fn push_mod ( & mut self , new_mod : Module < ' hir > ) {
94
+ if let Some ( existing_mod) = self . mods . iter_mut ( ) . find ( |mod_| mod_. name == new_mod. name ) {
95
+ match ( existing_mod. from_glob , new_mod. from_glob ) {
96
+ ( true , _) => {
97
+ // `existing_mod` is from glob, no matter whether `new_mod` is from glob
98
+ // `new_mod` should always shadow `existing_mod`
99
+ debug ! ( "push_mod: {:?} shadowed by {:?}" , existing_mod. name, new_mod. name) ;
100
+ * existing_mod = new_mod;
101
+ return ;
102
+ } ,
103
+ ( false , true ) => {
104
+ // `existing_mod` is not from glob but `new_mod` is
105
+ // just keep `existing_mod` and return at once
106
+ return ;
107
+ } ,
108
+ ( false , false ) => unreachable ! ( ) ,
89
109
}
90
110
}
91
- self . mods . push ( new_item) ;
111
+ // no mod with same name exists, just collect `new_mod`
112
+ self . mods . push ( new_mod) ;
92
113
}
93
114
}
0 commit comments