From 4f00d8bd781455535e0710879bc31c197e51c71b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 15 Dec 2018 22:04:30 +0100 Subject: [PATCH] codegen/statics: forward #[cfg] attributes fixes #110 --- macros/src/codegen.rs | 3 +++ macros/src/syntax.rs | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 8e1970f53c..6b8e159416 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -1808,14 +1808,17 @@ fn mk_locals(locals: &HashMap, 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 24586dcf5b..c1b0d5d279 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, + /// Attributes that are not `#[cfg]` pub attrs: Vec, pub ty: Box, pub expr: Box, @@ -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) -> (Vec, Vec) { + 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) -> (Statics, Vec) { let mut istmts = stmts.into_iter();