Skip to content

Commit 9605d90

Browse files
Fix spans when macros are involved
1 parent 6d9c4ed commit 9605d90

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ enum AdjustMode {
139139
/// with mutability matching the pattern,
140140
/// mark the pattern as having consumed this reference.
141141
///
142-
/// `Span` is that of the inside of the reference pattern
142+
/// `Span` is that of the `&` or `&mut` itself
143143
ResetAndConsumeRef(Mutability, Span),
144144
/// Pass on the input binding mode and expected type.
145145
Pass,
@@ -344,14 +344,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
344344
match adjust_mode {
345345
AdjustMode::Pass => (expected, def_br, max_ref_mutbl, false),
346346
AdjustMode::Reset => (expected, ByRef::No, MutblCap::Mut, false),
347-
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl, inner_span) => {
347+
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl, ref_span) => {
348348
// `&` pattern eats `&mut`
349349
let mutbls_match =
350350
if let ByRef::Yes(def_mut) = def_br { ref_pat_mutbl <= def_mut } else { false };
351351

352352
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
353353
let max_ref_mutbl = if ref_pat_mutbl == Mutability::Not {
354-
max_ref_mutbl.cap_mutbl_to_not(Some(pat.span.until(inner_span)))
354+
max_ref_mutbl.cap_mutbl_to_not(Some(ref_span))
355355
} else {
356356
max_ref_mutbl
357357
};
@@ -436,7 +436,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
436436
// ```
437437
//
438438
// See issue #46688.
439-
PatKind::Ref(inner, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl, inner.span),
439+
PatKind::Ref(inner, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl, pat.span.until(inner.span.find_ancestor_inside(pat.span).unwrap())),
440440
// A `_` pattern works with any expected type, so there's no need to do anything.
441441
PatKind::Wild
442442
// A malformed pattern doesn't have an expected type, so let's just accept any type.

tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.stderr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
116116
error[E0596]: cannot borrow as mutable inside an `&` pattern
117117
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:45:15
118118
|
119-
LL | ($var:ident) => { ref mut $var };
120-
| ------------ help: replace this `&` with `&mut`: `&mut`
121-
LL | }
122119
LL | let &pat!(x) = &mut 0;
123-
| ^
120+
| - ^
121+
| |
122+
| help: replace this `&` with `&mut`: `&mut`
124123

125124
error: aborting due to 12 previous errors
126125

0 commit comments

Comments
 (0)