From bdd07f932b6d707c99f9c6127a424a9e801c997f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 22 Mar 2020 12:38:50 +0300 Subject: [PATCH] proc_macro_harness: Use item header spans for errors --- .../proc_macro_harness.rs | 13 ++++++---- src/test/ui/proc-macro/export-macro.stderr | 6 ++--- src/test/ui/proc-macro/exports.stderr | 6 ++--- src/test/ui/proc-macro/non-root.stderr | 2 +- .../ui/proc-macro/pub-at-crate-root.stderr | 24 +++++-------------- 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 71622a3b7e657..6540bcc415605 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, 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 bc64caa07f972..36a6a9bb3e72b 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 0ecbdf98dd31c..7b23d08f2a8a5 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 8f84ddeeddb20..90f94b677e90f 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 3b69b7875bde0..2e7536a0c4f09 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