aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-05-02 19:27:02 +0100
committerBenjamin Coenen <[email protected]>2020-05-02 19:27:02 +0100
commit4613497a7714c6cd87166e6525d764d75f8acefd (patch)
tree2527ae2c0ef2ef100efee3fcb8899f8e34d0d573 /crates/ra_syntax/src
parent19e28888aa41b2845b47adb7314aed99d3c48679 (diff)
parent89e1f97515c36ab97bd378d972cabec0feb6d77e (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into fix_4202
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/ast.rs17
-rw-r--r--crates/ra_syntax/src/ast/edit.rs2
-rw-r--r--crates/ra_syntax/src/ast/expr_extensions.rs39
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs6
-rw-r--r--crates/ra_syntax/src/ast/generated/nodes.rs76
-rw-r--r--crates/ra_syntax/src/ast/make.rs11
-rw-r--r--crates/ra_syntax/src/ast/tokens.rs17
-rw-r--r--crates/ra_syntax/src/lib.rs7
-rw-r--r--crates/ra_syntax/src/parsing/lexer.rs37
-rw-r--r--crates/ra_syntax/src/validation.rs81
-rw-r--r--crates/ra_syntax/src/validation/block.rs20
11 files changed, 206 insertions, 107 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 7fca5661e..1876afe95 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -16,7 +16,7 @@ use crate::{
16}; 16};
17 17
18pub use self::{ 18pub use self::{
19 expr_extensions::{ArrayExprKind, BinOp, ElseBranch, LiteralKind, PrefixOp, RangeOp}, 19 expr_extensions::{ArrayExprKind, BinOp, Effect, ElseBranch, LiteralKind, PrefixOp, RangeOp},
20 extensions::{ 20 extensions::{
21 AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents, 21 AttrKind, FieldKind, NameOrNameRef, PathSegmentKind, SelfParamKind, SlicePatComponents,
22 StructKind, TypeBoundKind, VisibilityKind, 22 StructKind, TypeBoundKind, VisibilityKind,
@@ -243,6 +243,21 @@ fn test_comments_preserve_trailing_whitespace() {
243} 243}
244 244
245#[test] 245#[test]
246fn test_four_slash_line_comment() {
247 let file = SourceFile::parse(
248 r#"
249 //// too many slashes to be a doc comment
250 /// doc comment
251 mod foo {}
252 "#,
253 )
254 .ok()
255 .unwrap();
256 let module = file.syntax().descendants().find_map(Module::cast).unwrap();
257 assert_eq!("doc comment", module.doc_comment_text().unwrap());
258}
259
260#[test]
246fn test_where_predicates() { 261fn test_where_predicates() {
247 fn assert_bound(text: &str, bound: Option<TypeBound>) { 262 fn assert_bound(text: &str, bound: Option<TypeBound>) {
248 assert_eq!(text, bound.unwrap().syntax().text().to_string()); 263 assert_eq!(text, bound.unwrap().syntax().text().to_string());
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
29impl ast::FnDef { 29impl 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 93aa3d45f..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::TryBlockExpr(_) => 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,6 +359,33 @@ impl ast::Literal {
359 } 359 }
360} 360}
361 361
362#[derive(Debug, Clone, PartialEq, Eq)]
363pub enum Effect {
364 Async(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),
369}
370
371impl ast::EffectExpr {
372 pub fn effect(&self) -> Effect {
373 if let Some(token) = self.async_token() {
374 return Effect::Async(token);
375 }
376 if let Some(token) = self.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);
384 }
385 unreachable!("ast::EffectExpr without Effect")
386 }
387}
388
362impl ast::BlockExpr { 389impl ast::BlockExpr {
363 /// 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
364 /// replaced with arbitrary expression. 391 /// replaced with arbitrary expression.
@@ -368,12 +395,12 @@ impl ast::BlockExpr {
368 /// const FOO: () = { stand_alone }; 395 /// const FOO: () = { stand_alone };
369 /// ``` 396 /// ```
370 pub fn is_standalone(&self) -> bool { 397 pub fn is_standalone(&self) -> bool {
371 let kind = match self.syntax().parent() { 398 let parent = match self.syntax().parent() {
399 Some(it) => it,
372 None => return true, 400 None => return true,
373 Some(it) => it.kind(),
374 }; 401 };
375 match kind { 402 match parent.kind() {
376 FN_DEF | MATCH_ARM | IF_EXPR | WHILE_EXPR | LOOP_EXPR | TRY_BLOCK_EXPR => false, 403 FN_DEF | IF_EXPR | WHILE_EXPR | LOOP_EXPR | EFFECT_EXPR => false,
377 _ => true, 404 _ => true,
378 } 405 }
379 } 406 }
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs
index f2ea5088e..528c873e0 100644
--- a/crates/ra_syntax/src/ast/extensions.rs
+++ b/crates/ra_syntax/src/ast/extensions.rs
@@ -407,7 +407,7 @@ impl ast::Visibility {
407 } else if self.super_token().is_some() { 407 } else if self.super_token().is_some() {
408 VisibilityKind::PubSuper 408 VisibilityKind::PubSuper
409 } else if self.self_token().is_some() { 409 } else if self.self_token().is_some() {
410 VisibilityKind::PubSuper 410 VisibilityKind::PubSelf
411 } else { 411 } else {
412 VisibilityKind::Pub 412 VisibilityKind::Pub
413 } 413 }
@@ -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
428impl ast::LifetimeParam { 432impl ast::LifetimeParam {
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs
index 2cb3ad011..5e844d5ae 100644
--- a/crates/ra_syntax/src/ast/generated/nodes.rs
+++ b/crates/ra_syntax/src/ast/generated/nodes.rs
@@ -476,13 +476,16 @@ impl LoopExpr {
476} 476}
477 477
478#[derive(Debug, Clone, PartialEq, Eq, Hash)] 478#[derive(Debug, Clone, PartialEq, Eq, Hash)]
479pub struct TryBlockExpr { 479pub struct EffectExpr {
480 pub(crate) syntax: SyntaxNode, 480 pub(crate) syntax: SyntaxNode,
481} 481}
482impl ast::AttrsOwner for TryBlockExpr {} 482impl ast::AttrsOwner for EffectExpr {}
483impl TryBlockExpr { 483impl EffectExpr {
484 pub fn label(&self) -> Option<Label> { support::child(&self.syntax) }
484 pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) } 485 pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) }
485 pub fn body(&self) -> Option<BlockExpr> { support::child(&self.syntax) } 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) }
486} 489}
487 490
488#[derive(Debug, Clone, PartialEq, Eq, Hash)] 491#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -551,10 +554,12 @@ pub struct BlockExpr {
551 pub(crate) syntax: SyntaxNode, 554 pub(crate) syntax: SyntaxNode,
552} 555}
553impl ast::AttrsOwner for BlockExpr {} 556impl ast::AttrsOwner for BlockExpr {}
557impl ast::ModuleItemOwner for BlockExpr {}
554impl BlockExpr { 558impl BlockExpr {
555 pub fn label(&self) -> Option<Label> { support::child(&self.syntax) } 559 pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
556 pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } 560 pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) }
557 pub fn block(&self) -> Option<Block> { support::child(&self.syntax) } 561 pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
562 pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
558} 563}
559 564
560#[derive(Debug, Clone, PartialEq, Eq, Hash)] 565#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -626,8 +631,8 @@ pub struct TryExpr {
626} 631}
627impl ast::AttrsOwner for TryExpr {} 632impl ast::AttrsOwner for TryExpr {}
628impl TryExpr { 633impl TryExpr {
629 pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) }
630 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![?]) }
631} 636}
632 637
633#[derive(Debug, Clone, PartialEq, Eq, Hash)] 638#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -1121,19 +1126,6 @@ impl Condition {
1121} 1126}
1122 1127
1123#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1128#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1124pub struct Block {
1125 pub(crate) syntax: SyntaxNode,
1126}
1127impl ast::AttrsOwner for Block {}
1128impl ast::ModuleItemOwner for Block {}
1129impl Block {
1130 pub fn l_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['{']) }
1131 pub fn statements(&self) -> AstChildren<Stmt> { support::children(&self.syntax) }
1132 pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
1133 pub fn r_curly_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['}']) }
1134}
1135
1136#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1137pub struct ParamList { 1129pub struct ParamList {
1138 pub(crate) syntax: SyntaxNode, 1130 pub(crate) syntax: SyntaxNode,
1139} 1131}
@@ -1249,6 +1241,9 @@ pub struct PathSegment {
1249} 1241}
1250impl PathSegment { 1242impl PathSegment {
1251 pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) } 1243 pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) }
1244 pub fn crate_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![crate]) }
1245 pub fn self_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![self]) }
1246 pub fn super_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![super]) }
1252 pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) } 1247 pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
1253 pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) } 1248 pub fn name_ref(&self) -> Option<NameRef> { support::child(&self.syntax) }
1254 pub fn type_arg_list(&self) -> Option<TypeArgList> { support::child(&self.syntax) } 1249 pub fn type_arg_list(&self) -> Option<TypeArgList> { support::child(&self.syntax) }
@@ -1473,7 +1468,7 @@ pub enum Expr {
1473 FieldExpr(FieldExpr), 1468 FieldExpr(FieldExpr),
1474 AwaitExpr(AwaitExpr), 1469 AwaitExpr(AwaitExpr),
1475 TryExpr(TryExpr), 1470 TryExpr(TryExpr),
1476 TryBlockExpr(TryBlockExpr), 1471 EffectExpr(EffectExpr),
1477 CastExpr(CastExpr), 1472 CastExpr(CastExpr),
1478 RefExpr(RefExpr), 1473 RefExpr(RefExpr),
1479 PrefixExpr(PrefixExpr), 1474 PrefixExpr(PrefixExpr),
@@ -1956,8 +1951,8 @@ impl AstNode for LoopExpr {
1956 } 1951 }
1957 fn syntax(&self) -> &SyntaxNode { &self.syntax } 1952 fn syntax(&self) -> &SyntaxNode { &self.syntax }
1958} 1953}
1959impl AstNode for TryBlockExpr { 1954impl AstNode for EffectExpr {
1960 fn can_cast(kind: SyntaxKind) -> bool { kind == TRY_BLOCK_EXPR } 1955 fn can_cast(kind: SyntaxKind) -> bool { kind == EFFECT_EXPR }
1961 fn cast(syntax: SyntaxNode) -> Option<Self> { 1956 fn cast(syntax: SyntaxNode) -> Option<Self> {
1962 if Self::can_cast(syntax.kind()) { 1957 if Self::can_cast(syntax.kind()) {
1963 Some(Self { syntax }) 1958 Some(Self { syntax })
@@ -2649,17 +2644,6 @@ impl AstNode for Condition {
2649 } 2644 }
2650 fn syntax(&self) -> &SyntaxNode { &self.syntax } 2645 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2651} 2646}
2652impl AstNode for Block {
2653 fn can_cast(kind: SyntaxKind) -> bool { kind == BLOCK }
2654 fn cast(syntax: SyntaxNode) -> Option<Self> {
2655 if Self::can_cast(syntax.kind()) {
2656 Some(Self { syntax })
2657 } else {
2658 None
2659 }
2660 }
2661 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2662}
2663impl AstNode for ParamList { 2647impl AstNode for ParamList {
2664 fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM_LIST } 2648 fn can_cast(kind: SyntaxKind) -> bool { kind == PARAM_LIST }
2665 fn cast(syntax: SyntaxNode) -> Option<Self> { 2649 fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -3308,8 +3292,8 @@ impl From<AwaitExpr> for Expr {
3308impl From<TryExpr> for Expr { 3292impl From<TryExpr> for Expr {
3309 fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) } 3293 fn from(node: TryExpr) -> Expr { Expr::TryExpr(node) }
3310} 3294}
3311impl From<TryBlockExpr> for Expr { 3295impl From<EffectExpr> for Expr {
3312 fn from(node: TryBlockExpr) -> Expr { Expr::TryBlockExpr(node) } 3296 fn from(node: EffectExpr) -> Expr { Expr::EffectExpr(node) }
3313} 3297}
3314impl From<CastExpr> for Expr { 3298impl From<CastExpr> for Expr {
3315 fn from(node: CastExpr) -> Expr { Expr::CastExpr(node) } 3299 fn from(node: CastExpr) -> Expr { Expr::CastExpr(node) }
@@ -3341,9 +3325,10 @@ impl AstNode for Expr {
3341 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
3342 | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL 3326 | LOOP_EXPR | FOR_EXPR | WHILE_EXPR | CONTINUE_EXPR | BREAK_EXPR | LABEL
3343 | 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
3344 | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | TRY_BLOCK_EXPR 3328 | METHOD_CALL_EXPR | FIELD_EXPR | AWAIT_EXPR | TRY_EXPR | EFFECT_EXPR | CAST_EXPR
3345 | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL 3329 | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR | LITERAL | MACRO_CALL | BOX_EXPR => {
3346 | BOX_EXPR => true, 3330 true
3331 }
3347 _ => false, 3332 _ => false,
3348 } 3333 }
3349 } 3334 }
@@ -3371,7 +3356,7 @@ impl AstNode for Expr {
3371 FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }), 3356 FIELD_EXPR => Expr::FieldExpr(FieldExpr { syntax }),
3372 AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }), 3357 AWAIT_EXPR => Expr::AwaitExpr(AwaitExpr { syntax }),
3373 TRY_EXPR => Expr::TryExpr(TryExpr { syntax }), 3358 TRY_EXPR => Expr::TryExpr(TryExpr { syntax }),
3374 TRY_BLOCK_EXPR => Expr::TryBlockExpr(TryBlockExpr { syntax }), 3359 EFFECT_EXPR => Expr::EffectExpr(EffectExpr { syntax }),
3375 CAST_EXPR => Expr::CastExpr(CastExpr { syntax }), 3360 CAST_EXPR => Expr::CastExpr(CastExpr { syntax }),
3376 REF_EXPR => Expr::RefExpr(RefExpr { syntax }), 3361 REF_EXPR => Expr::RefExpr(RefExpr { syntax }),
3377 PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }), 3362 PREFIX_EXPR => Expr::PrefixExpr(PrefixExpr { syntax }),
@@ -3408,7 +3393,7 @@ impl AstNode for Expr {
3408 Expr::FieldExpr(it) => &it.syntax, 3393 Expr::FieldExpr(it) => &it.syntax,
3409 Expr::AwaitExpr(it) => &it.syntax, 3394 Expr::AwaitExpr(it) => &it.syntax,
3410 Expr::TryExpr(it) => &it.syntax, 3395 Expr::TryExpr(it) => &it.syntax,
3411 Expr::TryBlockExpr(it) => &it.syntax, 3396 Expr::EffectExpr(it) => &it.syntax,
3412 Expr::CastExpr(it) => &it.syntax, 3397 Expr::CastExpr(it) => &it.syntax,
3413 Expr::RefExpr(it) => &it.syntax, 3398 Expr::RefExpr(it) => &it.syntax,
3414 Expr::PrefixExpr(it) => &it.syntax, 3399 Expr::PrefixExpr(it) => &it.syntax,
@@ -3889,7 +3874,7 @@ impl std::fmt::Display for LoopExpr {
3889 std::fmt::Display::fmt(self.syntax(), f) 3874 std::fmt::Display::fmt(self.syntax(), f)
3890 } 3875 }
3891} 3876}
3892impl std::fmt::Display for TryBlockExpr { 3877impl std::fmt::Display for EffectExpr {
3893 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 3878 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3894 std::fmt::Display::fmt(self.syntax(), f) 3879 std::fmt::Display::fmt(self.syntax(), f)
3895 } 3880 }
@@ -4204,11 +4189,6 @@ impl std::fmt::Display for Condition {
4204 std::fmt::Display::fmt(self.syntax(), f) 4189 std::fmt::Display::fmt(self.syntax(), f)
4205 } 4190 }
4206} 4191}
4207impl std::fmt::Display for Block {
4208 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
4209 std::fmt::Display::fmt(self.syntax(), f)
4210 }
4211}
4212impl std::fmt::Display for ParamList { 4192impl std::fmt::Display for ParamList {
4213 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 4193 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
4214 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 ee0f5cc40..7b17fef49 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -22,8 +22,7 @@ pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
22pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { 22pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
23 path_from_text(&format!("{}::{}", qual, segment)) 23 path_from_text(&format!("{}::{}", qual, segment))
24} 24}
25 25fn path_from_text(text: &str) -> ast::Path {
26pub fn path_from_text(text: &str) -> ast::Path {
27 ast_from_text(text) 26 ast_from_text(text)
28} 27}
29 28
@@ -83,14 +82,6 @@ pub fn block_expr(
83 ast_from_text(&format!("fn f() {}", buf)) 82 ast_from_text(&format!("fn f() {}", buf))
84} 83}
85 84
86pub fn block_from_expr(e: ast::Expr) -> ast::Block {
87 return from_text(&format!("{{ {} }}", e));
88
89 fn from_text(text: &str) -> ast::Block {
90 ast_from_text(&format!("fn f() {}", text))
91 }
92}
93
94pub fn expr_unit() -> ast::Expr { 85pub fn expr_unit() -> ast::Expr {
95 expr_from_text("()") 86 expr_from_text("()")
96} 87}
diff --git a/crates/ra_syntax/src/ast/tokens.rs b/crates/ra_syntax/src/ast/tokens.rs
index 3865729b8..74906d8a6 100644
--- a/crates/ra_syntax/src/ast/tokens.rs
+++ b/crates/ra_syntax/src/ast/tokens.rs
@@ -13,7 +13,12 @@ impl Comment {
13 } 13 }
14 14
15 pub fn prefix(&self) -> &'static str { 15 pub fn prefix(&self) -> &'static str {
16 prefix_by_kind(self.kind()) 16 for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
17 if *k == self.kind() && self.text().starts_with(prefix) {
18 return prefix;
19 }
20 }
21 unreachable!()
17 } 22 }
18} 23}
19 24
@@ -48,6 +53,7 @@ pub enum CommentPlacement {
48const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = { 53const COMMENT_PREFIX_TO_KIND: &[(&str, CommentKind)] = {
49 use {CommentPlacement::*, CommentShape::*}; 54 use {CommentPlacement::*, CommentShape::*};
50 &[ 55 &[
56 ("////", CommentKind { shape: Line, doc: None }),
51 ("///", CommentKind { shape: Line, doc: Some(Outer) }), 57 ("///", CommentKind { shape: Line, doc: Some(Outer) }),
52 ("//!", CommentKind { shape: Line, doc: Some(Inner) }), 58 ("//!", CommentKind { shape: Line, doc: Some(Inner) }),
53 ("/**", CommentKind { shape: Block, doc: Some(Outer) }), 59 ("/**", CommentKind { shape: Block, doc: Some(Outer) }),
@@ -69,15 +75,6 @@ fn kind_by_prefix(text: &str) -> CommentKind {
69 panic!("bad comment text: {:?}", text) 75 panic!("bad comment text: {:?}", text)
70} 76}
71 77
72fn prefix_by_kind(kind: CommentKind) -> &'static str {
73 for (prefix, k) in COMMENT_PREFIX_TO_KIND.iter() {
74 if *k == kind {
75 return prefix;
76 }
77 }
78 unreachable!()
79}
80
81impl Whitespace { 78impl Whitespace {
82 pub fn spans_multiple_lines(&self) -> bool { 79 pub fn spans_multiple_lines(&self) -> bool {
83 let text = self.text(); 80 let text = self.text();
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/parsing/lexer.rs b/crates/ra_syntax/src/parsing/lexer.rs
index f450ef4a2..1a5a6dc06 100644
--- a/crates/ra_syntax/src/parsing/lexer.rs
+++ b/crates/ra_syntax/src/parsing/lexer.rs
@@ -180,7 +180,7 @@ fn rustc_token_kind_to_syntax_kind(
180 return (syntax_kind, None); 180 return (syntax_kind, None);
181 181
182 fn match_literal_kind(kind: &rustc_lexer::LiteralKind) -> (SyntaxKind, Option<&'static str>) { 182 fn match_literal_kind(kind: &rustc_lexer::LiteralKind) -> (SyntaxKind, Option<&'static str>) {
183 use rustc_lexer::LiteralKind as LK; 183 use rustc_lexer::{LexRawStrError, LiteralKind as LK};
184 184
185 #[rustfmt::skip] 185 #[rustfmt::skip]
186 let syntax_kind = match *kind { 186 let syntax_kind = match *kind {
@@ -215,21 +215,28 @@ fn rustc_token_kind_to_syntax_kind(
215 return (BYTE_STRING, Some("Missing trailing `\"` symbol to terminate the byte string literal")) 215 return (BYTE_STRING, Some("Missing trailing `\"` symbol to terminate the byte string literal"))
216 } 216 }
217 217
218 LK::RawStr { started: true, terminated: true, .. } => RAW_STRING, 218 LK::RawStr(str) => match str.validate() {
219 LK::RawStr { started: true, terminated: false, .. } => { 219 Ok(_) => RAW_STRING,
220 return (RAW_STRING, Some("Missing trailing `\"` with `#` symbols to terminate the raw string literal")) 220 Err(LexRawStrError::InvalidStarter) => return (RAW_STRING, Some("Missing `\"` symbol after `#` symbols to begin the raw string literal")),
221 } 221 Err(LexRawStrError::NoTerminator { expected, found, .. }) => if expected == found {
222 LK::RawStr { started: false, .. } => { 222 return (RAW_STRING, Some("Missing trailing `\"` to terminate the raw string literal"))
223 return (RAW_STRING, Some("Missing `\"` symbol after `#` symbols to begin the raw string literal")) 223 } else {
224 } 224 return (RAW_STRING, Some("Missing trailing `\"` with `#` symbols to terminate the raw string literal"))
225
226 },
227 Err(LexRawStrError::TooManyDelimiters { .. }) => return (RAW_STRING, Some("Too many `#` symbols: raw strings may be delimited by up to 65535 `#` symbols")),
228 },
229 LK::RawByteStr(str) => match str.validate() {
230 Ok(_) => RAW_BYTE_STRING,
231 Err(LexRawStrError::InvalidStarter) => return (RAW_BYTE_STRING, Some("Missing `\"` symbol after `#` symbols to begin the raw byte string literal")),
232 Err(LexRawStrError::NoTerminator { expected, found, .. }) => if expected == found {
233 return (RAW_BYTE_STRING, Some("Missing trailing `\"` to terminate the raw byte string literal"))
234 } else {
235 return (RAW_BYTE_STRING, Some("Missing trailing `\"` with `#` symbols to terminate the raw byte string literal"))
225 236
226 LK::RawByteStr { started: true, terminated: true, .. } => RAW_BYTE_STRING, 237 },
227 LK::RawByteStr { started: true, terminated: false, .. } => { 238 Err(LexRawStrError::TooManyDelimiters { .. }) => return (RAW_BYTE_STRING, Some("Too many `#` symbols: raw byte strings may be delimited by up to 65535 `#` symbols")),
228 return (RAW_BYTE_STRING, Some("Missing trailing `\"` with `#` symbols to terminate the raw byte string literal")) 239 },
229 }
230 LK::RawByteStr { started: false, .. } => {
231 return (RAW_BYTE_STRING, Some("Missing `\"` symbol after `#` symbols to begin the raw byte string literal"))
232 }
233 }; 240 };
234 241
235 (syntax_kind, None) 242 (syntax_kind, None)
diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs
index 5e93895ec..e075cd801 100644
--- a/crates/ra_syntax/src/validation.rs
+++ b/crates/ra_syntax/src/validation.rs
@@ -96,6 +96,7 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
96 ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors), 96 ast::RecordField(it) => validate_numeric_name(it.name_ref(), &mut errors),
97 ast::Visibility(it) => validate_visibility(it, &mut errors), 97 ast::Visibility(it) => validate_visibility(it, &mut errors),
98 ast::RangeExpr(it) => validate_range_expr(it, &mut errors), 98 ast::RangeExpr(it) => validate_range_expr(it, &mut errors),
99 ast::PathSegment(it) => validate_path_keywords(it, &mut errors),
99 _ => (), 100 _ => (),
100 } 101 }
101 } 102 }
@@ -222,3 +223,83 @@ fn validate_range_expr(expr: ast::RangeExpr, errors: &mut Vec<SyntaxError>) {
222 )); 223 ));
223 } 224 }
224} 225}
226
227fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxError>) {
228 use ast::PathSegmentKind;
229
230 let path = segment.parent_path();
231 let is_path_start = segment.coloncolon_token().is_none() && path.qualifier().is_none();
232
233 if let Some(token) = segment.self_token() {
234 if !is_path_start {
235 errors.push(SyntaxError::new(
236 "The `self` keyword is only allowed as the first segment of a path",
237 token.text_range(),
238 ));
239 }
240 } else if let Some(token) = segment.crate_token() {
241 if !is_path_start || use_prefix(path).is_some() {
242 errors.push(SyntaxError::new(
243 "The `crate` keyword is only allowed as the first segment of a path",
244 token.text_range(),
245 ));
246 }
247 } else if let Some(token) = segment.super_token() {
248 if !all_supers(&path) {
249 errors.push(SyntaxError::new(
250 "The `super` keyword may only be preceded by other `super`s",
251 token.text_range(),
252 ));
253 return;
254 }
255
256 let mut curr_path = path;
257 while let Some(prefix) = use_prefix(curr_path) {
258 if !all_supers(&prefix) {
259 errors.push(SyntaxError::new(
260 "The `super` keyword may only be preceded by other `super`s",
261 token.text_range(),
262 ));
263 return;
264 }
265 curr_path = prefix;
266 }
267 }
268
269 fn use_prefix(mut path: ast::Path) -> Option<ast::Path> {
270 for node in path.syntax().ancestors().skip(1) {
271 match_ast! {
272 match node {
273 ast::UseTree(it) => if let Some(tree_path) = it.path() {
274 // Even a top-level path exists within a `UseTree` so we must explicitly
275 // allow our path but disallow anything else
276 if tree_path != path {
277 return Some(tree_path);
278 }
279 },
280 ast::UseTreeList(_it) => continue,
281 ast::Path(parent) => path = parent,
282 _ => return None,
283 }
284 };
285 }
286 return None;
287 }
288
289 fn all_supers(path: &ast::Path) -> bool {
290 let segment = match path.segment() {
291 Some(it) => it,
292 None => return false,
293 };
294
295 if segment.kind() != Some(PathSegmentKind::SuperKw) {
296 return false;
297 }
298
299 if let Some(ref subpath) = path.qualifier() {
300 return all_supers(subpath);
301 }
302
303 return true;
304 }
305}
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
9pub(crate) fn validate_block_expr(expr: ast::BlockExpr, errors: &mut Vec<SyntaxError>) { 9pub(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}