diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/expr_extensions.rs | 33 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 4 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated/nodes.rs | 81 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 8 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 7 | ||||
-rw-r--r-- | crates/ra_syntax/src/validation/block.rs | 20 |
8 files changed, 84 insertions, 75 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 521ca8ab8..1876afe95 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -16,9 +16,7 @@ use crate::{ | |||
16 | }; | 16 | }; |
17 | 17 | ||
18 | pub use self::{ | 18 | pub use self::{ |
19 | expr_extensions::{ | 19 | expr_extensions::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp}, |
20 | ArrayExprKind, BinOp, BlockModifier, ElseBranch, LiteralKind, PrefixOp, RangeOp, | ||
21 | }, | ||
22 | extensions::{ | 20 | extensions::{ |
23 | AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, | 21 | AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, |
24 | StructKind, TypeBoundKind, VisibilityKind, | 22 | StructKind, TypeBoundKind, VisibilityKind, |
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 26e4576ff..c507dc683 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -28,7 +28,7 @@ impl ast::BinExpr { | |||
28 | 28 | ||
29 | impl ast::FnDef { | 29 | impl ast::FnDef { |
30 | #[must_use] | 30 | #[must_use] |
31 | pub fn with_body(&self, body: ast::Block) -> ast::FnDef { | 31 | pub fn with_body(&self, body: ast::BlockExpr) -> ast::FnDef { |
32 | let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); | 32 | let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); |
33 | let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { | 33 | let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { |
34 | old_body.syntax().clone().into() | 34 | old_body.syntax().clone().into() |
diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 352c0d2c5..7771d6759 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs | |||
@@ -16,7 +16,7 @@ impl ast::Expr { | |||
16 | | ast::Expr::WhileExpr(_) | 16 | | ast::Expr::WhileExpr(_) |
17 | | ast::Expr::BlockExpr(_) | 17 | | ast::Expr::BlockExpr(_) |
18 | | ast::Expr::MatchExpr(_) | 18 | | ast::Expr::MatchExpr(_) |
19 | | ast::Expr::TryExpr(_) => true, | 19 | | ast::Expr::EffectExpr(_) => true, |
20 | _ => false, | 20 | _ => false, |
21 | } | 21 | } |
22 | } | 22 | } |
@@ -43,7 +43,7 @@ impl ast::IfExpr { | |||
43 | Some(res) | 43 | Some(res) |
44 | } | 44 | } |
45 | 45 | ||
46 | fn blocks(&self) -> AstChildren<ast::BlockExpr> { | 46 | pub fn blocks(&self) -> AstChildren<ast::BlockExpr> { |
47 | support::children(self.syntax()) | 47 | support::children(self.syntax()) |
48 | } | 48 | } |
49 | } | 49 | } |
@@ -359,22 +359,34 @@ impl ast::Literal { | |||
359 | } | 359 | } |
360 | } | 360 | } |
361 | 361 | ||
362 | pub enum BlockModifier { | 362 | #[derive(Debug, Clone, PartialEq, Eq)] |
363 | pub enum Effect { | ||
363 | Async(SyntaxToken), | 364 | Async(SyntaxToken), |
364 | Unsafe(SyntaxToken), | 365 | Unsafe(SyntaxToken), |
366 | Try(SyntaxToken), | ||
367 | // Very much not an effect, but we stuff it into this node anyway | ||
368 | Label(ast::Label), | ||
365 | } | 369 | } |
366 | 370 | ||
367 | impl ast::BlockExpr { | 371 | impl ast::EffectExpr { |
368 | pub fn modifier(&self) -> Option<BlockModifier> { | 372 | pub fn effect(&self) -> Effect { |
369 | if let Some(token) = self.async_token() { | 373 | if let Some(token) = self.async_token() { |
370 | return Some(BlockModifier::Async(token)); | 374 | return Effect::Async(token); |
371 | } | 375 | } |
372 | if let Some(token) = self.unsafe_token() { | 376 | if let Some(token) = self.unsafe_token() { |
373 | return Some(BlockModifier::Unsafe(token)); | 377 | return Effect::Unsafe(token); |
378 | } | ||
379 | if let Some(token) = self.try_token() { | ||
380 | return Effect::Try(token); | ||
381 | } | ||
382 | if let Some(label) = self.label() { | ||
383 | return Effect::Label(label); | ||
374 | } | 384 | } |
375 | None | 385 | unreachable!("ast::EffectExpr without Effect") |
376 | } | 386 | } |
387 | } | ||
377 | 388 | ||
389 | impl ast::BlockExpr { | ||
378 | /// false if the block is an intrinsic part of the syntax and can't be | 390 | /// false if the block is an intrinsic part of the syntax and can't be |
379 | /// replaced with arbitrary expression. | 391 | /// replaced with arbitrary expression. |
380 | /// | 392 | /// |
@@ -383,15 +395,12 @@ impl ast::BlockExpr { | |||
383 | /// const FOO: () = { stand_alone }; | 395 | /// const FOO: () = { stand_alone }; |
384 | /// ``` | 396 | /// ``` |
385 | pub fn is_standalone(&self) -> bool { | 397 | pub fn is_standalone(&self) -> bool { |
386 | if self.modifier().is_some() { | ||
387 | return false; | ||
388 | } | ||
389 | let parent = match self.syntax().parent() { | 398 | let parent = match self.syntax().parent() { |
390 | Some(it) => it, | 399 | Some(it) => it, |
391 | None => return true, | 400 | None => return true, |
392 | }; | 401 | }; |
393 | match parent.kind() { | 402 | match parent.kind() { |
394 | FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR => false, | 403 | FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | EFFECT_EXPR => false, |
395 | _ => true, | 404 | _ => true, |
396 | } | 405 | } |
397 | } | 406 | } |
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 45e3dd2d3..528c873e0 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs | |||
@@ -423,6 +423,10 @@ impl ast::MacroCall { | |||
423 | None | 423 | None |
424 | } | 424 | } |
425 | } | 425 | } |
426 | |||
427 | pub fn is_bang(&self) -> bool { | ||
428 | self.is_macro_rules().is_none() | ||
429 | } | ||
426 | } | 430 | } |
427 | 431 | ||
428 | impl ast::LifetimeParam { | 432 | impl ast::LifetimeParam { |
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 3f16592b6..5e844d5ae 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs | |||
@@ -476,6 +476,19 @@ impl LoopExpr { | |||
476 | } | 476 | } |
477 | 477 | ||
478 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 478 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
479 | pub struct EffectExpr { | ||
480 | pub(crate) syntax: SyntaxNode, | ||
481 | } | ||
482 | impl ast::AttrsOwner for EffectExpr {} | ||
483 | impl EffectExpr { | ||
484 | pub fn label(&self) -> Option<Label> { support::child(&self.syntax) } | ||
485 | pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) } | ||
486 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | ||
487 | pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } | ||
488 | pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) } | ||
489 | } | ||
490 | |||
491 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
479 | pub struct ForExpr { | 492 | pub struct ForExpr { |
480 | pub(crate) syntax: SyntaxNode, | 493 | pub(crate) syntax: SyntaxNode, |
481 | } | 494 | } |
@@ -541,11 +554,12 @@ pub struct BlockExpr { | |||
541 | pub(crate) syntax: SyntaxNode, | 554 | pub(crate) syntax: SyntaxNode, |
542 | } | 555 | } |
543 | impl ast::AttrsOwner for BlockExpr {} | 556 | impl ast::AttrsOwner for BlockExpr {} |
557 | impl ast::ModuleItemOwner for BlockExpr {} | ||
544 | impl BlockExpr { | 558 | impl BlockExpr { |
545 | pub fn label(&self) -> Option<Label> { support::child(&self.syntax) } | 559 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } |
546 | pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } | 560 | pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } |
547 | pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } | 561 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } |
548 | pub fn block(&self) -> Option<Block> { support::child(&self.syntax) } | 562 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } |
549 | } | 563 | } |
550 | 564 | ||
551 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 565 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -617,8 +631,8 @@ pub struct TryExpr { | |||
617 | } | 631 | } |
618 | impl ast::AttrsOwner for TryExpr {} | 632 | impl ast::AttrsOwner for TryExpr {} |
619 | impl TryExpr { | 633 | impl TryExpr { |
620 | pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) } | ||
621 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | 634 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } |
635 | pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) } | ||
622 | } | 636 | } |
623 | 637 | ||
624 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 638 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -1112,19 +1126,6 @@ impl Condition { | |||
1112 | } | 1126 | } |
1113 | 1127 | ||
1114 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 1128 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
1115 | pub struct Block { | ||
1116 | pub(crate) syntax: SyntaxNode, | ||
1117 | } | ||
1118 | impl ast::AttrsOwner for Block {} | ||
1119 | impl ast::ModuleItemOwner for Block {} | ||
1120 | impl Block { | ||
1121 | pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) } | ||
1122 | pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) } | ||
1123 | pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } | ||
1124 | pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) } | ||
1125 | } | ||
1126 | |||
1127 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1128 | pub struct ParamList { | 1129 | pub struct ParamList { |
1129 | pub(crate) syntax: SyntaxNode, | 1130 | pub(crate) syntax: SyntaxNode, |
1130 | } | 1131 | } |
@@ -1467,6 +1468,7 @@ pub enum Expr { | |||
1467 | FieldExpr(FieldExpr), | 1468 | FieldExpr(FieldExpr), |
1468 | AwaitExpr(AwaitExpr), | 1469 | AwaitExpr(AwaitExpr), |
1469 | TryExpr(TryExpr), | 1470 | TryExpr(TryExpr), |
1471 | EffectExpr(EffectExpr), | ||
1470 | CastExpr(CastExpr), | 1472 | CastExpr(CastExpr), |
1471 | RefExpr(RefExpr), | 1473 | RefExpr(RefExpr), |
1472 | PrefixExpr(PrefixExpr), | 1474 | PrefixExpr(PrefixExpr), |
@@ -1949,6 +1951,17 @@ impl AstNode for LoopExpr { | |||
1949 | } | 1951 | } |
1950 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 1952 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
1951 | } | 1953 | } |
1954 | impl AstNode for EffectExpr { | ||
1955 | fn can_cast(kind: SyntaxKind) -> bool { kind == EFFECT_EXPR } | ||
1956 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
1957 | if Self::can_cast(syntax.kind()) { | ||
1958 | Some(Self { syntax }) | ||
1959 | } else { | ||
1960 | None | ||
1961 | } | ||
1962 | } | ||
1963 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1964 | } | ||
1952 | impl AstNode for ForExpr { | 1965 | impl AstNode for ForExpr { |
1953 | fn can_cast(kind: SyntaxKind) -> bool { kind == FOR_EXPR } | 1966 | fn can_cast(kind: SyntaxKind) -> bool { kind == FOR_EXPR } |
1954 | fn cast(syntax: SyntaxNode) -> Option<Self> { | 1967 | fn cast(syntax: SyntaxNode) -> Option<Self> { |
@@ -2631,17 +2644,6 @@ impl AstNode for Condition { | |||
2631 | } | 2644 | } |
2632 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 2645 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
2633 | } | 2646 | } |
2634 | impl AstNode for Block { | ||
2635 | fn can_cast(kind: SyntaxKind) -> bool { kind == BLOCK } | ||
2636 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
2637 | if Self::can_cast(syntax.kind()) { | ||
2638 | Some(Self { syntax }) | ||
2639 | } else { | ||
2640 | None | ||
2641 | } | ||
2642 | } | ||
2643 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
2644 | } | ||
2645 | impl AstNode for ParamList { | 2647 | impl AstNode for ParamList { |
2646 | fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM_LIST } | 2648 | fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM_LIST } |
2647 | fn cast(syntax: SyntaxNode) -> Option<Self> { | 2649 | fn cast(syntax: SyntaxNode) -> Option<Self> { |
@@ -3290,6 +3292,9 @@ impl From<AwaitExpr> for Expr { | |||
3290 | impl From<TryExpr> for Expr { | 3292 | impl From<TryExpr> for Expr { |
3291 | fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) } | 3293 | fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) } |
3292 | } | 3294 | } |
3295 | impl From<EffectExpr> for Expr { | ||
3296 | fn from(node: EffectExpr) -> Expr { Expr::EffectExpr(node) } | ||
3297 | } | ||
3293 | impl From<CastExpr> for Expr { | 3298 | impl From<CastExpr> for Expr { |
3294 | fn from(node: CastExpr) -> Expr { Expr::CastExpr(node) } | 3299 | fn from(node: CastExpr) -> Expr { Expr::CastExpr(node) } |
3295 | } | 3300 | } |
@@ -3320,8 +3325,10 @@ impl AstNode for Expr { | |||
3320 | TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR | 3325 | TUPLE_EXPR | ARRAY_EXPR | PAREN_EXPR | PATH_EXPR | LAMBDA_EXPR | IF_EXPR |
3321 | | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL | 3326 | | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL |
3322 | | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | RECORD_LIT | CALL_EXPR | INDEX_EXPR | 3327 | | BLOCK_EXPR | RETURN_EXPR | MATCH_EXPR | RECORD_LIT | CALL_EXPR | INDEX_EXPR |
3323 | | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | CAST_EXPR | REF_EXPR | 3328 | | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | EFFECT_EXPR | CAST_EXPR |
3324 | | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL | BOX_EXPR => true, | 3329 | | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL | BOX_EXPR => { |
3330 | true | ||
3331 | } | ||
3325 | _ => false, | 3332 | _ => false, |
3326 | } | 3333 | } |
3327 | } | 3334 | } |
@@ -3349,6 +3356,7 @@ impl AstNode for Expr { | |||
3349 | FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }), | 3356 | FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }), |
3350 | AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }), | 3357 | AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }), |
3351 | TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), | 3358 | TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), |
3359 | EFFECT_EXPR => Expr::EffectExpr(EffectExpr { syntax }), | ||
3352 | CAST_EXPR => Expr::CastExpr(CastExpr { syntax }), | 3360 | CAST_EXPR => Expr::CastExpr(CastExpr { syntax }), |
3353 | REF_EXPR => Expr::RefExpr(RefExpr { syntax }), | 3361 | REF_EXPR => Expr::RefExpr(RefExpr { syntax }), |
3354 | PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }), | 3362 | PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }), |
@@ -3385,6 +3393,7 @@ impl AstNode for Expr { | |||
3385 | Expr::FieldExpr(it) => &it.syntax, | 3393 | Expr::FieldExpr(it) => &it.syntax, |
3386 | Expr::AwaitExpr(it) => &it.syntax, | 3394 | Expr::AwaitExpr(it) => &it.syntax, |
3387 | Expr::TryExpr(it) => &it.syntax, | 3395 | Expr::TryExpr(it) => &it.syntax, |
3396 | Expr::EffectExpr(it) => &it.syntax, | ||
3388 | Expr::CastExpr(it) => &it.syntax, | 3397 | Expr::CastExpr(it) => &it.syntax, |
3389 | Expr::RefExpr(it) => &it.syntax, | 3398 | Expr::RefExpr(it) => &it.syntax, |
3390 | Expr::PrefixExpr(it) => &it.syntax, | 3399 | Expr::PrefixExpr(it) => &it.syntax, |
@@ -3865,6 +3874,11 @@ impl std::fmt::Display for LoopExpr { | |||
3865 | std::fmt::Display::fmt(self.syntax(), f) | 3874 | std::fmt::Display::fmt(self.syntax(), f) |
3866 | } | 3875 | } |
3867 | } | 3876 | } |
3877 | impl std::fmt::Display for EffectExpr { | ||
3878 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
3879 | std::fmt::Display::fmt(self.syntax(), f) | ||
3880 | } | ||
3881 | } | ||
3868 | impl std::fmt::Display for ForExpr { | 3882 | impl std::fmt::Display for ForExpr { |
3869 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | 3883 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
3870 | std::fmt::Display::fmt(self.syntax(), f) | 3884 | std::fmt::Display::fmt(self.syntax(), f) |
@@ -4175,11 +4189,6 @@ impl std::fmt::Display for Condition { | |||
4175 | std::fmt::Display::fmt(self.syntax(), f) | 4189 | std::fmt::Display::fmt(self.syntax(), f) |
4176 | } | 4190 | } |
4177 | } | 4191 | } |
4178 | impl std::fmt::Display for Block { | ||
4179 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
4180 | std::fmt::Display::fmt(self.syntax(), f) | ||
4181 | } | ||
4182 | } | ||
4183 | impl std::fmt::Display for ParamList { | 4192 | impl std::fmt::Display for ParamList { |
4184 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | 4193 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
4185 | std::fmt::Display::fmt(self.syntax(), f) | 4194 | std::fmt::Display::fmt(self.syntax(), f) |
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 492088353..7b17fef49 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -82,14 +82,6 @@ pub fn block_expr( | |||
82 | ast_from_text(&format!("fn f() {}", buf)) | 82 | ast_from_text(&format!("fn f() {}", buf)) |
83 | } | 83 | } |
84 | 84 | ||
85 | pub fn block_from_expr(e: ast::Expr) -> ast::Block { | ||
86 | return from_text(&format!("{{ {} }}", e)); | ||
87 | |||
88 | fn from_text(text: &str) -> ast::Block { | ||
89 | ast_from_text(&format!("fn f() {}", text)) | ||
90 | } | ||
91 | } | ||
92 | |||
93 | pub fn expr_unit() -> ast::Expr { | 85 | pub fn expr_unit() -> ast::Expr { |
94 | expr_from_text("()") | 86 | expr_from_text("()") |
95 | } | 87 | } |
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index ceeb2bde9..d0234cada 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -237,8 +237,7 @@ fn api_walkthrough() { | |||
237 | 237 | ||
238 | // Let's get the `1 + 1` expression! | 238 | // Let's get the `1 + 1` expression! |
239 | let body: ast::BlockExpr = func.body().unwrap(); | 239 | let body: ast::BlockExpr = func.body().unwrap(); |
240 | let block = body.block().unwrap(); | 240 | let expr: ast::Expr = body.expr().unwrap(); |
241 | let expr: ast::Expr = block.expr().unwrap(); | ||
242 | 241 | ||
243 | // Enums are used to group related ast nodes together, and can be used for | 242 | // Enums are used to group related ast nodes together, and can be used for |
244 | // matching. However, because there are no public fields, it's possible to | 243 | // matching. However, because there are no public fields, it's possible to |
@@ -274,8 +273,8 @@ fn api_walkthrough() { | |||
274 | assert_eq!(text.to_string(), "1 + 1"); | 273 | assert_eq!(text.to_string(), "1 + 1"); |
275 | 274 | ||
276 | // There's a bunch of traversal methods on `SyntaxNode`: | 275 | // There's a bunch of traversal methods on `SyntaxNode`: |
277 | assert_eq!(expr_syntax.parent().as_ref(), Some(block.syntax())); | 276 | assert_eq!(expr_syntax.parent().as_ref(), Some(body.syntax())); |
278 | assert_eq!(block.syntax().first_child_or_token().map(|it| it.kind()), Some(T!['{'])); | 277 | assert_eq!(body.syntax().first_child_or_token().map(|it| it.kind()), Some(T!['{'])); |
279 | assert_eq!( | 278 | assert_eq!( |
280 | expr_syntax.next_sibling_or_token().map(|it| it.kind()), | 279 | expr_syntax.next_sibling_or_token().map(|it| it.kind()), |
281 | Some(SyntaxKind::WHITESPACE) | 280 | Some(SyntaxKind::WHITESPACE) |
diff --git a/crates/ra_syntax/src/validation/block.rs b/crates/ra_syntax/src/validation/block.rs index 8e962ab5b..2c08f7e6e 100644 --- a/crates/ra_syntax/src/validation/block.rs +++ b/crates/ra_syntax/src/validation/block.rs | |||
@@ -6,19 +6,17 @@ use crate::{ | |||
6 | SyntaxKind::*, | 6 | SyntaxKind::*, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | pub(crate) fn validate_block_expr(expr: ast::BlockExpr, errors: &mut Vec<SyntaxError>) { | 9 | pub(crate) fn validate_block_expr(block: ast::BlockExpr, errors: &mut Vec<SyntaxError>) { |
10 | if let Some(parent) = expr.syntax().parent() { | 10 | if let Some(parent) = block.syntax().parent() { |
11 | match parent.kind() { | 11 | match parent.kind() { |
12 | FN_DEF | EXPR_STMT | BLOCK => return, | 12 | FN_DEF | EXPR_STMT | BLOCK_EXPR => return, |
13 | _ => {} | 13 | _ => {} |
14 | } | 14 | } |
15 | } | 15 | } |
16 | if let Some(block) = expr.block() { | 16 | errors.extend(block.attrs().map(|attr| { |
17 | errors.extend(block.attrs().map(|attr| { | 17 | SyntaxError::new( |
18 | SyntaxError::new( | 18 | "A block in this position cannot accept inner attributes", |
19 | "A block in this position cannot accept inner attributes", | 19 | attr.syntax().text_range(), |
20 | attr.syntax().text_range(), | 20 | ) |
21 | ) | 21 | })) |
22 | })) | ||
23 | } | ||
24 | } | 22 | } |