Skip to content

Commit 20473eb

Browse files
authored
Rollup merge of #118394 - nnethercote:rm-hir-Ops, r=cjgillot
Remove HIR opkinds `hir::BinOp`, `hir::BinOpKind`, and `hir::UnOp` are identical to `ast::BinOp`, `ast::BinOpKind`, and `ast::UnOp`, respectively. This seems silly, so this PR removes the HIR ones. (A re-export lets the AST ones be referred to using a `hir::` qualifier, which avoids renaming churn.) r? `@cjgillot`
2 parents 68d31b1 + d9fef77 commit 20473eb

File tree

12 files changed

+42
-202
lines changed

12 files changed

+42
-202
lines changed

compiler/rustc_ast/src/ast.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ pub enum BorrowKind {
817817
Raw,
818818
}
819819

820-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
820+
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
821821
pub enum BinOpKind {
822822
/// The `+` operator (addition)
823823
Add,
@@ -858,9 +858,9 @@ pub enum BinOpKind {
858858
}
859859

860860
impl BinOpKind {
861-
pub fn to_string(&self) -> &'static str {
861+
pub fn as_str(&self) -> &'static str {
862862
use BinOpKind::*;
863-
match *self {
863+
match self {
864864
Add => "+",
865865
Sub => "-",
866866
Mul => "*",
@@ -881,27 +881,33 @@ impl BinOpKind {
881881
Gt => ">",
882882
}
883883
}
884-
pub fn lazy(&self) -> bool {
884+
885+
pub fn is_lazy(&self) -> bool {
885886
matches!(self, BinOpKind::And | BinOpKind::Or)
886887
}
887888

888889
pub fn is_comparison(&self) -> bool {
889890
use BinOpKind::*;
890-
// Note for developers: please keep this as is;
891+
// Note for developers: please keep this match exhaustive;
891892
// we want compilation to fail if another variant is added.
892893
match *self {
893894
Eq | Lt | Le | Ne | Gt | Ge => true,
894895
And | Or | Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr => false,
895896
}
896897
}
898+
899+
/// Returns `true` if the binary operator takes its arguments by value.
900+
pub fn is_by_value(self) -> bool {
901+
!self.is_comparison()
902+
}
897903
}
898904

899905
pub type BinOp = Spanned<BinOpKind>;
900906

901907
/// Unary operator.
902908
///
903909
/// Note that `&data` is not an operator, it's an `AddrOf` expression.
904-
#[derive(Clone, Encodable, Decodable, Debug, Copy)]
910+
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
905911
pub enum UnOp {
906912
/// The `*` operator for dereferencing
907913
Deref,
@@ -912,13 +918,18 @@ pub enum UnOp {
912918
}
913919

914920
impl UnOp {
915-
pub fn to_string(op: UnOp) -> &'static str {
916-
match op {
921+
pub fn as_str(&self) -> &'static str {
922+
match self {
917923
UnOp::Deref => "*",
918924
UnOp::Not => "!",
919925
UnOp::Neg => "-",
920926
}
921927
}
928+
929+
/// Returns `true` if the unary operator takes its argument by value.
930+
pub fn is_by_value(self) -> bool {
931+
matches!(self, Self::Neg | Self::Not)
932+
}
922933
}
923934

924935
/// A statement

compiler/rustc_ast_lowering/src/expr.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -350,30 +350,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
350350
}
351351
}
352352

353-
fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
354-
Spanned {
355-
node: match b.node {
356-
BinOpKind::Add => hir::BinOpKind::Add,
357-
BinOpKind::Sub => hir::BinOpKind::Sub,
358-
BinOpKind::Mul => hir::BinOpKind::Mul,
359-
BinOpKind::Div => hir::BinOpKind::Div,
360-
BinOpKind::Rem => hir::BinOpKind::Rem,
361-
BinOpKind::And => hir::BinOpKind::And,
362-
BinOpKind::Or => hir::BinOpKind::Or,
363-
BinOpKind::BitXor => hir::BinOpKind::BitXor,
364-
BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
365-
BinOpKind::BitOr => hir::BinOpKind::BitOr,
366-
BinOpKind::Shl => hir::BinOpKind::Shl,
367-
BinOpKind::Shr => hir::BinOpKind::Shr,
368-
BinOpKind::Eq => hir::BinOpKind::Eq,
369-
BinOpKind::Lt => hir::BinOpKind::Lt,
370-
BinOpKind::Le => hir::BinOpKind::Le,
371-
BinOpKind::Ne => hir::BinOpKind::Ne,
372-
BinOpKind::Ge => hir::BinOpKind::Ge,
373-
BinOpKind::Gt => hir::BinOpKind::Gt,
374-
},
375-
span: self.lower_span(b.span),
376-
}
353+
fn lower_binop(&mut self, b: BinOp) -> BinOp {
354+
Spanned { node: b.node, span: self.lower_span(b.span) }
377355
}
378356

379357
fn lower_legacy_const_generics(

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ impl<'a> State<'a> {
255255

256256
self.print_expr_maybe_paren(lhs, left_prec);
257257
self.space();
258-
self.word_space(op.node.to_string());
258+
self.word_space(op.node.as_str());
259259
self.print_expr_maybe_paren(rhs, right_prec)
260260
}
261261

262262
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
263-
self.word(ast::UnOp::to_string(op));
263+
self.word(op.as_str());
264264
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
265265
}
266266

@@ -470,7 +470,7 @@ impl<'a> State<'a> {
470470
let prec = AssocOp::Assign.precedence() as i8;
471471
self.print_expr_maybe_paren(lhs, prec + 1);
472472
self.space();
473-
self.word(op.node.to_string());
473+
self.word(op.node.as_str());
474474
self.word_space("=");
475475
self.print_expr_maybe_paren(rhs, prec);
476476
}

compiler/rustc_hir/src/hir.rs

+2-151
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::LangItem;
77
use rustc_ast as ast;
88
use rustc_ast::util::parser::ExprPrecedence;
99
use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, TraitObjectSyntax, UintTy};
10-
pub use rustc_ast::{BindingAnnotation, BorrowKind, ByRef, ImplPolarity, IsAuto};
11-
pub use rustc_ast::{CaptureBy, Movability, Mutability};
10+
pub use rustc_ast::{BinOp, BinOpKind, BindingAnnotation, BorrowKind, ByRef, CaptureBy};
11+
pub use rustc_ast::{ImplPolarity, IsAuto, Movability, Mutability, UnOp};
1212
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1313
use rustc_data_structures::fingerprint::Fingerprint;
1414
use rustc_data_structures::fx::FxHashMap;
@@ -1174,155 +1174,6 @@ pub enum PatKind<'hir> {
11741174
Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]),
11751175
}
11761176

