Skip to content

Commit d8a798b

Browse files
No more Option<Option<>>
1 parent 0f03c2b commit d8a798b

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

+15-19
Original file line numberDiff line numberDiff line change
@@ -155,34 +155,33 @@ enum AdjustMode {
155155
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
156156
enum MutblCap {
157157
/// Mutability restricted to immutable.
158+
Not,
159+
160+
/// Mutability restricted to immutable, but only because of the pattern
161+
/// (not the scrutinee type).
158162
///
159163
/// The contained span, if present, points to an `&` pattern
160164
/// that is the reason for the restriction,
161165
/// and which will be reported in a diagnostic.
162166
/// (Said diagnostic is shown only if
163167
/// replacing the `&` pattern with `&mut` would allow the code to compile.)
164-
///
165-
/// (Outer [`Option`] is for whether to show the diagnostic,
166-
/// inner [`Option`] is for whether we have a span we can report)
167-
Not(Option<Option<Span>>),
168+
WeaklyNot(Option<Span>),
169+
168170
/// No restriction on mutability
169171
Mut,
170172
}
171173

172174
impl MutblCap {
173-
fn cap_mutbl_to_not(self, span: Option<Option<Span>>) -> Self {
174-
if let Some(s) = span
175-
&& self != MutblCap::Not(None)
176-
{
177-
MutblCap::Not(Some(s))
178-
} else {
179-
MutblCap::Not(None)
175+
fn cap_to_weakly_not(self, span: Option<Span>) -> Self {
176+
match self {
177+
MutblCap::Not => MutblCap::Not,
178+
_ => MutblCap::WeaklyNot(span),
180179
}
181180
}
182181

183182
fn as_mutbl(self) -> Mutability {
184183
match self {
185-
MutblCap::Not(_) => Mutability::Not,
184+
MutblCap::Not | MutblCap::WeaklyNot(_) => Mutability::Not,
186185
MutblCap::Mut => Mutability::Mut,
187186
}
188187
}
@@ -357,7 +356,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
357356

358357
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
359358
let max_ref_mutbl = if ref_pat_mutbl == Mutability::Not {
360-
max_ref_mutbl.cap_mutbl_to_not(Some(ref_span))
359+
max_ref_mutbl.cap_to_weakly_not(ref_span)
361360
} else {
362361
max_ref_mutbl
363362
};
@@ -505,7 +504,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
505504
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
506505
def_br = def_br.cap_ref_mutability(max_ref_mutability.as_mutbl());
507506
if def_br == ByRef::Yes(Mutability::Not) {
508-
max_ref_mutability = max_ref_mutability.cap_mutbl_to_not(None);
507+
max_ref_mutability = MutblCap::Not;
509508
}
510509
}
511510

@@ -752,7 +751,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
752751
};
753752

754753
if bm.0 == ByRef::Yes(Mutability::Mut)
755-
&& let MutblCap::Not(Some(and_pat_span)) = pat_info.max_ref_mutbl
754+
&& let MutblCap::WeaklyNot(and_pat_span) = pat_info.max_ref_mutbl
756755
{
757756
let mut err = struct_span_code_err!(
758757
self.tcx.dcx(),
@@ -2215,10 +2214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22152214
&& self.tcx.features().ref_pat_eat_one_layer_2024)
22162215
|| self.tcx.features().ref_pat_everywhere)
22172216
{
2218-
PatInfo {
2219-
max_ref_mutbl: pat_info.max_ref_mutbl.cap_mutbl_to_not(None),
2220-
..pat_info
2221-
}
2217+
PatInfo { max_ref_mutbl: MutblCap::Not, ..pat_info }
22222218
} else {
22232219
pat_info
22242220
};

0 commit comments

Comments
 (0)