Skip to content

Commit 998cc72

Browse files
committed
don't suggest removals inside macro expansions
This fixes the regression in the previous commit.
1 parent 84c3aa1 commit 998cc72

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

compiler/rustc_mir_build/src/thir/pattern/migration.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,14 @@ impl<'a, 'tcx> PatMigration<'a, 'tcx> {
293293
self.add_default_mode_label_if_needed();
294294
// This sets the default binding mode to by-value in the user's pattern, but we'll try to
295295
// suggest removing it.
296-
// TODO: if this is inside a macro expansion, we won't be able to remove it.
297296
self.push_deref(pat_span, mutbl, PatDerefKind::Explicit { inner_span: subpat.span });
298297

298+
// If this is inside a macro expansion, we won't be able to remove it.
299+
if pat_span.from_expansion() {
300+
self.add_derefs_to_suggestion(self.innermost_deref);
301+
return;
302+
}
303+
299304
// If the immediate subpattern is a binding, removing this reference pattern would change
300305
// its type. To avoid that, we include it and all its parents in that case.
301306
// FIXME(ref_pat_eat_one_layer_2024): This assumes ref pats can't eat the binding mode
@@ -378,8 +383,11 @@ impl<'a, 'tcx> PatMigration<'a, 'tcx> {
378383

379384
// If `mode` doesn't match the default, we'll need to specify its binding modifiers
380385
// explicitly, which in turn necessitates a by-move default binding mode.
381-
// TODO: if this is inside a macro expansion, we won't be able to change it.
382-
let suggest = mode != BindingMode(self.sugg_default_mode(), Mutability::Not);
386+
// Additionally, if this is inside a macro expansion, we won't be able to change it. If a
387+
// binding modifier is missing inside the expansion, there's not much we can do, but we can
388+
// avoid suggestions to elide binding modifiers that are explicit within expansions.
389+
let suggest = mode != BindingMode(self.sugg_default_mode(), Mutability::Not)
390+
|| pat_span.from_expansion() && explicit_ba != BindingMode::NONE;
383391

384392
// Track the binding
385393
let span = if explicit_ba == BindingMode::NONE {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn main() {
239239
assert_type_eq(d, 0u32);
240240

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

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

+4
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,10 @@ note: matching on a reference type with a non-reference pattern changes the defa
580580
LL | let [migration_lint_macros::bind_ref!(a)] = &[0];
581581
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this matches on type `&_`
582582
= note: this error originates in the macro `migration_lint_macros::bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
583+
help: make the implied reference pattern explicit
584+
|
585+
LL | let &[migration_lint_macros::bind_ref!(a)] = &[0];
586+
| +
583587

584588
error: reference patterns may only be written when the default binding mode is `move` in Rust 2024
585589
--> $DIR/migration_lint.rs:249:10

0 commit comments

Comments
 (0)