From 3c2dea7f55830ba2391e5eb8525cdbb43ac666c2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 19:33:02 +0300 Subject: [PATCH] always wrap block into an expression --- crates/ra_parser/src/grammar.rs | 2 +- crates/ra_parser/src/grammar/expressions.rs | 5 +++++ .../ra_parser/src/grammar/expressions/atom.rs | 4 ++-- crates/ra_syntax/src/validation.rs | 2 +- crates/ra_syntax/src/validation/block.rs | 20 +++++++++---------- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index d0f0dd4ac5..be9419e0c8 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -144,7 +144,7 @@ pub(crate) fn reparser( parent: Option, ) -> Option { let res = match node { - BLOCK => expressions::block, + BLOCK => expressions::naked_block, RECORD_FIELD_DEF_LIST => items::record_field_def_list, RECORD_FIELD_LIST => items::record_field_list, ENUM_VARIANT_LIST => items::enum_variant_list, diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 783d6a6f08..ba8386d11f 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -40,6 +40,11 @@ pub(crate) fn block(p: &mut Parser) { p.error("expected a block"); return; } + atom::block_expr(p, None); +} + +pub(crate) fn naked_block(p: &mut Parser) { + assert!(p.at(T!['{'])); let m = p.start(); p.bump(); expr_block_contents(p); diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index bc942ae010..ec7f2441df 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -463,10 +463,10 @@ fn match_guard(p: &mut Parser) -> CompletedMarker { // unsafe {}; // 'label: {}; // } -fn block_expr(p: &mut Parser, m: Option) -> CompletedMarker { +pub(super) fn block_expr(p: &mut Parser, m: Option) -> CompletedMarker { assert!(p.at(T!['{'])); let m = m.unwrap_or_else(|| p.start()); - block(p); + naked_block(p); m.complete(p, BLOCK_EXPR) } diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index ee87974109..16824f3c40 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -97,7 +97,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec { for node in root.descendants() { let _ = visitor_ctx(&mut errors) .visit::(validate_literal) - .visit::(block::validate_block_node) + .visit::(block::validate_block_expr) .visit::(|it, errors| validate_numeric_name(it.name_ref(), errors)) .visit::(|it, errors| validate_numeric_name(it.name_ref(), errors)) .accept(&node); diff --git a/crates/ra_syntax/src/validation/block.rs b/crates/ra_syntax/src/validation/block.rs index c5588658d8..3c9e96eb30 100644 --- a/crates/ra_syntax/src/validation/block.rs +++ b/crates/ra_syntax/src/validation/block.rs @@ -5,18 +5,18 @@ use crate::{ SyntaxKind::*, }; -pub(crate) fn validate_block_node(node: ast::Block, errors: &mut Vec) { - if let Some(parent) = node.syntax().parent() { +pub(crate) fn validate_block_expr(expr: ast::BlockExpr, errors: &mut Vec) { + if let Some(parent) = expr.syntax().parent() { match parent.kind() { - FN_DEF => return, - BLOCK_EXPR => match parent.parent().map(|v| v.kind()) { - Some(EXPR_STMT) | Some(BLOCK) => return, - _ => {} - }, + FN_DEF | EXPR_STMT | BLOCK => return, _ => {} } } - errors.extend( - node.attrs().map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().text_range())), - ) + if let Some(block) = expr.block() { + errors.extend( + block + .attrs() + .map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().text_range())), + ) + } }