diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index f1eceed0ac8..d8e21574cf4 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -137,7 +137,7 @@ enum AdjustMode { /// with mutability matching the pattern, /// 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), /// Pass on the input binding mode and expected type. Pass, @@ -342,14 +342,14 @@ fn calc_default_binding_mode( match adjust_mode { AdjustMode::Pass => (expected, def_br, max_ref_mutbl, 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` let mutbls_match = 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 { 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 { max_ref_mutbl }; @@ -434,7 +434,7 @@ fn calc_adjust_mode(&self, pat: &'tcx Pat<'tcx>, opt_path_res: Option) -> A // ``` // // 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. PatKind::Wild // A malformed pattern doesn't have an expected type, so let's just accept any type. diff --git a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.stderr b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.stderr index 0512a31011d..d51b92230e8 100644 --- a/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.stderr +++ b/tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.stderr @@ -116,11 +116,10 @@ LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0; error[E0596]: cannot borrow as mutable inside an `&` pattern --> $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; - | ^ + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` error: aborting due to 12 previous errors