Skip to content

Commit d19e48d

Browse files
Store ByRef instead of BindingAnnotation in PatInfo
1 parent 6c6b302 commit d19e48d

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

compiler/rustc_ast/src/ast.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,15 @@ pub enum ByRef {
707707
No,
708708
}
709709

710+
impl ByRef {
711+
pub fn cap_ref_mutability(mut self, mutbl: Mutability) -> Self {
712+
if let ByRef::Yes(old_mutbl) = &mut self {
713+
*old_mutbl = cmp::min(*old_mutbl, mutbl);
714+
}
715+
self
716+
}
717+
}
718+
710719
/// Explicit binding annotations given in the HIR for a binding. Note
711720
/// that this is not the final binding *mode* that we infer after type
712721
/// inference.
@@ -732,13 +741,6 @@ impl BindingAnnotation {
732741
Self::MUT_REF_MUT => "mut ref mut ",
733742
}
734743
}
735-
736-
pub fn cap_ref_mutability(mut self, mutbl: Mutability) -> Self {
737-
if let ByRef::Yes(old_mutbl) = &mut self.0 {
738-
*old_mutbl = cmp::min(*old_mutbl, mutbl);
739-
}
740-
self
741-
}
742744
}
743745

744746
#[derive(Clone, Encodable, Decodable, Debug)]

compiler/rustc_hir_typeck/src/pat.rs

+21-23
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct TopInfo<'tcx> {
7979

8080
#[derive(Copy, Clone)]
8181
struct PatInfo<'tcx, 'a> {
82-
binding_mode: BindingAnnotation,
82+
binding_mode: ByRef,
8383
max_ref_mutbl: Mutability,
8484
top_info: TopInfo<'tcx>,
8585
decl_origin: Option<DeclOrigin<'a>>,
@@ -125,8 +125,6 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
125125
}
126126
}
127127

