Skip to content

Commit 54db888

Browse files
authored
Rollup merge of rust-lang#137161 - dianne:pat-migration-bookkeeping-for-macros, r=Nadrieril
Pattern Migration 2024: fix incorrect messages/suggestions when errors arise in macro expansions See the diff between the two commits for how this affected the error message and suggestion. In order to decide how to format those, the pattern migration diagnostic keeps track of which parts of the user's pattern cause problems in Edition 2024. However, it neglected to do some of this bookkeeping when pointing to macro expansion sites. This fixes that.
2 parents ac3b179 + 82678df commit 54db888

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

Diff for: compiler/rustc_hir_typeck/src/pat.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -2806,31 +2806,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28062806
&& !self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
28072807
});
28082808

2809+
let pat_kind = if let PatKind::Binding(user_bind_annot, _, _, _) = subpat.kind {
2810+
info.bad_modifiers = true;
2811+
// If the user-provided binding modifier doesn't match the default binding mode, we'll
2812+
// need to suggest reference patterns, which can affect other bindings.
2813+
// For simplicity, we opt to suggest making the pattern fully explicit.
2814+
info.suggest_eliding_modes &=
2815+
user_bind_annot == BindingMode(ByRef::Yes(def_br_mutbl), Mutability::Not);
2816+
"binding modifier"
2817+
} else {
2818+
info.bad_ref_pats = true;
2819+
// For simplicity, we don't try to suggest eliding reference patterns. Thus, we'll
2820+
// suggest adding them instead, which can affect the types assigned to bindings.
2821+
// As such, we opt to suggest making the pattern fully explicit.
2822+
info.suggest_eliding_modes = false;
2823+
"reference pattern"
2824+
};
28092825
// Only provide a detailed label if the problematic subpattern isn't from an expansion.
28102826
// In the case that it's from a macro, we'll add a more detailed note in the emitter.
28112827
let from_expansion = subpat.span.from_expansion();
28122828
let primary_label = if from_expansion {
2829+
// We can't suggest eliding modifiers within expansions.
2830+
info.suggest_eliding_modes = false;
28132831
// NB: This wording assumes the only expansions that can produce problematic reference
28142832
// patterns and bindings are macros. If a desugaring or AST pass is added that can do
28152833
// so, we may want to inspect the span's source callee or macro backtrace.
28162834
"occurs within macro expansion".to_owned()
28172835
} else {
2818-
let pat_kind = if let PatKind::Binding(user_bind_annot, _, _, _) = subpat.kind {
2819-
info.bad_modifiers |= true;
2820-
// If the user-provided binding modifier doesn't match the default binding mode, we'll
2821-
// need to suggest reference patterns, which can affect other bindings.
2822-
// For simplicity, we opt to suggest making the pattern fully explicit.
2823-
info.suggest_eliding_modes &=
2824-
user_bind_annot == BindingMode(ByRef::Yes(def_br_mutbl), Mutability::Not);
2825-
"binding modifier"
2826-
} else {
2827-
info.bad_ref_pats |= true;
2828-
// For simplicity, we don't try to suggest eliding reference patterns. Thus, we'll
2829-
// suggest adding them instead, which can affect the types assigned to bindings.
2830-
// As such, we opt to suggest making the pattern fully explicit.
2831-
info.suggest_eliding_modes = false;
2832-
"reference pattern"
2833-
};
28342836
let dbm_str = match def_br_mutbl {
28352837
Mutability::Not => "ref",
28362838
Mutability::Mut => "ref mut",

Diff for: tests/ui/pattern/rfc-3627-match-ergonomics-2024/auxiliary/migration_lint_macros.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ macro_rules! mixed_edition_pat {
99
Some(mut $foo)
1010
};
1111
}
12+
13+
#[macro_export]
14+
macro_rules! bind_ref {
15+
($foo:ident) => {
16+
ref $foo
17+
};
18+
}

Diff for: tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,9 @@ fn main() {
239239
assert_type_eq(b, &0u32);
240240
assert_type_eq(c, &[0u32]);
241241
assert_type_eq(d, 0u32);
242+
243+
// Test that we use the correct message and suggestion style when pointing inside expansions.
244+
let &[migration_lint_macros::bind_ref!(a)] = &[0];
245+
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
246+
assert_type_eq(a, &0u32);
242247
}

Diff for: tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.rs

+5
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,9 @@ fn main() {
239239
assert_type_eq(b, &0u32);
240240
assert_type_eq(c, &[0u32]);
241241
assert_type_eq(d, 0u32);
242+
243+
// Test that we use the correct message and suggestion style when pointing inside expansions.
244+
let [migration_lint_macros::bind_ref!(a)] = &[0];
245+
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
246+
assert_type_eq(a, &0u32);
242247
}

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -562,5 +562,23 @@ help: make the implied reference patterns explicit
562562
LL | let [&Foo(&ref a @ [ref b]), &Foo(&ref c @ [d])] = [&Foo(&[0]); 2];
563563
| + +
564564

565-
error: aborting due to 29 previous errors
565+
error: binding modifiers may only be written when the default binding mode is `move`
566+
--> $DIR/migration_lint.rs:244:10
567+
|
568+
LL | let [migration_lint_macros::bind_ref!(a)] = &[0];
569+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ occurs within macro expansion
570+
|
571+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
572+
note: matching on a reference type with a non-reference pattern changes the default binding mode
573+
--> $DIR/migration_lint.rs:244:9
574+
|
575+
LL | let [migration_lint_macros::bind_ref!(a)] = &[0];
576+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this matches on type `&_`
577+
= note: this error originates in the macro `migration_lint_macros::bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
578+
help: make the implied reference pattern explicit
579+
|
580+
LL | let &[migration_lint_macros::bind_ref!(a)] = &[0];
581+
| +
582+
583+
error: aborting due to 30 previous errors
566584

0 commit comments

Comments
 (0)