about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-08-30 11:11:10 -0500
committerGitHub <noreply@github.com>2017-08-30 11:11:10 -0500
commit63cc2dda63b7f26933fcb2324ff2274f7fbfec30 (patch)
tree9c15d2d1775209c530519a38a03116a3028cbdcb
parent581dc932932199ab0f2d0beb00144e0e56fb5aae (diff)
parentce322eedff7d665ed3e5ea142ac6bb40d6a72c66 (diff)
downloadrust-63cc2dda63b7f26933fcb2324ff2274f7fbfec30.tar.gz
rust-63cc2dda63b7f26933fcb2324ff2274f7fbfec30.zip
Rollup merge of #44089 - alexcrichton:trait-proc-macro, r=nrc
rustc: Fix proc_macro expansions on trait methods

This commit fixes procedural macro attributes being attached to trait methods,
ensuring that they get resolved and expanded as other procedural macro
attributes. The bug here was that `current_module` on the resolver was
accidentally set to be a trait when it's otherwise only ever expecting a
`mod`/block module. The actual fix here came from @jseyfried, I'm just helping
to land it in the compiler!

Closes #42493
-rw-r--r--src/librustc_resolve/macros.rs3
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/attr-on-trait.rs28
-rw-r--r--src/test/run-pass-fulldeps/proc-macro/auxiliary/attr-on-trait.rs25
3 files changed, 55 insertions, 1 deletions
diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs
index f8dc341653e..9531c8baa0b 100644
--- a/src/librustc_resolve/macros.rs
+++ b/src/librustc_resolve/macros.rs
@@ -402,7 +402,8 @@ impl<'a> Resolver<'a> {
         let ast::Path { ref segments, span } = *path;
         let path: Vec<_> = segments.iter().map(|seg| respan(seg.span, seg.identifier)).collect();
         let invocation = self.invocations[&scope];
-        self.current_module = invocation.module.get();
+        let module = invocation.module.get();
+        self.current_module = if module.is_trait() { module.parent.unwrap() } else { module };
 
         if path.len() > 1 {
             if !self.use_extern_macros && self.gated_errors.insert(span) {
diff --git a/src/test/run-pass-fulldeps/proc-macro/attr-on-trait.rs b/src/test/run-pass-fulldeps/proc-macro/attr-on-trait.rs
new file mode 100644
index 00000000000..8ba38875eff
--- /dev/null
+++ b/src/test/run-pass-fulldeps/proc-macro/attr-on-trait.rs
@@ -0,0 +1,28 @@
+// 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.
+
+// aux-build:attr-on-trait.rs
+
+#![feature(proc_macro)]
+
+extern crate attr_on_trait;
+
+trait Foo {
+    #[attr_on_trait::foo]
+    fn foo() {}
+}
+
+impl Foo for i32 {
+    fn foo(&self) {}
+}
+
+fn main() {
+    3i32.foo();
+}
diff --git a/src/test/run-pass-fulldeps/proc-macro/auxiliary/attr-on-trait.rs b/src/test/run-pass-fulldeps/proc-macro/auxiliary/attr-on-trait.rs
new file mode 100644
index 00000000000..8e977034027
--- /dev/null
+++ b/src/test/run-pass-fulldeps/proc-macro/auxiliary/attr-on-trait.rs
@@ -0,0 +1,25 @@
+// 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.
+
+// no-prefer-dynamic
+
+#![feature(proc_macro)]
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+
+use proc_macro::TokenStream;
+
+#[proc_macro_attribute]
+pub fn foo(attr: TokenStream, item: TokenStream) -> TokenStream {
+    drop(attr);
+    assert_eq!(item.to_string(), "fn foo() { }");
+    "fn foo(&self);".parse().unwrap()
+}