]> git.lizzy.rs Git - rust.git/commitdiff
Fix bug parsing `#[derive]` macro invocations.
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Mon, 3 Apr 2017 22:23:32 +0000 (22:23 +0000)
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Mon, 3 Apr 2017 23:02:49 +0000 (23:02 +0000)
src/librustc_resolve/macros.rs
src/libsyntax/ext/derive.rs
src/libsyntax/parse/parser.rs
src/test/run-pass/issue-40962.rs [new file with mode: 0644]

index 05f30f039c8f0649dd3eaf3bab6fc28c909219ee..966cb7ee8d8d83c6299955357d2a2306469c1328 100644 (file)
@@ -222,8 +222,10 @@ fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>)
             let name = unwrap_or!(attrs[i].name(), continue);
 
             if name == "derive" {
-                let result = attrs[i].parse_list(&self.session.parse_sess,
-                                                 |parser| parser.parse_path(PathStyle::Mod));
+                let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
+                    parser.parse_path_allowing_meta(PathStyle::Mod)
+                });
+
                 let mut traits = match result {
                     Ok(traits) => traits,
                     Err(mut e) => {
index c79040424f619e0c4c8a745f3fae66138d235408..e7c5d8278d9775065d1c800633ceaba2458e7e17 100644 (file)
@@ -26,7 +26,8 @@ pub fn collect_derives(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>) -> Vec
             return true;
         }
 
-        match attr.parse_list(cx.parse_sess, |parser| parser.parse_path(PathStyle::Mod)) {
+        match attr.parse_list(cx.parse_sess,
+                              |parser| parser.parse_path_allowing_meta(PathStyle::Mod)) {
             Ok(ref traits) if traits.is_empty() => {
                 cx.span_warn(attr.span, "empty trait list in `derive`");
                 false
index c2c3e5a6855af4ca8e415b7bbc5f3a48e97c6920..a89811d8abb0b1d4b0322fdd634b702a468e87dd 100644 (file)
@@ -1754,6 +1754,26 @@ pub fn parse_path(&mut self, mode: PathStyle) -> PResult<'a, ast::Path> {
         })
     }
 
+    /// Like `parse_path`, but also supports parsing `Word` meta items into paths for back-compat.
+    /// This is used when parsing derive macro paths in `#[derive]` attributes.
+    pub fn parse_path_allowing_meta(&mut self, mode: PathStyle) -> PResult<'a, ast::Path> {
+        let meta_ident = match self.token {
+            token::Interpolated(ref nt) => match **nt {
+                token::NtMeta(ref meta) => match meta.node {
+                    ast::MetaItemKind::Word => Some(ast::Ident::with_empty_ctxt(meta.name)),
+                    _ => None,
+                },
+                _ => None,
+            },
+            _ => None,
+        };
+        if let Some(ident) = meta_ident {
+            self.bump();
+            return Ok(ast::Path::from_ident(self.prev_span, ident));
+        }
+        self.parse_path(mode)
+    }
+
     /// Examples:
     /// - `a::b<T,U>::c<V,W>`
     /// - `a::b<T,U>::c(V) -> W`
diff --git a/src/test/run-pass/issue-40962.rs b/src/test/run-pass/issue-40962.rs
new file mode 100644 (file)
index 0000000..b35cfa1
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+macro_rules! m {
+    ($i:meta) => {
+        #[derive($i)]
+        struct S;
+    }
+}
+
+m!(Clone);
+
+fn main() {}