From b63f260bbcf89a2b40358f534b97f672468294fb Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Thu, 3 Oct 2019 00:10:58 -0700 Subject: Lower the precedence of the `as` operator. Previously, the `as` operator was being parsed like a postfix expression, and therefore being given the highest possible precedence. That caused it to bind more tightly than prefix operators, which it should not. Instead, parse it somewhat like a normal binary expression with some special-casing. --- crates/ra_parser/src/grammar/expressions.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 413ecb278..448b87505 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -250,6 +250,7 @@ fn current_op(p: &Parser) -> (u8, SyntaxKind) { T![!] if p.at(T![!=]) => (5, T![!=]), T![-] if p.at(T![-=]) => (1, T![-=]), T![-] => (10, T![-]), + T![as] => (12, T![as]), _ => NOT_AN_OP } @@ -278,6 +279,10 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, if op_bp < bp { break; } + if p.at(T![as]) { + lhs = cast_expr(p, lhs); + continue; + } let m = lhs.precede(p); p.bump(op); @@ -296,6 +301,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> // fn foo() { // let _ = &1; // let _ = &mut &f(); + // let _ = &1 as *const i32; // } T![&] => { m = p.start(); @@ -305,9 +311,13 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> } // test unary_expr // fn foo() { - // **&1; + // **&1 + 1; // !!true; // --1; + // *&1 as u64; + // *x(1); + // &x[1]; + // -1..2; // } T![*] | T![!] | T![-] => { m = p.start(); @@ -338,6 +348,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> return Some(postfix_expr(p, lhs, blocklike, !(r.prefer_stmt && blocklike.is_block()))); } }; + // parse the interior of the unary expression expr_bp(p, r, 255); Some((m.complete(p, kind), BlockLike::NotBlock)) } @@ -371,7 +382,6 @@ fn postfix_expr( } }, T![?] => try_expr(p, lhs), - T![as] => cast_expr(p, lhs), _ => break, }; allow_calls = true; -- cgit v1.2.3