aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/codegen.rs3
-rw-r--r--macros/src/syntax.rs23
2 files changed, 25 insertions, 1 deletions
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs
index 8e1970f5..6b8e1594 100644
--- a/macros/src/codegen.rs
+++ b/macros/src/codegen.rs
@@ -1808,14 +1808,17 @@ fn mk_locals(locals: &HashMap<Ident, Static>, once: bool) -> proc_macro2::TokenS
.iter()
.map(|(name, static_)| {
let attrs = &static_.attrs;
+ let cfgs = &static_.cfgs;
let expr = &static_.expr;
let ident = name;
let ty = &static_.ty;
quote!(
#[allow(non_snake_case)]
+ #(#cfgs)*
let #ident: &#lt mut #ty = {
#(#attrs)*
+ #(#cfgs)*
static mut #ident: #ty = #expr;
unsafe { &mut #ident }
diff --git a/macros/src/syntax.rs b/macros/src/syntax.rs
index 24586dcf..c1b0d5d2 100644
--- a/macros/src/syntax.rs
+++ b/macros/src/syntax.rs
@@ -1003,6 +1003,9 @@ fn parse_args(input: ParseStream, accept_capacity: bool) -> parse::Result<TaskAr
}
pub struct Static {
+ /// `#[cfg]` attributes
+ pub cfgs: Vec<Attribute>,
+ /// Attributes that are not `#[cfg]`
pub attrs: Vec<Attribute>,
pub ty: Box<Type>,
pub expr: Box<Expr>,
@@ -1020,10 +1023,13 @@ impl Static {
));
}
+ let (cfgs, attrs) = extract_cfgs(item.attrs);
+
statics.insert(
item.ident,
Static {
- attrs: item.attrs,
+ cfgs,
+ attrs,
ty: item.ty,
expr: item.expr,
},
@@ -1150,6 +1156,21 @@ fn eq(attr: &Attribute, name: &str) -> bool {
}
}
+fn extract_cfgs(attrs: Vec<Attribute>) -> (Vec<Attribute>, Vec<Attribute>) {
+ let mut cfgs = vec![];
+ let mut not_cfgs = vec![];
+
+ for attr in attrs {
+ if eq(&attr, "cfg") {
+ cfgs.push(attr);
+ } else {
+ not_cfgs.push(attr);
+ }
+ }
+
+ (cfgs, not_cfgs)
+}
+
/// Extracts `static mut` vars from the beginning of the given statements
fn extract_statics(stmts: Vec<Stmt>) -> (Statics, Vec<Stmt>) {
let mut istmts = stmts.into_iter();