expand: Move some more derive logic to rustc_builtin_macros
This commit is contained in:
parent
75d1500f02
commit
3f0729f378
5 changed files with 13 additions and 38 deletions
|
@ -24,19 +24,18 @@ crate fn expand(
|
||||||
annotatable: Annotatable,
|
annotatable: Annotatable,
|
||||||
) -> Vec<Annotatable> {
|
) -> Vec<Annotatable> {
|
||||||
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
|
check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
|
||||||
cfg_eval(ecx, annotatable)
|
vec![cfg_eval(ecx, annotatable)]
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Vec<Annotatable> {
|
crate fn cfg_eval(ecx: &ExtCtxt<'_>, annotatable: Annotatable) -> Annotatable {
|
||||||
let mut visitor = CfgEval {
|
CfgEval {
|
||||||
cfg: &mut StripUnconfigured {
|
cfg: &mut StripUnconfigured {
|
||||||
sess: ecx.sess,
|
sess: ecx.sess,
|
||||||
features: ecx.ecfg.features,
|
features: ecx.ecfg.features,
|
||||||
config_tokens: true,
|
config_tokens: true,
|
||||||
},
|
},
|
||||||
};
|
}
|
||||||
let annotatable = visitor.configure_annotatable(annotatable);
|
.configure_annotatable(annotatable)
|
||||||
vec![annotatable]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CfgEval<'a, 'b> {
|
struct CfgEval<'a, 'b> {
|
||||||
|
|
|
@ -26,6 +26,8 @@ impl MultiItemModifier for Expander {
|
||||||
return ExpandResult::Ready(vec![item]);
|
return ExpandResult::Ready(vec![item]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let item = cfg_eval(ecx, item);
|
||||||
|
|
||||||
let result =
|
let result =
|
||||||
ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| {
|
ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| {
|
||||||
let template =
|
let template =
|
||||||
|
@ -54,12 +56,12 @@ impl MultiItemModifier for Expander {
|
||||||
report_path_args(sess, &meta);
|
report_path_args(sess, &meta);
|
||||||
meta.path
|
meta.path
|
||||||
})
|
})
|
||||||
.map(|path| (path, None))
|
.map(|path| (path, item.clone(), None))
|
||||||
.collect()
|
.collect()
|
||||||
});
|
});
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(()) => ExpandResult::Ready(cfg_eval(ecx, item)),
|
Ok(()) => ExpandResult::Ready(vec![item]),
|
||||||
Err(Indeterminate) => ExpandResult::Retry(item),
|
Err(Indeterminate) => ExpandResult::Retry(item),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -835,7 +835,7 @@ impl SyntaxExtension {
|
||||||
/// Error type that denotes indeterminacy.
|
/// Error type that denotes indeterminacy.
|
||||||
pub struct Indeterminate;
|
pub struct Indeterminate;
|
||||||
|
|
||||||
pub type DeriveResolutions = Vec<(ast::Path, Option<Lrc<SyntaxExtension>>)>;
|
pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>)>;
|
||||||
|
|
||||||
pub trait ResolverExpand {
|
pub trait ResolverExpand {
|
||||||
fn next_node_id(&mut self) -> NodeId;
|
fn next_node_id(&mut self) -> NodeId;
|
||||||
|
|
|
@ -500,42 +500,16 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
||||||
.resolver
|
.resolver
|
||||||
.take_derive_resolutions(expn_id)
|
.take_derive_resolutions(expn_id)
|
||||||
.map(|derives| {
|
.map(|derives| {
|
||||||
enum AnnotatableRef<'a> {
|
|
||||||
Item(&'a P<ast::Item>),
|
|
||||||
Stmt(&'a ast::Stmt),
|
|
||||||
}
|
|
||||||
let item = match &fragment {
|
|
||||||
AstFragment::Items(items) => match &items[..] {
|
|
||||||
[item] => AnnotatableRef::Item(item),
|
|
||||||
_ => unreachable!(),
|
|
||||||
},
|
|
||||||
AstFragment::Stmts(stmts) => match &stmts[..] {
|
|
||||||
[stmt] => AnnotatableRef::Stmt(stmt),
|
|
||||||
_ => unreachable!(),
|
|
||||||
},
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
derive_invocations.reserve(derives.len());
|
derive_invocations.reserve(derives.len());
|
||||||
derives
|
derives
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(path, _exts)| {
|
.map(|(path, item, _exts)| {
|
||||||
// FIXME: Consider using the derive resolutions (`_exts`)
|
// FIXME: Consider using the derive resolutions (`_exts`)
|
||||||
// instead of enqueuing the derives to be resolved again later.
|
// instead of enqueuing the derives to be resolved again later.
|
||||||
let expn_id = ExpnId::fresh(None);
|
let expn_id = ExpnId::fresh(None);
|
||||||
derive_invocations.push((
|
derive_invocations.push((
|
||||||
Invocation {
|
Invocation {
|
||||||
kind: InvocationKind::Derive {
|
kind: InvocationKind::Derive { path, item },
|
||||||
path,
|
|
||||||
item: match item {
|
|
||||||
AnnotatableRef::Item(item) => {
|
|
||||||
Annotatable::Item(item.clone())
|
|
||||||
}
|
|
||||||
AnnotatableRef::Stmt(stmt) => {
|
|
||||||
Annotatable::Stmt(P(stmt.clone()))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
fragment_kind,
|
fragment_kind,
|
||||||
expansion_data: ExpansionData {
|
expansion_data: ExpansionData {
|
||||||
id: expn_id,
|
id: expn_id,
|
||||||
|
|
|
@ -380,7 +380,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
|
||||||
has_derive_copy: false,
|
has_derive_copy: false,
|
||||||
});
|
});
|
||||||
let parent_scope = self.invocation_parent_scopes[&expn_id];
|
let parent_scope = self.invocation_parent_scopes[&expn_id];
|
||||||
for (i, (path, opt_ext)) in entry.resolutions.iter_mut().enumerate() {
|
for (i, (path, _, opt_ext)) in entry.resolutions.iter_mut().enumerate() {
|
||||||
if opt_ext.is_none() {
|
if opt_ext.is_none() {
|
||||||
*opt_ext = Some(
|
*opt_ext = Some(
|
||||||
match self.resolve_macro_path(
|
match self.resolve_macro_path(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue