about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-03-22 15:48:43 +0100
committerGitHub <noreply@github.com>2020-03-22 15:48:43 +0100
commit69c0bcd3d5d65e508751af5e7cb1c35b7e9ec4c9 (patch)
tree31518fd812614d8630bddab5443624187e61159f
parent8fe8bad96b88cf981458e836e6866ddc7966cecb (diff)
parentbdd07f932b6d707c99f9c6127a424a9e801c997f (diff)
downloadrust-69c0bcd3d5d65e508751af5e7cb1c35b7e9ec4c9.tar.gz
rust-69c0bcd3d5d65e508751af5e7cb1c35b7e9ec4c9.zip
Rollup merge of #70266 - petrochenkov:prochead, r=varkor
proc_macro_harness: Use item header spans for errors

Addresses https://github.com/rust-lang/rust/pull/70233#discussion_r396043004.
-rw-r--r--src/librustc_builtin_macros/proc_macro_harness.rs13
-rw-r--r--src/test/ui/proc-macro/export-macro.stderr6
-rw-r--r--src/test/ui/proc-macro/exports.stderr6
-rw-r--r--src/test/ui/proc-macro/non-root.stderr2
-rw-r--r--src/test/ui/proc-macro/pub-at-crate-root.stderr24
5 files changed, 20 insertions, 31 deletions
diff --git a/src/librustc_builtin_macros/proc_macro_harness.rs b/src/librustc_builtin_macros/proc_macro_harness.rs
index 71622a3b7e6..6540bcc4156 100644
--- a/src/librustc_builtin_macros/proc_macro_harness.rs
+++ b/src/librustc_builtin_macros/proc_macro_harness.rs
@@ -10,6 +10,7 @@ use rustc_expand::base::{ExtCtxt, Resolver};
 use rustc_expand::expand::{AstFragment, ExpansionConfig};
 use rustc_session::parse::ParseSess;
 use rustc_span::hygiene::AstPass;
+use rustc_span::source_map::SourceMap;
 use rustc_span::symbol::{kw, sym};
 use rustc_span::{Span, DUMMY_SP};
 use smallvec::smallvec;
@@ -44,6 +45,7 @@ struct CollectProcMacros<'a> {
     macros: Vec<ProcMacro>,
     in_root: bool,
     handler: &'a rustc_errors::Handler,
+    source_map: &'a SourceMap,
     is_proc_macro_crate: bool,
     is_test_crate: bool,
 }
@@ -65,6 +67,7 @@ pub fn inject(
         macros: Vec::new(),
         in_root: true,
         handler,
+        source_map: sess.source_map(),
         is_proc_macro_crate,
         is_test_crate,
     };
@@ -195,7 +198,7 @@ impl<'a> CollectProcMacros<'a> {
             } else {
                 "functions tagged with `#[proc_macro_derive]` must be `pub`"
             };
-            self.handler.span_err(item.span, msg);
+            self.handler.span_err(self.source_map.def_span(item.span), msg);
         }
     }
 
@@ -214,7 +217,7 @@ impl<'a> CollectProcMacros<'a> {
             } else {
                 "functions tagged with `#[proc_macro_attribute]` must be `pub`"
             };
-            self.handler.span_err(item.span, msg);
+            self.handler.span_err(self.source_map.def_span(item.span), msg);
         }
     }
 
@@ -233,7 +236,7 @@ impl<'a> CollectProcMacros<'a> {
             } else {
                 "functions tagged with `#[proc_macro]` must be `pub`"
             };
-            self.handler.span_err(item.span, msg);
+            self.handler.span_err(self.source_map.def_span(item.span), msg);
         }
     }
 }
@@ -244,7 +247,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
             if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) {
                 let msg =
                     "cannot export macro_rules! macros from a `proc-macro` crate type currently";
-                self.handler.span_err(item.span, msg);
+                self.handler.span_err(self.source_map.def_span(item.span), msg);
             }
         }
 
@@ -295,7 +298,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
 
         let attr = match found_attr {
             None => {
-                self.check_not_pub_in_root(&item.vis, item.span);
+                self.check_not_pub_in_root(&item.vis, self.source_map.def_span(item.span));
                 let prev_in_root = mem::replace(&mut self.in_root, false);
                 visit::walk_item(self, item);
                 self.in_root = prev_in_root;
diff --git a/src/test/ui/proc-macro/export-macro.stderr b/src/test/ui/proc-macro/export-macro.stderr
index bc64caa07f9..36a6a9bb3e7 100644
--- a/src/test/ui/proc-macro/export-macro.stderr
+++ b/src/test/ui/proc-macro/export-macro.stderr
@@ -1,10 +1,8 @@
 error: cannot export macro_rules! macros from a `proc-macro` crate type currently
   --> $DIR/export-macro.rs:9:1
    |
-LL | / macro_rules! foo {
-LL | |     ($e:expr) => ($e)
-LL | | }
-   | |_^
+LL | macro_rules! foo {
+   | ^^^^^^^^^^^^^^^^
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/proc-macro/exports.stderr b/src/test/ui/proc-macro/exports.stderr
index 0ecbdf98dd3..7b23d08f2a8 100644
--- a/src/test/ui/proc-macro/exports.stderr
+++ b/src/test/ui/proc-macro/exports.stderr
@@ -2,7 +2,7 @@ error: `proc-macro` crate types currently cannot export any items other than fun
   --> $DIR/exports.rs:7:1
    |
 LL | pub fn a() {}
-   | ^^^^^^^^^^^^^
+   | ^^^^^^^^^^
 
 error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
   --> $DIR/exports.rs:8:1
@@ -14,13 +14,13 @@ error: `proc-macro` crate types currently cannot export any items other than fun
   --> $DIR/exports.rs:9:1
    |
 LL | pub enum C {}
-   | ^^^^^^^^^^^^^
+   | ^^^^^^^^^^
 
 error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
   --> $DIR/exports.rs:10:1
    |
 LL | pub mod d {}
-   | ^^^^^^^^^^^^
+   | ^^^^^^^^^
 
 error: aborting due to 4 previous errors
 
diff --git a/src/test/ui/proc-macro/non-root.stderr b/src/test/ui/proc-macro/non-root.stderr
index 8f84ddeeddb..90f94b677e9 100644
--- a/src/test/ui/proc-macro/non-root.stderr
+++ b/src/test/ui/proc-macro/non-root.stderr
@@ -2,7 +2,7 @@ error: functions tagged with `#[proc_macro]` must currently reside in the root o
   --> $DIR/non-root.rs:11:5
    |
 LL |     pub fn foo(arg: TokenStream) -> TokenStream { arg }
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/proc-macro/pub-at-crate-root.stderr b/src/test/ui/proc-macro/pub-at-crate-root.stderr
index 3b69b7875bd..2e7536a0c4f 100644
--- a/src/test/ui/proc-macro/pub-at-crate-root.stderr
+++ b/src/test/ui/proc-macro/pub-at-crate-root.stderr
@@ -1,32 +1,20 @@
 error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
   --> $DIR/pub-at-crate-root.rs:8:1
    |
-LL | / pub mod a {
-LL | |     use proc_macro::TokenStream;
-LL | |
-LL | |     #[proc_macro_derive(B)]
-...  |
-LL | |     }
-LL | | }
-   | |_^
+LL | pub mod a {
+   | ^^^^^^^^^
 
 error: functions tagged with `#[proc_macro_derive]` must currently reside in the root of the crate
   --> $DIR/pub-at-crate-root.rs:12:5
    |
-LL | /     pub fn bar(a: TokenStream) -> TokenStream {
-LL | |
-LL | |         a
-LL | |     }
-   | |_____^
+LL |     pub fn bar(a: TokenStream) -> TokenStream {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: functions tagged with `#[proc_macro_derive]` must be `pub`
   --> $DIR/pub-at-crate-root.rs:19:1
    |
-LL | / fn bar(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
-LL | |
-LL | |     a
-LL | | }
-   | |_^
+LL | fn bar(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 error: aborting due to 3 previous errors