1951: Lower the precedence of the `as` operator. r=matklad a=goffrie

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.

Fixes #1851.

Co-authored-by: Geoffry Song <goffrie@gmail.com>
This commit is contained in:
bors[bot] 2019-10-08 08:44:26 +00:00 committed by GitHub
commit d9338dfa98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 160 additions and 1 deletions

View file

@ -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,14 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>,
if op_bp < bp {
break;
}
// test as_precedence
// fn foo() {
// let _ = &1 as *const i32;
// }
if p.at(T![as]) {
lhs = cast_expr(p, lhs);
continue;
}
let m = lhs.precede(p);
p.bump(op);
@ -344,6 +353,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)>
));
}
};
// parse the interior of the unary expression
expr_bp(p, r, 255);
Some((m.complete(p, kind), BlockLike::NotBlock))
}
@ -378,7 +388,6 @@ fn postfix_expr(
}
},
T![?] => try_expr(p, lhs),
T![as] => cast_expr(p, lhs),
_ => break,
};
allow_calls = true;