Skip to content

Commit 56ee852

Browse files
committed
Auto merge of #106090 - WaffleLapkin:dereffffffffff, r=Nilstrieb
Remove some `ref` patterns from the compiler Previous PR: #105368 r? `@Nilstrieb`
2 parents 51d50ea + 65d1e8d commit 56ee852

File tree

53 files changed

+529
-592
lines changed

Some content is hidden

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

53 files changed

+529
-592
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ impl<'tcx> Into<InterpErrorInfo<'tcx>> for ConstEvalErrKind {
3636
impl fmt::Display for ConstEvalErrKind {
3737
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3838
use self::ConstEvalErrKind::*;
39-
match *self {
39+
match self {
4040
ConstAccessesStatic => write!(f, "constant accesses static"),
4141
ModifiedGlobal => {
4242
write!(f, "modifying a static's initial value from another static's initializer")
4343
}
44-
AssertFailure(ref msg) => write!(f, "{:?}", msg),
44+
AssertFailure(msg) => write!(f, "{:?}", msg),
4545
Panic { msg, line, col, file } => {
4646
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col)
4747
}
48-
Abort(ref msg) => write!(f, "{}", msg),
48+
Abort(msg) => write!(f, "{}", msg),
4949
}
5050
}
5151
}

compiler/rustc_const_eval/src/const_eval/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
533533
let eval_to_int =
534534
|op| ecx.read_immediate(&ecx.eval_operand(op, None)?).map(|x| x.to_const_int());
535535
let err = match msg {
536-
BoundsCheck { ref len, ref index } => {
536+
BoundsCheck { len, index } => {
537537
let len = eval_to_int(len)?;
538538
let index = eval_to_int(index)?;
539539
BoundsCheck { len, index }

compiler/rustc_const_eval/src/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
347347
let new_vptr = self.get_vtable_ptr(ty, data_b.principal())?;
348348
self.write_immediate(Immediate::new_dyn_trait(old_data, new_vptr, self), dest)
349349
}
350-
(_, &ty::Dynamic(ref data, _, ty::Dyn)) => {
350+
(_, &ty::Dynamic(data, _, ty::Dyn)) => {
351351
// Initial cast from sized to dyn trait
352352
let vtable = self.get_vtable_ptr(src_pointee_ty, data.principal())?;
353353
let ptr = self.read_scalar(src)?;

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
7979
}
8080
sym::variant_count => match tp_ty.kind() {
8181
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
82-
ty::Adt(ref adt, _) => {
83-
ConstValue::from_machine_usize(adt.variants().len() as u64, &tcx)
84-
}
82+
ty::Adt(adt, _) => ConstValue::from_machine_usize(adt.variants().len() as u64, &tcx),
8583
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
8684
throw_inval!(TooGeneric)
8785
}

compiler/rustc_const_eval/src/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
863863

864864
write!(fmt, "{id:?}")?;
865865
match self.ecx.memory.alloc_map.get(id) {
866-
Some(&(kind, ref alloc)) => {
866+
Some((kind, alloc)) => {
867867
// normal alloc
868868
write!(fmt, " ({}, ", kind)?;
869869
write_allocation_track_relocs(

compiler/rustc_const_eval/src/interpret/operand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
533533
layout: Option<TyAndLayout<'tcx>>,
534534
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
535535
use rustc_middle::mir::Operand::*;
536-
let op = match *mir_op {
536+
let op = match mir_op {
537537
// FIXME: do some more logic on `move` to invalidate the old location
538-
Copy(place) | Move(place) => self.eval_place_to_op(place, layout)?,
538+
&Copy(place) | &Move(place) => self.eval_place_to_op(place, layout)?,
539539

540-
Constant(ref constant) => {
540+
Constant(constant) => {
541541
let c =
542542
self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal)?;
543543

compiler/rustc_const_eval/src/interpret/step.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
111111
M::retag_place_contents(self, *kind, &dest)?;
112112
}
113113

114-
Intrinsic(box ref intrinsic) => self.emulate_nondiverging_intrinsic(intrinsic)?,
114+
Intrinsic(box intrinsic) => self.emulate_nondiverging_intrinsic(intrinsic)?,
115115

116116
// Statements we do not track.
117117
AscribeUserType(..) => {}
@@ -163,8 +163,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
163163
self.copy_op(&op, &dest, /*allow_transmute*/ false)?;
164164
}
165165

166-
CopyForDeref(ref place) => {
167-
let op = self.eval_place_to_op(*place, Some(dest.layout))?;
166+
CopyForDeref(place) => {
167+
let op = self.eval_place_to_op(place, Some(dest.layout))?;
168168
self.copy_op(&op, &dest, /* allow_transmute*/ false)?;
169169
}
170170

compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
419419
)
420420
}
421421
// Recursive checking
422-
if let Some(ref mut ref_tracking) = self.ref_tracking {
422+
if let Some(ref_tracking) = self.ref_tracking.as_deref_mut() {
423423
// Proceed recursively even for ZST, no reason to skip them!
424424
// `!` is a ZST and we want to validate it.
425425
if let Ok((alloc_id, _offset, _prov)) = self.ecx.ptr_try_get_alloc_id(place.ptr) {

compiler/rustc_const_eval/src/interpret/visitor.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,12 @@ macro_rules! make_value_visitor {
481481
};
482482

483483
// Visit the fields of this value.
484-
match v.layout().fields {
484+
match &v.layout().fields {
485485
FieldsShape::Primitive => {}
486-
FieldsShape::Union(fields) => {
486+
&FieldsShape::Union(fields) => {
487487
self.visit_union(v, fields)?;
488488
}
489-
FieldsShape::Arbitrary { ref offsets, .. } => {
489+
FieldsShape::Arbitrary { offsets, .. } => {
490490
// FIXME: We collect in a vec because otherwise there are lifetime
491491
// errors: Projecting to a field needs access to `ecx`.
492492
let fields: Vec<InterpResult<'tcx, Self::V>> =

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
442442

443443
self.super_rvalue(rvalue, location);
444444

445-
match *rvalue {
445+
match rvalue {
446446
Rvalue::ThreadLocalRef(_) => self.check_op(ops::ThreadLocalAccess),
447447

448448
Rvalue::Use(_)
@@ -451,18 +451,15 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
451451
| Rvalue::Discriminant(..)
452452
| Rvalue::Len(_) => {}
453453

454-
Rvalue::Aggregate(ref kind, ..) => {
455-
if let AggregateKind::Generator(def_id, ..) = kind.as_ref() {
456-
if let Some(generator_kind) = self.tcx.generator_kind(def_id.to_def_id()) {
457-
if matches!(generator_kind, hir::GeneratorKind::Async(..)) {
458-
self.check_op(ops::Generator(generator_kind));
459-
}
460-
}
454+
Rvalue::Aggregate(kind, ..) => {
455+
if let AggregateKind::Generator(def_id, ..) = kind.as_ref()
456+
&& let Some(generator_kind @ hir::GeneratorKind::Async(..)) = self.tcx.generator_kind(def_id.to_def_id())
457+
{
458+
self.check_op(ops::Generator(generator_kind));
461459
}
462460
}
463461

464-
Rvalue::Ref(_, kind @ BorrowKind::Mut { .. }, ref place)
465-
| Rvalue::Ref(_, kind @ BorrowKind::Unique, ref place) => {
462+
Rvalue::Ref(_, kind @ (BorrowKind::Mut { .. } | BorrowKind::Unique), place) => {
466463
let ty = place.ty(self.body, self.tcx).ty;
467464
let is_allowed = match ty.kind() {
468465
// Inside a `static mut`, `&mut [...]` is allowed.
@@ -491,12 +488,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
491488
}
492489
}
493490

494-
Rvalue::AddressOf(Mutability::Mut, ref place) => {
491+
Rvalue::AddressOf(Mutability::Mut, place) => {
495492
self.check_mut_borrow(place.local, hir::BorrowKind::Raw)
496493
}
497494

498-
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, ref place)
499-
| Rvalue::AddressOf(Mutability::Not, ref place) => {
495+
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Shallow, place)
496+
| Rvalue::AddressOf(Mutability::Not, place) => {
500497
let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(
501498
&self.ccx,
502499
&mut |local| self.qualifs.has_mut_interior(self.ccx, local, location),
@@ -564,7 +561,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
564561
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => {}
565562
Rvalue::ShallowInitBox(_, _) => {}
566563

567-
Rvalue::UnaryOp(_, ref operand) => {
564+
Rvalue::UnaryOp(_, operand) => {
568565
let ty = operand.ty(self.body, self.tcx);
569566
if is_int_bool_or_char(ty) {
570567
// Int, bool, and char operations are fine.
@@ -575,8 +572,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
575572
}
576573
}
577574

578-
Rvalue::BinaryOp(op, box (ref lhs, ref rhs))
579-
| Rvalue::CheckedBinaryOp(op, box (ref lhs, ref rhs)) => {
575+
Rvalue::BinaryOp(op, box (lhs, rhs))
576+
| Rvalue::CheckedBinaryOp(op, box (lhs, rhs)) => {
580577
let lhs_ty = lhs.ty(self.body, self.tcx);
581578
let rhs_ty = rhs.ty(self.body, self.tcx);
582579

@@ -585,13 +582,16 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
585582
} else if lhs_ty.is_fn_ptr() || lhs_ty.is_unsafe_ptr() {
586583
assert_eq!(lhs_ty, rhs_ty);
587584
assert!(
588-
op == BinOp::Eq
589-
|| op == BinOp::Ne
590-
|| op == BinOp::Le
591-
|| op == BinOp::Lt
592-
|| op == BinOp::Ge
593-
|| op == BinOp::Gt
594-
|| op == BinOp::Offset
585+
matches!(
586+
op,
587+
BinOp::Eq
588+
| BinOp::Ne
589+
| BinOp::Le
590+
| BinOp::Lt
591+
| BinOp::Ge
592+
| BinOp::Gt
593+
| BinOp::Offset
594+
)
595595
);
596596

597597
self.check_op(ops::RawPtrComparison);

compiler/rustc_const_eval/src/transform/promote_consts.rs

+49-53
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
133133
}
134134
_ => { /* mark as unpromotable below */ }
135135
}
136-
} else if let TempState::Defined { ref mut uses, .. } = *temp {
136+
} else if let TempState::Defined { uses, .. } = temp {
137137
// We always allow borrows, even mutable ones, as we need
138138
// to promote mutable borrows of some ZSTs e.g., `&mut []`.
139139
let allowed_use = match context {
@@ -748,7 +748,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
748748
if loc.statement_index < num_stmts {
749749
let (mut rvalue, source_info) = {
750750
let statement = &mut self.source[loc.block].statements[loc.statement_index];
751-
let StatementKind::Assign(box (_, ref mut rhs)) = statement.kind else {
751+
let StatementKind::Assign(box (_, rhs)) = &mut statement.kind else {
752752
span_bug!(
753753
statement.source_info.span,
754754
"{:?} is not an assignment",
@@ -778,9 +778,9 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
778778
self.source[loc.block].terminator().clone()
779779
} else {
780780
let terminator = self.source[loc.block].terminator_mut();
781-
let target = match terminator.kind {
782-
TerminatorKind::Call { target: Some(target), .. } => target,
783-
ref kind => {
781+
let target = match &terminator.kind {
782+
TerminatorKind::Call { target: Some(target), .. } => *target,
783+
kind => {
784784
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
785785
}
786786
};
@@ -814,7 +814,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
814814
..terminator
815815
};
816816
}
817-
ref kind => {
817+
kind => {
818818
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
819819
}
820820
};
@@ -847,54 +847,50 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
847847
let local_decls = &mut self.source.local_decls;
848848
let loc = candidate.location;
849849
let statement = &mut blocks[loc.block].statements[loc.statement_index];
850-
match statement.kind {
851-
StatementKind::Assign(box (
852-
_,
853-
Rvalue::Ref(ref mut region, borrow_kind, ref mut place),
854-
)) => {
855-
// Use the underlying local for this (necessarily interior) borrow.
856-
let ty = local_decls[place.local].ty;
857-
let span = statement.source_info.span;
858-
859-
let ref_ty = tcx.mk_ref(
860-
tcx.lifetimes.re_erased,
861-
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
862-
);
850+
let StatementKind::Assign(box (_, Rvalue::Ref(region, borrow_kind, place))) = &mut statement.kind else {
851+
bug!()
852+
};
863853

864-
*region = tcx.lifetimes.re_erased;
865-
866-
let mut projection = vec![PlaceElem::Deref];
867-
projection.extend(place.projection);
868-
place.projection = tcx.intern_place_elems(&projection);
869-
870-
// Create a temp to hold the promoted reference.
871-
// This is because `*r` requires `r` to be a local,
872-
// otherwise we would use the `promoted` directly.
873-
let mut promoted_ref = LocalDecl::new(ref_ty, span);
874-
promoted_ref.source_info = statement.source_info;
875-
let promoted_ref = local_decls.push(promoted_ref);
876-
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
877-
878-
let promoted_ref_statement = Statement {
879-
source_info: statement.source_info,
880-
kind: StatementKind::Assign(Box::new((
881-
Place::from(promoted_ref),
882-
Rvalue::Use(promoted_operand(ref_ty, span)),
883-
))),
884-
};
885-
self.extra_statements.push((loc, promoted_ref_statement));
886-
887-
Rvalue::Ref(
888-
tcx.lifetimes.re_erased,
889-
borrow_kind,
890-
Place {
891-
local: mem::replace(&mut place.local, promoted_ref),
892-
projection: List::empty(),
893-
},
894-
)
895-
}
896-
_ => bug!(),
897-
}
854+
// Use the underlying local for this (necessarily interior) borrow.
855+
let ty = local_decls[place.local].ty;
856+
let span = statement.source_info.span;
857+
858+
let ref_ty = tcx.mk_ref(
859+
tcx.lifetimes.re_erased,
860+
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
861+
);
862+
863+
*region = tcx.lifetimes.re_erased;
864+
865+
let mut projection = vec![PlaceElem::Deref];
866+
projection.extend(place.projection);
867+
place.projection = tcx.intern_place_elems(&projection);
868+
869+
// Create a temp to hold the promoted reference.
870+
// This is because `*r` requires `r` to be a local,
871+
// otherwise we would use the `promoted` directly.
872+
let mut promoted_ref = LocalDecl::new(ref_ty, span);
873+
promoted_ref.source_info = statement.source_info;
874+
let promoted_ref = local_decls.push(promoted_ref);
875+
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
876+
877+
let promoted_ref_statement = Statement {
878+
source_info: statement.source_info,
879+
kind: StatementKind::Assign(Box::new((
880+
Place::from(promoted_ref),
881+
Rvalue::Use(promoted_operand(ref_ty, span)),
882+
))),
883+
};
884+
self.extra_statements.push((loc, promoted_ref_statement));
885+
886+
Rvalue::Ref(
887+
tcx.lifetimes.re_erased,
888+
*borrow_kind,
889+
Place {
890+
local: mem::replace(&mut place.local, promoted_ref),
891+
projection: List::empty(),
892+
},
893+
)
898894
};
899895

900896
assert_eq!(self.new_block(), START_BLOCK);

compiler/rustc_data_structures/src/graph/implementation/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ fn test_adjacent_edges<N: PartialEq + Debug, E: PartialEq + Debug>(
7070
"counter={:?} expected={:?} edge_index={:?} edge={:?}",
7171
counter, expected_incoming[counter], edge_index, edge
7272
);
73-
match expected_incoming[counter] {
74-
(ref e, ref n) => {
73+
match &expected_incoming[counter] {
74+
(e, n) => {
7575
assert!(e == &edge.data);
7676
assert!(n == graph.node_data(edge.source()));
7777
assert!(start_index == edge.target);
@@ -88,8 +88,8 @@ fn test_adjacent_edges<N: PartialEq + Debug, E: PartialEq + Debug>(
8888
"counter={:?} expected={:?} edge_index={:?} edge={:?}",
8989
counter, expected_outgoing[counter], edge_index, edge
9090
);
91-
match expected_outgoing[counter] {
92-
(ref e, ref n) => {
91+
match &expected_outgoing[counter] {
92+
(e, n) => {
9393
assert!(e == &edge.data);
9494
assert!(start_index == edge.source);
9595
assert!(n == graph.node_data(edge.target));

0 commit comments

Comments
 (0)