1177-
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
1178-
pub enum BinOpKind {
1179-
/// The `+` operator (addition).
1180-
Add,
1181-
/// The `-` operator (subtraction).
1182-
Sub,
1183-
/// The `*` operator (multiplication).
1184-
Mul,
1185-
/// The `/` operator (division).
1186-
Div,
1187-
/// The `%` operator (modulus).
1188-
Rem,
1189-
/// The `&&` operator (logical and).
1190-
And,
1191-
/// The `||` operator (logical or).
1192-
Or,
1193-
/// The `^` operator (bitwise xor).
1194-
BitXor,
1195-
/// The `&` operator (bitwise and).
1196-
BitAnd,
1197-
/// The `|` operator (bitwise or).
1198-
BitOr,
1199-
/// The `<<` operator (shift left).
1200-
Shl,
1201-
/// The `>>` operator (shift right).
1202-
Shr,
1203-
/// The `==` operator (equality).
1204-
Eq,
1205-
/// The `<` operator (less than).
1206-
Lt,
1207-
/// The `<=` operator (less than or equal to).
1208-
Le,
1209-
/// The `!=` operator (not equal to).
1210-
Ne,
1211-
/// The `>=` operator (greater than or equal to).
1212-
Ge,
1213-
/// The `>` operator (greater than).
1214-
Gt,
1215-
}
1216-
1217-
impl BinOpKind {
1218-
pub fn as_str(self) -> &'static str {
1219-
match self {
1220-
BinOpKind::Add => "+",
1221-
BinOpKind::Sub => "-",
1222-
BinOpKind::Mul => "*",
1223-
BinOpKind::Div => "/",
1224-
BinOpKind::Rem => "%",
1225-
BinOpKind::And => "&&",
1226-
BinOpKind::Or => "||",
1227-
BinOpKind::BitXor => "^",
1228-
BinOpKind::BitAnd => "&",
1229-
BinOpKind::BitOr => "|",
1230-
BinOpKind::Shl => "<<",
1231-
BinOpKind::Shr => ">>",
1232-
BinOpKind::Eq => "==",
1233-
BinOpKind::Lt => "<",
1234-
BinOpKind::Le => "<=",
1235-
BinOpKind::Ne => "!=",
1236-
BinOpKind::Ge => ">=",
1237-
BinOpKind::Gt => ">",
1238-
}
1239-
}
1240-
1241-
pub fn is_lazy(self) -> bool {
1242-
matches!(self, BinOpKind::And | BinOpKind::Or)
1243-
}
1244-
1245-
pub fn is_comparison(self) -> bool {
1246-
match self {
1247-
BinOpKind::Eq
1248-
| BinOpKind::Lt
1249-
| BinOpKind::Le
1250-
| BinOpKind::Ne
1251-
| BinOpKind::Gt
1252-
| BinOpKind::Ge => true,
1253-
BinOpKind::And
1254-
| BinOpKind::Or
1255-
| BinOpKind::Add
1256-
| BinOpKind::Sub
1257-
| BinOpKind::Mul
1258-
| BinOpKind::Div
1259-
| BinOpKind::Rem
1260-
| BinOpKind::BitXor
1261-
| BinOpKind::BitAnd
1262-
| BinOpKind::BitOr
1263-
| BinOpKind::Shl
1264-
| BinOpKind::Shr => false,
1265-
}
1266-
}
1267-
1268-
/// Returns `true` if the binary operator takes its arguments by value.
1269-
pub fn is_by_value(self) -> bool {
1270-
!self.is_comparison()
1271-
}
1272-
}
1273-
1274-
impl Into<ast::BinOpKind> for BinOpKind {
1275-
fn into(self) -> ast::BinOpKind {
1276-
match self {
1277-
BinOpKind::Add => ast::BinOpKind::Add,
1278-
BinOpKind::Sub => ast::BinOpKind::Sub,
1279-
BinOpKind::Mul => ast::BinOpKind::Mul,
1280-
BinOpKind::Div => ast::BinOpKind::Div,
1281-
BinOpKind::Rem => ast::BinOpKind::Rem,
1282-
BinOpKind::And => ast::BinOpKind::And,
1283-
BinOpKind::Or => ast::BinOpKind::Or,
1284-
BinOpKind::BitXor => ast::BinOpKind::BitXor,
1285-
BinOpKind::BitAnd => ast::BinOpKind::BitAnd,
1286-
BinOpKind::BitOr => ast::BinOpKind::BitOr,
1287-
BinOpKind::Shl => ast::BinOpKind::Shl,
1288-
BinOpKind::Shr => ast::BinOpKind::Shr,
1289-
BinOpKind::Eq => ast::BinOpKind::Eq,
1290-
BinOpKind::Lt => ast::BinOpKind::Lt,
1291-
BinOpKind::Le => ast::BinOpKind::Le,
1292-
BinOpKind::Ne => ast::BinOpKind::Ne,
1293-
BinOpKind::Ge => ast::BinOpKind::Ge,
1294-
BinOpKind::Gt => ast::BinOpKind::Gt,
1295-
}
1296-
}
1297-
}
1298-
1299-
pub type BinOp = Spanned<BinOpKind>;
1300-
1301-
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
1302-
pub enum UnOp {
1303-
/// The `*` operator (dereferencing).
1304-
Deref,
1305-
/// The `!` operator (logical negation).
1306-
Not,
1307-
/// The `-` operator (negation).
1308-
Neg,
1309-
}
1310-
1311-
impl UnOp {
1312-
pub fn as_str(self) -> &'static str {
1313-
match self {
1314-
Self::Deref => "*",
1315-
Self::Not => "!",
1316-
Self::Neg => "-",
1317-
}
1318-
}
1319-
1320-
/// Returns `true` if the unary operator takes its argument by value.
1321-
pub fn is_by_value(self) -> bool {
1322-
matches!(self, Self::Neg | Self::Not)
1323-
}
1324-
}
1325-
13261177
/// A statement.
13271178
#[derive(Debug, Clone, Copy, HashStable_Generic)]
13281179
pub struct Stmt<'hir> {