128-
const INITIAL_BM: BindingAnnotation = BindingAnnotation(ByRef::No, Mutability::Not);
129-
130128
/// Mode for adjusting the expected type and binding mode.
131129
enum AdjustMode {
132130
/// Peel off all immediate reference types.
@@ -163,7 +161,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
163161
) {
164162
let info = TopInfo { expected, origin_expr, span };
165163
let pat_info = PatInfo {
166-
binding_mode: INITIAL_BM,
164+
binding_mode: ByRef::No,
167165
max_ref_mutbl: Mutability::Mut,
168166
top_info: info,
169167
decl_origin,
@@ -296,43 +294,43 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
296294
&self,
297295
pat: &'tcx Pat<'tcx>,
298296
expected: Ty<'tcx>,
299-
def_bm: BindingAnnotation,
297+
def_br: ByRef,
300298
adjust_mode: AdjustMode,
301299
max_ref_mutbl: Mutability,
302-
) -> (Ty<'tcx>, BindingAnnotation, Mutability, bool) {
303-
if let ByRef::Yes(mutbl) = def_bm.0 {
300+
) -> (Ty<'tcx>, ByRef, Mutability, bool) {
301+
if let ByRef::Yes(mutbl) = def_br {
304302
debug_assert!(mutbl <= max_ref_mutbl);
305303
}
306304
match adjust_mode {
307-
AdjustMode::Pass => (expected, def_bm, max_ref_mutbl, false),
308-
AdjustMode::Reset => (expected, INITIAL_BM, Mutability::Mut, false),
305+
AdjustMode::Pass => (expected, def_br, max_ref_mutbl, false),
306+
AdjustMode::Reset => (expected, ByRef::No, Mutability::Mut, false),
309307
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl) => {
310-
let mutbls_match = def_bm.0 == ByRef::Yes(ref_pat_mutbl);
308+
let mutbls_match = def_br == ByRef::Yes(ref_pat_mutbl);
311309
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
312310
if mutbls_match {
313311
debug!("consuming inherited reference");
314-
(expected, INITIAL_BM, cmp::min(max_ref_mutbl, ref_pat_mutbl), true)
312+
(expected, ByRef::No, cmp::min(max_ref_mutbl, ref_pat_mutbl), true)
315313
} else {
316314
let (new_ty, new_bm, max_ref_mutbl) = if ref_pat_mutbl == Mutability::Mut {
317315
self.peel_off_references(
318316
pat,
319317
expected,
320-
def_bm,
318+
def_br,
321319
Mutability::Not,
322320
max_ref_mutbl,
323321
)
324322
} else {
325-
(expected, def_bm.cap_ref_mutability(Mutability::Not), Mutability::Not)
323+
(expected, def_br.cap_ref_mutability(Mutability::Not), Mutability::Not)
326324
};
327325
(new_ty, new_bm, max_ref_mutbl, false)
328326
}
329327
} else {
330-
(expected, INITIAL_BM, max_ref_mutbl, mutbls_match)
328+
(expected, ByRef::No, max_ref_mutbl, mutbls_match)
331329
}
332330
}
333331
AdjustMode::Peel => {
334332
let peeled =
335-
self.peel_off_references(pat, expected, def_bm, Mutability::Mut, max_ref_mutbl);
333+
self.peel_off_references(pat, expected, def_br, Mutability::Mut, max_ref_mutbl);
336334
(peeled.0, peeled.1, peeled.2, false)
337335
}
338336
}
@@ -413,10 +411,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
413411
&self,
414412
pat: &'tcx Pat<'tcx>,
415413
expected: Ty<'tcx>,
416-
mut def_bm: BindingAnnotation,
414+
mut def_br: ByRef,
417415
max_peelable_mutability: Mutability,
418416
mut max_ref_mutability: Mutability,
419-
) -> (Ty<'tcx>, BindingAnnotation, Mutability) {
417+
) -> (Ty<'tcx>, ByRef, Mutability) {
420418
let mut expected = self.try_structurally_resolve_type(pat.span, expected);
421419
// Peel off as many `&` or `&mut` from the scrutinee type as possible. For example,
422420
// for `match &&&mut Some(5)` the loop runs three times, aborting when it reaches
@@ -437,7 +435,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
437435
pat_adjustments.push(expected);
438436

439437
expected = self.try_structurally_resolve_type(pat.span, inner_ty);
440-
def_bm.0 = ByRef::Yes(match def_bm.0 {
438+
def_br = ByRef::Yes(match def_br {
441439
// If default binding mode is by value, make it `ref` or `ref mut`
442440
// (depending on whether we observe `&` or `&mut`).
443441
ByRef::No |
@@ -450,21 +448,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
450448
}
451449

452450
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
453-
def_bm = def_bm.cap_ref_mutability(max_ref_mutability);
454-
if def_bm.0 == ByRef::Yes(Mutability::Not) {
451+
def_br = def_br.cap_ref_mutability(max_ref_mutability);
452+
if def_br == ByRef::Yes(Mutability::Not) {
455453
max_ref_mutability = Mutability::Not;
456454
}
457455
}
458456

459457
if !pat_adjustments.is_empty() {
460-
debug!("default binding mode is now {:?}", def_bm);
458+
debug!("default binding mode is now {:?}", def_br);
461459
self.typeck_results
462460
.borrow_mut()
463461
.pat_adjustments_mut()
464462
.insert(pat.hir_id, pat_adjustments);
465463
}
466464

467-
(expected, def_bm, max_ref_mutability)
465+
(expected, def_br, max_ref_mutability)
468466
}
469467

470468
fn check_pat_lit(
@@ -675,7 +673,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
675673
expected: Ty<'tcx>,
676674
pat_info: PatInfo<'tcx, '_>,
677675
) -> Ty<'tcx> {
678-
let PatInfo { binding_mode: BindingAnnotation(def_br, _), top_info: ti, .. } = pat_info;
676+
let PatInfo { binding_mode: def_br, top_info: ti, .. } = pat_info;
679677

680678
// Determine the binding mode...
681679
let bm = match ba {

0 commit comments

Comments
 (0)