Skip to content

Commit 0a8db69

Browse files
committed
nix panictry! in ParserAnyMacro::make
1 parent 0f2d968 commit 0a8db69

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
@@ -83,41 +83,56 @@ fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Pa
8383
);
8484
}
8585

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

122137
// We allow semicolons at the end of expressions -- e.g., the semicolon in
123138
// `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)