1
Fork 0

Rename the 2 unambiguous precedence levels to PREC_UNAMBIGUOUS

This commit is contained in:
David Tolnay 2024-06-23 18:30:13 -07:00
parent 8cfd4b180b
commit 273447cec7
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
8 changed files with 48 additions and 51 deletions

View file

@ -233,8 +233,7 @@ pub const PREC_JUMP: i8 = -30;
pub const PREC_RANGE: i8 = -10; pub const PREC_RANGE: i8 = -10;
// The range 2..=14 is reserved for AssocOp binary operator precedences. // The range 2..=14 is reserved for AssocOp binary operator precedences.
pub const PREC_PREFIX: i8 = 50; pub const PREC_PREFIX: i8 = 50;
pub const PREC_POSTFIX: i8 = 60; pub const PREC_UNAMBIGUOUS: i8 = 60;
pub const PREC_PAREN: i8 = 60;
pub const PREC_FORCE_PAREN: i8 = 100; pub const PREC_FORCE_PAREN: i8 = 100;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -325,37 +324,35 @@ impl ExprPrecedence {
| ExprPrecedence::Let | ExprPrecedence::Let
| ExprPrecedence::Unary => PREC_PREFIX, | ExprPrecedence::Unary => PREC_PREFIX,
// Unary, postfix
ExprPrecedence::Await
| ExprPrecedence::Call
| ExprPrecedence::MethodCall
| ExprPrecedence::Field
| ExprPrecedence::Index
| ExprPrecedence::Try
| ExprPrecedence::InlineAsm
| ExprPrecedence::Mac
| ExprPrecedence::FormatArgs
| ExprPrecedence::OffsetOf
| ExprPrecedence::PostfixMatch => PREC_POSTFIX,
// Never need parens // Never need parens
ExprPrecedence::Array ExprPrecedence::Array
| ExprPrecedence::Repeat | ExprPrecedence::Await
| ExprPrecedence::Tup
| ExprPrecedence::Lit
| ExprPrecedence::Path
| ExprPrecedence::Paren
| ExprPrecedence::If
| ExprPrecedence::While
| ExprPrecedence::ForLoop
| ExprPrecedence::Loop
| ExprPrecedence::Match
| ExprPrecedence::ConstBlock
| ExprPrecedence::Block | ExprPrecedence::Block
| ExprPrecedence::TryBlock | ExprPrecedence::Call
| ExprPrecedence::ConstBlock
| ExprPrecedence::Field
| ExprPrecedence::ForLoop
| ExprPrecedence::FormatArgs
| ExprPrecedence::Gen | ExprPrecedence::Gen
| ExprPrecedence::If
| ExprPrecedence::Index
| ExprPrecedence::InlineAsm
| ExprPrecedence::Lit
| ExprPrecedence::Loop
| ExprPrecedence::Mac
| ExprPrecedence::Match
| ExprPrecedence::MethodCall
| ExprPrecedence::OffsetOf
| ExprPrecedence::Paren
| ExprPrecedence::Path
| ExprPrecedence::PostfixMatch
| ExprPrecedence::Repeat
| ExprPrecedence::Struct | ExprPrecedence::Struct
| ExprPrecedence::Err => PREC_PAREN, | ExprPrecedence::Try
| ExprPrecedence::TryBlock
| ExprPrecedence::Tup
| ExprPrecedence::While
| ExprPrecedence::Err => PREC_UNAMBIGUOUS,
} }
} }
} }

View file

