Skip to content

Commit a9f3f02

Browse files
authored
Rollup merge of #137712 - meithecatte:extract-binding-mode, r=oli-obk
Clean up TypeckResults::extract_binding_mode - Remove the `Option` from the result type, as `None` is never returned. - Document the difference from the `BindingMode` in `PatKind::Binding`.
2 parents dedf61a + 5765005 commit a9f3f02

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

compiler/rustc_hir/src/hir.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,12 @@ pub enum PatKind<'hir> {
16831683
/// The `HirId` is the canonical ID for the variable being bound,
16841684
/// (e.g., in `Ok(x) | Err(x)`, both `x` use the same canonical ID),
16851685
/// which is the pattern ID of the first `x`.
1686+
///
1687+
/// The `BindingMode` is what's provided by the user, before match
1688+
/// ergonomics are applied. For the binding mode actually in use,
1689+
/// see [`TypeckResults::extract_binding_mode`].
1690+
///
1691+
/// [`TypeckResults::extract_binding_mode`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.extract_binding_mode
16861692
Binding(BindingMode, HirId, Ident, Option<&'hir Pat<'hir>>),
16871693

16881694
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+32-36
Original file line numberDiff line numberDiff line change
@@ -895,48 +895,44 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
895895
match pat.kind {
896896
PatKind::Binding(_, canonical_id, ..) => {
897897
debug!("walk_pat: binding place={:?} pat={:?}", place, pat);
898-
if let Some(bm) = self
898+
let bm = self
899899
.cx
900900
.typeck_results()
901-
.extract_binding_mode(tcx.sess, pat.hir_id, pat.span)
902-
{
903-
debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
901+
.extract_binding_mode(tcx.sess, pat.hir_id, pat.span);
902+
debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
904903

905-
// pat_ty: the type of the binding being produced.
906-
let pat_ty = self.node_ty(pat.hir_id)?;
907-
debug!("walk_pat: pat_ty={:?}", pat_ty);
904+
// pat_ty: the type of the binding being produced.
905+
let pat_ty = self.node_ty(pat.hir_id)?;
906+
debug!("walk_pat: pat_ty={:?}", pat_ty);
908907

909-
let def = Res::Local(canonical_id);
910-
if let Ok(ref binding_place) =
911-
self.cat_res(pat.hir_id, pat.span, pat_ty, def)
912-
{
913-
self.delegate.borrow_mut().bind(binding_place, binding_place.hir_id);
914-
}
908+
let def = Res::Local(canonical_id);
909+
if let Ok(ref binding_place) = self.cat_res(pat.hir_id, pat.span, pat_ty, def) {
910+
self.delegate.borrow_mut().bind(binding_place, binding_place.hir_id);
911+
}
915912

916-
// Subtle: MIR desugaring introduces immutable borrows for each pattern
917-
// binding when lowering pattern guards to ensure that the guard does not
918-
// modify the scrutinee.
919-
if has_guard {
920-
self.delegate.borrow_mut().borrow(
921-
place,
922-
discr_place.hir_id,
923-
BorrowKind::Immutable,
924-
);
925-
}
913+
// Subtle: MIR desugaring introduces immutable borrows for each pattern
914+
// binding when lowering pattern guards to ensure that the guard does not
915+
// modify the scrutinee.
916+
if has_guard {
917+
self.delegate.borrow_mut().borrow(
918+
place,
919+
discr_place.hir_id,
920+
BorrowKind::Immutable,
921+
);
922+
}
926923

927-
// It is also a borrow or copy/move of the value being matched.
928-
// In a cases of pattern like `let pat = upvar`, don't use the span
929-
// of the pattern, as this just looks confusing, instead use the span
930-
// of the discriminant.
931-
match bm.0 {
932-
hir::ByRef::Yes(m) => {
933-
let bk = ty::BorrowKind::from_mutbl(m);
934-
self.delegate.borrow_mut().borrow(place, discr_place.hir_id, bk);
935-
}
936-
hir::ByRef::No => {
937-
debug!("walk_pat binding consuming pat");
938-
self.consume_or_copy(place, discr_place.hir_id);
939-
}
924+
// It is also a borrow or copy/move of the value being matched.
925+
// In a cases of pattern like `let pat = upvar`, don't use the span
926+
// of the pattern, as this just looks confusing, instead use the span
927+
// of the discriminant.
928+
match bm.0 {
929+
hir::ByRef::Yes(m) => {
930+
let bk = ty::BorrowKind::from_mutbl(m);
931+
self.delegate.borrow_mut().borrow(place, discr_place.hir_id, bk);
932+
}
933+
hir::ByRef::No => {
934+
debug!("walk_pat binding consuming pat");
935+
self.consume_or_copy(place, discr_place.hir_id);
940936
}
941937
}
942938
}

compiler/rustc_hir_typeck/src/writeback.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
319319
match p.kind {
320320
hir::PatKind::Binding(..) => {
321321
let typeck_results = self.fcx.typeck_results.borrow();
322-
if let Some(bm) =
323-
typeck_results.extract_binding_mode(self.tcx().sess, p.hir_id, p.span)
324-
{
325-
self.typeck_results.pat_binding_modes_mut().insert(p.hir_id, bm);
326-
}
322+
let bm = typeck_results.extract_binding_mode(self.tcx().sess, p.hir_id, p.span);
323+
self.typeck_results.pat_binding_modes_mut().insert(p.hir_id, bm);
327324
}
328325
hir::PatKind::Struct(_, fields, _) => {
329326
for field in fields {

compiler/rustc_middle/src/ty/typeck_results.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,10 @@ impl<'tcx> TypeckResults<'tcx> {
394394
matches!(self.type_dependent_defs().get(expr.hir_id), Some(Ok((DefKind::AssocFn, _))))
395395
}
396396

397-
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {
398-
self.pat_binding_modes().get(id).copied().or_else(|| {
397+
/// Returns the computed binding mode for a `PatKind::Binding` pattern
398+
/// (after match ergonomics adjustments).
399+
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> BindingMode {
400+
self.pat_binding_modes().get(id).copied().unwrap_or_else(|| {
399401
s.dcx().span_bug(sp, "missing binding mode");
400402
})
401403
}

src/tools/clippy/clippy_utils/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,6 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
10941094
pat.each_binding_or_first(&mut |_, id, span, _| match cx
10951095
.typeck_results()
10961096
.extract_binding_mode(cx.sess(), id, span)
1097-
.unwrap()
10981097
.0
10991098
{
11001099
ByRef::No if !is_copy(cx, cx.typeck_results().node_type(id)) => {

0 commit comments

Comments
 (0)