diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-11-28 11:17:45 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-11-29 12:54:45 +1100 |
| commit | a709f87be7d66e8a91c5ee2818cac6f077caac08 (patch) | |
| tree | 49ff2e0daaff47dd3c7b9295ea6479859d7e89ab /compiler/rustc_builtin_macros/src/derive.rs | |
| parent | fc2a8a0c675bf7d111846b19fd3d5d7a13915425 (diff) | |
| download | rust-a709f87be7d66e8a91c5ee2818cac6f077caac08.tar.gz rust-a709f87be7d66e8a91c5ee2818cac6f077caac08.zip | |
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.
Diffstat (limited to 'compiler/rustc_builtin_macros/src/derive.rs')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/derive.rs | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/compiler/rustc_builtin_macros/src/derive.rs b/compiler/rustc_builtin_macros/src/derive.rs index 0358c0b1b5f..fa5a45730ac 100644 --- a/compiler/rustc_builtin_macros/src/derive.rs +++ b/compiler/rustc_builtin_macros/src/derive.rs @@ -1,7 +1,7 @@ use crate::cfg_eval::cfg_eval; use rustc_ast as ast; -use rustc_ast::{attr, token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind}; +use rustc_ast::{token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind}; use rustc_errors::{struct_span_err, Applicability}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier}; use rustc_feature::AttributeTemplate; @@ -40,28 +40,29 @@ impl MultiItemModifier for Expander { sym::derive, template, ); - let attr = - attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone()); - let mut resolutions: Vec<_> = attr - .meta_item_list() - .unwrap_or_default() - .into_iter() - .filter_map(|nested_meta| match nested_meta { - NestedMetaItem::MetaItem(meta) => Some(meta), - NestedMetaItem::Lit(lit) => { - // Reject `#[derive("Debug")]`. - report_unexpected_meta_item_lit(sess, &lit); - None - } - }) - .map(|meta| { - // Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the paths. - report_path_args(sess, &meta); - meta.path - }) - .map(|path| (path, dummy_annotatable(), None, self.0)) - .collect(); + let mut resolutions = match &meta_item.kind { + MetaItemKind::List(list) => { + list.iter() + .filter_map(|nested_meta| match nested_meta { + NestedMetaItem::MetaItem(meta) => Some(meta), + NestedMetaItem::Lit(lit) => { + // Reject `#[derive("Debug")]`. + report_unexpected_meta_item_lit(sess, &lit); + None + } + }) + .map(|meta| { + // Reject `#[derive(Debug = "value", Debug(abc))]`, but recover the + // paths. + report_path_args(sess, &meta); + meta.path.clone() + }) + .map(|path| (path, dummy_annotatable(), None, self.0)) + .collect() + } + _ => vec![], + }; // Do not configure or clone items unless necessary. match &mut resolutions[..] { |
