Skip to content

Commit baf12ac

Browse files
committed
Replace AssocOp::DotDot{,Eq} with AssocOp::Range.
It makes `AssocOp` more similar to `ExprKind` and makes things a little simpler. And the semantic names make more sense here than the syntactic names.
1 parent 5c48fc8 commit baf12ac

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

compiler/rustc_ast/src/ast.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,15 @@ pub enum RangeLimits {
14531453
Closed,
14541454
}
14551455

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

compiler/rustc_ast/src/util/parser.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

66
/// Associative operator.
@@ -14,10 +14,8 @@ pub enum AssocOp {
1414
Assign,
1515
/// `as`
1616
As,
17-
/// `..` range
18-
DotDot,
19-
/// `..=` range
20-
DotDotEq,
17+
/// `..` or `..=` range
18+
Range(RangeLimits),
2119
}
2220

2321
#[derive(PartialEq, Debug)]
@@ -64,10 +62,9 @@ impl AssocOp {
6462
token::Ne => Some(Binary(BinOpKind::Ne)),
6563
token::AndAnd => Some(Binary(BinOpKind::And)),
6664
token::OrOr => Some(Binary(BinOpKind::Or)),
67-
token::DotDot => Some(DotDot),
68-
token::DotDotEq => Some(DotDotEq),
65+
token::DotDot => Some(Range(RangeLimits::HalfOpen)),
6966
// DotDotDot is no longer supported, but we need some way to display the error
70-
token::DotDotDot => Some(DotDotEq),
67+
token::DotDotEq | token::DotDotDot => Some(Range(RangeLimits::Closed)),
7168
// `<-` should probably be `< -`
7269
token::LArrow => Some(Binary(BinOpKind::Lt)),
7370
_ if t.is_keyword(kw::As) => Some(As),
@@ -81,7 +78,7 @@ impl AssocOp {
8178
match *self {
8279
As => ExprPrecedence::Cast,
8380
Binary(bin_op) => bin_op.precedence(),
84-
DotDot | DotDotEq => ExprPrecedence::Range,
81+
Range(_) => ExprPrecedence::Range,
8582
Assign | AssignOp(_) => ExprPrecedence::Assign,
8683
}
8784
}
@@ -94,23 +91,23 @@ impl AssocOp {
9491
Assign | AssignOp(_) => Fixity::Right,
9592
Binary(binop) => binop.fixity(),
9693
As => Fixity::Left,
97-
DotDot | DotDotEq => Fixity::None,
94+
Range(_) => Fixity::None,
9895
}
9996
}
10097

10198
pub fn is_comparison(&self) -> bool {
10299
use AssocOp::*;
103100
match *self {
104101
Binary(binop) => binop.is_comparison(),
105-
Assign | AssignOp(_) | As | DotDot | DotDotEq => false,
102+
Assign | AssignOp(_) | As | Range(_) => false,
106103
}
107104
}
108105

109106
pub fn is_assign_like(&self) -> bool {
110107
use AssocOp::*;
111108
match *self {
112109
Assign | AssignOp(_) => true,
113-
As | Binary(_) | DotDot | DotDotEq => false,
110+
As | Binary(_) | Range(_) => false,
114111
}
115112
}
116113

