From 3c2dea7f55830ba2391e5eb8525cdbb43ac666c2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 19:33:02 +0300 Subject: always wrap block into an expression --- crates/ra_syntax/src/validation.rs | 2 +- crates/ra_syntax/src/validation/block.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index ee8797410..16824f3c4 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 c5588658d..3c9e96eb3 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())), + ) + } } -- cgit v1.2.3 From dcf8e895038a7677711b8168ee12e1d47f6018bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 19:42:14 +0300 Subject: fix generated AST --- crates/ra_syntax/src/ast/generated.rs | 2 +- crates/ra_syntax/src/grammar.ron | 2 +- crates/ra_syntax/src/lib.rs | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 7f91417c5..fd85a3231 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1003,7 +1003,7 @@ impl FnDef { pub fn param_list(&self) -> Option { AstChildren::new(&self.syntax).next() } - pub fn body(&self) -> Option { + pub fn body(&self) -> Option { AstChildren::new(&self.syntax).next() } pub fn ret_type(&self) -> Option { diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 9f17a10ed..37166182f 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -275,7 +275,7 @@ Grammar( "AttrsOwner", "DocCommentsOwner" ], - options: [ "ParamList", ["body", "Block"], "RetType" ], + options: [ "ParamList", ["body", "BlockExpr"], "RetType" ], ), "RetType": (options: ["TypeRef"]), "StructDef": ( diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 2bced1867..edb6076bb 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -203,7 +203,8 @@ fn api_walkthrough() { assert_eq!(name.text(), "foo"); // Let's get the `1 + 1` expression! - let block: ast::Block = func.body().unwrap(); + let body: ast::BlockExpr = func.body().unwrap(); + let block = body.block().unwrap(); let expr: ast::Expr = block.expr().unwrap(); // Enums are used to group related ast nodes together, and can be used for -- cgit v1.2.3 From 5e3f291195b580580be7ce5622f54ebca75fb9f0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 21:23:19 +0300 Subject: fix hir for new block syntax --- crates/ra_syntax/src/ast/expr_extensions.rs | 6 +++--- crates/ra_syntax/src/ast/generated.rs | 2 +- crates/ra_syntax/src/ast/traits.rs | 2 +- crates/ra_syntax/src/grammar.ron | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index d7ea4354d..1324965cf 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -9,12 +9,12 @@ use crate::{ #[derive(Debug, Clone, PartialEq, Eq)] pub enum ElseBranch { - Block(ast::Block), + Block(ast::BlockExpr), IfExpr(ast::IfExpr), } impl ast::IfExpr { - pub fn then_branch(&self) -> Option { + pub fn then_branch(&self) -> Option { self.blocks().nth(0) } pub fn else_branch(&self) -> Option { @@ -28,7 +28,7 @@ impl ast::IfExpr { Some(res) } - fn blocks(&self) -> AstChildren { + fn blocks(&self) -> AstChildren { children(self) } } diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index fd85a3231..e2a92ae60 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -3135,7 +3135,7 @@ impl AstNode for TryBlockExpr { } } impl TryBlockExpr { - pub fn block(&self) -> Option { + pub fn body(&self) -> Option { AstChildren::new(&self.syntax).next() } } diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index 20c251fba..c3e676d4c 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -28,7 +28,7 @@ pub trait VisibilityOwner: AstNode { } pub trait LoopBodyOwner: AstNode { - fn loop_body(&self) -> Option { + fn loop_body(&self) -> Option { child_opt(self) } } diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 37166182f..c14ee0e85 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -426,7 +426,7 @@ Grammar( traits: ["LoopBodyOwner"], ), "TryBlockExpr": ( - options: ["Block"], + options: [["body", "BlockExpr"]], ), "ForExpr": ( traits: ["LoopBodyOwner"], -- cgit v1.2.3 From e94587e3153b52684fd3f6b82c8e7efc09ff5c8d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 21:41:50 +0300 Subject: fix assists --- crates/ra_syntax/src/ast/expr_extensions.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 1324965cf..25dbd0bed 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -289,6 +289,26 @@ impl ast::Literal { } } +impl ast::BlockExpr { + /// false if the block is an intrinsic part of the syntax and can't be + /// replaced with arbitrary expression. + /// + /// ```not_rust + /// fn foo() { not_stand_alone } + /// const FOO: () = { stand_alone }; + /// ``` + pub fn is_standalone(&self) -> bool { + let kind = match self.syntax().parent() { + None => return true, + Some(it) => it.kind(), + }; + match kind { + FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, + _ => true, + } + } +} + #[test] fn test_literal_with_attr() { let parse = ast::SourceFile::parse(r#"const _: &str = { #[attr] "Hello" };"#); -- cgit v1.2.3