1
Fork 0

No more Option<Option<>>

This commit is contained in:
Jules Bertholet 2024-05-05 11:35:49 -04:00
parent 0f03c2be58
commit d8a798b5e9
No known key found for this signature in database
GPG key ID: 32034DAFC38C1BFC

View file

@ -155,34 +155,33 @@ enum AdjustMode {
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MutblCap { enum MutblCap {
/// Mutability restricted to immutable. /// Mutability restricted to immutable.
Not,
/// Mutability restricted to immutable, but only because of the pattern
/// (not the scrutinee type).
/// ///
/// The contained span, if present, points to an `&` pattern /// The contained span, if present, points to an `&` pattern
/// that is the reason for the restriction, /// that is the reason for the restriction,
/// and which will be reported in a diagnostic. /// and which will be reported in a diagnostic.
/// (Said diagnostic is shown only if /// (Said diagnostic is shown only if
/// replacing the `&` pattern with `&mut` would allow the code to compile.) /// replacing the `&` pattern with `&mut` would allow the code to compile.)
/// WeaklyNot(Option<Span>),
/// (Outer [`Option`] is for whether to show the diagnostic,
/// inner [`Option`] is for whether we have a span we can report)
Not(Option<Option<Span>>),
/// No restriction on mutability /// No restriction on mutability
Mut, Mut,
} }
impl MutblCap { impl MutblCap {
fn cap_mutbl_to_not(self, span: Option<Option<Span>>) -> Self { fn cap_to_weakly_not(self, span: Option<Span>) -> Self {
if let Some(s) = span match self {
&& self != MutblCap::Not(None) MutblCap::Not => MutblCap::Not,
{ _ => MutblCap::WeaklyNot(span),
MutblCap::Not(Some(s))
} else {
MutblCap::Not(None)
} }
} }
fn as_mutbl(self) -> Mutability { fn as_mutbl(self) -> Mutability {
match self { match self {
MutblCap::Not(_) => Mutability::Not, MutblCap::Not | MutblCap::WeaklyNot(_) => Mutability::Not,
MutblCap::Mut => Mutability::Mut, MutblCap::Mut => Mutability::Mut,
} }
} }
@ -357,7 +356,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 { if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
let max_ref_mutbl = if ref_pat_mutbl == Mutability::Not { let max_ref_mutbl = if ref_pat_mutbl == Mutability::Not {
max_ref_mutbl.cap_mutbl_to_not(Some(ref_span)) max_ref_mutbl.cap_to_weakly_not(ref_span)
} else { } else {
max_ref_mutbl max_ref_mutbl
}; };
@ -505,7 +504,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 { if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
def_br = def_br.cap_ref_mutability(max_ref_mutability.as_mutbl()); def_br = def_br.cap_ref_mutability(max_ref_mutability.as_mutbl());
if def_br == ByRef::Yes(Mutability::Not) { if def_br == ByRef::Yes(Mutability::Not) {
max_ref_mutability = max_ref_mutability.cap_mutbl_to_not(None); max_ref_mutability = MutblCap::Not;
} }
} }
@ -752,7 +751,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
if bm.0 == ByRef::Yes(Mutability::Mut) if bm.0 == ByRef::Yes(Mutability::Mut)
&& let MutblCap::Not(Some(and_pat_span)) = pat_info.max_ref_mutbl && let MutblCap::WeaklyNot(and_pat_span) = pat_info.max_ref_mutbl
{ {
let mut err = struct_span_code_err!( let mut err = struct_span_code_err!(
self.tcx.dcx(), self.tcx.dcx(),
@ -2215,10 +2214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& self.tcx.features().ref_pat_eat_one_layer_2024) && self.tcx.features().ref_pat_eat_one_layer_2024)
|| self.tcx.features().ref_pat_everywhere) || self.tcx.features().ref_pat_everywhere)
{ {
PatInfo { PatInfo { max_ref_mutbl: MutblCap::Not, ..pat_info }
max_ref_mutbl: pat_info.max_ref_mutbl.cap_mutbl_to_not(None),
..pat_info
}
} else { } else {
pat_info pat_info
}; };