]> git.lizzy.rs Git - rust.git/commitdiff
Avoid more unnecessary `MetaItem`/`Attribute` conversions.
authorNicholas Nethercote <n.nethercote@gmail.com>
Mon, 28 Nov 2022 00:17:45 +0000 (11:17 +1100)
committerNicholas Nethercote <n.nethercote@gmail.com>
Tue, 29 Nov 2022 01:54:45 +0000 (12:54 +1100)
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.

compiler/rustc_builtin_macros/src/derive.rs
src/test/ui/span/macro-ty-params.rs
src/test/ui/span/macro-ty-params.stderr

index 0358c0b1b5fab4479dc63601a2e25c54d284cb11..fa5a45730ac7afd8379eb696f2ef7098df42d389 100644 (file)
@@ -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 @@ fn expand(
                     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[..] {
index 0a93105b6647784ab82818a8d475e59a929b1870..cf28b0255d17db4c611ea55cf461be1a51fbc1c1 100644 (file)
@@ -9,5 +9,7 @@ macro_rules! foo { () => () }
 fn main() {
     foo::<T>!(); //~ ERROR generic arguments in macro path
     foo::<>!(); //~ ERROR generic arguments in macro path
-    m!(Default<>); //~ ERROR unexpected generic arguments in path
+    m!(Default<>);
+    //~^ ERROR unexpected generic arguments in path
+    //~^^ ERROR generic arguments in macro path
 }
index 138cd2598a1c5f7ab38ec6558d6925086f87f591..7023ef8cd1c33a43ad91d03e37b3da9c7026b288 100644 (file)
@@ -16,5 +16,11 @@ error: unexpected generic arguments in path
 LL |     m!(Default<>);
    |               ^^
 
-error: aborting due to 3 previous errors
+error: generic arguments in macro path
+  --> $DIR/macro-ty-params.rs:12:15
+   |
+LL |     m!(Default<>);
+   |               ^^
+
+error: aborting due to 4 previous errors