aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/grammar/types.rs
diff options
context:
space:
mode:
authorRoland Ruckerbauer <[email protected]>2018-12-17 16:10:05 +0000
committerRoland Ruckerbauer <[email protected]>2018-12-17 16:26:24 +0000
commitd0f1334226fc25a8c00ecccd64c0021e4aef9ca5 (patch)
tree273bdffe892f2eeef1850e2d3f7b1ca9ed228285 /crates/ra_syntax/src/grammar/types.rs
parent8d42deeac354d1f8297d6c4d52b2707fb8d7a771 (diff)
Fixed cast expression parsing in ra_syntax.
The cast expression expected any type via types::type_() function, but the language spec does only allow TypeNoBounds (types without direct extra bounds via `+`). **Example:** ```rust fn test() { 6i8 as i32 + 5; } ``` This fails, because the types::type_() function which should parse the type after the as keyword is greedy, and takes all plus sign after path types as extra. My proposed fix is to replace the not implemented `type_no_plus()` just calls (`type_()`) function, which is used at several places. The replacement is `type_with_bounds_cond(p: &mut Parser, allow_bounds: bool)`, which passes the condition to relevant sub-parsers. This function is then called by `type_()` and the new public `type_no_bounds()`.
Diffstat (limited to 'crates/ra_syntax/src/grammar/types.rs')
-rw-r--r--crates/ra_syntax/src/grammar/types.rs21
1 files changed, 13 insertions, 8 deletions
diff --git a/crates/ra_syntax/src/grammar/types.rs b/crates/ra_syntax/src/grammar/types.rs
index ed2718e73..811d399d4 100644
--- a/crates/ra_syntax/src/grammar/types.rs
+++ b/crates/ra_syntax/src/grammar/types.rs
@@ -11,6 +11,14 @@ pub(super) const TYPE_FIRST: TokenSet = token_set_union![
11const TYPE_RECOVERY_SET: TokenSet = token_set![R_PAREN, COMMA]; 11const TYPE_RECOVERY_SET: TokenSet = token_set![R_PAREN, COMMA];
12 12
13pub(super) fn type_(p: &mut Parser) { 13pub(super) fn type_(p: &mut Parser) {
14 type_with_bounds_cond(p, true);
15}
16
17pub(super) fn type_no_bounds(p: &mut Parser) {
18 type_with_bounds_cond(p, false);
19}
20
21fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) {
14 match p.current() { 22 match p.current() {
15 L_PAREN => paren_or_tuple_type(p), 23 L_PAREN => paren_or_tuple_type(p),
16 EXCL => never_type(p), 24 EXCL => never_type(p),
@@ -22,8 +30,9 @@ pub(super) fn type_(p: &mut Parser) {
22 FOR_KW => for_type(p), 30 FOR_KW => for_type(p),
23 IMPL_KW => impl_trait_type(p), 31 IMPL_KW => impl_trait_type(p),
24 DYN_KW => dyn_trait_type(p), 32 DYN_KW => dyn_trait_type(p),
25 L_ANGLE => path_type(p), 33 // Some path types are not allowed to have bounds (no plus)
26 _ if paths::is_path_start(p) => path_type(p), 34 L_ANGLE => path_type_(p, allow_bounds),
35 _ if paths::is_path_start(p) => path_type_(p, allow_bounds),
27 _ => { 36 _ => {
28 p.err_recover("expected type", TYPE_RECOVERY_SET); 37 p.err_recover("expected type", TYPE_RECOVERY_SET);
29 } 38 }
@@ -35,10 +44,6 @@ pub(super) fn ascription(p: &mut Parser) {
35 type_(p) 44 type_(p)
36} 45}
37 46
38fn type_no_plus(p: &mut Parser) {
39 type_(p);
40}
41
42fn paren_or_tuple_type(p: &mut Parser) { 47fn paren_or_tuple_type(p: &mut Parser) {
43 assert!(p.at(L_PAREN)); 48 assert!(p.at(L_PAREN));
44 let m = p.start(); 49 let m = p.start();
@@ -101,7 +106,7 @@ fn pointer_type(p: &mut Parser) {
101 } 106 }
102 }; 107 };
103 108
104 type_no_plus(p); 109 type_no_bounds(p);
105 m.complete(p, POINTER_TYPE); 110 m.complete(p, POINTER_TYPE);
106} 111}
107 112
@@ -147,7 +152,7 @@ fn reference_type(p: &mut Parser) {
147 p.bump(); 152 p.bump();
148 p.eat(LIFETIME); 153 p.eat(LIFETIME);
149 p.eat(MUT_KW); 154 p.eat(MUT_KW);
150 type_no_plus(p); 155 type_no_bounds(p);
151 m.complete(p, REFERENCE_TYPE); 156 m.complete(p, REFERENCE_TYPE);
152} 157}
153 158