Skip to content

Commit 1ae9604

Browse files
committed
Don't use ByRef for the thir::PatKind::DerefPattern borrow mode
This avoids having to awkwardly ignore the `Pinnedness` field.
1 parent 0383197 commit 1ae9604

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_middle::hir::place::ProjectionKind;
2323
// Export these here so that Clippy can use them.
2424
pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
2525
use rustc_middle::mir::FakeReadCause;
26+
use rustc_middle::thir::DerefPatBorrowMode;
2627
use rustc_middle::ty::{
2728
self, BorrowKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt as _, adjustment,
2829
};
@@ -1004,7 +1005,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10041005
// Deref patterns on boxes don't borrow, so we ignore them here.
10051006
// HACK: this could be a fake pattern corresponding to a deref inserted by match
10061007
// ergonomics, in which case `pat.hir_id` will be the id of the subpattern.
1007-
if let hir::ByRef::Yes(_, mutability) =
1008+
if let DerefPatBorrowMode::Borrow(mutability) =
10081009
self.cx.typeck_results().deref_pat_borrow_mode(place.place.ty(), subpattern)
10091010
{
10101011
let bk = ty::BorrowKind::from_mutbl(mutability);
@@ -1891,9 +1892,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18911892
) -> Result<PlaceWithHirId<'tcx>, Cx::Error> {
18921893
match self.cx.typeck_results().deref_pat_borrow_mode(base_place.place.ty(), inner) {
18931894
// Deref patterns on boxes are lowered using a built-in deref.
1894-
hir::ByRef::No => self.cat_deref(hir_id, base_place),
1895+
DerefPatBorrowMode::Box => self.cat_deref(hir_id, base_place),
18951896
// For other types, we create a temporary to match on.
1896-
hir::ByRef::Yes(_, mutability) => {
1897+
DerefPatBorrowMode::Borrow(mutability) => {
18971898
let re_erased = self.cx.tcx().lifetimes.re_erased;
18981899
let ty = Ty::new_ref(self.cx.tcx(), re_erased, target_ty, mutability);
18991900
// A deref pattern stores the result of `Deref::deref` or `DerefMut::deref_mut` ...

compiler/rustc_middle/src/thir.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::ops::Index;
1414
use std::sync::Arc;
1515

1616
use rustc_abi::{FieldIdx, Integer, Size, VariantIdx};
17-
use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece};
17+
use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece, Mutability};
1818
use rustc_hir as hir;
1919
use rustc_hir::def_id::DefId;
2020
use rustc_hir::{BindingMode, ByRef, HirId, MatchSource, RangeEnd};
@@ -811,11 +811,11 @@ pub enum PatKind<'tcx> {
811811
DerefPattern {
812812
subpattern: Box<Pat<'tcx>>,
813813
/// Whether the pattern scrutinee needs to be borrowed in order to call `Deref::deref` or
814-
/// `DerefMut::deref_mut`, and if so, which. This is `ByRef::No` for deref patterns on
814+
/// `DerefMut::deref_mut`, and if so, which. This is `DerefPatBorrowMode::Box` for deref patterns on
815815
/// boxes; they are lowered using a built-in deref rather than a method call, thus they
816816
/// don't borrow the scrutinee.
817817
#[type_visitable(ignore)]
818-
borrow: ByRef,
818+
borrow: DerefPatBorrowMode,
819819
},
820820

821821
/// One of the following:
@@ -879,6 +879,12 @@ pub enum PatKind<'tcx> {
879879
Error(ErrorGuaranteed),
880880
}
881881

882+
#[derive(Copy, Clone, Debug, HashStable)]
883+
pub enum DerefPatBorrowMode {
884+
Borrow(Mutability),
885+
Box,
886+
}
887+
882888
/// A range pattern.
883889
/// The boundaries must be of the same type and that type must be numeric.
884890
#[derive(Clone, Debug, PartialEq, HashStable, TypeVisitable)]