compiler/rustc_lint/src/unused.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ trait UnusedDelimLint {
656656
) -> bool {
657657
if followed_by_else {
658658
match inner.kind {
659-
ast::ExprKind::Binary(op, ..) if op.node.lazy() => return true,
659+
ast::ExprKind::Binary(op, ..) if op.node.is_lazy() => return true,
660660
_ if classify::expr_trailing_brace(inner).is_some() => return true,
661661
_ => {}
662662
}
@@ -1016,7 +1016,7 @@ impl UnusedDelimLint for UnusedParens {
10161016
rustc_span::source_map::Spanned { node, .. },
10171017
_,
10181018
_,
1019-
) if node.lazy()))
1019+
) if node.is_lazy()))
10201020
{
10211021
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
10221022
}

compiler/rustc_parse/src/parser/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ impl<'a> Parser<'a> {
384384

385385
fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
386386
if let ast::ExprKind::Binary(op, ..) = init.kind {
387-
if op.node.lazy() {
387+
if op.node.is_lazy() {
388388
self.sess.emit_err(errors::InvalidExpressionInLetElse {
389389
span: init.span,
390-
operator: op.node.to_string(),
390+
operator: op.node.as_str(),
391391
sugg: errors::WrapExpressionInParentheses {
392392
left: init.span.shrink_to_lo(),
393393
right: init.span.shrink_to_hi(),

src/tools/clippy/clippy_lints/src/formatting.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
22
use clippy_utils::is_span_if;
33
use clippy_utils::source::snippet_opt;
4-
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
4+
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind};
55
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -144,7 +144,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
144144
let eq_span = lhs.span.between(rhs.span);
145145
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
146146
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
147-
let op = UnOp::to_string(op);
147+
let op = op.as_str();
148148
let eqop_span = lhs.span.between(sub_rhs.span);
149149
if eq_snippet.ends_with('=') {
150150
span_lint_and_note(
@@ -177,11 +177,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
177177
&& let unop_operand_span = rhs.span.until(un_rhs.span)
178178
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
179179
&& let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_span)
180-
&& let binop_str = BinOpKind::to_string(&binop.node)
180+
&& let binop_str = binop.node.as_str()
181181
// no space after BinOp operator and space after UnOp operator
182182
&& binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ')
183183
{
184-
let unop_str = UnOp::to_string(op);
184+
let unop_str = op.as_str();
185185
let eqop_span = lhs.span.between(un_rhs.span);
186186
span_lint_and_help(
187187
cx,

src/tools/clippy/clippy_lints/src/precedence.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl EarlyLintPass for Precedence {
7878
let sugg = format!(
7979
"({}) {} ({})",
8080
snippet_with_applicability(cx, left.span, "..", &mut applicability),
81-
op.to_string(),
81+
op.as_str(),
8282
snippet_with_applicability(cx, right.span, "..", &mut applicability)
8383
);
8484
span_sugg(expr, sugg, applicability);
@@ -87,7 +87,7 @@ impl EarlyLintPass for Precedence {
8787
let sugg = format!(
8888
"({}) {} {}",
8989
snippet_with_applicability(cx, left.span, "..", &mut applicability),
90-
op.to_string(),
90+
op.as_str(),
9191
snippet_with_applicability(cx, right.span, "..", &mut applicability)
9292
);
9393
span_sugg(expr, sugg, applicability);
@@ -96,7 +96,7 @@ impl EarlyLintPass for Precedence {
9696
let sugg = format!(
9797
"{} {} ({})",
9898
snippet_with_applicability(cx, left.span, "..", &mut applicability),
99-
op.to_string(),
99+
op.as_str(),
100100
snippet_with_applicability(cx, right.span, "..", &mut applicability)
101101
);
102102
span_sugg(expr, sugg, applicability);

0 commit comments

Comments
 (0)