Skip to content

Commit cb08599

Browse files
committed
Auto merge of #137710 - matthiaskrgr:rollup-3vmxxu9, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #136542 ([`compiletest`-related cleanups 4/7] Make the distinction between root build directory vs test suite specific build directory in compiletest less confusing) - #136579 (Fix UB in ThinVec::flat_map_in_place) - #136688 (require trait impls to have matching const stabilities as the traits) - #136846 (Make `AssocOp` more like `ExprKind`) - #137304 (add `IntoBounds::intersect` and `RangeBounds::is_empty`) - #137455 (Reuse machinery from `tail_expr_drop_order` for `if_let_rescope`) - #137480 (Return unexpected termination error instead of panicing in `Thread::join`) - #137694 (Spruce up `AttributeKind` docs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents e6059f5 + 89a202d commit cb08599

File tree

43 files changed

+1067
-644
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1067
-644
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,7 @@ dependencies = [
39163916
"rustc_target",
39173917
"rustc_trait_selection",
39183918
"rustc_type_ir",
3919+
"smallvec",
39193920
"tracing",
39203921
"unicode-security",
39213922
]

compiler/rustc_ast/src/ast.rs

+41-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use crate::format::*;
3939
use crate::ptr::P;
4040
use crate::token::{self, CommentKind, Delimiter};
4141
use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream};
42-
use crate::util::parser::{AssocOp, ExprPrecedence};
42+
use crate::util::parser::{ExprPrecedence, Fixity};
4343

4444
/// A "Label" is an identifier of some point in sources,
4545
/// e.g. in the following code:
@@ -937,8 +937,37 @@ impl BinOpKind {
937937
matches!(self, BinOpKind::And | BinOpKind::Or)
938938
}
939939

940+
pub fn precedence(&self) -> ExprPrecedence {
941+
use BinOpKind::*;
942+
match *self {
943+
Mul | Div | Rem => ExprPrecedence::Product,
944+
Add | Sub => ExprPrecedence::Sum,
945+
Shl | Shr => ExprPrecedence::Shift,
946+
BitAnd => ExprPrecedence::BitAnd,
947+
BitXor => ExprPrecedence::BitXor,
948+
BitOr => ExprPrecedence::BitOr,
949+
Lt | Gt | Le | Ge | Eq | Ne => ExprPrecedence::Compare,
950+
And => ExprPrecedence::LAnd,
951+
Or => ExprPrecedence::LOr,
952+
}
953+
}
954+
955+
pub fn fixity(&self) -> Fixity {
956+
use BinOpKind::*;
957+
match self {
958+
Eq | Ne | Lt | Le | Gt | Ge => Fixity::None,
959+
Add | Sub | Mul | Div | Rem | And | Or | BitXor | BitAnd | BitOr | Shl | Shr => {
960+
Fixity::Left
961+
}
962+
}
963+
}
964+
940965
pub fn is_comparison(self) -> bool {
941-
crate::util::parser::AssocOp::from_ast_binop(self).is_comparison()
966+
use BinOpKind::*;
967+
match self {
968+
Eq | Ne | Lt | Le | Gt | Ge => true,
969+
Add | Sub | Mul | Div | Rem | And | Or | BitXor | BitAnd | BitOr | Shl | Shr => false,
970+
}
942971
}
943972

