aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-09-15 08:02:51 +0100
committerGitHub <[email protected]>2019-09-15 08:02:51 +0100
commitbcdba777bdc43762cff73ac3ded4ae737a503692 (patch)
treee65797200723aeb35afc10acb24a7a7d385d5c2b
parent211171ffe6a4e8cbf31b9513972e59a8b9bdc811 (diff)
parentc413f444a02ad7898b7a259e0d7aeaed91fd1997 (diff)
Merge #1847
1847: Allow an underscore as the identifier in `const` items r=matklad a=ecstatic-morse [RFC 2526](https://github.com/rust-lang/rust/issues/54912) was recently stabilized, meaning `const _: i32 = 5;` is now a valid item. Co-authored-by: Dylan MacKenzie <[email protected]>
-rw-r--r--crates/ra_parser/src/grammar/items.rs2
-rw-r--r--crates/ra_parser/src/grammar/items/consts.rs11
-rw-r--r--crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.rs1
-rw-r--r--crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.txt21
-rw-r--r--crates/ra_syntax/test_data/parser/ok/0024_const_item.rs1
-rw-r--r--crates/ra_syntax/test_data/parser/ok/0024_const_item.txt94
6 files changed, 90 insertions, 40 deletions
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index eff9d67e4..4dd80d443 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -258,7 +258,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
258 } 258 }
259 T![enum] => nominal::enum_def(p, m), 259 T![enum] => nominal::enum_def(p, m),
260 T![use] => use_item::use_item(p, m), 260 T![use] => use_item::use_item(p, m),
261 T![const] if (la == IDENT || la == T![mut]) => consts::const_def(p, m), 261 T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::const_def(p, m),
262 T![static] => consts::static_def(p, m), 262 T![static] => consts::static_def(p, m),
263 // test extern_block 263 // test extern_block
264 // extern {} 264 // extern {}
diff --git a/crates/ra_parser/src/grammar/items/consts.rs b/crates/ra_parser/src/grammar/items/consts.rs
index e11546333..63e0e6e0c 100644
--- a/crates/ra_parser/src/grammar/items/consts.rs
+++ b/crates/ra_parser/src/grammar/items/consts.rs
@@ -12,7 +12,16 @@ fn const_or_static(p: &mut Parser, m: Marker, kw: SyntaxKind, def: SyntaxKind) {
12 assert!(p.at(kw)); 12 assert!(p.at(kw));
13 p.bump_any(); 13 p.bump_any();
14 p.eat(T![mut]); // FIXME: validator to forbid const mut 14 p.eat(T![mut]); // FIXME: validator to forbid const mut
15 name(p); 15
16 // Allow `_` in place of an identifier in a `const`.
17 let is_const_underscore = kw == T![const] && p.eat(T![_]);
18 if !is_const_underscore {
19 name(p);
20 }
21
22 // test_err static_underscore
23 // static _: i32 = 5;
24
16 types::ascription(p); 25 types::ascription(p);
17 if p.eat(T![=]) { 26 if p.eat(T![=]) {
18 expressions::expr(p); 27 expressions::expr(p);
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.rs b/crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.rs
new file mode 100644
index 000000000..df8cecb43
--- /dev/null
+++ b/crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.rs
@@ -0,0 +1 @@
static _: i32 = 5;
diff --git a/crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.txt b/crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.txt
new file mode 100644
index 000000000..28f75a341
--- /dev/null
+++ b/crates/ra_syntax/test_data/parser/inline/err/0013_static_underscore.txt
@@ -0,0 +1,21 @@
1SOURCE_FILE@[0; 19)
2 STATIC_DEF@[0; 18)
3 STATIC_KW@[0; 6) "static"
4 WHITESPACE@[6; 7) " "
5 ERROR@[7; 8)
6 UNDERSCORE@[7; 8) "_"
7 COLON@[8; 9) ":"
8 WHITESPACE@[9; 10) " "
9 PATH_TYPE@[10; 13)
10 PATH@[10; 13)
11 PATH_SEGMENT@[10; 13)
12 NAME_REF@[10; 13)
13 IDENT@[10; 13) "i32"
14 WHITESPACE@[13; 14) " "
15 EQ@[14; 15) "="
16 WHITESPACE@[15; 16) " "
17 LITERAL@[16; 17)
18 INT_NUMBER@[16; 17) "5"
19 SEMI@[17; 18) ";"
20 WHITESPACE@[18; 19) "\n"
21error 7: expected a name
diff --git a/crates/ra_syntax/test_data/parser/ok/0024_const_item.rs b/crates/ra_syntax/test_data/parser/ok/0024_const_item.rs
index 7446859b5..a806a209d 100644
--- a/crates/ra_syntax/test_data/parser/ok/0024_const_item.rs
+++ b/crates/ra_syntax/test_data/parser/ok/0024_const_item.rs
@@ -1,2 +1,3 @@
1const _: u32 = 0;
1const FOO: u32 = 92; 2const FOO: u32 = 92;
2const mut BAR: u32 = 62; 3const mut BAR: u32 = 62;
diff --git a/crates/ra_syntax/test_data/parser/ok/0024_const_item.txt b/crates/ra_syntax/test_data/parser/ok/0024_const_item.txt
index dea8a3a97..abe9f851b 100644
--- a/crates/ra_syntax/test_data/parser/ok/0024_const_item.txt
+++ b/crates/ra_syntax/test_data/parser/ok/0024_const_item.txt
@@ -1,41 +1,59 @@
1SOURCE_FILE@[0; 46) 1SOURCE_FILE@[0; 64)
2 CONST_DEF@[0; 20) 2 CONST_DEF@[0; 17)
3 CONST_KW@[0; 5) "const" 3 CONST_KW@[0; 5) "const"
4 WHITESPACE@[5; 6) " " 4 WHITESPACE@[5; 6) " "
5 NAME@[6; 9) 5 UNDERSCORE@[6; 7) "_"
6 IDENT@[6; 9) "FOO" 6 COLON@[7; 8) ":"
7 COLON@[9; 10) ":" 7 WHITESPACE@[8; 9) " "
8 WHITESPACE@[10; 11) " " 8 PATH_TYPE@[9; 12)
9 PATH_TYPE@[11; 14) 9 PATH@[9; 12)
10 PATH@[11; 14) 10 PATH_SEGMENT@[9; 12)
11 PATH_SEGMENT@[11; 14) 11 NAME_REF@[9; 12)
12 NAME_REF@[11; 14) 12 IDENT@[9; 12) "u32"
13 IDENT@[11; 14) "u32" 13 WHITESPACE@[12; 13) " "
14 EQ@[13; 14) "="
14 WHITESPACE@[14; 15) " " 15 WHITESPACE@[14; 15) " "
15 EQ@[15; 16) "=" 16 LITERAL@[15; 16)
16 WHITESPACE@[16; 17) " " 17 INT_NUMBER@[15; 16) "0"
17 LITERAL@[17; 19) 18 SEMI@[16; 17) ";"
18 INT_NUMBER@[17; 19) "92" 19 WHITESPACE@[17; 18) "\n"
19 SEMI@[19; 20) ";" 20 CONST_DEF@[18; 38)
20 WHITESPACE@[20; 21) "\n" 21 CONST_KW@[18; 23) "const"
21 CONST_DEF@[21; 45) 22 WHITESPACE@[23; 24) " "
22 CONST_KW@[21; 26) "const" 23 NAME@[24; 27)
23 WHITESPACE@[26; 27) " " 24 IDENT@[24; 27) "FOO"
24 MUT_KW@[27; 30) "mut" 25 COLON@[27; 28) ":"
25 WHITESPACE@[30; 31) " " 26 WHITESPACE@[28; 29) " "
26 NAME@[31; 34) 27 PATH_TYPE@[29; 32)
27 IDENT@[31; 34) "BAR" 28 PATH@[29; 32)
28 COLON@[34; 35) ":" 29 PATH_SEGMENT@[29; 32)
29 WHITESPACE@[35; 36) " " 30 NAME_REF@[29; 32)
30 PATH_TYPE@[36; 39) 31 IDENT@[29; 32) "u32"
31 PATH@[36; 39) 32 WHITESPACE@[32; 33) " "
32 PATH_SEGMENT@[36; 39) 33 EQ@[33; 34) "="
33 NAME_REF@[36; 39) 34 WHITESPACE@[34; 35) " "
34 IDENT@[36; 39) "u32" 35 LITERAL@[35; 37)
35 WHITESPACE@[39; 40) " " 36 INT_NUMBER@[35; 37) "92"
36 EQ@[40; 41) "=" 37 SEMI@[37; 38) ";"
37 WHITESPACE@[41; 42) " " 38 WHITESPACE@[38; 39) "\n"
38 LITERAL@[42; 44) 39 CONST_DEF@[39; 63)
39 INT_NUMBER@[42; 44) "62" 40 CONST_KW@[39; 44) "const"
40 SEMI@[44; 45) ";" 41 WHITESPACE@[44; 45) " "
41 WHITESPACE@[45; 46) "\n" 42 MUT_KW@[45; 48) "mut"
43 WHITESPACE@[48; 49) " "
44 NAME@[49; 52)
45 IDENT@[49; 52) "BAR"
46 COLON@[52; 53) ":"
47 WHITESPACE@[53; 54) " "
48 PATH_TYPE@[54; 57)
49 PATH@[54; 57)
50 PATH_SEGMENT@[54; 57)
51 NAME_REF@[54; 57)
52 IDENT@[54; 57) "u32"
53 WHITESPACE@[57; 58) " "
54 EQ@[58; 59) "="
55 WHITESPACE@[59; 60) " "
56 LITERAL@[60; 62)
57 INT_NUMBER@[60; 62) "62"
58 SEMI@[62; 63) ";"
59 WHITESPACE@[63; 64) "\n"