Auto merge of #12962 - lowr:patch/parse-last-range-pattern, r=Veykril

Parse range patterns in struct and slice without trailing comma

Resolves #12935

This patch includes the support for range patterns in slices, which is unstable (tracked in https://github.com/rust-lang/rust/issues/67264). If it's not desired I can remove it.
This commit is contained in:
bors 2022-08-08 15:03:39 +00:00
commit 79c22d5fb1
5 changed files with 181 additions and 7 deletions

View File

@ -75,6 +75,16 @@ fn pattern_single_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
// Some(1..) => ()
// }
//
// match () {
// S { a: 0 } => (),
// S { a: 1.. } => (),
// }
//
// match () {
// [0] => (),
// [1..] => (),
// }
//
// match (10 as u8, 5 as u8) {
// (0, _) => (),
// (1.., _) => ()
@ -88,11 +98,27 @@ fn pattern_single_r(p: &mut Parser<'_>, recovery_set: TokenSet) {
let m = lhs.precede(p);
p.bump(range_op);
// `0 .. =>` or `let 0 .. =` or `Some(0 .. )`
// ^ ^ ^
if p.at(T![=]) | p.at(T![')']) | p.at(T![,]) {
// testing if we're at one of the following positions:
// `0 .. =>`
// ^
// `let 0 .. =`
// ^
// `let 0..: _ =`
// ^
// (1.., _)
// ^
// `Some(0 .. )`
// ^
// `S { t: 0.. }`
// ^
// `[0..]`
// ^
if matches!(p.current(), T![=] | T![,] | T![:] | T![')'] | T!['}'] | T![']']) {
// test half_open_range_pat
// fn f() { let 0 .. = 1u32; }
// fn f() {
// let 0 .. = 1u32;
// let 0..: _ = 1u32;
// }
} else {
atom_pat(p, recovery_set);
}

View File

@ -172,6 +172,122 @@ SOURCE_FILE
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
EXPR_STMT
MATCH_EXPR
MATCH_KW "match"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
MATCH_ARM_LIST
L_CURLY "{"
WHITESPACE "\n "
MATCH_ARM
RECORD_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_PAT_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_PAT_FIELD
NAME_REF
IDENT "a"
COLON ":"
WHITESPACE " "
LITERAL_PAT
LITERAL
INT_NUMBER "0"
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
RECORD_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_PAT_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_PAT_FIELD
NAME_REF
IDENT "a"
COLON ":"
WHITESPACE " "
RANGE_PAT
LITERAL_PAT
LITERAL
INT_NUMBER "1"
DOT2 ".."
WHITESPACE " "
R_CURLY "}"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
EXPR_STMT
MATCH_EXPR
MATCH_KW "match"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
MATCH_ARM_LIST
L_CURLY "{"
WHITESPACE "\n "
MATCH_ARM
SLICE_PAT
L_BRACK "["
LITERAL_PAT
LITERAL
INT_NUMBER "0"
R_BRACK "]"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
SLICE_PAT
L_BRACK "["
RANGE_PAT
LITERAL_PAT
LITERAL
INT_NUMBER "1"
DOT2 ".."
R_BRACK "]"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
TUPLE_EXPR
L_PAREN "("
R_PAREN ")"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE "\n\n "
MATCH_EXPR
MATCH_KW "match"
WHITESPACE " "

View File

@ -11,6 +11,16 @@ fn main() {
Some(1..) => ()
}
match () {
S { a: 0 } => (),
S { a: 1.. } => (),
}
match () {
[0] => (),
[1..] => (),
}
match (10 as u8, 5 as u8) {
(0, _) => (),
(1.., _) => ()

View File

@ -11,7 +11,7 @@ SOURCE_FILE
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
@ -27,6 +27,25 @@ SOURCE_FILE
LITERAL
INT_NUMBER "1u32"
SEMICOLON ";"
WHITESPACE " "
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
RANGE_PAT
LITERAL_PAT
LITERAL
INT_NUMBER "0"
DOT2 ".."
COLON ":"
WHITESPACE " "
INFER_TYPE
UNDERSCORE "_"
WHITESPACE " "
EQ "="
WHITESPACE " "
LITERAL
INT_NUMBER "1u32"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"

View File

@ -1 +1,4 @@
fn f() { let 0 .. = 1u32; }
fn f() {
let 0 .. = 1u32;
let 0..: _ = 1u32;
}