Skip to content

Commit bffc747

Browse files
No more Option<Option<>>
1 parent 2efa237 commit bffc747

File tree

1 file changed

+15
-19
lines changed
  • compiler/rustc_hir_typeck/src

1 file changed

+15
-19
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,33 @@ enum AdjustMode {
157157
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
158158
enum MutblCap {
159159
/// Mutability restricted to immutable.
160+
Not,
161+
162+
/// Mutability restricted to immutable, but only because of the pattern
163+
/// (not the scrutinee type).
160164
///
161165
/// The contained span, if present, points to an `&` pattern
162166
/// that is the reason for the restriction,
163167
/// and which will be reported in a diagnostic.
164168
/// (Said diagnostic is shown only if
165169
/// replacing the `&` pattern with `&mut` would allow the code to compile.)
166-
///
167-
/// (Outer [`Option`] is for whether to show the diagnostic,
168-
/// inner [`Option`] is for whether we have a span we can report)
169-
Not(Option<Option<Span>>),
170+
WeaklyNot(Option<Span>),
171+
170172
/// No restriction on mutability
171173
Mut,
172174
}
173175

174176
impl MutblCap {
175-
fn cap_mutbl_to_not(self, span: Option<Option<Span>>) -> Self {
176-
if let Some(s) = span
177-
&& self != MutblCap::Not(None)
178-
{
179-
MutblCap::Not(Some(s))
180-
} else {
181-
MutblCap::Not(None)
177+
fn cap_to_weakly_not(self, span: Option<Span>) -> Self {
178+
match self {
179+
MutblCap::Not => MutblCap::Not,
180+
_ => MutblCap::WeaklyNot(span),
182181
}
183182
}
184183

185184
fn as_mutbl(self) -> Mutability {
186185
match self {
187-
MutblCap::Not(_) => Mutability::Not,
186+
MutblCap::Not | MutblCap::WeaklyNot(_) => Mutability::Not,
188187
MutblCap::Mut => Mutability::Mut,
189188
}
190189
}
@@ -359,7 +358,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
359358

360359
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
361360
let max_ref_mutbl = if ref_pat_mutbl == Mutability::Not {
362-
max_ref_mutbl.cap_mutbl_to_not(Some(ref_span))
361+
max_ref_mutbl.cap_to_weakly_not(ref_span)
363362
} else {
364363
max_ref_mutbl
365364
};
@@ -507,7 +506,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
507506
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
508507
def_br = def_br.cap_ref_mutability(max_ref_mutability.as_mutbl());
509508
if def_br == ByRef::Yes(Mutability::Not) {
510-
max_ref_mutability = max_ref_mutability.cap_mutbl_to_not(None);
509+
max_ref_mutability = MutblCap::Not;
511510
}
512511
}
513512

@@ -754,7 +753,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
754753
};
755754

756755
if bm.0 == ByRef::Yes(Mutability::Mut)
757-
&& let MutblCap::Not(Some(and_pat_span)) = pat_info.max_ref_mutbl
756+
&& let MutblCap::WeaklyNot(and_pat_span) = pat_info.max_ref_mutbl
758757
{
759758
let mut err = struct_span_code_err!(
760759
self.tcx.dcx(),
@@ -2213,10 +2212,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22132212
&& self.tcx.features().ref_pat_eat_one_layer_2024)
22142213
|| self.tcx.features().ref_pat_everywhere)
22152214
{
2216-
PatInfo {
2217-
max_ref_mutbl: pat_info.max_ref_mutbl.cap_mutbl_to_not(None),
2218-
..pat_info
2219-
}
2215+
PatInfo { max_ref_mutbl: MutblCap::Not, ..pat_info }
22202216
} else {
22212217
pat_info
22222218
};

0 commit comments

Comments
 (0)