Skip to content

Commit dece622

Browse files
committed
Recover from some macros
1 parent 287db04 commit dece622

9 files changed

+124
-102
lines changed

compiler/rustc_parse/src/parser/item.rs

+35-11
Original file line numberDiff line numberDiff line change
@@ -1343,10 +1343,14 @@ impl<'a> Parser<'a> {
13431343
let ident = this.parse_field_ident("enum", vlo)?;
13441344

13451345
if this.token == token::Not {
1346-
return this.unexpected().map_err(|mut err| {
1347-
err.note(fluent::parse_macro_expands_to_enum_variant);
1348-
err
1349-
});
1346+
if let Err(mut err) = this.unexpected::<()>() {
1347+
err.note(fluent::parse_macro_expands_to_enum_variant).emit();
1348+
}
1349+
1350+
this.bump();
1351+
this.parse_delim_args()?;
1352+
1353+
return Ok((None, TrailingToken::MaybeComma));
13501354
}
13511355

13521356
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
@@ -1586,7 +1590,8 @@ impl<'a> Parser<'a> {
15861590
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
15871591
let lo = this.token.span;
15881592
let vis = this.parse_visibility(FollowedByType::No)?;
1589-
Ok((this.parse_single_struct_field(adt_ty, lo, vis, attrs)?, TrailingToken::None))
1593+
this.parse_single_struct_field(adt_ty, lo, vis, attrs)
1594+
.map(|field| (field, TrailingToken::None))
15901595
})
15911596
}
15921597

@@ -1698,10 +1703,9 @@ impl<'a> Parser<'a> {
16981703
Ok(a_var)
16991704
}
17001705

