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

Rollup of 13 pull requests #133760

Merged
merged 32 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c02032c
Eliminate precedence arithmetic from rustc_ast_pretty
dtolnay Nov 28, 2024
34a65f2
Eliminate precedence arithmetic from rustc_hir_pretty
dtolnay Nov 28, 2024
ca8f474
Eliminate PREC_FORCE_PAREN
dtolnay Nov 28, 2024
539c863
Eliminate precedence arithmetic from rustc_parse
dtolnay Nov 28, 2024
7ced18f
Eliminate magic numbers from expression precedence
dtolnay Nov 28, 2024
51ea7c1
rustdoc-json: Add tests for `static`s
aDotInTheVoid Dec 1, 2024
f33dba0
rustdoc-json: Include safety of `static`s
aDotInTheVoid Dec 1, 2024
b5d73fc
rustdoc-json: Add test for `impl Trait for dyn Trait`
aDotInTheVoid Dec 1, 2024
2f17ea0
Remove `//@ compare-output-lines-by-subset`
jyn514 Dec 2, 2024
267dcb2
Add pretty-printer parenthesis insertion test
dtolnay Dec 1, 2024
99c2322
Add `needs-target-has-atomic` directive
jieyouxu Dec 2, 2024
1a976e0
Re-add myself to rotation
oli-obk Dec 2, 2024
b1ff3c8
Fix docs for '<[T]>::as_array';
bjoernager Dec 2, 2024
c8e0724
Fix typos on tests/ui/README.md
HypheX Dec 2, 2024
b863c0d
Remove static HashSet for default IDs list
GuillaumeGomez Dec 2, 2024
8b90e70
mir validator: don't store mir phase
lcnr Dec 2, 2024
cfee10c
remove outdated comment
lcnr Dec 2, 2024
e089bea
remove `Ty::is_copy_modulo_regions`
lcnr Dec 2, 2024
42174f0
`impl Default for EarlyDiagCtxt`
jyn514 Dec 2, 2024
7dd0c83
Rollup merge of #133603 - dtolnay:precedence, r=lcnr
GuillaumeGomez Dec 2, 2024
08f1f28
Rollup merge of #133715 - aDotInTheVoid:rdj-static, r=GuillaumeGomez
GuillaumeGomez Dec 2, 2024
7fdbde0
Rollup merge of #133721 - aDotInTheVoid:rdj-dyn-link, r=GuillaumeGomez
GuillaumeGomez Dec 2, 2024
15fda1e
Rollup merge of #133725 - jyn514:remove-compare-output-subset, r=jiey…
GuillaumeGomez Dec 2, 2024
8f5a801
Rollup merge of #133730 - dtolnay:parentest, r=compiler-errors
GuillaumeGomez Dec 2, 2024
80e0448
Rollup merge of #133736 - jieyouxu:needs-target-has-atomic, r=compile…
GuillaumeGomez Dec 2, 2024
97cc31a
Rollup merge of #133739 - oli-obk:push-zymwlvwttxnr, r=oli-obk
GuillaumeGomez Dec 2, 2024
1638a55
Rollup merge of #133743 - bjoernager:slice-as-array, r=joboet
GuillaumeGomez Dec 2, 2024
ed08803
Rollup merge of #133744 - HypheX:typo-fix, r=fmease
GuillaumeGomez Dec 2, 2024
7145e4e
Rollup merge of #133745 - GuillaumeGomez:default-ids-match, r=notriddle
GuillaumeGomez Dec 2, 2024
7c92266
Rollup merge of #133749 - lcnr:validator-mir_phase, r=compiler-errors
GuillaumeGomez Dec 2, 2024
4c68112
Rollup merge of #133751 - lcnr:no-trait-solving-on-type, r=compiler-e…
GuillaumeGomez Dec 2, 2024
586591f
Rollup merge of #133757 - jyn514:error-handler, r=compiler-errors
GuillaumeGomez Dec 2, 2024
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
22 changes: 10 additions & 12 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ pub use crate::format::*;
use crate::ptr::P;
use crate::token::{self, CommentKind, Delimiter};
use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream};
use crate::util::parser::{
AssocOp, PREC_CLOSURE, PREC_JUMP, PREC_PREFIX, PREC_RANGE, PREC_UNAMBIGUOUS,
};
use crate::util::parser::{AssocOp, ExprPrecedence};

/// A "Label" is an identifier of some point in sources,
/// e.g. in the following code:
Expand Down Expand Up @@ -1317,29 +1315,29 @@ impl Expr {
Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
}

