Skip to content

Commit 3bd06f4

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#46231 - ritiek:verbs, r=arielb1
MIR: Fix value moved diagnose messages rust-lang#45960. I believe this will take a different approach. Simply replacing all nouns to verbs (`desired_action`) messes up the message `use of moved value` (although fixes the message in original issue). Here is what happens: <pre> $ rustc -Zborrowck-mir src/test/ui/borrowck/borrowck-reinit.rs error[E0382]: <b>used</b> of moved value: `x` (Mir) --> src/test/ui/borrowck/borrowck-reinit.rs:18:16 | 17 | drop(x); | - value moved here 18 | let _ = (1,x); | ^ value used here after move error: aborting due to 2 previous errors </pre> (Notice: *"**used** of moved value: `x`"* instead of *"**use**"*) Which does not seem to be okay. After experimenting a bit, it looks like [`report_use_of_moved_value()`](https://github.com/rust-lang/rust/blob/1dc0b573e7ce4314eb196b21b7e0ea4a1bf1f673/src/librustc_mir/borrow_check.rs#L1319) tries to handle both these messages by taking in only one form of`desired_action`. These messages rise from: *"[{noun} of moved value](https://github.com/rust-lang/rust/blob/1dc0b573e7ce4314eb196b21b7e0ea4a1bf1f673/src/librustc_mir/borrow_check.rs#L1338-L1342)"* and *"[value {verb} here after move](https://github.com/rust-lang/rust/blob/1dc0b573e7ce4314eb196b21b7e0ea4a1bf1f673/src/librustc_mir/borrow_check.rs#L1343)"*. This PR fixes *"value {verb} here after move"* type messages by passing a corresponding verb (`desired_action`) instead of the original noun.
2 parents def3d47 + 1be38e0 commit 3bd06f4

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

src/librustc_mir/borrow_check.rs

+45-10
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,34 @@ enum WriteKind {
484484
Move,
485485
}
486486

487+
#[derive(Copy, Clone)]
488+
enum InitializationRequiringAction {
489+
Update,
490+
Borrow,
491+
Use,
492+
Assignment,
493+
}
494+
495+
impl InitializationRequiringAction {
496+
fn as_noun(self) -> &'static str {
497+
match self {
498+
InitializationRequiringAction::Update => "update",
499+
InitializationRequiringAction::Borrow => "borrow",
500+
InitializationRequiringAction::Use => "use",
501+
InitializationRequiringAction::Assignment => "assign"
502+
}
503+
}
504+
505+
fn as_verb_in_past_tense(self) -> &'static str {
506+
match self {
507+
InitializationRequiringAction::Update => "updated",
508+
InitializationRequiringAction::Borrow => "borrowed",
509+
InitializationRequiringAction::Use => "used",
510+
InitializationRequiringAction::Assignment => "assigned"
511+
}
512+
}
513+
}
514+
487515
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
488516
/// Checks an access to the given lvalue to see if it is allowed. Examines the set of borrows
489517
/// that are in scope, as well as which paths have been initialized, to ensure that (a) the
@@ -574,7 +602,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
574602
// Write of P[i] or *P, or WriteAndRead of any P, requires P init'd.
575603
match mode {
576604
MutateMode::WriteAndRead => {
577-
self.check_if_path_is_moved(context, "update", lvalue_span, flow_state);
605+
self.check_if_path_is_moved(context, InitializationRequiringAction::Update,
606+
lvalue_span, flow_state);
578607
}
579608
MutateMode::JustWrite => {
580609
self.check_if_assigned_path_is_moved(context, lvalue_span, flow_state);
@@ -600,7 +629,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
600629
BorrowKind::Mut => (Deep, Write(WriteKind::MutableBorrow(bk))),
601630
};
602631
self.access_lvalue(context, (lvalue, span), access_kind, flow_state);
603-
self.check_if_path_is_moved(context, "borrow", (lvalue, span), flow_state);
632+
self.check_if_path_is_moved(context, InitializationRequiringAction::Borrow,
633+
(lvalue, span), flow_state);
604634
}
605635

