Skip to content

Commit 51a2ee3

Browse files
committed
don't get trapped inside of expansions when trimming labels
1 parent ef6df3b commit 51a2ee3

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -835,20 +835,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
835835
self.add_rust_2024_migration_desugared_pat(
836836
pat_info.top_info.hir_id,
837837
pat,
838-
ident.span,
838+
't',
839839
def_br_mutbl,
840840
);
841841
BindingMode(ByRef::No, Mutability::Mut)
842842
}
843843
}
844844
BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
845-
BindingMode(ByRef::Yes(_), _) => {
845+
BindingMode(ByRef::Yes(user_br_mutbl), _) => {
846846
if let ByRef::Yes(def_br_mutbl) = def_br {
847847
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
848848
self.add_rust_2024_migration_desugared_pat(
849849
pat_info.top_info.hir_id,
850850
pat,
851-
ident.span,
851+
if user_br_mutbl.is_mut() { 't' } else { 'f' },
852852
def_br_mutbl,
853853
);
854854
}
@@ -2387,7 +2387,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23872387
self.add_rust_2024_migration_desugared_pat(
23882388
pat_info.top_info.hir_id,
23892389
pat,
2390-
inner.span,
2390+
if pat_mutbl.is_mut() { 't' } else { '&' },
23912391
inh_mut,
23922392
)
23932393
}
@@ -2779,18 +2779,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27792779
&self,
27802780
pat_id: HirId,
27812781
subpat: &'tcx Pat<'tcx>,
2782-
cutoff_span: Span,
2782+
final_char: char,
27832783
def_br_mutbl: Mutability,
27842784
) {
27852785
// Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
27862786
// If the subpattern's span is is from an expansion, the emitted label will not be trimmed.
2787-
let source_map = self.tcx.sess.source_map();
2788-
let cutoff_span = source_map
2789-
.span_extend_prev_while(cutoff_span, |c| c.is_whitespace() || c == '(')
2790-
.unwrap_or(cutoff_span);
2791-
// Ensure we use the syntax context and thus edition of `subpat.span`; this will be a hard
2792-
// error if the subpattern is of edition >= 2024.
2793-
let trimmed_span = subpat.span.until(cutoff_span).with_ctxt(subpat.span.ctxt());
2787+
// Importantly, the edition of the trimmed span should be the same as `subpat.span`; this
2788+
// will be a hard error if the subpattern is of edition >= 2024.
2789+
let from_expansion = subpat.span.from_expansion();
2790+
let trimmed_span = if from_expansion {
2791+
subpat.span
2792+
} else {
2793+
self.tcx.sess.source_map().span_through_char(subpat.span, final_char)
2794+
};
27942795

27952796
let mut typeck_results = self.typeck_results.borrow_mut();
27962797
let mut table = typeck_results.rust_2024_migration_desugared_pats_mut();
@@ -2824,7 +2825,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28242825
};
28252826
// Only provide a detailed label if the problematic subpattern isn't from an expansion.
28262827
// In the case that it's from a macro, we'll add a more detailed note in the emitter.
2827-
let from_expansion = subpat.span.from_expansion();
28282828
let primary_label = if from_expansion {
28292829
// We can't suggest eliding modifiers within expansions.
28302830
info.suggest_eliding_modes = false;

tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,10 @@ LL | let &[migration_lint_macros::bind_ref!(a)] = &[0];
581581
| +
582582

583583
error: reference patterns may only be written when the default binding mode is `move` in Rust 2024
584-
--> $DIR/auxiliary/migration_lint_macros.rs:15:22
584+
--> $DIR/migration_lint.rs:249:10
585585
|
586-
LL | ($foo:ident) => {
587-
| ______________________^
588-
LL | | ref $foo
589-
| |________________^ reference pattern not allowed under `ref` default binding mode
586+
LL | let [&migration_lint_macros::bind_ref!(a)] = &[&0];
587+
| ^ reference pattern not allowed under `ref` default binding mode
590588
|
591589
= warning: this changes meaning in Rust 2024
592590
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
@@ -596,7 +594,6 @@ note: matching on a reference type with a non-reference pattern changes the defa
596594
LL | let [&migration_lint_macros::bind_ref!(a)] = &[&0];
597595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this matches on type `&_`
598596
help: make the implied reference pattern explicit
599-
--> $DIR/migration_lint.rs:249:9
600597
|
601598
LL | let &[&migration_lint_macros::bind_ref!(a)] = &[&0];
602599
| +

0 commit comments

Comments
 (0)