Skip to content

Commit a709f87

Browse files
committed
Avoid more unnecessary MetaItem/Attribute conversions.
In `Expander::expand` the code currently uses `mk_attr_outer` to convert a `MetaItem` to an `Attribute`, and then follows that with `meta_item_list` which converts back. This commit avoids the unnecessary conversions. There was one wrinkle: the existing conversions caused the bogus `<>` on `Default<>` to be removed. With the conversion gone, we get a second error message about the `<>`. This is a rare case, so I think it probably doesn't matter much.
1 parent fc2a8a0 commit a709f87

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

compiler/rustc_builtin_macros/src/derive.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cfg_eval::cfg_eval;
22

33
use rustc_ast as ast;
4-
use rustc_ast::{attr, token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
4+
use rustc_ast::{token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
55
use rustc_errors::{struct_span_err, Applicability};
66
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
77
use rustc_feature::AttributeTemplate;
@@ -40,28 +40,29 @@ impl MultiItemModifier for Expander {
4040
sym::derive,
4141
template,
4242
);
43-
let attr =
44-
attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone());
4543

46-
let mut resolutions: Vec<_> = attr
47-
.meta_item_list()
48-
.unwrap_or_default()
49-
.into_iter()
50-
.filter_map(|nested_meta| match nested_meta {
51-
NestedMetaItem::MetaItem(meta) => Some(meta),
52-
NestedMetaItem::Lit(lit) => {
53-
// Reject `#[derive("Debug")]`.
54-
report_unexpected_meta_item_lit(sess, &lit);
55-
None
56-
}
57-
})
58-
.map(|meta| {
59-
// Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the paths.
60-
report_path_args(sess, &meta);
61-
meta.path
62-
})
63-
.map(|path| (path, dummy_annotatable(), None, self.0))
64-
.collect();
44+
let mut resolutions = match &meta_item.kind {
45+
MetaItemKind::List(list) => {
46+
list.iter()
47+
.filter_map(|nested_meta| match nested_meta {
48+
NestedMetaItem::MetaItem(meta) => Some(meta),
49+
NestedMetaItem::Lit(lit) => {
50+
// Reject `#[derive("Debug")]`.
51+
report_unexpected_meta_item_lit(sess, &lit);
52+
None
53+
}
54+
})
55+
.map(|meta| {
56+
// Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the
57+
// paths.
58+
report_path_args(sess, &meta);
59+
meta.path.clone()
60+
})
61+
.map(|path| (path, dummy_annotatable(), None, self.0))
62+
.collect()
63+
}
64+
_ => vec![],
65+
};
6566

6667
// Do not configure or clone items unless necessary.
6768
match &mut resolutions[..] {

src/test/ui/span/macro-ty-params.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ macro_rules! foo { () => () }
99
fn main() {
1010
foo::<T>!(); //~ ERROR generic arguments in macro path
1111
foo::<>!(); //~ ERROR generic arguments in macro path
12-
m!(Default<>); //~ ERROR unexpected generic arguments in path
12+
m!(Default<>);
13+
//~^ ERROR unexpected generic arguments in path
14+
//~^^ ERROR generic arguments in macro path
1315
}

src/test/ui/span/macro-ty-params.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ error: unexpected generic arguments in path
1616
LL | m!(Default<>);
1717
| ^^
1818

19-
error: aborting due to 3 previous errors
19+
error: generic arguments in macro path
20+
--> $DIR/macro-ty-params.rs:12:15
21+
|
22+
LL | m!(Default<>);
23+
| ^^
24+
25+
error: aborting due to 4 previous errors
2026

0 commit comments

Comments
 (0)