compiler/rustc_parse/src/parser/expr.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a> Parser<'a> {
171171
break;
172172
}
173173
// Check for deprecated `...` syntax
174-
if self.token == token::DotDotDot && op.node == AssocOp::DotDotEq {
174+
if self.token == token::DotDotDot && op.node == AssocOp::Range(RangeLimits::Closed) {
175175
self.err_dotdotdot_syntax(self.token.span);
176176
}
177177

@@ -267,10 +267,10 @@ impl<'a> Parser<'a> {
267267
if op == AssocOp::As {
268268
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
269269
continue;
270-
} else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
270+
} else if let AssocOp::Range(limits) = op {
271271
// If we didn't have to handle `x..`/`x..=`, it would be pretty easy to
272272
// generalise it to the Fixity::None code.
273-
lhs = self.parse_expr_range(prec, lhs, op, cur_op_span)?;
273+
lhs = self.parse_expr_range(prec, lhs, limits, cur_op_span)?;
274274
break;
275275
}
276276

@@ -294,7 +294,7 @@ impl<'a> Parser<'a> {
294294
let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs);
295295
self.mk_expr(span, aopexpr)
296296
}
297-
AssocOp::As | AssocOp::DotDot | AssocOp::DotDotEq => {
297+
AssocOp::As | AssocOp::Range(_) => {
298298
self.dcx().span_bug(span, "AssocOp should have been handled by special case")
299299
}
300300
};
@@ -372,8 +372,7 @@ impl<'a> Parser<'a> {
372372
AssocOp::Assign
373373
| AssocOp::AssignOp(_)
374374
| AssocOp::Binary(BinOpKind::BitOr)
375-
| AssocOp::DotDot
376-
| AssocOp::DotDotEq,
375+
| AssocOp::Range(_),
377376
),
378377
_,
379378
) if self.restrictions.contains(Restrictions::IS_PAT) => {
@@ -414,7 +413,7 @@ impl<'a> Parser<'a> {
414413
&mut self,
415414
prec: ExprPrecedence,
416415
lhs: P<Expr>,
417-
op: AssocOp,
416+
limits: RangeLimits,
418417
cur_op_span: Span,
419418
) -> PResult<'a, P<Expr>> {
420419
let rhs = if self.is_at_start_of_range_notation_rhs() {
@@ -430,8 +429,6 @@ impl<'a> Parser<'a> {
430429
};
431430
let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
432431
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
433-
let limits =
434-
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
435432
let range = self.mk_range(Some(lhs), rhs, limits);
436433
Ok(self.mk_expr(span, range))
437434
}

src/tools/clippy/clippy_utils/src/sugg.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ impl<'a> Sugg<'a> {
113113
/// function variants of `Sugg`, since these use different snippet functions.
114114
fn hir_from_snippet(expr: &hir::Expr<'_>, mut get_snippet: impl FnMut(Span) -> Cow<'a, str>) -> Self {
115115
if let Some(range) = higher::Range::hir(expr) {
116-
let op = match range.limits {
117-
ast::RangeLimits::HalfOpen => AssocOp::DotDot,
118-
ast::RangeLimits::Closed => AssocOp::DotDotEq,
119-
};
116+
let op = AssocOp::Range(range.limits);
120117
let start = range.start.map_or("".into(), |expr| get_snippet(expr.span));
121118
let end = range.end.map_or("".into(), |expr| get_snippet(expr.span));
122119

@@ -178,8 +175,6 @@ impl<'a> Sugg<'a> {
178175
ctxt: SyntaxContext,
179176
app: &mut Applicability,
180177
) -> Self {
181-
use rustc_ast::ast::RangeLimits;
182-
183178
let mut snippet = |span: Span| snippet_with_context(cx, span, ctxt, default, app).0;
184179

185180
match expr.kind {
@@ -228,13 +223,8 @@ impl<'a> Sugg<'a> {
228223
| ast::ExprKind::Err(_)
229224
| ast::ExprKind::Dummy
230225
| ast::ExprKind::UnsafeBinderCast(..) => Sugg::NonParen(snippet(expr.span)),
231-
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
232-
AssocOp::DotDot,
233-
lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
234-
rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
235-
),
236-
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::Closed) => Sugg::BinOp(
237-
AssocOp::DotDotEq,
226+
ast::ExprKind::Range(ref lhs, ref rhs, limits) => Sugg::BinOp(
227+
AssocOp::Range(limits),
238228
lhs.as_ref().map_or("".into(), |lhs| snippet(lhs.span)),
239229
rhs.as_ref().map_or("".into(), |rhs| snippet(rhs.span)),
240230
),
@@ -326,11 +316,8 @@ impl<'a> Sugg<'a> {
326316

327317
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
328318
/// suggestion.
329-
pub fn range(self, end: &Self, limit: ast::RangeLimits) -> Sugg<'static> {
330-
match limit {
331-
ast::RangeLimits::HalfOpen => make_assoc(AssocOp::DotDot, &self, end),
332-
ast::RangeLimits::Closed => make_assoc(AssocOp::DotDotEq, &self, end),
333-
}
319+
pub fn range(self, end: &Self, limits: ast::RangeLimits) -> Sugg<'static> {
320+
make_assoc(AssocOp::Range(limits), &self, end)
334321
}
335322

336323
/// Adds parentheses to any expression that might need them. Suitable to the
@@ -370,8 +357,7 @@ fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
370357
AssocOp::Assign => format!("{lhs} = {rhs}"),
371358
AssocOp::AssignOp(op) => format!("{lhs} {}= {rhs}", op.as_str()),
372359
AssocOp::As => format!("{lhs} as {rhs}"),
373-
AssocOp::DotDot => format!("{lhs}..{rhs}"),
374-
AssocOp::DotDotEq => format!("{lhs}..={rhs}"),
360+
AssocOp::Range(limits) => format!("{lhs}{}{rhs}", limits.as_str()),
375361
}
376362
}
377363

@@ -590,7 +576,7 @@ enum Associativity {
590576
/// associative.
591577
#[must_use]
592578
fn associativity(op: AssocOp) -> Associativity {
593-
use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, DotDot, DotDotEq};
579+
use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, Range};
594580
use ast::BinOpKind::{
595581
Add, BitAnd, BitOr, BitXor, Div, Eq, Gt, Ge, And, Or, Lt, Le, Rem, Mul, Ne, Shl, Shr, Sub,
596582
};
@@ -599,7 +585,7 @@ fn associativity(op: AssocOp) -> Associativity {
599585
Assign | AssignOp(_) => Associativity::Right,
600586
Binary(Add | BitAnd | BitOr | BitXor | And | Or | Mul) | As => Associativity::Both,
601587
Binary(Div | Eq | Gt | Ge | Lt | Le | Rem | Ne | Shl | Shr | Sub) => Associativity::Left,
602-
DotDot | DotDotEq => Associativity::None,
588+
Range(_) => Associativity::None,
603589
}
604590
}
605591

0 commit comments

Comments
 (0)