Skip to content

Commit 273447c

Browse files
committed
Rename the 2 unambiguous precedence levels to PREC_UNAMBIGUOUS
1 parent 8cfd4b1 commit 273447c

File tree

8 files changed

+44
-47
lines changed

8 files changed

+44
-47
lines changed

compiler/rustc_ast/src/util/parser.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ pub const PREC_JUMP: i8 = -30;
233233
pub const PREC_RANGE: i8 = -10;
234234
// The range 2..=14 is reserved for AssocOp binary operator precedences.
235235
pub const PREC_PREFIX: i8 = 50;
236-
pub const PREC_POSTFIX: i8 = 60;
237-
pub const PREC_PAREN: i8 = 60;
236+
pub const PREC_UNAMBIGUOUS: i8 = 60;
238237
pub const PREC_FORCE_PAREN: i8 = 100;
239238

240239
#[derive(Debug, Clone, Copy)]
@@ -325,37 +324,35 @@ impl ExprPrecedence {
325324
| ExprPrecedence::Let
326325
| ExprPrecedence::Unary => PREC_PREFIX,
327326

328-
// Unary, postfix
329-
ExprPrecedence::Await
327+
// Never need parens
328+
ExprPrecedence::Array
329+
| ExprPrecedence::Await
330+
| ExprPrecedence::Block
330331
| ExprPrecedence::Call
331-
| ExprPrecedence::MethodCall
332+
| ExprPrecedence::ConstBlock
332333
| ExprPrecedence::Field
334+
| ExprPrecedence::ForLoop
335+
| ExprPrecedence::FormatArgs
336+
| ExprPrecedence::Gen
337+
| ExprPrecedence::If
333338
| ExprPrecedence::Index
334-
| ExprPrecedence::Try
335339
| ExprPrecedence::InlineAsm
340+
| ExprPrecedence::Lit
341+
| ExprPrecedence::Loop
336342
| ExprPrecedence::Mac
337-
| ExprPrecedence::FormatArgs
343+
| ExprPrecedence::Match
344+
| ExprPrecedence::MethodCall
338345
| ExprPrecedence::OffsetOf
339-
| ExprPrecedence::PostfixMatch => PREC_POSTFIX,
340-
341-
// Never need parens
342-
ExprPrecedence::Array
346+
| ExprPrecedence::Paren
347+
| ExprPrecedence::Path
348+
| ExprPrecedence::PostfixMatch
343349
| ExprPrecedence::Repeat
350+
| ExprPrecedence::Struct
351+
| ExprPrecedence::Try
352+
| ExprPrecedence::TryBlock
344353
| ExprPrecedence::Tup
345-
| ExprPrecedence::Lit
346-
| ExprPrecedence::Path
347-
| ExprPrecedence::Paren
348-
| ExprPrecedence::If
349354
| ExprPrecedence::While
350-
| ExprPrecedence::ForLoop
351-
| ExprPrecedence::Loop
352-
| ExprPrecedence::Match
353-
| ExprPrecedence::ConstBlock
354-
| ExprPrecedence::Block
355-
| ExprPrecedence::TryBlock
356-
| ExprPrecedence::Gen
357-
| ExprPrecedence::Struct
358-
| ExprPrecedence::Err => PREC_PAREN,
355+
| ExprPrecedence::Err => PREC_UNAMBIGUOUS,
359356
}
360357
}
361358
}

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'a> State<'a> {
217217
fn print_expr_call(&mut self, func: &ast::Expr, args: &[P<ast::Expr>], fixup: FixupContext) {
218218
let prec = match func.kind {
219219
ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
220-
_ => parser::PREC_POSTFIX,
220+
_ => parser::PREC_UNAMBIGUOUS,
221221
};
222222

223223
// Independent of parenthesization related to precedence, we must
@@ -257,7 +257,7 @@ impl<'a> State<'a> {
257257
// boundaries, `$receiver.method()` can be parsed back as a statement
258258
// containing an expression if and only if `$receiver` can be parsed as
259259
// a statement containing an expression.
260-
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX, fixup);
260+
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS, fixup);
261261

262262
self.word(".");
263263
self.print_ident(segment.ident);
@@ -489,7 +489,7 @@ impl<'a> State<'a> {
489489
self.space();
490490
}
491491
MatchKind::Postfix => {
492-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
492+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
493493
self.word_nbsp(".match");
494494
}
495495
}
@@ -549,7 +549,7 @@ impl<'a> State<'a> {
549549
self.print_block_with_attrs(blk, attrs);
550550
}
551551
ast::ExprKind::Await(expr, _) => {
552-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
552+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
553553
self.word(".await");
554554
}
555555
ast::ExprKind::Assign(lhs, rhs, _) => {
@@ -568,14 +568,14 @@ impl<'a> State<'a> {
568568
self.print_expr_maybe_paren(rhs, prec, fixup.subsequent_subexpression());
569569
}
570570
ast::ExprKind::Field(expr, ident) => {
571-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX, fixup);
571+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS, fixup);
572572
self.word(".");
573573
self.print_ident(*ident);
574574
}
575575
ast::ExprKind::Index(expr, index, _) => {
576576
self.print_expr_maybe_paren(
577577
expr,
578-
parser::PREC_POSTFIX,
578+
parser::PREC_UNAMBIGUOUS,
579579
fixup.leftmost_subexpression(),
580580
);
581581
self.word("[");
@@ -713,7 +713,7 @@ impl<'a> State<'a> {
713713
}
714714
}
715715
ast::ExprKind::Try(e) => {
716-
self.print_expr_maybe_paren(e, parser::PREC_POSTFIX, fixup);
716+
self.print_expr_maybe_paren(e, parser::PREC_UNAMBIGUOUS, fixup);
717717
self.word("?")
718718
}
719719
ast::ExprKind::TryBlock(blk) => {

compiler/rustc_hir_pretty/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ impl<'a> State<'a> {
11201120
fn print_expr_call(&mut self, func: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
11211121
let prec = match func.kind {
11221122
hir::ExprKind::Field(..) => parser::PREC_FORCE_PAREN,
1123-
_ => parser::PREC_POSTFIX,
1123+
_ => parser::PREC_UNAMBIGUOUS,
11241124
};
11251125

11261126
self.print_expr_maybe_paren(func, prec);
@@ -1134,7 +1134,7 @@ impl<'a> State<'a> {
11341134
args: &[hir::Expr<'_>],
11351135
) {
11361136
let base_args = args;
1137-
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX);
1137+
self.print_expr_maybe_paren(receiver, parser::PREC_UNAMBIGUOUS);
11381138
self.word(".");
11391139
self.print_ident(segment.ident);
11401140

@@ -1478,12 +1478,12 @@ impl<'a> State<'a> {
14781478
self.print_expr_maybe_paren(rhs, prec);
14791479
}
14801480
hir::ExprKind::Field(expr, ident) => {
1481-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1481+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS);
14821482
self.word(".");
14831483
self.print_ident(ident);
14841484
}
14851485
hir::ExprKind::Index(expr, index, _) => {
1486-
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
1486+
self.print_expr_maybe_paren(expr, parser::PREC_UNAMBIGUOUS);
14871487
self.word("[");
14881488
self.print_expr(index);
14891489
self.word("]");

compiler/rustc_hir_typeck/src/callee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::method::MethodCallee;
33
use super::{Expectation, FnCtxt, TupleArgumentsFlag};
44

55
use crate::errors;
6-
use rustc_ast::util::parser::PREC_POSTFIX;
6+
use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
77
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, StashKey};
88
use rustc_hir::def::{self, CtorKind, Namespace, Res};
99
use rustc_hir::def_id::DefId;
@@ -656,7 +656,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
656656
};
657657

658658
if let Ok(rest_snippet) = rest_snippet {
659-
let sugg = if callee_expr.precedence().order() >= PREC_POSTFIX {
659+
let sugg = if callee_expr.precedence().order() >= PREC_UNAMBIGUOUS {
660660
vec![
661661
(up_to_rcvr_span, "".to_string()),
662662
(rest_span, format!(".{}({rest_snippet}", segment.ident)),

compiler/rustc_hir_typeck/src/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
946946

947947
fn lossy_provenance_ptr2int_lint(&self, fcx: &FnCtxt<'a, 'tcx>, t_c: ty::cast::IntTy) {
948948
let expr_prec = self.expr.precedence().order();
949-
let needs_parens = expr_prec < rustc_ast::util::parser::PREC_POSTFIX;
949+
let needs_parens = expr_prec < rustc_ast::util::parser::PREC_UNAMBIGUOUS;
950950

951951
let needs_cast = !matches!(t_c, ty::cast::IntTy::U(ty::UintTy::Usize));
952952
let cast_span = self.expr_span.shrink_to_hi().to(self.cast_span);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
99
use core::cmp::min;
1010
use core::iter;
1111
use hir::def_id::LocalDefId;
12-
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
12+
use rustc_ast::util::parser::{ExprPrecedence, PREC_UNAMBIGUOUS};
1313
use rustc_data_structures::packed::Pu128;
1414
use rustc_errors::{Applicability, Diag, MultiSpan};
1515
use rustc_hir as hir;
@@ -1287,7 +1287,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12871287
{
12881288
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
12891289

1290-
let mut sugg = if expr.precedence().order() >= PREC_POSTFIX {
1290+
let mut sugg = if expr.precedence().order() >= PREC_UNAMBIGUOUS {
12911291
vec![(span.shrink_to_hi(), ".into()".to_owned())]
12921292
} else {
12931293
vec![
@@ -2826,7 +2826,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28262826
"change the type of the numeric literal from `{checked_ty}` to `{expected_ty}`",
28272827
);
28282828

2829-
let close_paren = if expr.precedence().order() < PREC_POSTFIX {
2829+
let close_paren = if expr.precedence().order() < PREC_UNAMBIGUOUS {
28302830
sugg.push((expr.span.shrink_to_lo(), "(".to_string()));
28312831
")"
28322832
} else {
@@ -2851,7 +2851,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28512851
let len = src.trim_end_matches(&checked_ty.to_string()).len();
28522852
expr.span.with_lo(expr.span.lo() + BytePos(len as u32))
28532853
},
2854-
if expr.precedence().order() < PREC_POSTFIX {
2854+
if expr.precedence().order() < PREC_UNAMBIGUOUS {
28552855
// Readd `)`
28562856
format!("{expected_ty})")
28572857
} else {

src/tools/clippy/clippy_lints/src/dereference.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::{
66
expr_use_ctxt, get_parent_expr, is_block_like, is_lint_allowed, path_to_local, DefinedTy, ExprUseNode,
77
};
88
use core::mem;
9-
use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
9+
use rustc_ast::util::parser::{PREC_UNAMBIGUOUS, PREC_PREFIX};
1010
use rustc_data_structures::fx::FxIndexMap;
1111
use rustc_errors::Applicability;
1212
use rustc_hir::intravisit::{walk_ty, Visitor};
@@ -1013,7 +1013,7 @@ fn report<'tcx>(
10131013
let (precedence, calls_field) = match cx.tcx.parent_hir_node(data.first_expr.hir_id) {
10141014
Node::Expr(e) => match e.kind {
10151015
ExprKind::Call(callee, _) if callee.hir_id != data.first_expr.hir_id => (0, false),
1016-
ExprKind::Call(..) => (PREC_POSTFIX, matches!(expr.kind, ExprKind::Field(..))),
1016+
ExprKind::Call(..) => (PREC_UNAMBIGUOUS, matches!(expr.kind, ExprKind::Field(..))),
10171017
_ => (e.precedence().order(), false),
10181018
},
10191019
_ => (0, false),
@@ -1160,7 +1160,7 @@ impl<'tcx> Dereferencing<'tcx> {
11601160
},
11611161
Some(parent) if !parent.span.from_expansion() => {
11621162
// Double reference might be needed at this point.
1163-
if parent.precedence().order() == PREC_POSTFIX {
1163+
if parent.precedence().order() == PREC_UNAMBIGUOUS {
11641164
// Parentheses would be needed here, don't lint.
11651165
*outer_pat = None;
11661166
} else {

src/tools/clippy/clippy_lints/src/matches/manual_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::{
77
can_move_expr_to_closure, is_else_clause, is_lint_allowed, is_res_lang_ctor, path_res, path_to_local_id,
88
peel_blocks, peel_hir_expr_refs, peel_hir_expr_while, CaptureKind,
99
};
10-
use rustc_ast::util::parser::PREC_POSTFIX;
10+
use rustc_ast::util::parser::PREC_UNAMBIGUOUS;
1111
use rustc_errors::Applicability;
1212
use rustc_hir::def::Res;
1313
use rustc_hir::LangItem::{OptionNone, OptionSome};
@@ -117,7 +117,7 @@ where
117117
// it's being passed by value.
118118
let scrutinee = peel_hir_expr_refs(scrutinee).0;
119119
let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app);
120-
let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && scrutinee.precedence().order() < PREC_POSTFIX {
120+
let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && scrutinee.precedence().order() < PREC_UNAMBIGUOUS {
121121
format!("({scrutinee_str})")
122122
} else {
123123
scrutinee_str.into()

0 commit comments

Comments
 (0)