@@ -156,33 +156,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
156
156
}
157
157
}
158
158
159
- pub ( crate ) fn get_macro ( & mut self , res : Res ) -> Option < MacroData > {
159
+ pub ( crate ) fn get_macro ( & mut self , res : Res ) -> Option < & MacroData > {
160
160
match res {
161
161
Res :: Def ( DefKind :: Macro ( ..) , def_id) => Some ( self . get_macro_by_def_id ( def_id) ) ,
162
- Res :: NonMacroAttr ( _) => {
163
- Some ( MacroData { ext : self . non_macro_attr . clone ( ) , macro_rules : false } )
164
- }
162
+ Res :: NonMacroAttr ( _) => Some ( & self . non_macro_attr ) ,
165
163
_ => None ,
166
164
}
167
165
}
168
166
169
- pub ( crate ) fn get_macro_by_def_id ( & mut self , def_id : DefId ) -> MacroData {
170
- if let Some ( macro_data ) = self . macro_map . get ( & def_id) {
171
- return macro_data . clone ( ) ;
167
+ pub ( crate ) fn get_macro_by_def_id ( & mut self , def_id : DefId ) -> & MacroData {
168
+ if self . macro_map . contains_key ( & def_id) {
169
+ return & self . macro_map [ & def_id ] ;
172
170
}
173
171
174
- let load_macro_untracked = self . cstore ( ) . load_macro_untracked ( def_id, self . tcx ) ;
175
- let ( ext, macro_rules) = match load_macro_untracked {
176
- LoadedMacro :: MacroDef ( item, edition) => (
177
- Lrc :: new ( self . compile_macro ( & item, edition) . 0 ) ,
178
- matches ! ( item. kind, ItemKind :: MacroDef ( def) if def. macro_rules) ,
179
- ) ,
180
- LoadedMacro :: ProcMacro ( extz) => ( Lrc :: new ( extz) , false ) ,
172
+ let loaded_macro = self . cstore ( ) . load_macro_untracked ( def_id, self . tcx ) ;
173
+ let macro_data = match loaded_macro {
174
+ LoadedMacro :: MacroDef ( item, edition) => self . compile_macro ( & item, edition) ,
175
+ LoadedMacro :: ProcMacro ( ext) => MacroData :: new ( Lrc :: new ( ext) ) ,
181
176
} ;
182
177
183
- let macro_data = MacroData { ext, macro_rules } ;
184
- self . macro_map . insert ( def_id, macro_data. clone ( ) ) ;
185
- macro_data
178
+ self . macro_map . entry ( def_id) . or_insert ( macro_data)
186
179
}
187
180
188
181
pub ( crate ) fn build_reduced_graph (
@@ -1175,16 +1168,10 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1175
1168
// Mark the given macro as unused unless its name starts with `_`.
1176
1169
// Macro uses will remove items from this set, and the remaining
1177
1170
// items will be reported as `unused_macros`.
1178
- fn insert_unused_macro (
1179
- & mut self ,
1180
- ident : Ident ,
1181
- def_id : LocalDefId ,
1182
- node_id : NodeId ,
1183
- rule_spans : & [ ( usize , Span ) ] ,
1184
- ) {
1171
+ fn insert_unused_macro ( & mut self , ident : Ident , def_id : LocalDefId , node_id : NodeId ) {
1185
1172
if !ident. as_str ( ) . starts_with ( '_' ) {
1186
1173
self . r . unused_macros . insert ( def_id, ( node_id, ident) ) ;
1187
- for ( rule_i, rule_span) in rule_spans . iter ( ) {
1174
+ for ( rule_i, rule_span) in & self . r . macro_map [ & def_id . to_def_id ( ) ] . rule_spans {
1188
1175
self . r . unused_macro_rules . insert ( ( def_id, * rule_i) , ( ident, * rule_span) ) ;
1189
1176
}
1190
1177
}
@@ -1194,24 +1181,24 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1194
1181
let parent_scope = self . parent_scope ;
1195
1182
let expansion = parent_scope. expansion ;
1196
1183
let def_id = self . r . local_def_id ( item. id ) ;
1197
- let ( ext , ident, span, macro_rules, rule_spans ) = match & item. kind {
1184
+ let ( macro_kind , ident, span, macro_rules) = match & item. kind {
1198
1185
ItemKind :: MacroDef ( def) => {
1199
- let ( ext, rule_spans) = self . r . compile_macro ( item, self . r . tcx . sess . edition ( ) ) ;
1200
- let ext = Lrc :: new ( ext) ;
1201
- ( ext, item. ident , item. span , def. macro_rules , rule_spans)
1186
+ let macro_kind = self . r . macro_map [ & def_id. to_def_id ( ) ] . ext . macro_kind ( ) ;
1187
+ ( macro_kind, item. ident , item. span , def. macro_rules )
1202
1188
}
1203
1189
ItemKind :: Fn ( ..) => match self . proc_macro_stub ( item) {
1204
1190
Some ( ( macro_kind, ident, span) ) => {
1191
+ let macro_data = MacroData :: new ( self . r . dummy_ext ( macro_kind) ) ;
1192
+ self . r . macro_map . insert ( def_id. to_def_id ( ) , macro_data) ;
1205
1193
self . r . proc_macro_stubs . insert ( def_id) ;
1206
- ( self . r . dummy_ext ( macro_kind) , ident, span, false , Vec :: new ( ) )
1194
+ ( macro_kind, ident, span, false )
1207
1195
}
1208
1196
None => return parent_scope. macro_rules ,
1209
1197
} ,
1210
1198
_ => unreachable ! ( ) ,
1211
1199
} ;
1212
1200
1213
- let res = Res :: Def ( DefKind :: Macro ( ext. macro_kind ( ) ) , def_id. to_def_id ( ) ) ;
1214
- self . r . macro_map . insert ( def_id. to_def_id ( ) , MacroData { ext, macro_rules } ) ;
1201
+ let res = Res :: Def ( DefKind :: Macro ( macro_kind) , def_id. to_def_id ( ) ) ;
1215
1202
self . r . local_macro_def_scopes . insert ( def_id, parent_scope. module ) ;
1216
1203
1217
1204
if macro_rules {
@@ -1245,7 +1232,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1245
1232
self . r . define ( self . r . graph_root , ident, MacroNS , import_binding) ;
1246
1233
} else {
1247
1234
self . r . check_reserved_macro_name ( ident, res) ;
1248
- self . insert_unused_macro ( ident, def_id, item. id , & rule_spans ) ;
1235
+ self . insert_unused_macro ( ident, def_id, item. id ) ;
1249
1236
}
1250
1237
self . r . visibilities . insert ( def_id, vis) ;
1251
1238
let scope = self . r . arenas . alloc_macro_rules_scope ( MacroRulesScope :: Binding (
@@ -1268,7 +1255,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1268
1255
_ => self . resolve_visibility ( & item. vis ) ,
1269
1256
} ;
1270
1257
if !vis. is_public ( ) {
1271
- self . insert_unused_macro ( ident, def_id, item. id , & rule_spans ) ;
1258
+ self . insert_unused_macro ( ident, def_id, item. id ) ;
1272
1259
}
1273
1260
self . r . define ( module, ident, MacroNS , ( res, vis, span, expansion) ) ;
1274
1261
self . r . visibilities . insert ( def_id, vis) ;
0 commit comments