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`.
This commit is contained in:
Nicholas Nethercote 2023-11-28 09:36:09 +11:00
parent 705b484922
commit d9fef774e3
3 changed files with 17 additions and 178 deletions

View File

@ -817,7 +817,7 @@ pub enum BorrowKind {
Raw,
}
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
pub enum BinOpKind {
/// The `+` operator (addition)
Add,
@ -888,13 +888,18 @@ pub fn is_lazy(&self) -> bool {
pub fn is_comparison(&self) -> bool {
use BinOpKind::*;
// Note for developers: please keep this as is;
// Note for developers: please keep this match exhaustive;
// we want compilation to fail if another variant is added.
match *self {
Eq | Lt | Le | Ne | Gt | Ge => true,
And | Or | Add | Sub | Mul | Div | Rem | BitXor | BitAnd | BitOr | Shl | Shr => false,
}
}
/// Returns `true` if the binary operator takes its arguments by value.
pub fn is_by_value(self) -> bool {
!self.is_comparison()
}
}
pub type BinOp = Spanned<BinOpKind>;
@ -902,7 +907,7 @@ pub fn is_comparison(&self) -> bool {
/// Unary operator.
///
/// Note that `&data` is not an operator, it's an `AddrOf` expression.
#[derive(Clone, Encodable, Decodable, Debug, Copy)]
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
pub enum UnOp {
/// The `*` operator for dereferencing
Deref,
@ -920,6 +925,11 @@ pub fn as_str(&self) -> &'static str {
UnOp::Neg => "-",
}
}
/// Returns `true` if the unary operator takes its argument by value.
pub fn is_by_value(self) -> bool {
matches!(self, Self::Neg | Self::Not)
}
}
/// A statement

View File

@ -350,30 +350,8 @@ fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
}
}
fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
Spanned {
node: match b.node {
BinOpKind::Add => hir::BinOpKind::Add,
BinOpKind::Sub => hir::BinOpKind::Sub,
BinOpKind::Mul => hir::BinOpKind::Mul,
BinOpKind::Div => hir::BinOpKind::Div,
BinOpKind::Rem => hir::BinOpKind::Rem,
BinOpKind::And => hir::BinOpKind::And,
BinOpKind::Or => hir::BinOpKind::Or,
BinOpKind::BitXor => hir::BinOpKind::BitXor,
BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
BinOpKind::BitOr => hir::BinOpKind::BitOr,
BinOpKind::Shl => hir::BinOpKind::Shl,
BinOpKind::Shr => hir::BinOpKind::Shr,
BinOpKind::Eq => hir::BinOpKind::Eq,
BinOpKind::Lt => hir::BinOpKind::Lt,
BinOpKind::Le => hir::BinOpKind::Le,
BinOpKind::Ne => hir::BinOpKind::Ne,
BinOpKind::Ge => hir::BinOpKind::Ge,
BinOpKind::Gt => hir::BinOpKind::Gt,
},
span: self.lower_span(b.span),
}
fn lower_binop(&mut self, b: BinOp) -> BinOp {
Spanned { node: b.node, span: self.lower_span(b.span) }
}
fn lower_legacy_const_generics(

View File

@ -7,8 +7,8 @@
use rustc_ast as ast;
use rustc_ast::util::parser::ExprPrecedence;
use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, TraitObjectSyntax, UintTy};
pub use rustc_ast::{BindingAnnotation, BorrowKind, ByRef, ImplPolarity, IsAuto};
pub use rustc_ast::{CaptureBy, Movability, Mutability};
pub use rustc_ast::{BinOp, BinOpKind, BindingAnnotation, BorrowKind, ByRef, CaptureBy};
pub use rustc_ast::{ImplPolarity, IsAuto, Movability, Mutability, UnOp};
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
@ -1174,155 +1174,6 @@ pub enum PatKind<'hir> {
Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]),
}
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum BinOpKind {
/// The `+` operator (addition).
Add,
/// The `-` operator (subtraction).
Sub,
/// The `*` operator (multiplication).
Mul,
/// The `/` operator (division).
Div,
/// The `%` operator (modulus).
Rem,
/// The `&&` operator (logical and).
And,
/// The `||` operator (logical or).
Or,
/// The `^` operator (bitwise xor).
BitXor,
/// The `&` operator (bitwise and).
BitAnd,
/// The `|` operator (bitwise or).
BitOr,
/// The `<<` operator (shift left).
Shl,
/// The `>>` operator (shift right).
Shr,
/// The `==` operator (equality).
Eq,
/// The `<` operator (less than).
Lt,
/// The `<=` operator (less than or equal to).
Le,
/// The `!=` operator (not equal to).
Ne,
/// The `>=` operator (greater than or equal to).
Ge,
/// The `>` operator (greater than).
Gt,
}
impl BinOpKind {
pub fn as_str(self) -> &'static str {
match self {
BinOpKind::Add => "+",
BinOpKind::Sub => "-",
BinOpKind::Mul => "*",
BinOpKind::Div => "/",
BinOpKind::Rem => "%",
BinOpKind::And => "&&",
BinOpKind::Or => "||",
BinOpKind::BitXor => "^",
BinOpKind::BitAnd => "&",
BinOpKind::BitOr => "|",
BinOpKind::Shl => "<<",
BinOpKind::Shr => ">>",
BinOpKind::Eq => "==",
BinOpKind::Lt => "<",
BinOpKind::Le => "<=",
BinOpKind::Ne => "!=",
BinOpKind::Ge => ">=",
BinOpKind::Gt => ">",
}
}
pub fn is_lazy(self) -> bool {
matches!(self, BinOpKind::And | BinOpKind::Or)
}
pub fn is_comparison(self) -> bool {
match self {
BinOpKind::Eq
| BinOpKind::Lt
| BinOpKind::Le
| BinOpKind::Ne
| BinOpKind::Gt
| BinOpKind::Ge => true,
BinOpKind::And
| BinOpKind::Or
| BinOpKind::Add
| BinOpKind::Sub
| BinOpKind::Mul
| BinOpKind::Div
| BinOpKind::Rem
| BinOpKind::BitXor
| BinOpKind::BitAnd
| BinOpKind::BitOr
| BinOpKind::Shl
| BinOpKind::Shr => false,
}
}
/// Returns `true` if the binary operator takes its arguments by value.
pub fn is_by_value(self) -> bool {
!self.is_comparison()
}
}
impl Into<ast::BinOpKind> for BinOpKind {
fn into(self) -> ast::BinOpKind {
match self {
BinOpKind::Add => ast::BinOpKind::Add,
BinOpKind::Sub => ast::BinOpKind::Sub,
BinOpKind::Mul => ast::BinOpKind::Mul,
BinOpKind::Div => ast::BinOpKind::Div,
BinOpKind::Rem => ast::BinOpKind::Rem,
BinOpKind::And => ast::BinOpKind::And,
BinOpKind::Or => ast::BinOpKind::Or,
BinOpKind::BitXor => ast::BinOpKind::BitXor,
BinOpKind::BitAnd => ast::BinOpKind::BitAnd,
BinOpKind::BitOr => ast::BinOpKind::BitOr,
BinOpKind::Shl => ast::BinOpKind::Shl,
BinOpKind::Shr => ast::BinOpKind::Shr,
BinOpKind::Eq => ast::BinOpKind::Eq,
BinOpKind::Lt => ast::BinOpKind::Lt,
BinOpKind::Le => ast::BinOpKind::Le,
BinOpKind::Ne => ast::BinOpKind::Ne,
BinOpKind::Ge => ast::BinOpKind::Ge,
BinOpKind::Gt => ast::BinOpKind::Gt,
}
}
}
pub type BinOp = Spanned<BinOpKind>;
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum UnOp {
/// The `*` operator (dereferencing).
Deref,
/// The `!` operator (logical negation).
Not,
/// The `-` operator (negation).
Neg,
}
impl UnOp {
pub fn as_str(self) -> &'static str {
match self {
Self::Deref => "*",
Self::Not => "!",
Self::Neg => "-",
}
}
/// Returns `true` if the unary operator takes its argument by value.
pub fn is_by_value(self) -> bool {
matches!(self, Self::Neg | Self::Not)
}
}
/// A statement.
#[derive(Debug, Clone, Copy, HashStable_Generic)]
pub struct Stmt<'hir> {