112: codegen/statics: forward #[cfg] attributes r=japaric a=japaric

fixes #110

Co-authored-by: Jorge Aparicio <jorge@japaric.io>
This commit is contained in:
bors[bot] 2018-12-15 21:05:34 +00:00
commit c4b7fbeb02
2 changed files with 25 additions and 1 deletions

View file

@ -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 }

View file

@ -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();