Skip to content

Commit 91bbbaa

Browse files
Fix spans when macros are involved
1 parent ed96c65 commit 91bbbaa

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
@@ -137,7 +137,7 @@ enum AdjustMode {
137137
/// with mutability matching the pattern,
138138
/// mark the pattern as having consumed this reference.
139139
///
140-
/// `Span` is that of the inside of the reference pattern
140+
/// `Span` is that of the `&` or `&mut` itself
141141
ResetAndConsumeRef(Mutability, Span),
142142
/// Pass on the input binding mode and expected type.
143143
Pass,
@@ -342,14 +342,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
342342
match adjust_mode {
343343
AdjustMode::Pass => (expected, def_br, max_ref_mutbl, false),
344344
AdjustMode::Reset => (expected, ByRef::No, MutblCap::Mut, false),
345-
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl, inner_span) => {
345+
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl, ref_span) => {
346346
// `&` pattern eats `&mut`
347347
let mutbls_match =
348348
if let ByRef::Yes(def_mut) = def_br { ref_pat_mutbl <= def_mut } else { false };
349349

350350
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
351351
let max_ref_mutbl = if ref_pat_mutbl == Mutability::Not {
352-
max_ref_mutbl.cap_mutbl_to_not(Some(pat.span.until(inner_span)))
352+
max_ref_mutbl.cap_mutbl_to_not(Some(ref_span))
353353
} else {
354354
max_ref_mutbl
355355
};
@@ -434,7 +434,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
434434
// ```
435435
//
436436
// See issue #46688.
437-
PatKind::Ref(inner, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl, inner.span),
437+
PatKind::Ref(inner, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl, pat.span.until(inner.span.find_ancestor_inside(pat.span).unwrap())),
438438
// A `_` pattern works with any expected type, so there's no need to do anything.
439439
PatKind::Wild
440440
// 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)