Skip to content

Commit 82678df

Browse files
committed
bookkeep properly when pointing into macro expansions
1 parent 7af4630 commit 82678df

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

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",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn main() {
241241
assert_type_eq(d, 0u32);
242242

243243
// Test that we use the correct message and suggestion style when pointing inside expansions.
244-
let [migration_lint_macros::bind_ref!(a)] = &[0];
244+
let &[migration_lint_macros::bind_ref!(a)] = &[0];
245245
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
246246
assert_type_eq(a, &0u32);
247247
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ 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: reference patterns may only be written when the default binding mode is `move`
565+
error: binding modifiers may only be written when the default binding mode is `move`
566566
--> $DIR/migration_lint.rs:244:10
567567
|
568568
LL | let [migration_lint_macros::bind_ref!(a)] = &[0];
@@ -575,6 +575,10 @@ note: matching on a reference type with a non-reference pattern changes the defa
575575
LL | let [migration_lint_macros::bind_ref!(a)] = &[0];
576576
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this matches on type `&_`
577577
= 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+
| +
578582

579583
error: aborting due to 30 previous errors
580584

0 commit comments

Comments
 (0)