Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c4e3558

Browse files
committed
Rename HIR UnOp variants
This renames the variants in HIR UnOp from enum UnOp { UnDeref, UnNot, UnNeg, } to enum UnOp { Deref, Not, Neg, } Motivations: - This is more consistent with the rest of the code base where most enum variants don't have a prefix. - These variants are never used without the `UnOp` prefix so the extra `Un` prefix doesn't help with readability. E.g. we don't have any `UnDeref`s in the code, we only have `UnOp::UnDeref`. - MIR `UnOp` type variants don't have a prefix so this is more consistent with MIR types. - "un" prefix reads like "inverse" or "reverse", so as a beginner in rustc code base when I see "UnDeref" what comes to my mind is something like "&*" instead of just "*".
1 parent 36931ce commit c4e3558

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+80
-80
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
260260

261261
fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
262262
match u {
263-
UnOp::Deref => hir::UnOp::UnDeref,
264-
UnOp::Not => hir::UnOp::UnNot,
265-
UnOp::Neg => hir::UnOp::UnNeg,
263+
UnOp::Deref => hir::UnOp::Deref,
264+
UnOp::Not => hir::UnOp::Not,
265+
UnOp::Neg => hir::UnOp::Neg,
266266
}
267267
}
268268

compiler/rustc_hir/src/hir.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,25 +1112,25 @@ pub type BinOp = Spanned<BinOpKind>;
11121112
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)]
11131113
pub enum UnOp {
11141114
/// The `*` operator (deferencing).
1115-
UnDeref,
1115+
Deref,
11161116
/// The `!` operator (logical negation).
1117-
UnNot,
1117+
Not,
11181118
/// The `-` operator (negation).
1119-
UnNeg,
1119+
Neg,
11201120
}
11211121

11221122
impl UnOp {
11231123
pub fn as_str(self) -> &'static str {
11241124
match self {
1125-
Self::UnDeref => "*",
1126-
Self::UnNot => "!",
1127-
Self::UnNeg => "-",
1125+
Self::Deref => "*",
1126+
Self::Not => "!",
1127+
Self::Neg => "-",
11281128
}
11291129
}
11301130

11311131
/// Returns `true` if the unary operator takes its argument by value.
11321132
pub fn is_by_value(self) -> bool {
1133-
matches!(self, Self::UnNeg | Self::UnNot)
1133+
matches!(self, Self::Neg | Self::Not)
11341134
}
11351135
}
11361136

@@ -1477,7 +1477,7 @@ impl Expr<'_> {
14771477
// https://github.com/rust-lang/rfcs/blob/master/text/0803-type-ascription.md#type-ascription-and-temporaries
14781478
ExprKind::Type(ref e, _) => e.is_place_expr(allow_projections_from),
14791479

1480-
ExprKind::Unary(UnOp::UnDeref, _) => true,
1480+
ExprKind::Unary(UnOp::Deref, _) => true,
14811481

