Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precedence improvements: closures and jumps #133782

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,11 +1316,15 @@ impl Expr {
}

pub fn precedence(&self) -> ExprPrecedence {
match self.kind {
ExprKind::Closure(..) => ExprPrecedence::Closure,
match &self.kind {
ExprKind::Closure(closure) => {
match closure.fn_decl.output {
FnRetTy::Default(_) => ExprPrecedence::Jump,
FnRetTy::Ty(_) => ExprPrecedence::Unambiguous,
}
}

ExprKind::Break(..)
| ExprKind::Continue(..)
| ExprKind::Ret(..)
| ExprKind::Yield(..)
| ExprKind::Yeet(..)
Expand Down Expand Up @@ -1354,6 +1358,7 @@ impl Expr {
| ExprKind::Block(..)
| ExprKind::Call(..)
| ExprKind::ConstBlock(_)
| ExprKind::Continue(..)
| ExprKind::Field(..)
| ExprKind::ForLoop { .. }
| ExprKind::FormatArgs(..)
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_ast/src/util/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ impl AssocOp {

#[derive(Clone, Copy, PartialEq, PartialOrd)]
pub enum ExprPrecedence {
Closure,
// return, break, yield
// return, break, yield, closures
Jump,
// = += -= *= /= %= &= |= ^= <<= >>=
Assign,
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,11 +1696,15 @@ pub struct Expr<'hir> {

impl Expr<'_> {
pub fn precedence(&self) -> ExprPrecedence {
match self.kind {
ExprKind::Closure { .. } => ExprPrecedence::Closure,
match &self.kind {
ExprKind::Closure(closure) => {
match closure.fn_decl.output {
FnRetTy::DefaultReturn(_) => ExprPrecedence::Jump,
FnRetTy::Return(_) => ExprPrecedence::Unambiguous,
}
}

ExprKind::Break(..)
| ExprKind::Continue(..)
| ExprKind::Ret(..)
| ExprKind::Yield(..)
| ExprKind::Become(..) => ExprPrecedence::Jump,
Expand All @@ -1726,6 +1730,7 @@ impl Expr<'_> {
| ExprKind::Block(..)
| ExprKind::Call(..)
| ExprKind::ConstBlock(_)
| ExprKind::Continue(..)
| ExprKind::Field(..)
| ExprKind::If(..)
| ExprKind::Index(..)
Expand All @@ -1742,7 +1747,7 @@ impl Expr<'_> {
| ExprKind::Type(..)
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,

ExprKind::DropTemps(ref expr, ..) => expr.precedence(),
ExprKind::DropTemps(expr, ..) => expr.precedence(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/ui-fulldeps/pprust-parenthesis-insertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ static EXPRS: &[&str] = &[
// These mean different things.
"return - 2",
"(return) - 2",
// Closures and jumps have equal precedence.
"|| return break 2",
"return break || 2",
// Closures with a return type have especially high precedence.
"|| -> T { x } + 1",
"(|| { x }) + 1",
// These mean different things.
"if let _ = true && false {}",
"if let _ = (true && false) {}",
Expand Down
Loading