Skip to content

Commit 69c0bcd

Browse files
authored
Rollup merge of rust-lang#70266 - petrochenkov:prochead, r=varkor
proc_macro_harness: Use item header spans for errors Addresses rust-lang#70233 (comment).
2 parents 8fe8bad + bdd07f9 commit 69c0bcd

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

src/librustc_builtin_macros/proc_macro_harness.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_expand::base::{ExtCtxt, Resolver};
1010
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1111
use rustc_session::parse::ParseSess;
1212
use rustc_span::hygiene::AstPass;
13+
use rustc_span::source_map::SourceMap;
1314
use rustc_span::symbol::{kw, sym};
1415
use rustc_span::{Span, DUMMY_SP};
1516
use smallvec::smallvec;
@@ -44,6 +45,7 @@ struct CollectProcMacros<'a> {
4445
macros: Vec<ProcMacro>,
4546
in_root: bool,
4647
handler: &'a rustc_errors::Handler,
48+
source_map: &'a SourceMap,
4749
is_proc_macro_crate: bool,
4850
is_test_crate: bool,
4951
}
@@ -65,6 +67,7 @@ pub fn inject(
6567
macros: Vec::new(),
6668
in_root: true,
6769
handler,
70+
source_map: sess.source_map(),
6871
is_proc_macro_crate,
6972
is_test_crate,
7073
};
@@ -195,7 +198,7 @@ impl<'a> CollectProcMacros<'a> {
195198
} else {
196199
"functions tagged with `#[proc_macro_derive]` must be `pub`"
197200
};
198-
self.handler.span_err(item.span, msg);
201+
self.handler.span_err(self.source_map.def_span(item.span), msg);
199202
}
200203
}
201204

@@ -214,7 +217,7 @@ impl<'a> CollectProcMacros<'a> {
214217
} else {
215218
"functions tagged with `#[proc_macro_attribute]` must be `pub`"
216219
};
217-
self.handler.span_err(item.span, msg);
220+
self.handler.span_err(self.source_map.def_span(item.span), msg);
218221
}
219222
}
220223

@@ -233,7 +236,7 @@ impl<'a> CollectProcMacros<'a> {
233236
} else {
234237
"functions tagged with `#[proc_macro]` must be `pub`"
235238
};
236-
self.handler.span_err(item.span, msg);
239+
self.handler.span_err(self.source_map.def_span(item.span), msg);
237240
}
238241
}
239242
}
@@ -244,7 +247,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
244247
if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) {
245248
let msg =
246249
"cannot export macro_rules! macros from a `proc-macro` crate type currently";
247-
self.handler.span_err(item.span, msg);
250+
self.handler.span_err(self.source_map.def_span(item.span), msg);
248251
}
249252
}
250253

@@ -295,7 +298,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
295298

296299
let attr = match found_attr {
297300
None => {
298-
self.check_not_pub_in_root(&item.vis, item.span);
301+
self.check_not_pub_in_root(&item.vis, self.source_map.def_span(item.span));
299302
let prev_in_root = mem::replace(&mut self.in_root, false);
300303
visit::walk_item(self, item);
301304
self.in_root = prev_in_root;
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error: cannot export macro_rules! macros from a `proc-macro` crate type currently
22
--> $DIR/export-macro.rs:9:1
33
|
4-
LL | / macro_rules! foo {
5-
LL | | ($e:expr) => ($e)
6-
LL | | }
7-
| |_^
4+
LL | macro_rules! foo {
5+
| ^^^^^^^^^^^^^^^^
86

97
error: aborting due to previous error
108

src/test/ui/proc-macro/exports.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: `proc-macro` crate types currently cannot export any items other than fun
22
--> $DIR/exports.rs:7:1
33
|
44
LL | pub fn a() {}
5-
| ^^^^^^^^^^^^^
5+
| ^^^^^^^^^^
66

77
error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
88
--> $DIR/exports.rs:8:1
@@ -14,13 +14,13 @@ error: `proc-macro` crate types currently cannot export any items other than fun
1414
--> $DIR/exports.rs:9:1
1515
|
1616
LL | pub enum C {}
17-
| ^^^^^^^^^^^^^
17+
| ^^^^^^^^^^
1818

1919
error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
2020
--> $DIR/exports.rs:10:1
2121
|
2222
LL | pub mod d {}
23-
| ^^^^^^^^^^^^
23+
| ^^^^^^^^^
2424

2525
error: aborting due to 4 previous errors
2626

src/test/ui/proc-macro/non-root.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: functions tagged with `#[proc_macro]` must currently reside in the root o
22
--> $DIR/non-root.rs:11:5
33
|
44
LL | pub fn foo(arg: TokenStream) -> TokenStream { arg }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
22
--> $DIR/pub-at-crate-root.rs:8:1
33
|
4-
LL | / pub mod a {
5-
LL | | use proc_macro::TokenStream;
6-
LL | |
7-
LL | | #[proc_macro_derive(B)]
8-
... |
9-
LL | | }
10-
LL | | }
11-
| |_^
4+
LL | pub mod a {
5+
| ^^^^^^^^^
126

137
error: functions tagged with `#[proc_macro_derive]` must currently reside in the root of the crate
148
--> $DIR/pub-at-crate-root.rs:12:5
159
|
16-
LL | / pub fn bar(a: TokenStream) -> TokenStream {
17-
LL | |
18-
LL | | a
19-
LL | | }
20-
| |_____^
10+
LL | pub fn bar(a: TokenStream) -> TokenStream {
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2112

2213
error: functions tagged with `#[proc_macro_derive]` must be `pub`
2314
--> $DIR/pub-at-crate-root.rs:19:1
2415
|
25-
LL | / fn bar(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
26-
LL | |
27-
LL | | a
28-
LL | | }
29-
| |_^
16+
LL | fn bar(a: proc_macro::TokenStream) -> proc_macro::TokenStream {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3018

3119
error: aborting due to 3 previous errors
3220

0 commit comments

Comments
 (0)