@ -217,7 +217,7 @@ impl<'a> State<'a> {
fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) { fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) {
let prec = match func.kind { let prec = match func.kind {
ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN, ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
_ => parser::PREC_POSTFIX, _ => parser::PREC_UNAMBIGUOUS,
}; };
// Independent of parenthesization related to precedence, we must // Independent of parenthesization related to precedence, we must
@ -257,7 +257,7 @@ impl<'a> State<'a> {
// boundaries, `$receiver.method()` can be parsed back as a statement // boundaries, `$receiver.method()` can be parsed back as a statement
// containing an expression if and only if `$receiver` can be parsed as // containing an expression if and only if `$receiver` can be parsed as
// a statement containing an expression. // a statement containing an expression.
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX, fixup); self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS, fixup);
self.word("."); self.word(".");
self.print_ident(segment.ident); self.print_ident(segment.ident);
@ -489,7 +489,7 @@ impl<'a> State<'a> {
self.space(); self.space();
} }
MatchKind::Postfix => { MatchKind::Postfix => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup); self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
self.word_nbsp(".match"); self.word_nbsp(".match");
} }
} }
@ -549,7 +549,7 @@ impl<'a> State<'a> {
self.print_block_with_attrs(blk, attrs); self.print_block_with_attrs(blk, attrs);
} }
ast::ExprKind::Await(expr, _) => { ast::ExprKind::Await(expr, _) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup); self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
self.word(".await"); self.word(".await");
} }
ast::ExprKind::Assign(lhs, rhs, _) => { ast::ExprKind::Assign(lhs, rhs, _) => {
@ -568,14 +568,14 @@ impl<'a> State<'a> {
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression()); self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression());
} }
ast::ExprKind::Field(expr, ident) => { ast::ExprKind::Field(expr, ident) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup); self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
self.word("."); self.word(".");
self.print_ident(*ident); self.print_ident(*ident);
} }
ast::ExprKind::Index(expr, index, _) => { ast::ExprKind::Index(expr, index, _) => {
self.print_expr_maybe_paren( self.print_expr_maybe_paren(
expr, expr,
parser::PREC_POSTFIX, parser::PREC_UNAMBIGUOUS,
fixup.leftmost_subexpression(), fixup.leftmost_subexpression(),
); );
self.word("["); self.word("[");
@ -713,7 +713,7 @@ impl<'a> State<'a> {
} }
} }
ast::ExprKind::Try(e) => { ast::ExprKind::Try(e) => {
self.print_expr_maybe_paren(e, parser::PREC_POSTFIX, fixup); self.print_expr_maybe_paren(e, parser::PREC_UNAMBIGUOUS, fixup);
self.word("?") self.word("?")
} }
ast::ExprKind::TryBlock(blk) => { ast::ExprKind::TryBlock(blk) => {

View file

@ -1120,7 +1120,7 @@ impl<'a> State<'a> {
fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) { fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let prec = match func.kind { let prec = match func.kind {
hir::ExprKind::Field(..) => parser::PREC_FORCE_PAREN, hir::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
_ => parser::PREC_POSTFIX, _ => parser::PREC_UNAMBIGUOUS,
}; };
self.print_expr_maybe_paren(func, prec); self.print_expr_maybe_paren(func, prec);
@ -1134,7 +1134,7 @@ impl<'a> State<'a> {
args: &[hir::Expr<'_>], args: &[hir::Expr<'_>],
) { ) {
let base_args = args; let base_args = args;
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX); self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS);
self.word("."); self.word(".");
self.print_ident(segment.ident); self.print_ident(segment.ident);
@ -1478,12 +1478,12 @@ impl<'a> State<'a> {
self.print_expr_maybe_paren(rhs, prec); self.print_expr_maybe_paren(rhs, prec);
} }
hir::ExprKind::Field(expr, ident) => { hir::ExprKind::Field(expr, ident) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS);
self.word("."); self.word(".");
self.print_ident(ident); self.print_ident(ident);
} }
hir::ExprKind::Index(expr, index, _) => { hir::ExprKind::Index(expr, index, _) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS);
self.word("["); self.word("[");
self.print_expr(index); self.print_expr(index);
self.word("]"); self.word("]");

View file

