1
Fork 0

Raise precedence of closure that has explicit return type

This commit is contained in:
David Tolnay 2024-12-02 17:41:47 -08:00
parent 4df47a09a4
commit 72ac961616
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 18 additions and 6 deletions

View file

@ -1316,9 +1316,15 @@ impl Expr {
}
pub fn precedence(&self) -> ExprPrecedence {
match self.kind {
match &self.kind {
ExprKind::Closure(closure) => {
match closure.fn_decl.output {
FnRetTy::Default(_) => ExprPrecedence::Jump,
FnRetTy::Ty(_) => ExprPrecedence::Unambiguous,
}
}
ExprKind::Break(..)
| ExprKind::Closure(..)
| ExprKind::Continue(..)
| ExprKind::Ret(..)
| ExprKind::Yield(..)

View file

@ -1696,9 +1696,15 @@ pub struct Expr<'hir> {
impl Expr<'_> {
pub fn precedence(&self) -> ExprPrecedence {
match self.kind {
match &self.kind {
ExprKind::Closure(closure) => {
match closure.fn_decl.output {
FnRetTy::DefaultReturn(_) => ExprPrecedence::Jump,
FnRetTy::Return(_) => ExprPrecedence::Unambiguous,
}
}
ExprKind::Break(..)
| ExprKind::Closure { .. }
| ExprKind::Continue(..)
| ExprKind::Ret(..)
| ExprKind::Yield(..)
@ -1741,7 +1747,7 @@ impl Expr<'_> {
| ExprKind::Type(..)
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
ExprKind::DropTemps(ref expr, ..) => expr.precedence(),
ExprKind::DropTemps(expr, ..) => expr.precedence(),
}
}

View file

@ -74,7 +74,7 @@ static EXPRS: &[&str] = &[
"|| return break 2",
"return break || 2",
// Closures with a return type have especially high precedence.
"(|| -> T { x }) + 1", // FIXME: no parenthesis needed.
"|| -> T { x } + 1",
"(|| { x }) + 1",
// These mean different things.
"if let _ = true && false {}",