Skip to content

Commit 72ac961

Browse files
committed
Raise precedence of closure that has explicit return type
1 parent 4df47a0 commit 72ac961

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

compiler/rustc_ast/src/ast.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1316,9 +1316,15 @@ impl Expr {
13161316
}
13171317

13181318
pub fn precedence(&self) -> ExprPrecedence {
1319-
match self.kind {
1319+
match &self.kind {
1320+
ExprKind::Closure(closure) => {
1321+
match closure.fn_decl.output {
1322+
FnRetTy::Default(_) => ExprPrecedence::Jump,
1323+
FnRetTy::Ty(_) => ExprPrecedence::Unambiguous,
1324+
}
1325+
}
1326+
13201327
ExprKind::Break(..)
1321-
| ExprKind::Closure(..)
13221328
| ExprKind::Continue(..)
13231329
| ExprKind::Ret(..)
13241330
| ExprKind::Yield(..)

compiler/rustc_hir/src/hir.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1696,9 +1696,15 @@ pub struct Expr<'hir> {
16961696

16971697
impl Expr<'_> {
16981698
pub fn precedence(&self) -> ExprPrecedence {
1699-
match self.kind {
1699+
match &self.kind {
1700+
ExprKind::Closure(closure) => {
1701+
match closure.fn_decl.output {
1702+
FnRetTy::DefaultReturn(_) => ExprPrecedence::Jump,
1703+
FnRetTy::Return(_) => ExprPrecedence::Unambiguous,
1704+
}
1705+
}
1706+
17001707
ExprKind::Break(..)
1701-
| ExprKind::Closure { .. }
17021708
| ExprKind::Continue(..)
17031709
| ExprKind::Ret(..)
17041710
| ExprKind::Yield(..)
@@ -1741,7 +1747,7 @@ impl Expr<'_> {
17411747
| ExprKind::Type(..)
17421748
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
17431749

1744-
ExprKind::DropTemps(ref expr, ..) => expr.precedence(),
1750+
ExprKind::DropTemps(expr, ..) => expr.precedence(),
17451751
}
17461752
}
17471753

tests/ui-fulldeps/pprust-parenthesis-insertion.rs

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

0 commit comments

Comments
 (0)