Skip to content

Commit 5c6a38a

Browse files
committed
Auto merge of rust-lang#14449 - Veykril:parser-vis-recov, r=Veykril
fix: Recover from `pub()` visibility modifier
2 parents 5390949 + cb54639 commit 5c6a38a

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

crates/parser/src/grammar.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,22 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
218218
// pub(self) struct S;
219219
// pub(super) struct S;
220220

221+
// test_err crate_visibility_empty_recover
222+
// pub() struct S;
223+
221224
// test pub_parens_typepath
222225
// struct B(pub (super::A));
223226
// struct B(pub (crate::A,));
224-
T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
227+
T![crate] | T![self] | T![super] | T![ident] | T![')'] if p.nth(2) != T![:] => {
225228
// If we are in a tuple struct, then the parens following `pub`
226229
// might be an tuple field, not part of the visibility. So in that
227230
// case we don't want to consume an identifier.
228231

229232
// test pub_tuple_field
230233
// struct MyStruct(pub (u32, u32));
231-
if !(in_tuple_field && matches!(p.nth(1), T![ident])) {
234+
// struct MyStruct(pub (u32));
235+
// struct MyStruct(pub ());
236+
if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) {
232237
p.bump(T!['(']);
233238
paths::use_path(p);
234239
p.expect(T![')']);
@@ -243,7 +248,7 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
243248
paths::use_path(p);
244249
p.expect(T![')']);
245250
}
246-
_ => (),
251+
_ => {}
247252
}
248253
}
249254
m.complete(p, VISIBILITY);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SOURCE_FILE
2+
STRUCT
3+
VISIBILITY
4+
PUB_KW "pub"
5+
L_PAREN "("
6+
PATH
7+
PATH_SEGMENT
8+
ERROR
9+
R_PAREN ")"
10+
WHITESPACE " "
11+
STRUCT_KW "struct"
12+
WHITESPACE " "
13+
NAME
14+
IDENT "S"
15+
SEMICOLON ";"
16+
WHITESPACE "\n"
17+
error 4: expected identifier
18+
error 5: expected R_PAREN
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub() struct S;

crates/parser/test_data/parser/inline/ok/0196_pub_tuple_field.rast

+39
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,42 @@ SOURCE_FILE
2828
R_PAREN ")"
2929
SEMICOLON ";"
3030
WHITESPACE "\n"
31+
STRUCT
32+
STRUCT_KW "struct"
33+
WHITESPACE " "
34+
NAME
35+
IDENT "MyStruct"
36+
TUPLE_FIELD_LIST
37+
L_PAREN "("
38+
TUPLE_FIELD
39+
VISIBILITY
40+
PUB_KW "pub"
41+
WHITESPACE " "
42+
PAREN_TYPE
43+
L_PAREN "("
44+
PATH_TYPE
45+
PATH
46+
PATH_SEGMENT
47+
NAME_REF
48+
IDENT "u32"
49+
R_PAREN ")"
50+
R_PAREN ")"
51+
SEMICOLON ";"
52+
WHITESPACE "\n"
53+
STRUCT
54+
STRUCT_KW "struct"
55+
WHITESPACE " "
56+
NAME
57+
IDENT "MyStruct"
58+
TUPLE_FIELD_LIST
59+
L_PAREN "("
60+
TUPLE_FIELD
61+
VISIBILITY
62+
PUB_KW "pub"
63+
WHITESPACE " "
64+
TUPLE_TYPE
65+
L_PAREN "("
66+
R_PAREN ")"
67+
R_PAREN ")"
68+
SEMICOLON ";"
69+
WHITESPACE "\n"
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
struct MyStruct(pub (u32, u32));
2+
struct MyStruct(pub (u32));
3+
struct MyStruct(pub ());

0 commit comments

Comments
 (0)