compiler/rustc_middle/src/ty/typeck_results.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap};
1111
use rustc_hir::hir_id::OwnerId;
1212
use rustc_hir::{
1313
self as hir, BindingMode, ByRef, HirId, ItemLocalId, ItemLocalMap, ItemLocalSet, Mutability,
14-
Pinnedness,
1514
};
1615
use rustc_index::IndexVec;
1716
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
@@ -21,6 +20,7 @@ use rustc_span::Span;
2120
use crate::hir::place::Place as HirPlace;
2221
use crate::infer::canonical::Canonical;
2322
use crate::mir::FakeReadCause;
23+
use crate::thir::DerefPatBorrowMode;
2424
use crate::traits::ObligationCause;
2525
use crate::ty::{
2626
self, BoundVar, CanonicalPolyFnSig, ClosureSizeProfileData, GenericArgKind, GenericArgs,
@@ -491,13 +491,18 @@ impl<'tcx> TypeckResults<'tcx> {
491491
/// In most cases, if the pattern recursively contains a `ref mut` binding, we find the inner
492492
/// pattern's scrutinee by calling `DerefMut::deref_mut`, and otherwise we call `Deref::deref`.
493493
/// However, for boxes we can use a built-in deref instead, which doesn't borrow the scrutinee;
494-
/// in this case, we return `ByRef::No`.
495-
pub fn deref_pat_borrow_mode(&self, pointer_ty: Ty<'_>, inner: &hir::Pat<'_>) -> ByRef {
494+
/// in this case, we return `DerefPatBorrowMode::Box`.
495+
pub fn deref_pat_borrow_mode(
496+
&self,
497+
pointer_ty: Ty<'_>,
498+
inner: &hir::Pat<'_>,
499+
) -> DerefPatBorrowMode {
496500
if pointer_ty.is_box() {
497-
ByRef::No
501+
DerefPatBorrowMode::Box
498502
} else {
499-
let mutable = self.pat_has_ref_mut_binding(inner);
500-
ByRef::Yes(Pinnedness::Not, if mutable { Mutability::Mut } else { Mutability::Not })
503+
let mutability =
504+
if self.pat_has_ref_mut_binding(inner) { Mutability::Mut } else { Mutability::Not };
505+
DerefPatBorrowMode::Borrow(mutability)
501506
}
502507
}
503508

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::sync::Arc;
22

33
use rustc_abi::FieldIdx;
4-
use rustc_hir::ByRef;
54
use rustc_middle::mir::*;
65
use rustc_middle::thir::*;
7-
use rustc_middle::ty::{self, Pinnedness, Ty, TypeVisitableExt};
6+
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
87

98
use crate::builder::Builder;
109
use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
@@ -303,12 +302,8 @@ impl<'tcx> MatchPairTree<'tcx> {
303302
None
304303
}
305304

306-
PatKind::DerefPattern { borrow: ByRef::Yes(Pinnedness::Pinned, _), .. } => {
307-
rustc_middle::bug!("RefPin pattern on non-`Pin` type {:?}", pattern.ty)
308-
}
309-
310305
PatKind::Deref { ref subpattern }
311-
| PatKind::DerefPattern { ref subpattern, borrow: ByRef::No } => {
306+
| PatKind::DerefPattern { ref subpattern, borrow: DerefPatBorrowMode::Box } => {
312307
MatchPairTree::for_pattern(
313308
place_builder.deref(),
314309
subpattern,
@@ -321,7 +316,7 @@ impl<'tcx> MatchPairTree<'tcx> {
321316

322317
PatKind::DerefPattern {
323318
ref subpattern,
324-
borrow: ByRef::Yes(Pinnedness::Not, mutability),
319+
borrow: DerefPatBorrowMode::Borrow(mutability),
325320
} => {
326321
// Create a new temporary for each deref pattern.
327322
// FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use std::mem;
1111
use std::sync::Arc;
1212

1313
use itertools::{Itertools, Position};
14-
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
14+
use rustc_abi::{FIRST_VARIANT, VariantIdx};
1515
use rustc_data_structures::fx::FxIndexMap;
1616
use rustc_data_structures::stack::ensure_sufficient_stack;
17-
use rustc_hir::{BindingMode, ByRef, LangItem, LetStmt, LocalSource, Node, Pinnedness};
17+
use rustc_hir::{BindingMode, ByRef, LangItem, LetStmt, LocalSource, Node};
1818
use rustc_middle::middle::region::{self, TempLifetime};
1919
use rustc_middle::mir::*;
2020
use rustc_middle::thir::{self, *};
@@ -920,10 +920,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
920920
visit_subpat(self, subpattern, &user_tys.deref(), f);
921921
}
922922

923-
PatKind::DerefPattern { ref subpattern, borrow: ByRef::Yes(Pinnedness::Pinned, _) } => {
924-
visit_subpat(self, subpattern, &user_tys.leaf(FieldIdx::ZERO).deref(), f);
925-
}
926-
927923
PatKind::DerefPattern { ref subpattern, .. } => {
928924
visit_subpat(self, subpattern, &ProjectedUserTypesNode::None, f);
929925
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_index::Idx;
1616
use rustc_infer::infer::TyCtxtInferExt;
1717
use rustc_middle::mir::interpret::LitToConstInput;
1818
use rustc_middle::thir::{
19-
Ascription, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
19+
Ascription, DerefPatBorrowMode, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
2020
};
2121
use rustc_middle::ty::adjustment::{PatAdjust, PatAdjustment};
2222
use rustc_middle::ty::layout::IntegerExt;
@@ -325,7 +325,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
325325
}
326326
hir::PatKind::Box(subpattern) => PatKind::DerefPattern {
327327
subpattern: self.lower_pattern(subpattern),
328-
borrow: hir::ByRef::No,
328+
borrow: DerefPatBorrowMode::Box,
329329
},
330330

331331
hir::PatKind::Slice(prefix, slice, suffix) => {

0 commit comments

Comments
 (0)