Skip to content

Commit a57ccf7

Browse files
committed
nix panictry! in ParserAnyMacro::make
1 parent bb9f14c commit a57ccf7

10 files changed

+153
-48
lines changed

src/librustc_expand/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ ast_fragments! {
204204
}
205205

206206
impl AstFragmentKind {
207-
fn dummy(self, span: Span) -> AstFragment {
207+
crate fn dummy(self, span: Span) -> AstFragment {
208208
self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
209209
}
210210

src/librustc_expand/mbe/macro_rules.rs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,56 @@ fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Pa
8484
);
8585
}
8686

87+
fn emit_frag_parse_err(
88+
mut e: DiagnosticBuilder<'_>,
89+
parser: &Parser<'_>,
90+
site_span: Span,
91+
macro_ident: ast::Ident,
92+
arm_span: Span,
93+
kind: AstFragmentKind,
94+
) {
95+
if parser.token == token::Eof && e.message().ends_with(", found `<eof>`") {
96+
if !e.span.is_dummy() {
97+
// early end of macro arm (#52866)
98+
e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
99+
}
100+
let msg = &e.message[0];
101+
e.message[0] = (
102+
format!(
103+
"macro expansion ends with an incomplete expression: {}",
104+
msg.0.replace(", found `<eof>`", ""),
105+
),
106+
msg.1,
107+
);
108+
}
109+
if e.span.is_dummy() {
110+
// Get around lack of span in error (#30128)
111+
e.replace_span_with(site_span);
112+
if !parser.sess.source_map().is_imported(arm_span) {
113+
e.span_label(arm_span, "in this macro arm");
114+
}
115+
} else if parser.sess.source_map().is_imported(parser.token.span) {
116+
e.span_label(site_span, "in this macro invocation");
117+
}
118+
match kind {
119+
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
120+
suggest_slice_pat(&mut e, site_span, parser);
121+
}
122+
_ => annotate_err_with_kind(&mut e, kind, site_span),
123+
};
124+
e.emit();
125+
}
126+
87127
impl<'a> ParserAnyMacro<'a> {
88128
crate fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
89129
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
90-
let fragment = panictry!(parse_ast_fragment(parser, kind).map_err(|mut e| {
91-
if parser.token == token::Eof && e.message().ends_with(", found `<eof>`") {
92-
if !e.span.is_dummy() {
93-
// early end of macro arm (#52866)
94-
e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
95-
}
96-
let msg = &e.message[0];
97-
e.message[0] = (
98-
format!(
99-
"macro expansion ends with an incomplete expression: {}",
100-
msg.0.replace(", found `<eof>`", ""),
101-
),
102-
msg.1,
103-
);
130+
let fragment = match parse_ast_fragment(parser, kind) {
131+
Ok(f) => f,
132+
Err(err) => {
133+
emit_frag_parse_err(err, parser, site_span, macro_ident, arm_span, kind);
134+
return kind.dummy(site_span);
104135
}
105-
if e.span.is_dummy() {
106-
// Get around lack of span in error (#30128)
107-
e.replace_span_with(site_span);
108-
if !parser.sess.source_map().is_imported(arm_span) {
109-
e.span_label(arm_span, "in this macro arm");
110-
}
111-
} else if parser.sess.source_map().is_imported(parser.token.span) {
112-
e.span_label(site_span, "in this macro invocation");
113-
}
114-
match kind {
115-
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
116-
suggest_slice_pat(&mut e, site_span, parser);
117-
}
118-
_ => annotate_err_with_kind(&mut e, kind, site_span),
119-
};
120-
e
121-
}));
136+
};
122137

123138
// We allow semicolons at the end of expressions -- e.g., the semicolon in
124139
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,

src/test/ui/editions/edition-keywords-2018-2015-parsing.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// edition:2018
22
// aux-build:edition-kw-macro-2015.rs
33

4+
fn main() {}
5+
46
#[macro_use]
57
extern crate edition_kw_macro_2015;
68

9+
mod module {
10+
pub fn r#async() {}
11+
}
12+
713
pub fn check_async() {
814
let mut async = 1; //~ ERROR expected identifier, found keyword `async`
915
let mut r#async = 1; // OK
@@ -13,7 +19,7 @@ pub fn check_async() {
1319
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
1420
r#async = consumes_async_raw!(r#async); // OK
1521

16-
if passes_ident!(async) == 1 {}
22+
if passes_ident!(async) == 1 {} //~ ERROR async closures are unstable
1723
if passes_ident!(r#async) == 1 {} // OK
1824
module::async(); //~ ERROR expected identifier, found keyword `async`
1925
module::r#async(); // OK

src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected identifier, found keyword `async`
2-
--> $DIR/edition-keywords-2018-2015-parsing.rs:8:13
2+
--> $DIR/edition-keywords-2018-2015-parsing.rs:14:13
33
|
44
LL | let mut async = 1;
55
| ^^^^^ expected identifier, found keyword
@@ -10,7 +10,7 @@ LL | let mut r#async = 1;
1010
| ^^^^^^^
1111

1212
error: expected identifier, found keyword `async`
13-
--> $DIR/edition-keywords-2018-2015-parsing.rs:18:13
13+
--> $DIR/edition-keywords-2018-2015-parsing.rs:24:13
1414
|
1515
LL | module::async();
1616
| ^^^^^ expected identifier, found keyword
@@ -21,13 +21,13 @@ LL | module::r#async();
2121
| ^^^^^^^
2222

2323
error: no rules expected the token `r#async`
24-
--> $DIR/edition-keywords-2018-2015-parsing.rs:12:31
24+
--> $DIR/edition-keywords-2018-2015-parsing.rs:18:31
2525
|
2626
LL | r#async = consumes_async!(r#async);
2727
| ^^^^^^^ no rules expected this token in macro call
2828

2929
error: no rules expected the token `async`
30-
--> $DIR/edition-keywords-2018-2015-parsing.rs:13:35
30+
--> $DIR/edition-keywords-2018-2015-parsing.rs:19:35
3131
|
3232
LL | r#async = consumes_async_raw!(async);
3333
| ^^^^^ no rules expected this token in macro call
@@ -38,10 +38,20 @@ error: macro expansion ends with an incomplete expression: expected one of `move
3838
LL | ($i: ident) => ($i)
3939
| ^ expected one of `move`, `|`, or `||`
4040
|
41-
::: $DIR/edition-keywords-2018-2015-parsing.rs:16:8
41+
::: $DIR/edition-keywords-2018-2015-parsing.rs:22:8
4242
|
4343
LL | if passes_ident!(async) == 1 {}
4444
| -------------------- in this macro invocation
4545

46-
error: aborting due to 5 previous errors
46+
error[E0658]: async closures are unstable
47+
--> $DIR/edition-keywords-2018-2015-parsing.rs:22:22
48+
|
49+
LL | if passes_ident!(async) == 1 {}
50+
| ^^^^^
51+
|
52+
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
53+
= help: add `#![feature(async_closure)]` to the crate attributes to enable
54+
55+
error: aborting due to 6 previous errors
4756

57+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/editions/edition-keywords-2018-2018-parsing.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// edition:2018
22
// aux-build:edition-kw-macro-2018.rs
33

4+
fn main() {}
5+
46
#[macro_use]
57
extern crate edition_kw_macro_2018;
68

9+
mod module {
10+
pub fn r#async() {}
11+
}
12+
713
pub fn check_async() {
814
let mut async = 1; //~ ERROR expected identifier, found keyword `async`
915
let mut r#async = 1; // OK
@@ -13,7 +19,7 @@ pub fn check_async() {
1319
r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
1420
r#async = consumes_async_raw!(r#async); // OK
1521

16-
if passes_ident!(async) == 1 {}
22+
if passes_ident!(async) == 1 {} //~ ERROR async closures are unstable
1723
if passes_ident!(r#async) == 1 {} // OK
1824
module::async(); //~ ERROR expected identifier, found keyword `async`
1925
module::r#async(); // OK

src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: expected identifier, found keyword `async`
2-
--> $DIR/edition-keywords-2018-2018-parsing.rs:8:13
2+
--> $DIR/edition-keywords-2018-2018-parsing.rs:14:13
33
|
44
LL | let mut async = 1;
55
| ^^^^^ expected identifier, found keyword
@@ -10,7 +10,7 @@ LL | let mut r#async = 1;
1010
| ^^^^^^^
1111

1212
error: expected identifier, found keyword `async`
13-
--> $DIR/edition-keywords-2018-2018-parsing.rs:18:13
13+
--> $DIR/edition-keywords-2018-2018-parsing.rs:24:13
1414
|
1515
LL | module::async();
1616
| ^^^^^ expected identifier, found keyword
@@ -21,13 +21,13 @@ LL | module::r#async();
2121
| ^^^^^^^
2222

2323
error: no rules expected the token `r#async`
24-
--> $DIR/edition-keywords-2018-2018-parsing.rs:12:31
24+
--> $DIR/edition-keywords-2018-2018-parsing.rs:18:31
2525
|
2626
LL | r#async = consumes_async!(r#async);
2727
| ^^^^^^^ no rules expected this token in macro call
2828

2929
error: no rules expected the token `async`
30-
--> $DIR/edition-keywords-2018-2018-parsing.rs:13:35
30+
--> $DIR/edition-keywords-2018-2018-parsing.rs:19:35
3131
|
3232
LL | r#async = consumes_async_raw!(async);
3333
| ^^^^^ no rules expected this token in macro call
@@ -38,10 +38,20 @@ error: macro expansion ends with an incomplete expression: expected one of `move
3838
LL | ($i: ident) => ($i)
3939
| ^ expected one of `move`, `|`, or `||`
4040
|
41-
::: $DIR/edition-keywords-2018-2018-parsing.rs:16:8
41+
::: $DIR/edition-keywords-2018-2018-parsing.rs:22:8
4242
|
4343
LL | if passes_ident!(async) == 1 {}
4444
| -------------------- in this macro invocation
4545

46-
error: aborting due to 5 previous errors
46+
error[E0658]: async closures are unstable
47+
--> $DIR/edition-keywords-2018-2018-parsing.rs:22:22
48+
|
49+
LL | if passes_ident!(async) == 1 {}
50+
| ^^^^^
51+
|
52+
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
53+
= help: add `#![feature(async_closure)]` to the crate attributes to enable
54+
55+
error: aborting due to 6 previous errors
4756

57+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/macros/macro-context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ macro_rules! m {
44
//~| ERROR macro expansion ignores token `typeof`
55
//~| ERROR macro expansion ignores token `;`
66
//~| ERROR macro expansion ignores token `;`
7+
//~| ERROR cannot find type `i` in this scope
8+
//~| ERROR cannot find value `i` in this scope
79
}
810

911
fn main() {

src/test/ui/macros/macro-context.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,29 @@ LL | m!();
4242
|
4343
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4444

45-
error: aborting due to 4 previous errors
45+
error[E0412]: cannot find type `i` in this scope
46+
--> $DIR/macro-context.rs:3:13
47+
|
48+
LL | () => ( i ; typeof );
49+
| ^ help: a builtin type with a similar name exists: `i8`
50+
...
51+
LL | let a: m!();
52+
| ---- in this macro invocation
53+
|
54+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
55+
56+
error[E0425]: cannot find value `i` in this scope
57+
--> $DIR/macro-context.rs:3:13
58+
|
59+
LL | () => ( i ; typeof );
60+
| ^ help: a local variable with a similar name exists: `a`
61+
...
62+
LL | let i = m!();
63+
| ---- in this macro invocation
64+
|
65+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
66+
67+
error: aborting due to 6 previous errors
4668

69+
Some errors have detailed explanations: E0412, E0425.
70+
For more information about an error, try `rustc --explain E0412`.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
fn main() {
22
let _ = Option:Some(vec![0, 1]); //~ ERROR expected type, found
3+
//~^ ERROR expected value, found enum `Option`
4+
//~| ERROR expected type, found variant `Some`
35
}
46

57
// This case isn't currently being handled gracefully due to the macro invocation.

src/test/ui/type/ascription/issue-47666.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,35 @@ LL | let _ = Option:Some(vec![0, 1]);
1313
= note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information
1414
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1515

16-
error: aborting due to previous error
16+
error[E0423]: expected value, found enum `Option`
17+
--> $DIR/issue-47666.rs:2:13
18+
|
19+
LL | let _ = Option:Some(vec![0, 1]);
20+
| ^^^^^^
21+
|
22+
help: try using one of the enum's variants
23+
|
24+
LL | let _ = std::option::Option::None:Some(vec![0, 1]);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
26+
LL | let _ = std::option::Option::Some:Some(vec![0, 1]);
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
28+
29+
error[E0573]: expected type, found variant `Some`
30+
--> $DIR/issue-47666.rs:2:20
31+
|
32+
LL | let _ = Option:Some(vec![0, 1]);
33+
| ^^^^^^^^^^^^^^^^ not a type
34+
|
35+
help: try using the variant's enum
36+
|
37+
LL | let _ = Option:std::option::Option;
38+
| ^^^^^^^^^^^^^^^^^^^
39+
help: maybe you meant to write a path separator here
40+
|
41+
LL | let _ = Option::Some(vec![0, 1]);
42+
| ^^
43+
44+
error: aborting due to 3 previous errors
1745

46+
Some errors have detailed explanations: E0423, E0573.
47+
For more information about an error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)