1
Fork 0

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`.
This commit is contained in:
Maja Kądziołka 2025-02-27 09:14:31 +01:00
parent 96cfc75584
commit 5765005a7f
No known key found for this signature in database
5 changed files with 45 additions and 45 deletions

View file

@ -1683,6 +1683,12 @@ pub enum PatKind<'hir> {
/// The `HirId` is the canonical ID for the variable being bound, /// The `HirId` is the canonical ID for the variable being bound,
/// (e.g., in `Ok(x) | Err(x)`, both `x` use the same canonical ID), /// (e.g., in `Ok(x) | Err(x)`, both `x` use the same canonical ID),
/// which is the pattern ID of the first `x`. /// which is the pattern ID of the first `x`.
///
/// The `BindingMode` is what's provided by the user, before match
/// ergonomics are applied. For the binding mode actually in use,
/// see [`TypeckResults::extract_binding_mode`].
///
/// [`TypeckResults::extract_binding_mode`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.extract_binding_mode
Binding(BindingMode, HirId, Ident, Option<&'hir Pat<'hir>>), Binding(BindingMode, HirId, Ident, Option<&'hir Pat<'hir>>),
/// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`). /// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).

View file

@ -895,11 +895,10 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
match pat.kind { match pat.kind {
PatKind::Binding(_, canonical_id, ..) => { PatKind::Binding(_, canonical_id, ..) => {
debug!("walk_pat: binding place={:?} pat={:?}", place, pat); debug!("walk_pat: binding place={:?} pat={:?}", place, pat);
if let Some(bm) = self let bm = self
.cx .cx
.typeck_results() .typeck_results()
.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) .extract_binding_mode(tcx.sess, pat.hir_id, pat.span);
{
debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm); debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
// pat_ty: the type of the binding being produced. // pat_ty: the type of the binding being produced.
@ -907,9 +906,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
debug!("walk_pat: pat_ty={:?}", pat_ty); debug!("walk_pat: pat_ty={:?}", pat_ty);
let def = Res::Local(canonical_id); let def = Res::Local(canonical_id);
if let Ok(ref binding_place) = if let Ok(ref binding_place) = self.cat_res(pat.hir_id, pat.span, pat_ty, def) {
self.cat_res(pat.hir_id, pat.span, pat_ty, def)
{
self.delegate.borrow_mut().bind(binding_place, binding_place.hir_id); self.delegate.borrow_mut().bind(binding_place, binding_place.hir_id);
} }
@ -939,7 +936,6 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
} }
} }
} }
}
PatKind::Deref(subpattern) => { PatKind::Deref(subpattern) => {
// A deref pattern is a bit special: the binding mode of its inner bindings // A deref pattern is a bit special: the binding mode of its inner bindings
// determines whether to borrow *at the level of the deref pattern* rather than // determines whether to borrow *at the level of the deref pattern* rather than

View file

@ -319,12 +319,9 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
match p.kind { match p.kind {
hir::PatKind::Binding(..) => { hir::PatKind::Binding(..) => {
let typeck_results = self.fcx.typeck_results.borrow(); let typeck_results = self.fcx.typeck_results.borrow();
if let Some(bm) = let bm = typeck_results.extract_binding_mode(self.tcx().sess, p.hir_id, p.span);
typeck_results.extract_binding_mode(self.tcx().sess, p.hir_id, p.span)
{
self.typeck_results.pat_binding_modes_mut().insert(p.hir_id, bm); self.typeck_results.pat_binding_modes_mut().insert(p.hir_id, bm);
} }
}
hir::PatKind::Struct(_, fields, _) => { hir::PatKind::Struct(_, fields, _) => {
for field in fields { for field in fields {
self.visit_field_id(field.hir_id); self.visit_field_id(field.hir_id);

View file

@ -394,8 +394,10 @@ impl<'tcx> TypeckResults<'tcx> {
matches!(self.type_dependent_defs().get(expr.hir_id), Some(Ok((DefKind::AssocFn, _)))) matches!(self.type_dependent_defs().get(expr.hir_id), Some(Ok((DefKind::AssocFn, _))))
} }
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> { /// Returns the computed binding mode for a `PatKind::Binding` pattern
self.pat_binding_modes().get(id).copied().or_else(|| { /// (after match ergonomics adjustments).
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> BindingMode {
self.pat_binding_modes().get(id).copied().unwrap_or_else(|| {
s.dcx().span_bug(sp, "missing binding mode"); s.dcx().span_bug(sp, "missing binding mode");
}) })
} }

View file

@ -1095,7 +1095,6 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
pat.each_binding_or_first(&mut |_, id, span, _| match cx pat.each_binding_or_first(&mut |_, id, span, _| match cx
.typeck_results() .typeck_results()
.extract_binding_mode(cx.sess(), id, span) .extract_binding_mode(cx.sess(), id, span)
.unwrap()
.0 .0
{ {
ByRef::No if !is_copy(cx, cx.typeck_results().node_type(id)) => { ByRef::No if !is_copy(cx, cx.typeck_results().node_type(id)) => {