@@ -11,7 +11,7 @@ use either::Either;
11
11
use hir_expand:: {
12
12
ast_id_map:: FileAstId ,
13
13
attrs:: { Attr , AttrId } ,
14
- builtin_attr_macro:: find_builtin_attr,
14
+ builtin_attr_macro:: { find_builtin_attr, BuiltinAttrExpander } ,
15
15
builtin_derive_macro:: find_builtin_derive,
16
16
builtin_fn_macro:: find_builtin_macro,
17
17
name:: { name, AsName , Name } ,
@@ -98,12 +98,12 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
98
98
} ;
99
99
(
100
100
name. as_name ( ) ,
101
- if it. expander . should_expand ( ) {
101
+ if it. disabled {
102
+ CustomProcMacroExpander :: disabled ( )
103
+ } else {
102
104
CustomProcMacroExpander :: new ( hir_expand:: proc_macro:: ProcMacroId (
103
105
idx as u32 ,
104
106
) )
105
- } else {
106
- CustomProcMacroExpander :: disabled ( )
107
107
} ,
108
108
)
109
109
} )
@@ -608,9 +608,6 @@ impl DefCollector<'_> {
608
608
id : ItemTreeId < item_tree:: Function > ,
609
609
fn_id : FunctionId ,
610
610
) {
611
- if self . def_map . block . is_some ( ) {
612
- return ;
613
- }
614
611
let kind = def. kind . to_basedb_kind ( ) ;
615
612
let ( expander, kind) =
616
613
match self . proc_macros . as_ref ( ) . map ( |it| it. iter ( ) . find ( |( n, _) | n == & def. name ) ) {
@@ -1124,9 +1121,16 @@ impl DefCollector<'_> {
1124
1121
let mut push_resolved = |directive : & MacroDirective , call_id| {
1125
1122
resolved. push ( ( directive. module_id , directive. depth , directive. container , call_id) ) ;
1126
1123
} ;
1124
+
1125
+ #[ derive( PartialEq , Eq ) ]
1126
+ enum Resolved {
1127
+ Yes ,
1128
+ No ,
1129
+ }
1130
+
1127
1131
let mut res = ReachedFixedPoint :: Yes ;
1128
1132
// Retain unresolved macros after this round of resolution.
1129
- macros . retain ( |directive| {
1133
+ let mut retain = |directive : & MacroDirective | {
1130
1134
let subns = match & directive. kind {
1131
1135
MacroDirectiveKind :: FnLike { .. } => MacroSubNs :: Bang ,
1132
1136
MacroDirectiveKind :: Attr { .. } | MacroDirectiveKind :: Derive { .. } => {
@@ -1161,34 +1165,10 @@ impl DefCollector<'_> {
1161
1165
. scope
1162
1166
. add_macro_invoc ( ast_id. ast_id , call_id) ;
1163
1167
1164
- let loc: MacroCallLoc = self . db . lookup_intern_macro_call ( call_id) ;
1165
-
1166
- if let MacroDefKind :: ProcMacro ( expander, _, _) = loc. def . kind {
1167
- if expander. is_dummy ( ) {
1168
- // If there's no expander for the proc macro (e.g.
1169
- // because proc macros are disabled, or building the
1170
- // proc macro crate failed), report this and skip
1171
- // expansion like we would if it was disabled
1172
- self . def_map . diagnostics . push (
1173
- DefDiagnostic :: unresolved_proc_macro (
1174
- directive. module_id ,
1175
- loc. kind ,
1176
- loc. def . krate ,
1177
- ) ,
1178
- ) ;
1179
-
1180
- res = ReachedFixedPoint :: No ;
1181
- return false ;
1182
- } else if expander. is_disabled ( ) {
1183
- res = ReachedFixedPoint :: No ;
1184
- return false ;
1185
- }
1186
- }
1187
-
1188
1168
push_resolved ( directive, call_id) ;
1189
1169
1190
1170
res = ReachedFixedPoint :: No ;
1191
- return false ;
1171
+ return Resolved :: Yes ;
1192
1172
}
1193
1173
}
1194
1174
MacroDirectiveKind :: Derive { ast_id, derive_attr, derive_pos, call_site } => {
@@ -1227,7 +1207,7 @@ impl DefCollector<'_> {
1227
1207
1228
1208
push_resolved ( directive, call_id) ;
1229
1209
res = ReachedFixedPoint :: No ;
1230
- return false ;
1210
+ return Resolved :: Yes ;
1231
1211
}
1232
1212
}
1233
1213
MacroDirectiveKind :: Attr { ast_id : file_ast_id, mod_item, attr, tree } => {
@@ -1250,7 +1230,7 @@ impl DefCollector<'_> {
1250
1230
}
1251
1231
. collect ( & [ * mod_item] , directive. container ) ;
1252
1232
res = ReachedFixedPoint :: No ;
1253
- false
1233
+ Resolved :: Yes
1254
1234
} ;
1255
1235
1256
1236
if let Some ( ident) = path. as_ident ( ) {
@@ -1266,13 +1246,18 @@ impl DefCollector<'_> {
1266
1246
1267
1247
let def = match resolver_def_id ( path. clone ( ) ) {
1268
1248
Some ( def) if def. is_attribute ( ) => def,
1269
- _ => return true ,
1249
+ _ => return Resolved :: No ,
1270
1250
} ;
1271
- if matches ! (
1272
- def,
1273
- MacroDefId { kind: MacroDefKind :: BuiltInAttr ( expander, _) , .. }
1274
- if expander. is_derive( )
1275
- ) {
1251
+
1252
+ if let MacroDefId {
1253
+ kind :
1254
+ MacroDefKind :: BuiltInAttr (
1255
+ BuiltinAttrExpander :: Derive | BuiltinAttrExpander :: DeriveConst ,
1256
+ _,
1257
+ ) ,
1258
+ ..
1259
+ } = def
1260
+ {
1276
1261
// Resolved to `#[derive]`, we don't actually expand this attribute like
1277
1262
// normal (as that would just be an identity expansion with extra output)
1278
1263
// Instead we treat derive attributes special and apply them separately.
@@ -1345,16 +1330,6 @@ impl DefCollector<'_> {
1345
1330
let call_id =
1346
1331
attr_macro_as_call_id ( self . db , file_ast_id, attr, self . def_map . krate , def) ;
1347
1332
1348
- // If proc attribute macro expansion is disabled, skip expanding it here
1349
- if !self . db . expand_proc_attr_macros ( ) {
1350
- self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_proc_macro (
1351
- directive. module_id ,
1352
- self . db . lookup_intern_macro_call ( call_id) . kind ,
1353
- def. krate ,
1354
- ) ) ;
1355
- return recollect_without ( self ) ;
1356
- }
1357
-
1358
1333
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1359
1334
// due to duplicating functions into macro expansions
1360
1335
if matches ! (
@@ -1366,19 +1341,29 @@ impl DefCollector<'_> {
1366
1341
}
1367
1342
1368
1343
if let MacroDefKind :: ProcMacro ( exp, ..) = def. kind {
1369
- if exp. is_dummy ( ) {
1370
- // If there's no expander for the proc macro (e.g.
1371
- // because proc macros are disabled, or building the
1372
- // proc macro crate failed), report this and skip
1373
- // expansion like we would if it was disabled
1344
+ // If proc attribute macro expansion is disabled, skip expanding it here
1345
+ if !self . db . expand_proc_attr_macros ( ) {
1374
1346
self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_proc_macro (
1375
1347
directive. module_id ,
1376
1348
self . db . lookup_intern_macro_call ( call_id) . kind ,
1377
1349
def. krate ,
1378
1350
) ) ;
1351
+ return recollect_without ( self ) ;
1352
+ }
1379
1353
1354
+ // If there's no expander for the proc macro (e.g.
1355
+ // because proc macros are disabled, or building the
1356
+ // proc macro crate failed), report this and skip
1357
+ // expansion like we would if it was disabled
1358
+ if exp. is_dummy ( ) {
1359
+ self . def_map . diagnostics . push ( DefDiagnostic :: unresolved_proc_macro (
1360
+ directive. module_id ,
1361
+ self . db . lookup_intern_macro_call ( call_id) . kind ,
1362
+ def. krate ,
1363
+ ) ) ;
1380
1364
return recollect_without ( self ) ;
1381
- } else if exp. is_disabled ( ) {
1365
+ }
1366
+ if exp. is_disabled ( ) {
1382
1367
return recollect_without ( self ) ;
1383
1368
}
1384
1369
}
@@ -1389,12 +1374,13 @@ impl DefCollector<'_> {
1389
1374
1390
1375
push_resolved ( directive, call_id) ;
1391
1376
res = ReachedFixedPoint :: No ;
1392
- return false ;
1377
+ return Resolved :: Yes ;
1393
1378
}
1394
1379
}
1395
1380
1396
- true
1397
- } ) ;
1381
+ Resolved :: No
1382
+ } ;
1383
+ macros. retain ( |it| retain ( it) == Resolved :: No ) ;
1398
1384
// Attribute resolution can add unresolved macro invocations, so concatenate the lists.
1399
1385
macros. extend ( mem:: take ( & mut self . unresolved_macros ) ) ;
1400
1386
self . unresolved_macros = macros;
@@ -1704,7 +1690,11 @@ impl ModCollector<'_, '_> {
1704
1690
FunctionLoc { container, id : ItemTreeId :: new ( self . tree_id , id) } . intern ( db) ;
1705
1691
1706
1692
let vis = resolve_vis ( def_map, & self . item_tree [ it. visibility ] ) ;
1707
- if self . def_collector . is_proc_macro && self . module_id == DefMap :: ROOT {
1693
+
1694
+ if self . def_collector . def_map . block . is_none ( )
1695
+ && self . def_collector . is_proc_macro
1696
+ && self . module_id == DefMap :: ROOT
1697
+ {
1708
1698
if let Some ( proc_macro) = attrs. parse_proc_macro_decl ( & it. name ) {
1709
1699
self . def_collector . export_proc_macro (
1710
1700
proc_macro,
0 commit comments