Skip to content

Commit 8c6b69f

Browse files
committed
Remove hir::BinOp, hir::BinOpKind, and hir::UnOp.
They're identical to the same-named types from `ast`. I find it silly (and inefficient) to have all this boilerplate code to convert one type to an identical type. There is already a small amount of type sharing between the AST and HIR, e.g. `Attribute`, `MacroDef`. The commit adds a `pub use` to `rustc_hir` so that, for example, `ast::BinOp` can also be referred to as `hir::BinOp`. This is so the many existing `hir`-qualified mentions of these types don't need to change. The commit also moves a couple of operations from the (removed) HIR types to the AST types, e.g. `is_by_value`.
1 parent 705b484 commit 8c6b69f

File tree

3 files changed

+16
-176
lines changed

3 files changed

+16
-176
lines changed

compiler/rustc_ast/src/ast.rs

+13-3
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,
@@ -888,21 +888,26 @@ impl BinOpKind {
888888

889889
pub fn is_comparison(&self) -> bool {
890890
use BinOpKind::*;
891-
// Note for developers: please keep this as is;
891+
// Note for developers: please keep this match exhaustive;
892892
// we want compilation to fail if another variant is added.
893893
match *self {
894894
Eq | Lt | Le | Ne | Gt | Ge => true,
895895
And | Or | Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr => false,
896896
}
897897
}
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+
}
898903
}
899904

900905
pub type BinOp = Spanned<BinOpKind>;
901906

902907
/// Unary operator.
903908
///
904909
/// Note that `&data` is not an operator, it's an `AddrOf` expression.
905-
#[derive(Clone, Encodable, Decodable, Debug, Copy)]
910+
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
906911
pub enum UnOp {
907912
/// The `*` operator for dereferencing
908913
Deref,
@@ -920,6 +925,11 @@ impl UnOp {
920925
UnOp::Neg => "-",
921926
}
922927
}
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+
}
923933
}
924934

925935
/// 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_hir/src/hir.rs

+1-149
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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::{BinOp, BinOpKind, UnOp}; // shared between AST and HIR
1011
pub use rustc_ast::{BindingAnnotation, BorrowKind, ByRef, ImplPolarity, IsAuto};
1112
pub use rustc_ast::{CaptureBy, Movability, Mutability};
1213
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
@@ -1174,155 +1175,6 @@ pub enum PatKind<'hir> {
11741175
Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]),
11751176
}
11761177

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-
13261178
/// A statement.
13271179
#[derive(Debug, Clone, Copy, HashStable_Generic)]
13281180
pub struct Stmt<'hir> {

0 commit comments

Comments
 (0)