Skip to content

Commit 5993f5d

Browse files
committed
Tweak output to account for alternative bindings in the same pattern
1 parent 24f80ea commit 5993f5d

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -192,26 +192,23 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
192192
is_loop_move = true;
193193
}
194194

195-
self.suggest_ref_or_clone(
196-
mpi,
197-
move_span,
198-
&mut err,
199-
&mut seen_spans,
200-
&mut in_pattern,
201-
);
195+
if !seen_spans.contains(&move_span) {
196+
self.suggest_ref_or_clone(mpi, move_span, &mut err, &mut in_pattern);
202197

203-
self.explain_captures(
204-
&mut err,
205-
span,
206-
move_span,
207-
move_spans,
208-
*moved_place,
209-
partially_str,
210-
loop_message,
211-
move_msg,
212-
is_loop_move,
213-
maybe_reinitialized_locations.is_empty(),
214-
);
198+
self.explain_captures(
199+
&mut err,
200+
span,
201+
move_span,
202+
move_spans,
203+
*moved_place,
204+
partially_str,
205+
loop_message,
206+
move_msg,
207+
is_loop_move,
208+
maybe_reinitialized_locations.is_empty(),
209+
);
210+
}
211+
seen_spans.insert(move_span);
215212
}
216213

217214
use_spans.var_path_only_subdiag(&mut err, desired_action);
@@ -312,7 +309,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
312309
mpi: MovePathIndex,
313310
move_span: Span,
314311
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
315-
seen_spans: &mut FxHashSet<Span>,
316312
in_pattern: &mut bool,
317313
) {
318314
struct ExpressionFinder<'hir> {
@@ -436,15 +432,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
436432
}
437433
}
438434
}
439-
if let Some(pat) = finder.pat && !seen_spans.contains(&pat.span) {
435+
if let Some(pat) = finder.pat {
440436
*in_pattern = true;
441437
err.span_suggestion_verbose(
442438
pat.span.shrink_to_lo(),
443439
"borrow this binding in the pattern to avoid moving the value",
444440
"ref ".to_string(),
445441
Applicability::MachineApplicable,
446442
);
447-
seen_spans.insert(pat.span);
448443
}
449444
}
450445
}

src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr

+2-8
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ LL | fn bindings_after_at_or_patterns_move(x: Option<Test>) {
7676
| - move occurs because `x` has type `Option<Test>`, which does not implement the `Copy` trait
7777
LL | match x {
7878
LL | foo @ Some(Test::Foo | Test::Bar) => (),
79-
| ---
80-
| |
81-
| value moved here
82-
| value moved here
79+
| --- value moved here
8380
...
8481
LL | &x;
8582
| ^^ value borrowed here after move
@@ -132,10 +129,7 @@ LL | fn bindings_after_at_slice_patterns_or_patterns_moves(x: [Option<Test>; 4])
132129
| - move occurs because `x` has type `[Option<Test>; 4]`, which does not implement the `Copy` trait
133130
LL | match x {
134131
LL | a @ [.., Some(Test::Foo | Test::Bar)] => (),
135-
| -
136-
| |
137-
| value moved here
138-
| value moved here
132+
| - value moved here
139133
...
140134
LL | &x;
141135
| ^^ value borrowed here after move

0 commit comments

Comments
 (0)