aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-23 10:37:30 +0000
committerGitHub <[email protected]>2020-12-23 10:37:30 +0000
commitfd1fcf2c2e90ab04103a6aa9d033ec64dcc8d555 (patch)
tree31b5facb6a7a24a113e32fd6afa76f326ab8e3a2 /crates
parentb0d81d98dbc4cea6920793c7e2952a0d38ec543e (diff)
parentbdd8c0b68f097c7d1a65a5b85b94f0a79affa506 (diff)
Merge #7010
7010: Update ungrammar for const block patterns r=matklad a=Veykril Fixes #6848 Adds const blocks and const block patterns to the AST and parses them. Blocked on https://github.com/rust-analyzer/ungrammar/pull/17/, will merge that PR there once this one gets the OK so I can remove the local ungrammar dependency path and fix the Cargo.lock. Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/hir_def/src/body/lower.rs4
-rw-r--r--crates/parser/src/grammar/expressions/atom.rs9
-rw-r--r--crates/parser/src/grammar/items.rs7
-rw-r--r--crates/parser/src/grammar/patterns.rs14
-rw-r--r--crates/parser/src/syntax_kind/generated.rs1
-rw-r--r--crates/syntax/src/ast/generated/nodes.rs33
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rast76
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rs4
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0157_const_block.rast23
-rw-r--r--crates/syntax/test_data/parser/inline/ok/0157_const_block.rs1
10 files changed, 168 insertions, 4 deletions
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs
index 0f404be1b..978c3a324 100644
--- a/crates/hir_def/src/body/lower.rs
+++ b/crates/hir_def/src/body/lower.rs
@@ -933,7 +933,9 @@ impl ExprCollector<'_> {
933 Pat::Box { inner } 933 Pat::Box { inner }
934 } 934 }
935 // FIXME: implement 935 // FIXME: implement
936 ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing, 936 ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) | ast::Pat::ConstBlockPat(_) => {
937 Pat::Missing
938 }
937 }; 939 };
938 let ptr = AstPtr::new(&pat); 940 let ptr = AstPtr::new(&pat);
939 self.alloc_pat(pattern, Either::Left(ptr)) 941 self.alloc_pat(pattern, Either::Left(ptr))
diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs
index e897d5a52..c7a3556a7 100644
--- a/crates/parser/src/grammar/expressions/atom.rs
+++ b/crates/parser/src/grammar/expressions/atom.rs
@@ -46,6 +46,7 @@ pub(super) const ATOM_EXPR_FIRST: TokenSet =
46 T![continue], 46 T![continue],
47 T![async], 47 T![async],
48 T![try], 48 T![try],
49 T![const],
49 T![loop], 50 T![loop],
50 T![for], 51 T![for],
51 LIFETIME_IDENT, 52 LIFETIME_IDENT,
@@ -115,6 +116,14 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
115 block_expr(p); 116 block_expr(p);
116 m.complete(p, EFFECT_EXPR) 117 m.complete(p, EFFECT_EXPR)
117 } 118 }
119 // test const_block
120 // fn f() { const { } }
121 T![const] if la == T!['{'] => {
122 let m = p.start();
123 p.bump(T![const]);
124 block_expr(p);
125 m.complete(p, EFFECT_EXPR)
126 }
118 T!['{'] => { 127 T!['{'] => {
119 // test for_range_from 128 // test for_range_from
120 // fn foo() { 129 // fn foo() {
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs
index 8999829b4..72b73f891 100644
--- a/crates/parser/src/grammar/items.rs
+++ b/crates/parser/src/grammar/items.rs
@@ -96,7 +96,10 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
96 let mut has_mods = false; 96 let mut has_mods = false;
97 97
98 // modifiers 98 // modifiers
99 has_mods |= p.eat(T![const]); 99 if p.at(T![const]) && p.nth(1) != T!['{'] {
100 p.eat(T![const]);
101 has_mods = true;
102 }
100 103
101 // test_err async_without_semicolon 104 // test_err async_without_semicolon
102 // fn foo() { let _ = async {} } 105 // fn foo() { let _ = async {} }
@@ -167,7 +170,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
167 m.complete(p, TRAIT); 170 m.complete(p, TRAIT);
168 } 171 }
169 172
170 T![const] => { 173 T![const] if p.nth(1) != T!['{'] => {
171 consts::konst(p, m); 174 consts::konst(p, m);
172 } 175 }
173 176
diff --git a/crates/parser/src/grammar/patterns.rs b/crates/parser/src/grammar/patterns.rs
index 7e7f73dee..b53d5749f 100644
--- a/crates/parser/src/grammar/patterns.rs
+++ b/crates/parser/src/grammar/patterns.rs
@@ -89,6 +89,7 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
89 let m = match p.nth(0) { 89 let m = match p.nth(0) {
90 T![box] => box_pat(p), 90 T![box] => box_pat(p),
91 T![ref] | T![mut] => ident_pat(p, true), 91 T![ref] | T![mut] => ident_pat(p, true),
92 T![const] => const_block_pat(p),
92 IDENT => match p.nth(1) { 93 IDENT => match p.nth(1) {
93 // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro 94 // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro
94 // (T![x]). 95 // (T![x]).
@@ -386,3 +387,16 @@ fn box_pat(p: &mut Parser) -> CompletedMarker {
386 pattern_single(p); 387 pattern_single(p);
387 m.complete(p, BOX_PAT) 388 m.complete(p, BOX_PAT)
388} 389}
390
391// test const_block_pat
392// fn main() {
393// let const { 15 } = ();
394// let const { foo(); bar() } = ();
395// }
396fn const_block_pat(p: &mut Parser) -> CompletedMarker {
397 assert!(p.at(T![const]));
398 let m = p.start();
399 p.bump(T![const]);
400 expressions::block_expr(p);
401 m.complete(p, CONST_BLOCK_PAT)
402}
diff --git a/crates/parser/src/syntax_kind/generated.rs b/crates/parser/src/syntax_kind/generated.rs
index 980aa5979..f69e71bdb 100644
--- a/crates/parser/src/syntax_kind/generated.rs
+++ b/crates/parser/src/syntax_kind/generated.rs
@@ -170,6 +170,7 @@ pub enum SyntaxKind {
170 RANGE_PAT, 170 RANGE_PAT,
171 LITERAL_PAT, 171 LITERAL_PAT,
172 MACRO_PAT, 172 MACRO_PAT,
173 CONST_BLOCK_PAT,
173 TUPLE_EXPR, 174 TUPLE_EXPR,
174 ARRAY_EXPR, 175 ARRAY_EXPR,
175 PAREN_EXPR, 176 PAREN_EXPR,
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs
index 1588ba93e..c5b80bffe 100644
--- a/crates/syntax/src/ast/generated/nodes.rs
+++ b/crates/syntax/src/ast/generated/nodes.rs
@@ -763,6 +763,7 @@ impl EffectExpr {
763 pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) } 763 pub fn try_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![try]) }
764 pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } 764 pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
765 pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) } 765 pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
766 pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
766 pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) } 767 pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
767} 768}
768#[derive(Debug, Clone, PartialEq, Eq, Hash)] 769#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -1251,6 +1252,14 @@ impl TupleStructPat {
1251 pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } 1252 pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
1252} 1253}
1253#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1254#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1255pub struct ConstBlockPat {
1256 pub(crate) syntax: SyntaxNode,
1257}
1258impl ConstBlockPat {
1259 pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
1260 pub fn block_expr(&self) -> Option<BlockExpr> { support::child(&self.syntax) }
1261}
1262#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1254pub struct RecordPatFieldList { 1263pub struct RecordPatFieldList {
1255 pub(crate) syntax: SyntaxNode, 1264 pub(crate) syntax: SyntaxNode,
1256} 1265}
@@ -1369,6 +1378,7 @@ pub enum Pat {
1369 SlicePat(SlicePat), 1378 SlicePat(SlicePat),
1370 TuplePat(TuplePat), 1379 TuplePat(TuplePat),
1371 TupleStructPat(TupleStructPat), 1380 TupleStructPat(TupleStructPat),
1381 ConstBlockPat(ConstBlockPat),
1372} 1382}
1373#[derive(Debug, Clone, PartialEq, Eq, Hash)] 1383#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1374pub enum FieldList { 1384pub enum FieldList {
@@ -2772,6 +2782,17 @@ impl AstNode for TupleStructPat {
2772 } 2782 }
2773 fn syntax(&self) -> &SyntaxNode { &self.syntax } 2783 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2774} 2784}
2785impl AstNode for ConstBlockPat {
2786 fn can_cast(kind: SyntaxKind) -> bool { kind == CONST_BLOCK_PAT }
2787 fn cast(syntax: SyntaxNode) -> Option<Self> {
2788 if Self::can_cast(syntax.kind()) {
2789 Some(Self { syntax })
2790 } else {
2791 None
2792 }
2793 }
2794 fn syntax(&self) -> &SyntaxNode { &self.syntax }
2795}
2775impl AstNode for RecordPatFieldList { 2796impl AstNode for RecordPatFieldList {
2776 fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_PAT_FIELD_LIST } 2797 fn can_cast(kind: SyntaxKind) -> bool { kind == RECORD_PAT_FIELD_LIST }
2777 fn cast(syntax: SyntaxNode) -> Option<Self> { 2798 fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -3242,12 +3263,15 @@ impl From<TuplePat> for Pat {
3242impl From<TupleStructPat> for Pat { 3263impl From<TupleStructPat> for Pat {
3243 fn from(node: TupleStructPat) -> Pat { Pat::TupleStructPat(node) } 3264 fn from(node: TupleStructPat) -> Pat { Pat::TupleStructPat(node) }
3244} 3265}
3266impl From<ConstBlockPat> for Pat {
3267 fn from(node: ConstBlockPat) -> Pat { Pat::ConstBlockPat(node) }
3268}
3245impl AstNode for Pat { 3269impl AstNode for Pat {
3246 fn can_cast(kind: SyntaxKind) -> bool { 3270 fn can_cast(kind: SyntaxKind) -> bool {
3247 match kind { 3271 match kind {
3248 IDENT_PAT | BOX_PAT | REST_PAT | LITERAL_PAT | MACRO_PAT | OR_PAT | PAREN_PAT 3272 IDENT_PAT | BOX_PAT | REST_PAT | LITERAL_PAT | MACRO_PAT | OR_PAT | PAREN_PAT
3249 | PATH_PAT | WILDCARD_PAT | RANGE_PAT | RECORD_PAT | REF_PAT | SLICE_PAT 3273 | PATH_PAT | WILDCARD_PAT | RANGE_PAT | RECORD_PAT | REF_PAT | SLICE_PAT
3250 | TUPLE_PAT | TUPLE_STRUCT_PAT => true, 3274 | TUPLE_PAT | TUPLE_STRUCT_PAT | CONST_BLOCK_PAT => true,
3251 _ => false, 3275 _ => false,
3252 } 3276 }
3253 } 3277 }
@@ -3268,6 +3292,7 @@ impl AstNode for Pat {
3268 SLICE_PAT => Pat::SlicePat(SlicePat { syntax }), 3292 SLICE_PAT => Pat::SlicePat(SlicePat { syntax }),
3269 TUPLE_PAT => Pat::TuplePat(TuplePat { syntax }), 3293 TUPLE_PAT => Pat::TuplePat(TuplePat { syntax }),
3270 TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }), 3294 TUPLE_STRUCT_PAT => Pat::TupleStructPat(TupleStructPat { syntax }),
3295 CONST_BLOCK_PAT => Pat::ConstBlockPat(ConstBlockPat { syntax }),
3271 _ => return None, 3296 _ => return None,
3272 }; 3297 };
3273 Some(res) 3298 Some(res)
@@ -3289,6 +3314,7 @@ impl AstNode for Pat {
3289 Pat::SlicePat(it) => &it.syntax, 3314 Pat::SlicePat(it) => &it.syntax,
3290 Pat::TuplePat(it) => &it.syntax, 3315 Pat::TuplePat(it) => &it.syntax,
3291 Pat::TupleStructPat(it) => &it.syntax, 3316 Pat::TupleStructPat(it) => &it.syntax,
3317 Pat::ConstBlockPat(it) => &it.syntax,
3292 } 3318 }
3293 } 3319 }
3294} 3320}
@@ -4137,6 +4163,11 @@ impl std::fmt::Display for TupleStructPat {
4137 std::fmt::Display::fmt(self.syntax(), f) 4163 std::fmt::Display::fmt(self.syntax(), f)
4138 } 4164 }
4139} 4165}
4166impl std::fmt::Display for ConstBlockPat {
4167 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4168 std::fmt::Display::fmt(self.syntax(), f)
4169 }
4170}
4140impl std::fmt::Display for RecordPatFieldList { 4171impl std::fmt::Display for RecordPatFieldList {
4141 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 4172 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4142 std::fmt::Display::fmt(self.syntax(), f) 4173 std::fmt::Display::fmt(self.syntax(), f)
diff --git a/crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rast b/crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rast
new file mode 100644
index 000000000..8ff4822c4
--- /dev/null
+++ b/crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rast
@@ -0,0 +1,76 @@
1[email protected]
2 [email protected]
3 [email protected] "fn"
4 [email protected] " "
5 [email protected]
6 [email protected] "main"
7 [email protected]
8 [email protected] "("
9 [email protected] ")"
10 [email protected] " "
11 [email protected]
12 [email protected] "{"
13 [email protected] "\n "
14 [email protected]
15 [email protected] "let"
16 [email protected] " "
17 [email protected]
18 [email protected] "const"
19 [email protected] " "
20 [email protected]
21 [email protected] "{"
22 [email protected] " "
23 [email protected]
24 [email protected] "15"
25 [email protected] " "
26 [email protected] "}"
27 [email protected] " "
28 [email protected] "="
29 [email protected] " "
30 [email protected]
31 [email protected] "("
32 [email protected] ")"
33 [email protected] ";"
34 [email protected] "\n "
35 [email protected]
36 [email protected] "let"
37 [email protected] " "
38 [email protected]
39 [email protected] "const"
40 [email protected] " "
41 [email protected]
42 [email protected] "{"
43 [email protected] " "
44 [email protected]
45 [email protected]
46 [email protected]
47 [email protected]
48 [email protected]
49 [email protected]
50 [email protected] "foo"
51 [email protected]
52 [email protected] "("
53 [email protected] ")"
54 [email protected] ";"
55 [email protected] " "
56 [email protected]
57 [email protected]
58 [email protected]
59 [email protected]
60 [email protected]
61 [email protected] "bar"
62 [email protected]
63 [email protected] "("
64 [email protected] ")"
65 [email protected] " "
66 [email protected] "}"
67 [email protected] " "
68 [email protected] "="
69 [email protected] " "
70 [email protected]
71 [email protected] "("
72 [email protected] ")"
73 [email protected] ";"
74 [email protected] "\n"
75 [email protected] "}"
76 [email protected] "\n"
diff --git a/crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rs b/crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rs
new file mode 100644
index 000000000..dce9defac
--- /dev/null
+++ b/crates/syntax/test_data/parser/inline/ok/0156_const_block_pat.rs
@@ -0,0 +1,4 @@
1fn main() {
2 let const { 15 } = ();
3 let const { foo(); bar() } = ();
4}
diff --git a/crates/syntax/test_data/parser/inline/ok/0157_const_block.rast b/crates/syntax/test_data/parser/inline/ok/0157_const_block.rast
new file mode 100644
index 000000000..d5d2c8fe3
--- /dev/null
+++ b/crates/syntax/test_data/parser/inline/ok/0157_const_block.rast
@@ -0,0 +1,23 @@
1[email protected]
2 [email protected]
3 [email protected] "fn"
4 [email protected] " "
5 [email protected]
6 [email protected] "f"
7 [email protected]
8 [email protected] "("
9 [email protected] ")"
10 [email protected] " "
11 [email protected]
12 [email protected] "{"
13 [email protected] " "
14 [email protected]
15 [email protected] "const"
16 [email protected] " "
17 [email protected]
18 [email protected] "{"
19 [email protected] " "
20 [email protected] "}"
21 [email protected] " "
22 [email protected] "}"
23 [email protected] "\n"
diff --git a/crates/syntax/test_data/parser/inline/ok/0157_const_block.rs b/crates/syntax/test_data/parser/inline/ok/0157_const_block.rs
new file mode 100644
index 000000000..a2e3565a3
--- /dev/null
+++ b/crates/syntax/test_data/parser/inline/ok/0157_const_block.rs
@@ -0,0 +1 @@
fn f() { const { } }