Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc_macro_harness: Use item header spans for errors #70266

Merged
merged 1 commit into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/librustc_builtin_macros/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
};
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/proc-macro/export-macro.stderr
Original file line number Diff line number Diff line change
@@ -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

6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/exports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/non-root.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

24 changes: 6 additions & 18 deletions src/test/ui/proc-macro/pub-at-crate-root.stderr
Original file line number Diff line number Diff line change
@@ -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