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() .iter()
.map(|(name, static_)| { .map(|(name, static_)| {
let attrs = &static_.attrs; let attrs = &static_.attrs;
let cfgs = &static_.cfgs;
let expr = &static_.expr; let expr = &static_.expr;
let ident = name; let ident = name;
let ty = &static_.ty; let ty = &static_.ty;
quote!( quote!(
#[allow(non_snake_case)] #[allow(non_snake_case)]
#(#cfgs)*
let #ident: &#lt mut #ty = { let #ident: &#lt mut #ty = {
#(#attrs)* #(#attrs)*
#(#cfgs)*
static mut #ident: #ty = #expr; static mut #ident: #ty = #expr;
unsafe { &mut #ident } unsafe { &mut #ident }

View file

@ -1003,6 +1003,9 @@ fn parse_args(input: ParseStream, accept_capacity: bool) -> parse::Result<TaskAr
} }
pub struct Static { pub struct Static {
/// `#[cfg]` attributes
pub cfgs: Vec<Attribute>,
/// Attributes that are not `#[cfg]`
pub attrs: Vec<Attribute>, pub attrs: Vec<Attribute>,
pub ty: Box<Type>, pub ty: Box<Type>,
pub expr: Box<Expr>, pub expr: Box<Expr>,
@ -1020,10 +1023,13 @@ impl Static {
)); ));
} }
let (cfgs, attrs) = extract_cfgs(item.attrs);
statics.insert( statics.insert(
item.ident, item.ident,
Static { Static {
attrs: item.attrs, cfgs,
attrs,
ty: item.ty, ty: item.ty,
expr: item.expr, 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 /// Extracts `static mut` vars from the beginning of the given statements
fn extract_statics(stmts: Vec<Stmt>) -> (Statics, Vec<Stmt>) { fn extract_statics(stmts: Vec<Stmt>) -> (Statics, Vec<Stmt>) {
let mut istmts = stmts.into_iter(); let mut istmts = stmts.into_iter();