Skip to content

Commit 255e076

Browse files
Rollup merge of #81608 - Aaron1011:macro-res-parse-err, r=davidtwco
Improve handling of spans around macro result parse errors Fixes #81543 After we expand a macro, we try to parse the resulting tokens as a AST node. This commit makes several improvements to how we handle spans when an error occurs: * Only ovewrite the original `Span` if it's a dummy span. This preserves a more-specific span if one is available. * Use `self.prev_token` instead of `self.token` when emitting an error message after encountering EOF, since an EOF token always has a dummy span * Make `SourceMap::next_point` leave dummy spans unused. A dummy span does not have a logical 'next point', since it's a zero-length span. Re-using the span span preserves its 'dummy-ness' for other checks
2 parents 86d0e6d + 6c14aad commit 255e076

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

compiler/rustc_expand/src/expand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
896896
fragment
897897
}
898898
Err(mut err) => {
899-
err.set_span(span);
899+
if err.span.is_dummy() {
900+
err.set_span(span);
901+
}
900902
annotate_err_with_kind(&mut err, kind, span);
901903
err.emit();
902904
self.cx.trace_macros_diag();

compiler/rustc_parse/src/parser/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ impl<'a> Parser<'a> {
11041104
let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
11051105
// Point at the end of the macro call when reaching end of macro arguments.
11061106
(token::Eof, Some(_)) => {
1107-
let sp = self.sess.source_map().next_point(self.token.span);
1107+
let sp = self.sess.source_map().next_point(self.prev_token.span);
11081108
(sp, sp)
11091109
}
11101110
// We don't want to point at the following span after DUMMY_SP.
@@ -1721,7 +1721,7 @@ impl<'a> Parser<'a> {
17211721
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a> {
17221722
let (span, msg) = match (&self.token.kind, self.subparser_name) {
17231723
(&token::Eof, Some(origin)) => {
1724-
let sp = self.sess.source_map().next_point(self.token.span);
1724+
let sp = self.sess.source_map().next_point(self.prev_token.span);
17251725
(sp, format!("expected expression, found end of {}", origin))
17261726
}
17271727
_ => (

compiler/rustc_span/src/source_map.rs

+3
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,9 @@ impl SourceMap {
799799

800800
/// Returns a new span representing the next character after the end-point of this span.
801801
pub fn next_point(&self, sp: Span) -> Span {
802+
if sp.is_dummy() {
803+
return sp;
804+
}
802805
let start_of_next_point = sp.hi().0;
803806

804807
let width = self.find_width_of_character_at_span(sp.shrink_to_hi(), true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// aux-build:test-macros.rs
2+
3+
// Regression test for issue #81543
4+
// Tests that we emit a properly spanned error
5+
// when the output of a proc-macro cannot be parsed
6+
// as the expected AST node kind
7+
8+
extern crate test_macros;
9+
10+
test_macros::identity! {
11+
fn 32() {} //~ ERROR expected identifier
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected identifier, found `32`
2+
--> $DIR/issue-81543-item-parse-err.rs:11:8
3+
|
4+
LL | fn 32() {}
5+
| ^^ expected identifier
6+
7+
error: aborting due to previous error
8+

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ error: expected type, found `'`
22
--> $DIR/lifetimes.rs:7:10
33
|
44
LL | type A = single_quote_alone!();
5-
| ^^^^^^^^^^^^^^^^^^^^^ this macro call doesn't expand to a type
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected type
8+
| this macro call doesn't expand to a type
9+
|
10+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)