Skip to content

Commit

Permalink
Unrolled build for rust-lang#118394
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#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`
  • Loading branch information
rust-timer authored Nov 29, 2023
2 parents e9b7bf0 + d9fef77 commit e9f058f
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 202 deletions.
27 changes: 19 additions & 8 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -858,9 +858,9 @@ pub enum BinOpKind {
}

impl BinOpKind {
pub fn to_string(&self) -> &'static str {
pub fn as_str(&self) -> &'static str {
use BinOpKind::*;
match *self {
match self {
Add => "+",
Sub => "-",
Mul => "*",
Expand All @@ -881,27 +881,33 @@ impl BinOpKind {
Gt => ">",
}
}
pub fn lazy(&self) -> bool {

pub fn is_lazy(&self) -> bool {
matches!(self, BinOpKind::And | BinOpKind::Or)
}

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>;

/// 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,
Expand All @@ -912,13 +918,18 @@ pub enum UnOp {
}

impl UnOp {
pub fn to_string(op: UnOp) -> &'static str {
match op {
pub fn as_str(&self) -> &'static str {
match self {
UnOp::Deref => "*",
UnOp::Not => "!",
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
Expand Down
26 changes: 2 additions & 24 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,30 +350,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

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(
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ impl<'a> State<'a> {

self.print_expr_maybe_paren(lhs, left_prec);
self.space();
self.word_space(op.node.to_string());
self.word_space(op.node.as_str());
self.print_expr_maybe_paren(rhs, right_prec)
}

fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
self.word(ast::UnOp::to_string(op));
self.word(op.as_str());
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
}

Expand Down Expand Up @@ -470,7 +470,7 @@ impl<'a> State<'a> {
let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1);
self.space();
self.word(op.node.to_string());
self.word(op.node.as_str());
self.word_space("=");
self.print_expr_maybe_paren(rhs, prec);
}
Expand Down
153 changes: 2 additions & 151 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::LangItem;
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;
Expand Down Expand Up @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ trait UnusedDelimLint {
) -> bool {
if followed_by_else {
match inner.kind {
ast::ExprKind::Binary(op, ..) if op.node.lazy() => return true,
ast::ExprKind::Binary(op, ..) if op.node.is_lazy() => return true,
_ if classify::expr_trailing_brace(inner).is_some() => return true,
_ => {}
}
Expand Down Expand Up @@ -1016,7 +1016,7 @@ impl UnusedDelimLint for UnusedParens {
rustc_span::source_map::Spanned { node, .. },
_,
_,
) if node.lazy()))
) if node.is_lazy()))
{
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,10 @@ impl<'a> Parser<'a> {

fn check_let_else_init_bool_expr(&self, init: &ast::Expr) {
if let ast::ExprKind::Binary(op, ..) = init.kind {
if op.node.lazy() {
if op.node.is_lazy() {
self.sess.emit_err(errors::InvalidExpressionInLetElse {
span: init.span,
operator: op.node.to_string(),
operator: op.node.as_str(),
sugg: errors::WrapExpressionInParentheses {
left: init.span.shrink_to_lo(),
right: init.span.shrink_to_hi(),
Expand Down
8 changes: 4 additions & 4 deletions src/tools/clippy/clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
use clippy_utils::is_span_if;
use clippy_utils::source::snippet_opt;
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind};
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -144,7 +144,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
let eq_span = lhs.span.between(rhs.span);
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
let op = UnOp::to_string(op);
let op = op.as_str();
let eqop_span = lhs.span.between(sub_rhs.span);
if eq_snippet.ends_with('=') {
span_lint_and_note(
Expand Down Expand Up @@ -177,11 +177,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
&& let unop_operand_span = rhs.span.until(un_rhs.span)
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
&& let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_span)
&& let binop_str = BinOpKind::to_string(&binop.node)
&& let binop_str = binop.node.as_str()
// no space after BinOp operator and space after UnOp operator
&& binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ')
{
let unop_str = UnOp::to_string(op);
let unop_str = op.as_str();
let eqop_span = lhs.span.between(un_rhs.span);
span_lint_and_help(
cx,
Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/clippy_lints/src/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl EarlyLintPass for Precedence {
let sugg = format!(
"({}) {} ({})",
snippet_with_applicability(cx, left.span, "..", &mut applicability),
op.to_string(),
op.as_str(),
snippet_with_applicability(cx, right.span, "..", &mut applicability)
);
span_sugg(expr, sugg, applicability);
Expand All @@ -87,7 +87,7 @@ impl EarlyLintPass for Precedence {
let sugg = format!(
"({}) {} {}",
snippet_with_applicability(cx, left.span, "..", &mut applicability),
op.to_string(),
op.as_str(),
snippet_with_applicability(cx, right.span, "..", &mut applicability)
);
span_sugg(expr, sugg, applicability);
Expand All @@ -96,7 +96,7 @@ impl EarlyLintPass for Precedence {
let sugg = format!(
"{} {} ({})",
snippet_with_applicability(cx, left.span, "..", &mut applicability),
op.to_string(),
op.as_str(),
snippet_with_applicability(cx, right.span, "..", &mut applicability)
);
span_sugg(expr, sugg, applicability);
Expand Down
Loading

0 comments on commit e9f058f

Please sign in to comment.