Skip to content

Commit b1ae194

Browse files
authored
Auto merge of #34295 - jseyfried:cfg_decoration, r=eddyb
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
2 parents 9a68124 + c41cf30 commit b1ae194

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/libsyntax/ext/base.rs

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ impl Annotatable {
9494
_ => panic!("expected Item")
9595
}
9696
}
97+
98+
pub fn fold_with<F: Folder>(self, folder: &mut F) -> SmallVector<Self> {
99+
match self {
100+
Annotatable::Item(item) => folder.fold_item(item).map(Annotatable::Item),
101+
Annotatable::ImplItem(item) =>
102+
folder.fold_impl_item(item.unwrap()).map(|item| Annotatable::ImplItem(P(item))),
103+
Annotatable::TraitItem(item) =>
104+
folder.fold_trait_item(item.unwrap()).map(|item| Annotatable::TraitItem(P(item))),
105+
}
106+
}
97107
}
98108

99109
// A more flexible ItemDecorator.

src/libsyntax/ext/expand.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,6 @@ fn decorate(a: Annotatable, fld: &mut MacroExpander) -> SmallVector<Annotatable>
791791
let mut decorator_items = SmallVector::zero();
792792
let mut new_attrs = Vec::new();
793793
expand_decorators(a.clone(), fld, &mut decorator_items, &mut new_attrs);
794-
let decorator_items =
795-
decorator_items.into_iter().flat_map(|a| expand_annotatable(a, fld)).collect();
796794

797795
let mut new_items = SmallVector::one(a.fold_attrs(new_attrs));
798796
new_items.push_all(decorator_items);
@@ -845,16 +843,18 @@ fn expand_decorators(a: Annotatable,
845843
}
846844
});
847845

848-
// we'd ideally decorator_items.push_all(expand_annotatable(ann, fld)),
849-
// but that double-mut-borrows fld
850846
let mut items: SmallVector<Annotatable> = SmallVector::zero();
851847
dec.expand(fld.cx,
852848
attr.span,
853849
&attr.node.value,
854850
&a,
855851
&mut |ann| items.push(ann));
856-
decorator_items.extend(items.into_iter()
857-
.flat_map(|ann| expand_annotatable(ann, fld).into_iter()));
852+
853+
for item in items {
854+
for configured_item in item.fold_with(&mut fld.strip_unconfigured()) {
855+
decorator_items.extend(expand_annotatable(configured_item, fld));
856+
}
857+
}
858858

859859
fld.cx.bt_pop();
860860
}

src/libsyntax/util/small_vector.rs

+9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ impl<T> SmallVector<T> {
146146
}
147147

148148
pub fn is_empty(&self) -> bool { self.len() == 0 }
149+
150+
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> SmallVector<U> {
151+
let repr = match self.repr {
152+
Zero => Zero,
153+
One(t) => One(f(t)),
154+
Many(vec) => Many(vec.into_iter().map(f).collect()),
155+
};
156+
SmallVector { repr: repr }
157+
}
149158
}
150159

151160
impl<T> IntoIterator for SmallVector<T> {

0 commit comments

Comments
 (0)