1701-
fn expect_field_ty_separator(&mut self, adt_ty: &str) -> PResult<'a, ()> {
1706+
fn expect_field_ty_separator(&mut self) -> PResult<'a, ()> {
17021707
if let Err(mut err) = self.expect(&token::Colon) {
17031708
let sm = self.sess.source_map();
1704-
let mac_invoc = self.token.kind == token::Not;
17051709
let eq_typo = self.token.kind == token::Eq && self.look_ahead(1, |t| t.is_path_start());
17061710
let semi_typo = self.token.kind == token::Semi
17071711
&& self.look_ahead(1, |t| {
@@ -1713,9 +1717,7 @@ impl<'a> Parser<'a> {
17131717
_ => true,
17141718
}
17151719
});
1716-
if mac_invoc {
1717-
err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit();
1718-
} else if eq_typo || semi_typo {
1720+
if eq_typo || semi_typo {
17191721
self.bump();
17201722
// Gracefully handle small typos.
17211723
err.span_suggestion_short(
@@ -1741,7 +1743,29 @@ impl<'a> Parser<'a> {
17411743
attrs: AttrVec,
17421744
) -> PResult<'a, FieldDef> {
17431745
let name = self.parse_field_ident(adt_ty, lo)?;
1744-
self.expect_field_ty_separator(adt_ty)?;
1746+
// Parse the macro invocation and recover
1747+
if self.token.kind == token::Not {
1748+
if let Err(mut err) = self.unexpected::<FieldDef>() {
1749+
err.subdiagnostic(MacroExpandsToAdtField { adt_ty }).emit();
1750+
self.bump();
1751+
self.parse_delim_args()?;
1752+
return Ok(FieldDef {
1753+
span: DUMMY_SP,
1754+
ident: None,
1755+
vis,
1756+
id: DUMMY_NODE_ID,
1757+
ty: P(Ty {
1758+
id: DUMMY_NODE_ID,
1759+
kind: TyKind::Err,
1760+
span: DUMMY_SP,
1761+
tokens: None,
1762+
}),
1763+
attrs,
1764+
is_placeholder: false,
1765+
});
1766+
}
1767+
}
1768+
self.expect_field_ty_separator()?;
17451769
let ty = self.parse_ty()?;
17461770
if self.token.kind == token::Colon && self.look_ahead(1, |tok| tok.kind != token::Colon) {
17471771
self.sess.emit_err(errors::SingleColonStructType { span: self.token.span });

compiler/rustc_parse/src/parser/pat.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ impl<'a> Parser<'a> {
181181
err
182182
})?;
183183
if rc == RecoverComma::Yes {
184-
self.maybe_recover_unexpected_comma(
185-
pat.span,
186-
matches!(pat.kind, PatKind::MacCall(_)),
187-
rt,
188-
)?;
184+
self.maybe_recover_unexpected_comma(pat.span, false, rt)?;
189185
}
190186
pats.push(pat);
191187
}

tests/ui/parser/macro/macro-expand-to-field-2.rs

-16
This file was deleted.

tests/ui/parser/macro/macro-expand-to-field-2.stderr

-18
This file was deleted.

tests/ui/parser/macro/macro-expand-to-field-3.rs

-15
This file was deleted.

tests/ui/parser/macro/macro-expand-to-field-3.stderr

-19
This file was deleted.

tests/ui/parser/macro/macro-expand-to-field.rs

+36-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,46 @@ macro_rules! variant {
1212
}
1313
}
1414

15-
struct Struct { //~ NOTE while parsing this struct
15+
struct Struct {
1616
field!(bar:u128), //~ NOTE macros cannot expand to struct fields
17-
//~^ ERROR expected `:`, found `!`
18-
//~^^ NOTE expected `:`
19-
//~^^^ ERROR expected `,`, or `}`, found `(`
17+
//~^ ERROR unexpected token: `!`
18+
//~^^ NOTE unexpected token after this
19+
a: u32,
20+
b: u32,
21+
field!(recovers:()), //~ NOTE macros cannot expand to struct fields
22+
//~^ ERROR unexpected token: `!`
23+
//~^^ NOTE unexpected token after this
2024
}
2125

22-
enum EnumVariant { //~ NOTE while parsing this enum
26+
enum EnumVariant {
2327
variant!(whoops), //~ NOTE macros cannot expand to enum variants
2428
//~^ ERROR unexpected token: `!`
2529
//~^^ NOTE unexpected token after this
30+
U32,
31+
F64,
32+
variant!(recovers), //~ NOTE macros cannot expand to enum variants
33+
//~^ ERROR unexpected token: `!`
34+
//~^^ NOTE unexpected token after this
35+
}
36+
37+
enum EnumVariantField {
38+
Named {
39+
field!(oopsies:()), //~ NOTE macros cannot expand to struct fields
40+
//~^ ERROR unexpected token: `!`
41+
//~^^ unexpected token after this
42+
field!(oopsies2:()), //~ NOTE macros cannot expand to struct fields
43+
//~^ ERROR unexpected token: `!`
44+
//~^^ unexpected token after this
45+
},
46+
}
47+
48+
union Union {
49+
A: u32,
50+
field!(oopsies:()), //~ NOTE macros cannot expand to union fields
51+
//~^ ERROR unexpected token: `!`
52+
//~^^ unexpected token after this
53+
B: u32,
54+
field!(recovers:()), //~ NOTE macros cannot expand to union fields
55+
//~^ ERROR unexpected token: `!`
56+
//~^^ unexpected token after this
2657
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
1-
error: expected `:`, found `!`
1+
error: unexpected token: `!`
22
--> $DIR/macro-expand-to-field.rs:16:10
33
|
44
LL | field!(bar:u128),
5-
| ^ expected `:`
5+
| ^ unexpected token after this
66
|
77
= note: macros cannot expand to struct fields
88

9-
error: expected `,`, or `}`, found `(`
10-
--> $DIR/macro-expand-to-field.rs:16:11
9+
error: unexpected token: `!`
10+
--> $DIR/macro-expand-to-field.rs:21:10
1111
|
12-
LL | struct Struct {
13-
| ------ while parsing this struct
14-
LL | field!(bar:u128),
15-
| ^
12+
LL | field!(recovers:()),
13+
| ^ unexpected token after this
14+
|
15+
= note: macros cannot expand to struct fields
1616

1717
error: unexpected token: `!`
18-
--> $DIR/macro-expand-to-field.rs:23:12
18+
--> $DIR/macro-expand-to-field.rs:27:12
1919
|
20-
LL | enum EnumVariant {
21-
| ----------- while parsing this enum
2220
LL | variant!(whoops),
2321
| ^ unexpected token after this
2422
|
2523
= note: macros cannot expand to enum variants
26-
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
2724

28-
error: aborting due to 3 previous errors
25+
error: unexpected token: `!`
26+
--> $DIR/macro-expand-to-field.rs:32:12
27+
|
28+
LL | variant!(recovers),
29+
| ^ unexpected token after this
30+
|
31+
= note: macros cannot expand to enum variants
32+
33+
error: unexpected token: `!`
34+
--> $DIR/macro-expand-to-field.rs:39:14
35+
|
36+
LL | field!(oopsies:()),
37+
| ^ unexpected token after this
38+
|
39+
= note: macros cannot expand to struct fields
40+
41+
error: unexpected token: `!`
42+
--> $DIR/macro-expand-to-field.rs:42:14
43+
|
44+
LL | field!(oopsies2:()),
45+
| ^ unexpected token after this
46+
|
47+
= note: macros cannot expand to struct fields
48+
49+
error: unexpected token: `!`
50+
--> $DIR/macro-expand-to-field.rs:50:10
51+
|
52+
LL | field!(oopsies:()),
53+
| ^ unexpected token after this
54+
|
55+
= note: macros cannot expand to union fields
56+
57+
error: unexpected token: `!`
58+
--> $DIR/macro-expand-to-field.rs:54:10
59+
|
60+
LL | field!(recovers:()),
61+
| ^ unexpected token after this
62+
|
63+
= note: macros cannot expand to union fields
64+
65+
error: aborting due to 8 previous errors
2966

tests/ui/parser/macro/macro-expand-to-match-arm.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fn main() {
1010
Some(1) => {},
1111
arm!(None => {}), //~ NOTE macros cannot expand to match arms
1212
//~^ ERROR unexpected `,` in pattern
13+
// doesn't recover
14+
Some(2) => {},
1315
_ => {},
1416
};
1517
}

0 commit comments

Comments
 (0)