606636
Rvalue::Use(ref operand) |
@@ -619,7 +649,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
619649
};
620650
self.access_lvalue(
621651
context, (lvalue, span), (Shallow(Some(af)), Read(ReadKind::Copy)), flow_state);
622-
self.check_if_path_is_moved(context, "use", (lvalue, span), flow_state);
652+
self.check_if_path_is_moved(context, InitializationRequiringAction::Use,
653+
(lvalue, span), flow_state);
623654
}
624655

625656
Rvalue::BinaryOp(_bin_op, ref operand1, ref operand2) |
@@ -720,7 +751,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
720751
// skip this check in that case).
721752
}
722753
ConsumeKind::Consume => {
723-
self.check_if_path_is_moved(context, "use", lvalue_span, flow_state);
754+
self.check_if_path_is_moved(context, InitializationRequiringAction::Use,
755+
lvalue_span, flow_state);
724756
}
725757
}
726758
}
@@ -772,7 +804,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
772804

773805
fn check_if_path_is_moved(&mut self,
774806
context: Context,
775-
desired_action: &str,
807+
desired_action: InitializationRequiringAction,
776808
lvalue_span: (&Lvalue<'tcx>, Span),
777809
flow_state: &InProgress<'cx, 'gcx, 'tcx>) {
778810
// FIXME: analogous code in check_loans first maps `lvalue` to
@@ -943,7 +975,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
943975
// `base` to its base_path.
944976

945977
self.check_if_path_is_moved(
946-
context, "assignment", (base, span), flow_state);
978+
context, InitializationRequiringAction::Assignment,
979+
(base, span), flow_state);
947980

948981
// (base initialized; no need to
949982
// recur further)
@@ -1347,7 +1380,7 @@ mod prefixes {
13471380
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13481381
fn report_use_of_moved_or_uninitialized(&mut self,
13491382
_context: Context,
1350-
desired_action: &str,
1383+
desired_action: InitializationRequiringAction,
13511384
(lvalue, span): (&Lvalue<'tcx>, Span),
13521385
mpi: MovePathIndex,
13531386
curr_move_out: &IdxSetBuf<MoveOutIndex>) {
@@ -1357,7 +1390,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13571390

13581391
if mois.is_empty() {
13591392
self.tcx.cannot_act_on_uninitialized_variable(span,
1360-
desired_action,
1393+
desired_action.as_noun(),
13611394
&self.describe_lvalue(lvalue),
13621395
Origin::Mir)
13631396
.span_label(span, format!("use of possibly uninitialized `{}`",
@@ -1367,11 +1400,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13671400
let msg = ""; //FIXME: add "partially " or "collaterally "
13681401

13691402
let mut err = self.tcx.cannot_act_on_moved_value(span,
1370-
desired_action,
1403+
desired_action.as_noun(),
13711404
msg,
13721405
&self.describe_lvalue(lvalue),
13731406
Origin::Mir);
1374-
err.span_label(span, format!("value {} here after move", desired_action));
1407+
1408+
err.span_label(span, format!("value {} here after move",
1409+
desired_action.as_verb_in_past_tense()));
13751410
for moi in mois {
13761411
let move_msg = ""; //FIXME: add " (into closure)"
13771412
let move_span = self.mir.source_info(self.move_data.moves[*moi].source).span;

src/test/ui/borrowck/borrowck-reinit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0382]: use of moved value: `x` (Mir)
1414
17 | drop(x);
1515
| - value moved here
1616
18 | let _ = (1,x); //~ ERROR use of moved value: `x` (Ast)
17-
| ^ value use here after move
17+
| ^ value used here after move
1818

1919
error: aborting due to 2 previous errors
2020

0 commit comments

Comments
 (0)