Skip to content

Commit a930d44

Browse files
committed
Tweak output to account for alternative bindings in the same pattern
1 parent 6f77be4 commit a930d44

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,26 +193,23 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
193193
is_loop_move = true;
194194
}
195195

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

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

218215
use_spans.var_path_only_subdiag(&mut err, desired_action);
@@ -313,7 +310,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
313310
mpi: MovePathIndex,
314311
move_span: Span,
315312
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
316-
seen_spans: &mut FxHashSet<Span>,
317313
in_pattern: &mut bool,
318314
) {
319315
struct ExpressionFinder<'hir> {
@@ -437,15 +433,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
437433
}
438434
}
439435
}
440-
if let Some(pat) = finder.pat && !seen_spans.contains(&pat.span) {
436+
if let Some(pat) = finder.pat {
441437
*in_pattern = true;
442438
err.span_suggestion_verbose(
443439
pat.span.shrink_to_lo(),
444440
"borrow this binding in the pattern to avoid moving the value",
445441
"ref ".to_string(),
446442
Applicability::MachineApplicable,
447443
);
448-
seen_spans.insert(pat.span);
449444
}
450445
}
451446
}

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

Lines changed: 2 additions & 8 deletions
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)