14821482
ExprKind::Field(ref base, _) | ExprKind::Index(ref base, _) => {
14831483
allow_projections_from(base) || base.is_place_expr(allow_projections_from)

compiler/rustc_lint/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ fn lint_literal<'tcx>(
472472
impl<'tcx> LateLintPass<'tcx> for TypeLimits {
473473
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {
474474
match e.kind {
475-
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => {
475+
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => {
476476
// propagate negation, if the negation itself isn't negated
477477
if self.negated_expr_id != Some(e.hir_id) {
478478
self.negated_expr_id = Some(expr.hir_id);

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'tcx> Const<'tcx> {
5555

5656
let lit_input = match expr.kind {
5757
hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
58-
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => match expr.kind {
58+
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => match expr.kind {
5959
hir::ExprKind::Lit(ref lit) => {
6060
Some(LitToConstInput { lit: &lit.node, ty, neg: true })
6161
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,23 @@ fn make_mirror_unadjusted<'a, 'tcx>(
299299
}
300300
}
301301

302-
hir::ExprKind::Unary(hir::UnOp::UnDeref, ref arg) => {
302+
hir::ExprKind::Unary(hir::UnOp::Deref, ref arg) => {
303303
if cx.typeck_results().is_method_call(expr) {
304304
overloaded_place(cx, expr, expr_ty, None, vec![arg.to_ref()], expr.span)
305305
} else {
306306
ExprKind::Deref { arg: arg.to_ref() }
307307
}
308308
}
309309

310-
hir::ExprKind::Unary(hir::UnOp::UnNot, ref arg) => {
310+
hir::ExprKind::Unary(hir::UnOp::Not, ref arg) => {
311311
if cx.typeck_results().is_method_call(expr) {
312312
overloaded_operator(cx, expr, vec![arg.to_ref()])
313313
} else {
314314
ExprKind::Unary { op: UnOp::Not, arg: arg.to_ref() }
315315
}
316316
}
317317

318-
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref arg) => {
318+
hir::ExprKind::Unary(hir::UnOp::Neg, ref arg) => {
319319
if cx.typeck_results().is_method_call(expr) {
320320
overloaded_operator(cx, expr, vec![arg.to_ref()])
321321
} else if let hir::ExprKind::Lit(ref lit) = arg.kind {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
866866
return *self.const_to_pat(value, expr.hir_id, expr.span, false).kind;
867867
}
868868
hir::ExprKind::Lit(ref lit) => (lit, false),
869-
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref expr) => {
869+
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => {
870870
let lit = match expr.kind {
871871
hir::ExprKind::Lit(ref lit) => lit,
872872
_ => span_bug!(expr.span, "not a literal: {:?}", expr),

compiler/rustc_passes/src/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ fn resolve_local<'tcx>(
664664

665665
match expr.kind {
666666
hir::ExprKind::AddrOf(_, _, ref subexpr)
667-
| hir::ExprKind::Unary(hir::UnOp::UnDeref, ref subexpr)
667+
| hir::ExprKind::Unary(hir::UnOp::Deref, ref subexpr)
668668
| hir::ExprKind::Field(ref subexpr, _)
669669
| hir::ExprKind::Index(ref subexpr, _) => {
670670
expr = &subexpr;

compiler/rustc_typeck/src/check/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
773773
if let hir::ExprKind::Lit(lit) = &expr.kind { lit.node.is_suffixed() } else { false }
774774
};
775775
let is_negative_int =
776-
|expr: &hir::Expr<'_>| matches!(expr.kind, hir::ExprKind::Unary(hir::UnOp::UnNeg, ..));
776+
|expr: &hir::Expr<'_>| matches!(expr.kind, hir::ExprKind::Unary(hir::UnOp::Neg, ..));
777777
let is_uint = |ty: Ty<'_>| matches!(ty.kind(), ty::Uint(..));
778778

779779
let in_const_context = self.tcx.hir().is_inside_const_context(expr.hir_id);

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
323323
) -> Ty<'tcx> {
324324
let tcx = self.tcx;
325325
let expected_inner = match unop {
326-
hir::UnOp::UnNot | hir::UnOp::UnNeg => expected,
327-
hir::UnOp::UnDeref => NoExpectation,
326+
hir::UnOp::Not | hir::UnOp::Neg => expected,
327+
hir::UnOp::Deref => NoExpectation,
328328
};
329329
let mut oprnd_t = self.check_expr_with_expectation(&oprnd, expected_inner);
330330

331331
if !oprnd_t.references_error() {
332332
oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
333333
match unop {
334-
hir::UnOp::UnDeref => {
334+
hir::UnOp::Deref => {
335335
if let Some(ty) = self.lookup_derefing(expr, oprnd, oprnd_t) {
336336
oprnd_t = ty;
337337
} else {
@@ -353,14 +353,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
353353
oprnd_t = tcx.ty_error();
354354
}
355355
}
356-
hir::UnOp::UnNot => {
356+
hir::UnOp::Not => {
357357
let result = self.check_user_unop(expr, oprnd_t, unop);
358358
// If it's builtin, we can reuse the type, this helps inference.
359359
if !(oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool) {
360360
oprnd_t = result;
361361
}
362362
}
363-
hir::UnOp::UnNeg => {
363+
hir::UnOp::Neg => {
364364
let result = self.check_user_unop(expr, oprnd_t, unop);
365365
// If it's builtin, we can reuse the type, this helps inference.
366366
if !oprnd_t.is_numeric() {

compiler/rustc_typeck/src/check/op.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
681681
format!("cannot apply unary operator `{}`", op.as_str()),
682682
);
683683
match actual.kind() {
684-
Uint(_) if op == hir::UnOp::UnNeg => {
684+
Uint(_) if op == hir::UnOp::Neg => {
685685
err.note("unsigned values cannot be negated");
686686

687687
if let hir::ExprKind::Unary(
@@ -711,9 +711,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
711711
Ref(_, ref lty, _) if *lty.kind() == Str => {}
712712
_ => {
713713
let missing_trait = match op {
714-
hir::UnOp::UnNeg => "std::ops::Neg",
715-
hir::UnOp::UnNot => "std::ops::Not",
716-
hir::UnOp::UnDeref => "std::ops::UnDerf",
714+
hir::UnOp::Neg => "std::ops::Neg",
715+
hir::UnOp::Not => "std::ops::Not",
716+
hir::UnOp::Deref => "std::ops::UnDerf",
717717
};
718718
suggest_impl_missing(&mut err, operand_ty, &missing_trait);
719719
}
@@ -782,9 +782,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
782782
span_bug!(span, "&& and || are not overloadable")
783783
}
784784
}
785-
} else if let Op::Unary(hir::UnOp::UnNot, _) = op {
785+
} else if let Op::Unary(hir::UnOp::Not, _) = op {
786786
(sym::not, lang.not_trait())
787-
} else if let Op::Unary(hir::UnOp::UnNeg, _) = op {
787+
} else if let Op::Unary(hir::UnOp::Neg, _) = op {
788788
(sym::neg, lang.neg_trait())
789789
} else {
790790
bug!("lookup_op_method: op not supported: {:?}", op)

0 commit comments

Comments
 (0)