implement the ? operator

The `?` postfix operator is sugar equivalent to the try! macro, but is more amenable to chaining:
`File::open("foo")?.metadata()?.is_dir()`.

`?` is accepted on any *expression* that can return a `Result`, e.g. `x()?`, `y!()?`, `{z}?`,
`(w)?`, etc. And binds more tightly than unary operators, e.g. `!x?` is parsed as `!(x?)`.

cc #31436
This commit is contained in:
Jorge Aparicio
2016-02-28 17:38:48 -05:00
parent c116ae35cf
commit 210dd611aa
12 changed files with 355 additions and 2 deletions

View File

@@ -2534,6 +2534,12 @@ impl<'a> Parser<'a> {
let mut e = e0;
let mut hi;
loop {
// expr?
while self.eat(&token::Question) {
let hi = self.span.hi;
e = self.mk_expr(lo, hi, ExprKind::Try(e), None);
}
// expr.f
if self.eat(&token::Dot) {
match self.token {
@@ -2907,7 +2913,6 @@ impl<'a> Parser<'a> {
}
};
if self.expr_is_complete(&lhs) {
// Semi-statement forms are odd. See https://github.com/rust-lang/rust/issues/29071
return Ok(lhs);