@ -3,7 +3,7 @@ use super::method::MethodCallee;
use super::{Expectation, FnCtxt, TupleArgumentsFlag}; use super::{Expectation, FnCtxt, TupleArgumentsFlag};
use crate::errors; use crate::errors;
use rustc_ast::util::parser::PREC_POSTFIX; use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey}; use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
use rustc_hir::def::{self, CtorKind, Namespace, Res}; use rustc_hir::def::{self, CtorKind, Namespace, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
@ -656,7 +656,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
if let Ok(rest_snippet) = rest_snippet { if let Ok(rest_snippet) = rest_snippet {
let sugg = if callee_expr.precedence().order() >= PREC_POSTFIX { let sugg = if callee_expr.precedence().order() >= PREC_UNAMBIGUOUS {
vec![ vec![
(up_to_rcvr_span, "".to_string()), (up_to_rcvr_span, "".to_string()),
(rest_span, format!(".{}({rest_snippet}", segment.ident)), (rest_span, format!(".{}({rest_snippet}", segment.ident)),

View file

@ -946,7 +946,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) { fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
let expr_prec = self.expr.precedence().order(); let expr_prec = self.expr.precedence().order();
let needs_parens = expr_prec < rustc_ast::util::parser::PREC_POSTFIX; let needs_parens = expr_prec < rustc_ast::util::parser::PREC_UNAMBIGUOUS;
let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize)); let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span); let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);

View file

@ -9,7 +9,7 @@ use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
use core::cmp::min; use core::cmp::min;
use core::iter; use core::iter;
use hir::def_id::LocalDefId; use hir::def_id::LocalDefId;
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX}; use rustc_ast::util::parser::{ExprPrecedence, PREC_UNAMBIGUOUS};
use rustc_data_structures::packed::Pu128; use rustc_data_structures::packed::Pu128;
use rustc_errors::{Applicability, Diag, MultiSpan}; use rustc_errors::{Applicability, Diag, MultiSpan};
use rustc_hir as hir; use rustc_hir as hir;
@ -1287,7 +1287,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{ {
let span = expr.span.find_oldest_ancestor_in_same_ctxt(); let span = expr.span.find_oldest_ancestor_in_same_ctxt();
let mut sugg = if expr.precedence().order() >= PREC_POSTFIX { let mut sugg = if expr.precedence().order() >= PREC_UNAMBIGUOUS {
vec![(span.shrink_to_hi(), ".into()".to_owned())] vec![(span.shrink_to_hi(), ".into()".to_owned())]
} else { } else {
vec![ vec![
@ -2826,7 +2826,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"change the type of the numeric literal from `{checked_ty}` to `{expected_ty}`", "change the type of the numeric literal from `{checked_ty}` to `{expected_ty}`",
); );
let close_paren = if expr.precedence().order() < PREC_POSTFIX { let close_paren = if expr.precedence().order() < PREC_UNAMBIGUOUS {
sugg.push((expr.span.shrink_to_lo(), "(".to_string())); sugg.push((expr.span.shrink_to_lo(), "(".to_string()));
")" ")"
} else { } else {
@ -2851,7 +2851,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let len = src.trim_end_matches(&checked_ty.to_string()).len(); let len = src.trim_end_matches(&checked_ty.to_string()).len();
expr.span.with_lo(expr.span.lo() + BytePos(len as u32)) expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
}, },
if expr.precedence().order() < PREC_POSTFIX { if expr.precedence().order() < PREC_UNAMBIGUOUS {
// Readd `)` // Readd `)`
format!("{expected_ty})") format!("{expected_ty})")
} else { } else {

View file

@ -6,7 +6,7 @@ use clippy_utils::{
expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode, expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode,
}; };
use core::mem; use core::mem;
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX}; use rustc_ast::util::parser::{PREC_UNAMBIGUOUS, PREC_PREFIX};
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_ty, Visitor}; use rustc_hir::intravisit::{walk_ty, Visitor};
@ -1013,7 +1013,7 @@ fn report<'tcx>(
let (precedence, calls_field) = match cx.tcx.parent_hir_node(data.first_expr.hir_id) { let (precedence, calls_field) = match cx.tcx.parent_hir_node(data.first_expr.hir_id) {
Node::Expr(e) => match e.kind { Node::Expr(e) => match e.kind {
ExprKind::Call(callee, _) if callee.hir_id != data.first_expr.hir_id => (0, false), ExprKind::Call(callee, _) if callee.hir_id != data.first_expr.hir_id => (0, false),
ExprKind::Call(..) => (PREC_POSTFIX, matches!(expr.kind, ExprKind::Field(..))), ExprKind::Call(..) => (PREC_UNAMBIGUOUS, matches!(expr.kind, ExprKind::Field(..))),
_ => (e.precedence().order(), false), _ => (e.precedence().order(), false),
}, },
_ => (0, false), _ => (0, false),
@ -1160,7 +1160,7 @@ impl<'tcx> Dereferencing<'tcx> {
}, },
Some(parent) if !parent.span.from_expansion() => { Some(parent) if !parent.span.from_expansion() => {
// Double reference might be needed at this point. // Double reference might be needed at this point.
if parent.precedence().order() == PREC_POSTFIX { if parent.precedence().order() == PREC_UNAMBIGUOUS {
// Parentheses would be needed here, don't lint. // Parentheses would be needed here, don't lint.
*outer_pat = None; *outer_pat = None;
} else { } else {

View file

@ -7,7 +7,7 @@ use clippy_utils::{
can_move_expr_to_closure, is_else_clause, is_lint_allowed, is_res_lang_ctor, path_res, path_to_local_id, can_move_expr_to_closure, is_else_clause, is_lint_allowed, is_res_lang_ctor, path_res, path_to_local_id,
peel_blocks, peel_hir_expr_refs, peel_hir_expr_while, CaptureKind, peel_blocks, peel_hir_expr_refs, peel_hir_expr_while, CaptureKind,
}; };
use rustc_ast::util::parser::PREC_POSTFIX; use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::LangItem::{OptionNone, OptionSome}; use rustc_hir::LangItem::{OptionNone, OptionSome};
@ -117,7 +117,7 @@ where
// it's being passed by value. // it's being passed by value.
let scrutinee = peel_hir_expr_refs(scrutinee).0; let scrutinee = peel_hir_expr_refs(scrutinee).0;
let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app); let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app);
let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && scrutinee.precedence().order() < PREC_POSTFIX { let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && scrutinee.precedence().order() < PREC_UNAMBIGUOUS {
format!("({scrutinee_str})") format!("({scrutinee_str})")
} else { } else {
scrutinee_str.into() scrutinee_str.into()