Skip to content

Commit 39a1bb9

Browse files
committed
update blocklike
1 parent 337f2c9 commit 39a1bb9

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed

src/tools/rust-analyzer/crates/parser/src/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
/// `Parser` produces a flat list of `Event`s.
1313
/// They are converted to a tree-structure in
1414
/// a separate pass, via `TreeBuilder`.
15-
#[derive(Debug)]
15+
#[derive(Debug, PartialEq)]
1616
pub(crate) enum Event {
1717
/// This event signifies the start of the node.
1818
/// It should be either abandoned (in which case the

src/tools/rust-analyzer/crates/parser/src/grammar.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ impl BlockLike {
204204
self == BlockLike::Block
205205
}
206206

207-
fn is_blocklike(kind: SyntaxKind) -> bool {
208-
matches!(kind, BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
207+
fn is_blocklike(expr: &CompletedMarker, p: &Parser<'_>) -> bool {
208+
matches!(expr.kind(), BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
209+
|| (expr.last_token(p) == Some(T!['}']) && !matches!(expr.kind(), CLOSURE_EXPR))
209210
}
210211
}
211212

src/tools/rust-analyzer/crates/parser/src/grammar/expressions.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,10 @@ pub(super) fn let_stmt(p: &mut Parser<'_>, with_semi: Semicolon) {
134134
// test_err let_else_right_curly_brace
135135
// fn func() { let Some(_) = {Some(1)} else { panic!("h") };}
136136
if let Some(expr) = expr_after_eq {
137-
if let Some(token) = expr.last_token(p) {
138-
if token == T!['}'] {
139-
p.error(
140-
"right curly brace `}` before `else` in a `let...else` statement not allowed"
141-
)
142-
}
137+
if BlockLike::is_blocklike(&expr, p) {
138+
p.error(
139+
"right curly brace `}` before `else` in a `let...else` statement not allowed",
140+
)
143141
}
144142
}
145143

src/tools/rust-analyzer/crates/parser/src/grammar/expressions/atom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub(super) fn atom_expr(
198198
}
199199
};
200200
let blocklike =
201-
if BlockLike::is_blocklike(done.kind()) { BlockLike::Block } else { BlockLike::NotBlock };
201+
if BlockLike::is_blocklike(&done, p) { BlockLike::Block } else { BlockLike::NotBlock };
202202
Some((done, blocklike))
203203
}
204204

src/tools/rust-analyzer/crates/parser/src/parser.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,7 @@ impl CompletedMarker {
391391

392392
pub(crate) fn last_token(&self, p: &Parser<'_>) -> Option<SyntaxKind> {
393393
let end_pos = self.end_pos as usize;
394-
if end_pos > p.events.len() {
395-
return None;
396-
}
394+
debug_assert_eq!(p.events[end_pos - 1], Event::Finish);
397395
p.events[..end_pos].iter().rev().find_map(|event| match event {
398396
Event::Token { kind, .. } => Some(*kind),
399397
_ => None,

0 commit comments

Comments
 (0)