Skip to content

Commit

Permalink
Auto merge of rust-lang#14248 - XFFXFF:let_else_right_curly_brace, r=…
Browse files Browse the repository at this point in the history
…Veykril

fix: show diagnostic for } token followed by else in let else statement

fix rust-lang#14221

My thinking is to check if the `expr` after `=` is block like when parse `let ... lese` , and if so, emit error.
  • Loading branch information
bors committed Mar 8, 2023
2 parents ecc32c2 + 6e97527 commit a9d97b6
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 12 deletions.
4 changes: 4 additions & 0 deletions crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ impl BlockLike {
fn is_block(self) -> bool {
self == BlockLike::Block
}

fn is_blocklike(kind: SyntaxKind) -> bool {
matches!(kind, BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
}
}

const VISIBILITY_FIRST: TokenSet = TokenSet::new(&[T![pub], T![crate]]);
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/grammar/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(super) fn meta(p: &mut Parser<'_>) {
match p.current() {
T![=] => {
p.bump(T![=]);
if !expressions::expr(p) {
if expressions::expr(p).is_none() {
p.error("expected expression");
}
}
Expand Down
28 changes: 23 additions & 5 deletions crates/parser/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub(super) enum Semicolon {

const EXPR_FIRST: TokenSet = LHS_FIRST;

pub(super) fn expr(p: &mut Parser<'_>) -> bool {
pub(super) fn expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
expr_bp(p, None, r, 1).is_some()
expr_bp(p, None, r, 1).map(|(m, _)| m)
}

pub(super) fn expr_stmt(
Expand Down Expand Up @@ -120,16 +120,27 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
// fn f() { let x: i32; }
types::ascription(p);
}

let mut expr_after_eq: Option<CompletedMarker> = None;
if p.eat(T![=]) {
// test let_stmt_init
// fn f() { let x = 92; }
expressions::expr(p);
expr_after_eq = expressions::expr(p);
}

if p.at(T![else]) {
// test_err let_else_right_curly_brace
// fn func() { let Some(_) = {Some(1)} else { panic!("h") };}
if let Some(expr) = expr_after_eq {
if BlockLike::is_blocklike(expr.kind()) {
p.error(
"right curly brace `}` before `else` in a `let...else` statement not allowed",
)
}
}

// test let_else
// fn f() { let Some(x) = opt else { return }; }

let m = p.start();
p.bump(T![else]);
block_expr(p);
Expand Down Expand Up @@ -578,7 +589,14 @@ fn arg_list(p: &mut Parser<'_>) {
// fn main() {
// foo(#[attr] 92)
// }
delimited(p, T!['('], T![')'], T![,], EXPR_FIRST.union(ATTRIBUTE_FIRST), expr);
delimited(
p,
T!['('],
T![')'],
T![,],
EXPR_FIRST.union(ATTRIBUTE_FIRST),
|p: &mut Parser<'_>| expr(p).is_some(),
);
m.complete(p, ARG_LIST);
}

Expand Down
10 changes: 4 additions & 6 deletions crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ pub(super) fn atom_expr(
return None;
}
};
let blocklike = match done.kind() {
IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block,
_ => BlockLike::NotBlock,
};
let blocklike =
if BlockLike::is_blocklike(done.kind()) { BlockLike::Block } else { BlockLike::NotBlock };
Some((done, blocklike))
}

Expand All @@ -188,7 +186,7 @@ fn tuple_expr(p: &mut Parser<'_>) -> CompletedMarker {

// test tuple_attrs
// const A: (i64, i64) = (1, #[cfg(test)] 2);
if !expr(p) {
if expr(p).is_none() {
break;
}

Expand Down Expand Up @@ -221,7 +219,7 @@ fn array_expr(p: &mut Parser<'_>) -> CompletedMarker {

// test array_attrs
// const A: &[i64] = &[1, #[cfg(test)] 2];
if !expr(p) {
if expr(p).is_none() {
break;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
WILDCARD_PAT
UNDERSCORE "_"
WHITESPACE " "
EQ "="
WHITESPACE " "
FOR_EXPR
FOR_KW "for"
WHITESPACE " "
WILDCARD_PAT
UNDERSCORE "_"
WHITESPACE " "
IN_KW "in"
WHITESPACE " "
RANGE_EXPR
LITERAL
INT_NUMBER "0"
DOT2 ".."
LITERAL
INT_NUMBER "10"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE " "
LET_ELSE
ELSE_KW "else"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
RETURN_EXPR
RETURN_KW "return"
WHITESPACE "\n "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
error 43: right curly brace `}` before `else` in a `let...else` statement not allowed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn f() {
let _ = for _ in 0..10 {
} else {
return
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
WILDCARD_PAT
UNDERSCORE "_"
WHITESPACE " "
EQ "="
WHITESPACE " "
LOOP_EXPR
LOOP_KW "loop"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE " "
LET_ELSE
ELSE_KW "else"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
RETURN_EXPR
RETURN_KW "return"
WHITESPACE "\n "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
error 33: right curly brace `}` before `else` in a `let...else` statement not allowed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn f() {
let _ = loop {
} else {
return
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
WILDCARD_PAT
UNDERSCORE "_"
WHITESPACE " "
EQ "="
WHITESPACE " "
MATCH_EXPR
MATCH_KW "match"
WHITESPACE " "
CALL_EXPR
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "Some"
ARG_LIST
L_PAREN "("
LITERAL
INT_NUMBER "1"
R_PAREN ")"
WHITESPACE " "
MATCH_ARM_LIST
L_CURLY "{"
WHITESPACE "\n "
MATCH_ARM
TUPLE_STRUCT_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "Some"
L_PAREN "("
WILDCARD_PAT
UNDERSCORE "_"
R_PAREN ")"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
LITERAL
INT_NUMBER "1"
COMMA ","
WHITESPACE "\n "
MATCH_ARM
IDENT_PAT
NAME
IDENT "None"
WHITESPACE " "
FAT_ARROW "=>"
WHITESPACE " "
LITERAL
INT_NUMBER "2"
COMMA ","
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE " "
LET_ELSE
ELSE_KW "else"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
RETURN_EXPR
RETURN_KW "return"
WHITESPACE "\n "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
error 83: right curly brace `}` before `else` in a `let...else` statement not allowed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn f() {
let _ = match Some(1) {
Some(_) => 1,
None => 2,
} else {
return
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
WILDCARD_PAT
UNDERSCORE "_"
WHITESPACE " "
EQ "="
WHITESPACE " "
WHILE_EXPR
WHILE_KW "while"
WHITESPACE " "
LITERAL
TRUE_KW "true"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
R_CURLY "}"
WHITESPACE " "
LET_ELSE
ELSE_KW "else"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
RETURN_EXPR
RETURN_KW "return"
WHITESPACE "\n "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
error 39: right curly brace `}` before `else` in a `let...else` statement not allowed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn f() {
let _ = while true {
} else {
return
};
}
Loading

0 comments on commit a9d97b6

Please sign in to comment.