Skip to content

Commit

Permalink
Auto merge of #34295 - jseyfried:cfg_decoration, r=eddyb
Browse files Browse the repository at this point in the history
Perform `cfg` attribute processing on decorator-generated items

Fixes https://users.rust-lang.org/t/unused-attribute-warning-for-custom-derive-attribute/6180.
r? @nrc
  • Loading branch information
bors committed Jun 19, 2016
2 parents 9a68124 + c41cf30 commit b1ae194
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl Annotatable {
_ => panic!("expected Item")
}
}

pub fn fold_with<F: Folder>(self, folder: &mut F) -> SmallVector<Self> {
match self {
Annotatable::Item(item) => folder.fold_item(item).map(Annotatable::Item),
Annotatable::ImplItem(item) =>
folder.fold_impl_item(item.unwrap()).map(|item| Annotatable::ImplItem(P(item))),
Annotatable::TraitItem(item) =>
folder.fold_trait_item(item.unwrap()).map(|item| Annotatable::TraitItem(P(item))),
}
}
}

// A more flexible ItemDecorator.
Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,6 @@ fn decorate(a: Annotatable, fld: &mut MacroExpander) -> SmallVector<Annotatable>
let mut decorator_items = SmallVector::zero();
let mut new_attrs = Vec::new();
expand_decorators(a.clone(), fld, &mut decorator_items, &mut new_attrs);
let decorator_items =
decorator_items.into_iter().flat_map(|a| expand_annotatable(a, fld)).collect();

let mut new_items = SmallVector::one(a.fold_attrs(new_attrs));
new_items.push_all(decorator_items);
Expand Down Expand Up @@ -845,16 +843,18 @@ fn expand_decorators(a: Annotatable,
}
});

// we'd ideally decorator_items.push_all(expand_annotatable(ann, fld)),
// but that double-mut-borrows fld
let mut items: SmallVector<Annotatable> = SmallVector::zero();
dec.expand(fld.cx,
attr.span,
&attr.node.value,
&a,
&mut |ann| items.push(ann));
decorator_items.extend(items.into_iter()
.flat_map(|ann| expand_annotatable(ann, fld).into_iter()));

for item in items {
for configured_item in item.fold_with(&mut fld.strip_unconfigured()) {
decorator_items.extend(expand_annotatable(configured_item, fld));
}
}

fld.cx.bt_pop();
}
Expand Down
9 changes: 9 additions & 0 deletions src/libsyntax/util/small_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ impl<T> SmallVector<T> {
}

pub fn is_empty(&self) -> bool { self.len() == 0 }

pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> SmallVector<U> {
let repr = match self.repr {
Zero => Zero,
One(t) => One(f(t)),
Many(vec) => Many(vec.into_iter().map(f).collect()),
};
SmallVector { repr: repr }
}
}

impl<T> IntoIterator for SmallVector<T> {
Expand Down

0 comments on commit b1ae194

Please sign in to comment.