pub fn precedence(&self) -> i8 {
pub fn precedence(&self) -> ExprPrecedence {
match self.kind {
ExprKind::Closure(..) => PREC_CLOSURE,
ExprKind::Closure(..) => ExprPrecedence::Closure,

ExprKind::Break(..)
| ExprKind::Continue(..)
| ExprKind::Ret(..)
| ExprKind::Yield(..)
| ExprKind::Yeet(..)
| ExprKind::Become(..) => PREC_JUMP,
| ExprKind::Become(..) => ExprPrecedence::Jump,

// `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to
// parse, instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence
// ensures that `pprust` will add parentheses in the right places to get the desired
// parse.
ExprKind::Range(..) => PREC_RANGE,
ExprKind::Range(..) => ExprPrecedence::Range,

// Binop-like expr kinds, handled by `AssocOp`.
ExprKind::Binary(op, ..) => AssocOp::from_ast_binop(op.node).precedence() as i8,
ExprKind::Cast(..) => AssocOp::As.precedence() as i8,
ExprKind::Binary(op, ..) => AssocOp::from_ast_binop(op.node).precedence(),
ExprKind::Cast(..) => ExprPrecedence::Cast,

ExprKind::Assign(..) |
ExprKind::AssignOp(..) => AssocOp::Assign.precedence() as i8,
ExprKind::AssignOp(..) => ExprPrecedence::Assign,

// Unary, prefix
ExprKind::AddrOf(..)
Expand All @@ -1348,7 +1346,7 @@ impl Expr {
// need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
// but we need to print `(let _ = a) < b` as-is with parens.
| ExprKind::Let(..)
| ExprKind::Unary(..) => PREC_PREFIX,
| ExprKind::Unary(..) => ExprPrecedence::Prefix,

// Never need parens
ExprKind::Array(_)
Expand Down Expand Up @@ -1381,7 +1379,7 @@ impl Expr {
| ExprKind::Underscore
| ExprKind::While(..)
| ExprKind::Err(_)
| ExprKind::Dummy => PREC_UNAMBIGUOUS,
| ExprKind::Dummy => ExprPrecedence::Unambiguous,
}
}

Expand Down
75 changes: 51 additions & 24 deletions compiler/rustc_ast/src/util/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ impl AssocOp {
}

/// Gets the precedence of this operator
pub fn precedence(&self) -> usize {
pub fn precedence(&self) -> ExprPrecedence {
use AssocOp::*;
match *self {
As => 14,
Multiply | Divide | Modulus => 13,
Add | Subtract => 12,
ShiftLeft | ShiftRight => 11,
BitAnd => 10,
BitXor => 9,
BitOr => 8,
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => 7,
LAnd => 6,
LOr => 5,
DotDot | DotDotEq => 4,
Assign | AssignOp(_) => 2,
As => ExprPrecedence::Cast,
Multiply | Divide | Modulus => ExprPrecedence::Product,
Add | Subtract => ExprPrecedence::Sum,
ShiftLeft | ShiftRight => ExprPrecedence::Shift,
BitAnd => ExprPrecedence::BitAnd,
BitXor => ExprPrecedence::BitXor,
BitOr => ExprPrecedence::BitOr,
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => ExprPrecedence::Compare,
LAnd => ExprPrecedence::LAnd,
LOr => ExprPrecedence::LOr,
DotDot | DotDotEq => ExprPrecedence::Range,
Assign | AssignOp(_) => ExprPrecedence::Assign,
}
}

Expand Down Expand Up @@ -229,26 +229,53 @@ impl AssocOp {
}
}

pub const PREC_CLOSURE: i8 = -40;
pub const PREC_JUMP: i8 = -30;
pub const PREC_RANGE: i8 = -10;
// The range 2..=14 is reserved for AssocOp binary operator precedences.
pub const PREC_PREFIX: i8 = 50;
pub const PREC_UNAMBIGUOUS: i8 = 60;
pub const PREC_FORCE_PAREN: i8 = 100;
#[derive(Clone, Copy, PartialEq, PartialOrd)]
pub enum ExprPrecedence {
Closure,
// return, break, yield
Jump,
// = += -= *= /= %= &= |= ^= <<= >>=
Assign,
// .. ..=
Range,
// ||
LOr,
// &&
LAnd,
// == != < > <= >=
Compare,
// |
BitOr,
// ^
BitXor,
// &
BitAnd,
// << >>
Shift,
// + -
Sum,
// * / %
Product,
// as
Cast,
// unary - * ! & &mut
Prefix,
// paths, loops, function calls, array indexing, field expressions, method calls
Unambiguous,
}

/// In `let p = e`, operators with precedence `<=` this one requires parentheses in `e`.
pub fn prec_let_scrutinee_needs_par() -> usize {
AssocOp::LAnd.precedence()
pub fn prec_let_scrutinee_needs_par() -> ExprPrecedence {
ExprPrecedence::LAnd
}

/// Suppose we have `let _ = e` and the `order` of `e`.
/// Is the `order` such that `e` in `let _ = e` needs parentheses when it is on the RHS?
///
/// Conversely, suppose that we have `(let _ = a) OP b` and `order` is that of `OP`.
/// Can we print this as `let _ = a OP b`?
pub fn needs_par_as_let_scrutinee(order: i8) -> bool {
order <= prec_let_scrutinee_needs_par() as i8
pub fn needs_par_as_let_scrutinee(order: ExprPrecedence) -> bool {
order <= prec_let_scrutinee_needs_par()
}

/// Expressions that syntactically contain an "exterior" struct literal i.e., not surrounded by any
Expand Down
Loading
Loading