@@ -1183,6 +1183,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
1183
1183
}
1184
1184
1185
1185
impl < ' a , ' b > Folder for MacroExpander < ' a , ' b > {
1186
+ fn fold_crate ( & mut self , c : Crate ) -> Crate {
1187
+ self . cx . filename = Some ( self . cx . parse_sess . codemap ( ) . span_to_filename ( c. span ) ) ;
1188
+ noop_fold_crate ( c, self )
1189
+ }
1190
+
1186
1191
fn fold_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
1187
1192
expand_expr ( expr, self )
1188
1193
}
@@ -1192,7 +1197,27 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1192
1197
}
1193
1198
1194
1199
fn fold_item ( & mut self , item : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
1195
- expand_item ( item, self )
1200
+ use std:: mem:: replace;
1201
+ let result;
1202
+ if let ast:: ItemKind :: Mod ( ast:: Mod { inner, .. } ) = item. node {
1203
+ if item. span . contains ( inner) {
1204
+ self . push_mod_path ( item. ident , & item. attrs ) ;
1205
+ result = expand_item ( item, self ) ;
1206
+ self . pop_mod_path ( ) ;
1207
+ } else {
1208
+ let filename = if inner != codemap:: DUMMY_SP {
1209
+ Some ( self . cx . parse_sess . codemap ( ) . span_to_filename ( inner) )
1210
+ } else { None } ;
1211
+ let orig_filename = replace ( & mut self . cx . filename , filename) ;
1212
+ let orig_mod_path_stack = replace ( & mut self . cx . mod_path_stack , Vec :: new ( ) ) ;
1213
+ result = expand_item ( item, self ) ;
1214
+ self . cx . filename = orig_filename;
1215
+ self . cx . mod_path_stack = orig_mod_path_stack;
1216
+ }
1217
+ } else {
1218
+ result = expand_item ( item, self ) ;
1219
+ }
1220
+ result
1196
1221
}
1197
1222
1198
1223
fn fold_item_kind ( & mut self , item : ast:: ItemKind ) -> ast:: ItemKind {
@@ -1204,7 +1229,10 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1204
1229
}
1205
1230
1206
1231
fn fold_block ( & mut self , block : P < Block > ) -> P < Block > {
1207
- expand_block ( block, self )
1232
+ let was_in_block = :: std:: mem:: replace ( & mut self . cx . in_block , true ) ;
1233
+ let result = expand_block ( block, self ) ;
1234
+ self . cx . in_block = was_in_block;
1235
+ result
1208
1236
}
1209
1237
1210
1238
fn fold_arm ( & mut self , arm : ast:: Arm ) -> ast:: Arm {
@@ -1230,6 +1258,21 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1230
1258
}
1231
1259
}
1232
1260
1261
+ impl < ' a , ' b > MacroExpander < ' a , ' b > {
1262
+ fn push_mod_path ( & mut self , id : Ident , attrs : & [ ast:: Attribute ] ) {
1263
+ let default_path = id. name . as_str ( ) ;
1264
+ let file_path = match :: attr:: first_attr_value_str_by_name ( attrs, "path" ) {
1265
+ Some ( d) => d,
1266
+ None => default_path,
1267
+ } ;
1268
+ self . cx . mod_path_stack . push ( file_path)
1269
+ }
1270
+
1271
+ fn pop_mod_path ( & mut self ) {
1272
+ self . cx . mod_path_stack . pop ( ) . unwrap ( ) ;
1273
+ }
1274
+ }
1275
+
1233
1276
fn new_span ( cx : & ExtCtxt , sp : Span ) -> Span {
1234
1277
/* this discards information in the case of macro-defining macros */
1235
1278
Span {
0 commit comments