Skip to content

Commit 3f0729f

Browse files
committed
expand: Move some more derive logic to rustc_builtin_macros
1 parent 75d1500 commit 3f0729f

File tree

5 files changed

+13
-38
lines changed

5 files changed

+13
-38
lines changed

Diff for: compiler/rustc_builtin_macros/src/cfg_eval.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ crate fn expand(
2424
annotatable: Annotatable,
2525
) -> Vec<Annotatable> {
2626
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
27-
cfg_eval(ecx, annotatable)
27+
vec![cfg_eval(ecx, annotatable)]
2828
}
2929

30-
crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Vec<Annotatable> {
31-
let mut visitor = CfgEval {
30+
crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable {
31+
CfgEval {
3232
cfg: &mut StripUnconfigured {
3333
sess: ecx.sess,
3434
features: ecx.ecfg.features,
3535
config_tokens: true,
3636
},
37-
};
38-
let annotatable = visitor.configure_annotatable(annotatable);
39-
vec![annotatable]
37+
}
38+
.configure_annotatable(annotatable)
4039
}
4140

4241
struct CfgEval<'a, 'b> {

Diff for: compiler/rustc_builtin_macros/src/derive.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ impl MultiItemModifier for Expander {
2626
return ExpandResult::Ready(vec![item]);
2727
}
2828

29+
let item = cfg_eval(ecx, item);
30+
2931
let result =
3032
ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| {
3133
let template =
@@ -54,12 +56,12 @@ impl MultiItemModifier for Expander {
5456
report_path_args(sess, &meta);
5557
meta.path
5658
})
57-
.map(|path| (path, None))
59+
.map(|path| (path, item.clone(), None))
5860
.collect()
5961
});
6062

6163
match result {
62-
Ok(()) => ExpandResult::Ready(cfg_eval(ecx, item)),
64+
Ok(()) => ExpandResult::Ready(vec![item]),
6365
Err(Indeterminate) => ExpandResult::Retry(item),
6466
}
6567
}

Diff for: compiler/rustc_expand/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ impl SyntaxExtension {
835835
/// Error type that denotes indeterminacy.
836836
pub struct Indeterminate;
837837

838-
pub type DeriveResolutions = Vec<(ast::Path, Option<Lrc<SyntaxExtension>>)>;
838+
pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>)>;
839839

840840
pub trait ResolverExpand {
841841
fn next_node_id(&mut self) -> NodeId;

Diff for: compiler/rustc_expand/src/expand.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -500,42 +500,16 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
500500
.resolver
501501
.take_derive_resolutions(expn_id)
502502
.map(|derives| {
503-
enum AnnotatableRef<'a> {
504-
Item(&'a P<ast::Item>),
505-
Stmt(&'a ast::Stmt),
506-
}
507-
let item = match &fragment {
508-
AstFragment::Items(items) => match &items[..] {
509-
[item] => AnnotatableRef::Item(item),
510-
_ => unreachable!(),
511-
},
512-
AstFragment::Stmts(stmts) => match &stmts[..] {
513-
[stmt] => AnnotatableRef::Stmt(stmt),
514-
_ => unreachable!(),
515-
},
516-
_ => unreachable!(),
517-
};
518-
519503
derive_invocations.reserve(derives.len());
520504
derives
521505
.into_iter()
522-
.map(|(path, _exts)| {
506+
.map(|(path, item, _exts)| {
523507
// FIXME: Consider using the derive resolutions (`_exts`)
524508
// instead of enqueuing the derives to be resolved again later.
525509
let expn_id = ExpnId::fresh(None);
526510
derive_invocations.push((
527511
Invocation {
528-
kind: InvocationKind::Derive {
529-
path,
530-
item: match item {
531-
AnnotatableRef::Item(item) => {
532-
Annotatable::Item(item.clone())
533-
}
534-
AnnotatableRef::Stmt(stmt) => {
535-
Annotatable::Stmt(P(stmt.clone()))
536-
}
537-
},
538-
},
512+
kind: InvocationKind::Derive { path, item },
539513
fragment_kind,
540514
expansion_data: ExpansionData {
541515
id: expn_id,

Diff for: compiler/rustc_resolve/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
380380
has_derive_copy: false,
381381
});
382382
let parent_scope = self.invocation_parent_scopes[&expn_id];
383-
for (i, (path, opt_ext)) in entry.resolutions.iter_mut().enumerate() {
383+
for (i, (path, _, opt_ext)) in entry.resolutions.iter_mut().enumerate() {
384384
if opt_ext.is_none() {
385385
*opt_ext = Some(
386386
match self.resolve_macro_path(

0 commit comments

Comments
 (0)