944973
/// Returns `true` if the binary operator takes its arguments by value.
@@ -1332,7 +1361,7 @@ impl Expr {
13321361
ExprKind::Range(..) => ExprPrecedence::Range,
13331362

13341363
// Binop-like expr kinds, handled by `AssocOp`.
1335-
ExprKind::Binary(op, ..) => AssocOp::from_ast_binop(op.node).precedence(),
1364+
ExprKind::Binary(op, ..) => op.node.precedence(),
13361365
ExprKind::Cast(..) => ExprPrecedence::Cast,
13371366

13381367
ExprKind::Assign(..) |
@@ -1424,6 +1453,15 @@ pub enum RangeLimits {
14241453
Closed,
14251454
}
14261455

1456+
impl RangeLimits {
1457+
pub fn as_str(&self) -> &'static str {
1458+
match self {
1459+
RangeLimits::HalfOpen => "..",
1460+
RangeLimits::Closed => "..=",
1461+
}
1462+
}
1463+
}
1464+
14271465
/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
14281466
#[derive(Clone, Encodable, Decodable, Debug)]
14291467
pub struct MethodCall {

compiler/rustc_ast/src/util/parser.rs

+62-154
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,21 @@
11
use rustc_span::kw;
22

3-
use crate::ast::{self, BinOpKind};
3+
use crate::ast::{self, BinOpKind, RangeLimits};
44
use crate::token::{self, BinOpToken, Token};
55

6-
/// Associative operator with precedence.
7-
///
8-
/// This is the enum which specifies operator precedence and fixity to the parser.
6+
/// Associative operator.
97
#[derive(Copy, Clone, PartialEq, Debug)]
108
pub enum AssocOp {
11-
/// `+`
12-
Add,
13-
/// `-`
14-
Subtract,
15-
/// `*`
16-
Multiply,
17-
/// `/`
18-
Divide,
19-
/// `%`
20-
Modulus,
21-
/// `&&`
22-
LAnd,
23-
/// `||`
24-
LOr,
25-
/// `^`
26-
BitXor,
27-
/// `&`
28-
BitAnd,
29-
/// `|`
30-
BitOr,
31-
/// `<<`
32-
ShiftLeft,
33-
/// `>>`
34-
ShiftRight,
35-
/// `==`
36-
Equal,
37-
/// `<`
38-
Less,
39-
/// `<=`
40-
LessEqual,
41-
/// `!=`
42-
NotEqual,
43-
/// `>`
44-
Greater,
45-
/// `>=`
46-
GreaterEqual,
9+
/// A binary op.
10+
Binary(BinOpKind),
11+
/// `?=` where ? is one of the assignable BinOps
12+
AssignOp(BinOpKind),
4713
/// `=`
4814
Assign,
49-
/// `?=` where ? is one of the BinOpToken
50-
AssignOp(BinOpToken),
5115
/// `as`
52-
As,
53-
/// `..` range
54-
DotDot,
55-
/// `..=` range
56-
DotDotEq,
16+
Cast,
17+
/// `..` or `..=` range
18+
Range(RangeLimits),
5719
}
5820

5921
#[derive(PartialEq, Debug)]
@@ -67,81 +29,56 @@ pub enum Fixity {
6729
}
6830

6931
impl AssocOp {
70-
/// Creates a new AssocOP from a token
32+
/// Creates a new AssocOp from a token.
7133
pub fn from_token(t: &Token) -> Option<AssocOp> {
7234
use AssocOp::*;
7335
match t.kind {
74-
token::BinOpEq(k) => Some(AssignOp(k)),
7536
token::Eq => Some(Assign),
76-
token::BinOp(BinOpToken::Star) => Some(Multiply),
77-
token::BinOp(BinOpToken::Slash) => Some(Divide),
78-
token::BinOp(BinOpToken::Percent) => Some(Modulus),
79-
token::BinOp(BinOpToken::Plus) => Some(Add),
80-
token::BinOp(BinOpToken::Minus) => Some(Subtract),
81-
token::BinOp(BinOpToken::Shl) => Some(ShiftLeft),
82-
token::BinOp(BinOpToken::Shr) => Some(ShiftRight),
83-
token::BinOp(BinOpToken::And) => Some(BitAnd),
84-
token::BinOp(BinOpToken::Caret) => Some(BitXor),
85-
token::BinOp(BinOpToken::Or) => Some(BitOr),
86-
token::Lt => Some(Less),
87-
token::Le => Some(LessEqual),
88-
token::Ge => Some(GreaterEqual),
89-
token::Gt => Some(Greater),
90-
token::EqEq => Some(Equal),
91-
token::Ne => Some(NotEqual),
92-
token::AndAnd => Some(LAnd),
93-
token::OrOr => Some(LOr),
94-
token::DotDot => Some(DotDot),
95-
token::DotDotEq => Some(DotDotEq),
37+
token::BinOp(BinOpToken::Plus) => Some(Binary(BinOpKind::Add)),
38+
token::BinOp(BinOpToken::Minus) => Some(Binary(BinOpKind::Sub)),
39+
token::BinOp(BinOpToken::Star) => Some(Binary(BinOpKind::Mul)),
40+
token::BinOp(BinOpToken::Slash) => Some(Binary(BinOpKind::Div)),
41+
token::BinOp(BinOpToken::Percent) => Some(Binary(BinOpKind::Rem)),
42+
token::BinOp(BinOpToken::Caret) => Some(Binary(BinOpKind::BitXor)),
43+
token::BinOp(BinOpToken::And) => Some(Binary(BinOpKind::BitAnd)),
44+
token::BinOp(BinOpToken::Or) => Some(Binary(BinOpKind::BitOr)),
45+
token::BinOp(BinOpToken::Shl) => Some(Binary(BinOpKind::Shl)),
46+
token::BinOp(BinOpToken::Shr) => Some(Binary(BinOpKind::Shr)),
47+
token::BinOpEq(BinOpToken::Plus) => Some(AssignOp(BinOpKind::Add)),
48+
token::BinOpEq(BinOpToken::Minus) => Some(AssignOp(BinOpKind::Sub)),
49+
token::BinOpEq(BinOpToken::Star) => Some(AssignOp(BinOpKind::Mul)),
50+
token::BinOpEq(BinOpToken::Slash) => Some(AssignOp(BinOpKind::Div)),
51+
token::BinOpEq(BinOpToken::Percent) => Some(AssignOp(BinOpKind::Rem)),
52+
token::BinOpEq(BinOpToken::Caret) => Some(AssignOp(BinOpKind::BitXor)),
53+
token::BinOpEq(BinOpToken::And) => Some(AssignOp(BinOpKind::BitAnd)),
54+
token::BinOpEq(BinOpToken::Or) => Some(AssignOp(BinOpKind::BitOr)),
55+
token::BinOpEq(BinOpToken::Shl) => Some(AssignOp(BinOpKind::Shl)),
56+
token::BinOpEq(BinOpToken::Shr) => Some(AssignOp(BinOpKind::Shr)),
57+
token::Lt => Some(Binary(BinOpKind::Lt)),
58+
token::Le => Some(Binary(BinOpKind::Le)),
59+
token::Ge => Some(Binary(BinOpKind::Ge)),
60+
token::Gt => Some(Binary(BinOpKind::Gt)),
61+
token::EqEq => Some(Binary(BinOpKind::Eq)),
62+
token::Ne => Some(Binary(BinOpKind::Ne)),
63+
token::AndAnd => Some(Binary(BinOpKind::And)),
64+
token::OrOr => Some(Binary(BinOpKind::Or)),
65+
token::DotDot => Some(Range(RangeLimits::HalfOpen)),
9666
// DotDotDot is no longer supported, but we need some way to display the error
97-
token::DotDotDot => Some(DotDotEq),
67+
token::DotDotEq | token::DotDotDot => Some(Range(RangeLimits::Closed)),
9868
// `<-` should probably be `< -`
99-
token::LArrow => Some(Less),
100-
_ if t.is_keyword(kw::As) => Some(As),
69+
token::LArrow => Some(Binary(BinOpKind::Lt)),
70+
_ if t.is_keyword(kw::As) => Some(Cast),
10171
_ => None,
10272
}
10373
}
10474

105-
/// Creates a new AssocOp from ast::BinOpKind.
106-
pub fn from_ast_binop(op: BinOpKind) -> Self {
107-
use AssocOp::*;
108-
match op {
109-
BinOpKind::Lt => Less,
110-
BinOpKind::Gt => Greater,
111-
BinOpKind::Le => LessEqual,
112-
BinOpKind::Ge => GreaterEqual,
113-
BinOpKind::Eq => Equal,
114-
BinOpKind::Ne => NotEqual,
115-
BinOpKind::Mul => Multiply,
116-
BinOpKind::Div => Divide,
117-
BinOpKind::Rem => Modulus,
118-
BinOpKind::Add => Add,
119-
BinOpKind::Sub => Subtract,
120-
BinOpKind::Shl => ShiftLeft,
121-
BinOpKind::Shr => ShiftRight,
122-
BinOpKind::BitAnd => BitAnd,
123-
BinOpKind::BitXor => BitXor,
124-
BinOpKind::BitOr => BitOr,
125-
BinOpKind::And => LAnd,
126-
BinOpKind::Or => LOr,
127-
}
128-
}
129-
13075
/// Gets the precedence of this operator
13176
pub fn precedence(&self) -> ExprPrecedence {
13277
use AssocOp::*;
13378
match *self {
134-
As => ExprPrecedence::Cast,
135-
Multiply | Divide | Modulus => ExprPrecedence::Product,
136-
Add | Subtract => ExprPrecedence::Sum,
137-
ShiftLeft | ShiftRight => ExprPrecedence::Shift,
138-
BitAnd => ExprPrecedence::BitAnd,
139-
BitXor => ExprPrecedence::BitXor,
140-
BitOr => ExprPrecedence::BitOr,
141-
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => ExprPrecedence::Compare,
142-
LAnd => ExprPrecedence::LAnd,
143-
LOr => ExprPrecedence::LOr,
144-
DotDot | DotDotEq => ExprPrecedence::Range,
79+
Cast => ExprPrecedence::Cast,
80+
Binary(bin_op) => bin_op.precedence(),
81+
Range(_) => ExprPrecedence::Range,
14582
Assign | AssignOp(_) => ExprPrecedence::Assign,
14683
}
14784
}
@@ -152,57 +89,25 @@ impl AssocOp {
15289
// NOTE: it is a bug to have an operators that has same precedence but different fixities!
15390
match *self {
15491
Assign | AssignOp(_) => Fixity::Right,
155-
As | Multiply | Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd
156-
| BitXor | BitOr | LAnd | LOr => Fixity::Left,
157-
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | DotDot | DotDotEq => {
158-
Fixity::None
159-
}
92+
Binary(binop) => binop.fixity(),
93+
Cast => Fixity::Left,
94+
Range(_) => Fixity::None,
16095
}
16196
}
16297

16398
pub fn is_comparison(&self) -> bool {
16499
use AssocOp::*;
165100
match *self {
166-
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => true,
167-
Assign | AssignOp(_) | As | Multiply | Divide | Modulus | Add | Subtract
168-
| ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot | DotDotEq => {
169-
false
170-
}
101+
Binary(binop) => binop.is_comparison(),
102+
Assign | AssignOp(_) | Cast | Range(_) => false,
171103
}
172104
}
173105

174106
pub fn is_assign_like(&self) -> bool {
175107
use AssocOp::*;
176108
match *self {
177109
Assign | AssignOp(_) => true,
178-
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | As | Multiply
179-
| Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd | BitXor
180-
| BitOr | LAnd | LOr | DotDot | DotDotEq => false,
181-
}
182-
}
183-
184-
pub fn to_ast_binop(&self) -> Option<BinOpKind> {
185-
use AssocOp::*;
186-
match *self {
187-
Less => Some(BinOpKind::Lt),
188-
Greater => Some(BinOpKind::Gt),
189-
LessEqual => Some(BinOpKind::Le),
190-
GreaterEqual => Some(BinOpKind::Ge),
191-
Equal => Some(BinOpKind::Eq),
192-
NotEqual => Some(BinOpKind::Ne),
193-
Multiply => Some(BinOpKind::Mul),
194-
Divide => Some(BinOpKind::Div),
195-
Modulus => Some(BinOpKind::Rem),
196-
Add => Some(BinOpKind::Add),
197-
Subtract => Some(BinOpKind::Sub),
198-
ShiftLeft => Some(BinOpKind::Shl),
199-
ShiftRight => Some(BinOpKind::Shr),
200-
BitAnd => Some(BinOpKind::BitAnd),
201-
BitXor => Some(BinOpKind::BitXor),
202-
BitOr => Some(BinOpKind::BitOr),
203-
LAnd => Some(BinOpKind::And),
204-
LOr => Some(BinOpKind::Or),
205-
Assign | AssignOp(_) | As | DotDot | DotDotEq => None,
110+
Cast | Binary(_) | Range(_) => false,
206111
}
207112
}
208113

@@ -212,20 +117,23 @@ impl AssocOp {
212117
/// parentheses while having a high degree of confidence on the correctness of the suggestion.
213118
pub fn can_continue_expr_unambiguously(&self) -> bool {
214119
use AssocOp::*;
120+
use BinOpKind::*;
215121
matches!(
216122
self,
217-
BitXor | // `{ 42 } ^ 3`
218123
Assign | // `{ 42 } = { 42 }`
219-
Divide | // `{ 42 } / 42`
220-
Modulus | // `{ 42 } % 2`
221-
ShiftRight | // `{ 42 } >> 2`
222-
LessEqual | // `{ 42 } <= 3`
223-
Greater | // `{ 42 } > 3`
224-
GreaterEqual | // `{ 42 } >= 3`
124+
Binary(
125+
BitXor | // `{ 42 } ^ 3`
126+
Div | // `{ 42 } / 42`
127+
Rem | // `{ 42 } % 2`
128+
Shr | // `{ 42 } >> 2`
129+
Le | // `{ 42 } <= 3`
130+
Gt | // `{ 42 } > 3`
131+
Ge // `{ 42 } >= 3`
132+
) |
225133
AssignOp(_) | // `{ 42 } +=`
226134
// Equal | // `{ 42 } == { 42 }` Accepting these here would regress incorrect
227135
// NotEqual | // `{ 42 } != { 42 } struct literals parser recovery.
228-
As // `{ 42 } as usize`
136+
Cast // `{ 42 } as usize`
229137
)
230138
}
231139
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use itertools::{Itertools, Position};
55
use rustc_ast::ptr::P;
66
use rustc_ast::util::classify;
77
use rustc_ast::util::literal::escape_byte_str_symbol;
8-
use rustc_ast::util::parser::{self, AssocOp, ExprPrecedence, Fixity};
8+
use rustc_ast::util::parser::{self, ExprPrecedence, Fixity};
99
use rustc_ast::{
1010
self as ast, BlockCheckMode, FormatAlignment, FormatArgPosition, FormatArgsPiece, FormatCount,
1111
FormatDebugHex, FormatSign, FormatTrait, token,
@@ -279,12 +279,11 @@ impl<'a> State<'a> {
279279
rhs: &ast::Expr,
280280
fixup: FixupContext,
281281
) {
282-
let assoc_op = AssocOp::from_ast_binop(op.node);
283-
let binop_prec = assoc_op.precedence();
282+
let binop_prec = op.node.precedence();
284283
let left_prec = lhs.precedence();
285284
let right_prec = rhs.precedence();
286285

287-
let (mut left_needs_paren, right_needs_paren) = match assoc_op.fixity() {
286+
let (mut left_needs_paren, right_needs_paren) = match op.node.fixity() {
288287
Fixity::Left => (left_prec < binop_prec, right_prec <= binop_prec),
289288
Fixity::Right => (left_prec <= binop_prec, right_prec < binop_prec),
290289
Fixity::None => (left_prec <= binop_prec, right_prec <= binop_prec),

0 commit comments

Comments
 (0)