Auto merge of #14449 - Veykril:parser-vis-recov, r=Veykril

fix: Recover from `pub()` visibility modifier
This commit is contained in:
bors 2023-03-30 13:08:14 +00:00
commit 5c6a38abde
5 changed files with 68 additions and 3 deletions

View file

@ -218,17 +218,22 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
// pub(self) struct S;
// pub(super) struct S;
// test_err crate_visibility_empty_recover
// pub() struct S;
// test pub_parens_typepath
// struct B(pub (super::A));
// struct B(pub (crate::A,));
T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
T![crate] | T![self] | T![super] | T![ident] | T![')'] if p.nth(2) != T![:] => {
// If we are in a tuple struct, then the parens following `pub`
// might be an tuple field, not part of the visibility. So in that
// case we don't want to consume an identifier.
// test pub_tuple_field
// struct MyStruct(pub (u32, u32));
if !(in_tuple_field && matches!(p.nth(1), T![ident])) {
// struct MyStruct(pub (u32));
// struct MyStruct(pub ());
if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) {
p.bump(T!['(']);
paths::use_path(p);
p.expect(T![')']);
@ -243,7 +248,7 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
paths::use_path(p);
p.expect(T![')']);
}
_ => (),
_ => {}
}
}
m.complete(p, VISIBILITY);

View file

@ -0,0 +1,18 @@
SOURCE_FILE
STRUCT
VISIBILITY
PUB_KW "pub"
L_PAREN "("
PATH
PATH_SEGMENT
ERROR
R_PAREN ")"
WHITESPACE " "
STRUCT_KW "struct"
WHITESPACE " "
NAME
IDENT "S"
SEMICOLON ";"
WHITESPACE "\n"
error 4: expected identifier
error 5: expected R_PAREN

View file

@ -0,0 +1 @@
pub() struct S;

View file

@ -28,3 +28,42 @@ SOURCE_FILE
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
STRUCT
STRUCT_KW "struct"
WHITESPACE " "
NAME
IDENT "MyStruct"
TUPLE_FIELD_LIST
L_PAREN "("
TUPLE_FIELD
VISIBILITY
PUB_KW "pub"
WHITESPACE " "
PAREN_TYPE
L_PAREN "("
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "u32"
R_PAREN ")"
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
STRUCT
STRUCT_KW "struct"
WHITESPACE " "
NAME
IDENT "MyStruct"
TUPLE_FIELD_LIST
L_PAREN "("
TUPLE_FIELD
VISIBILITY
PUB_KW "pub"
WHITESPACE " "
TUPLE_TYPE
L_PAREN "("
R_PAREN ")"
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"

View file

@ -1 +1,3 @@
struct MyStruct(pub (u32, u32));
struct MyStruct(pub (u32));
struct MyStruct(pub ());