Fix spans when macros are involved

This commit is contained in:
Jules Bertholet 2024-05-04 15:20:06 -04:00
parent ed96c655c6
commit 91bbbaa0f7
No known key found for this signature in database
GPG Key ID: 32034DAFC38C1BFC
2 changed files with 7 additions and 8 deletions

View File

@ -137,7 +137,7 @@ enum AdjustMode {
/// with mutability matching the pattern, /// with mutability matching the pattern,
/// mark the pattern as having consumed this reference. /// mark the pattern as having consumed this reference.
/// ///
/// `Span` is that of the inside of the reference pattern /// `Span` is that of the `&` or `&mut` itself
ResetAndConsumeRef(Mutability, Span), ResetAndConsumeRef(Mutability, Span),
/// Pass on the input binding mode and expected type. /// Pass on the input binding mode and expected type.
Pass, Pass,
@ -342,14 +342,14 @@ fn calc_default_binding_mode(
match adjust_mode { match adjust_mode {
AdjustMode::Pass => (expected, def_br, max_ref_mutbl, false), AdjustMode::Pass => (expected, def_br, max_ref_mutbl, false),
AdjustMode::Reset => (expected, ByRef::No, MutblCap::Mut, false), AdjustMode::Reset => (expected, ByRef::No, MutblCap::Mut, false),
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl, inner_span) => { AdjustMode::ResetAndConsumeRef(ref_pat_mutbl, ref_span) => {
// `&` pattern eats `&mut` // `&` pattern eats `&mut`
let mutbls_match = let mutbls_match =
if let ByRef::Yes(def_mut) = def_br { ref_pat_mutbl <= def_mut } else { false }; if let ByRef::Yes(def_mut) = def_br { ref_pat_mutbl <= def_mut } else { false };
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(pat.span.until(inner_span))) max_ref_mutbl.cap_mutbl_to_not(Some(ref_span))
} else { } else {
max_ref_mutbl max_ref_mutbl
}; };
@ -434,7 +434,7 @@ fn calc_adjust_mode(&self, pat: &'tcx Pat<'tcx>, opt_path_res: Option<Res>) -> A
// ``` // ```
// //
// See issue #46688. // See issue #46688.
PatKind::Ref(inner, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl, inner.span), PatKind::Ref(inner, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl, pat.span.until(inner.span.find_ancestor_inside(pat.span).unwrap())),
// A `_` pattern works with any expected type, so there's no need to do anything. // A `_` pattern works with any expected type, so there's no need to do anything.
PatKind::Wild PatKind::Wild
// A malformed pattern doesn't have an expected type, so let's just accept any type. // A malformed pattern doesn't have an expected type, so let's just accept any type.

View File

@ -116,11 +116,10 @@ LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
error[E0596]: cannot borrow as mutable inside an `&` pattern error[E0596]: cannot borrow as mutable inside an `&` pattern
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:45:15 --> $DIR/ref_pat_eat_one_layer_2024_fail.rs:45:15
| |
LL | ($var:ident) => { ref mut $var };
| ------------ help: replace this `&` with `&mut`: `&mut`
LL | }
LL | let &pat!(x) = &mut 0; LL | let &pat!(x) = &mut 0;
| ^ | - ^
| |
| help: replace this `&` with `&mut`: `&mut`
error: aborting due to 12 previous errors error: aborting due to 12 previous errors