Skip to content

Commit 26330b1

Browse files
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 aa99bd9 + 1be38e0 commit 26330b1

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

src/librustc_mir/borrow_check.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,34 @@ enum WriteKind {
475475
Move,
476476
}
477477

478+
#[derive(Copy, Clone)]
479+
enum InitializationRequiringAction {
480+
Update,
481+
Borrow,
482+
Use,
483+
Assignment,
484+
}
485+
486+
impl InitializationRequiringAction {
487+
fn as_noun(self) -> &'static str {
488+
match self {
489+
InitializationRequiringAction::Update => "update",
490+
InitializationRequiringAction::Borrow => "borrow",
491+
InitializationRequiringAction::Use => "use",
492+
InitializationRequiringAction::Assignment => "assign"
493+
}
494+
}
495+
496+
fn as_verb_in_past_tense(self) -> &'static str {
497+
match self {
498+
InitializationRequiringAction::Update => "updated",
499+
InitializationRequiringAction::Borrow => "borrowed",
500+
InitializationRequiringAction::Use => "used",
501+
InitializationRequiringAction::Assignment => "assigned"
502+
}
503+
}
504+
}
505+
478506
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
479507
/// Checks an access to the given lvalue to see if it is allowed. Examines the set of borrows
480508
/// that are in scope, as well as which paths have been initialized, to ensure that (a) the
@@ -565,7 +593,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
565593
// Write of P[i] or *P, or WriteAndRead of any P, requires P init'd.
566594
match mode {
567595
MutateMode::WriteAndRead => {
568-
self.check_if_path_is_moved(context, "update", lvalue_span, flow_state);
596+
self.check_if_path_is_moved(context, InitializationRequiringAction::Update,
597+
lvalue_span, flow_state);
569598
}
570599
MutateMode::JustWrite => {
571600
self.check_if_assigned_path_is_moved(context, lvalue_span, flow_state);
@@ -591,7 +620,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
591620
BorrowKind::Mut => (Deep, Write(WriteKind::MutableBorrow(bk))),
592621
};
593622
self.access_lvalue(context, (lvalue, span), access_kind, flow_state);
594-
self.check_if_path_is_moved(context, "borrow", (lvalue, span), flow_state);
623+
self.check_if_path_is_moved(context, InitializationRequiringAction::Borrow,
624+
(lvalue, span), flow_state);
595625
}
596626

597627
Rvalue::Use(ref operand) |
@@ -610,7 +640,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
610640
};
611641
self.access_lvalue(
612642
context, (lvalue, span), (Shallow(Some(af)), Read(ReadKind::Copy)), flow_state);
613-
self.check_if_path_is_moved(context, "use", (lvalue, span), flow_state);
643+
self.check_if_path_is_moved(context, InitializationRequiringAction::Use,
644+
(lvalue, span), flow_state);
614645
}
615646

616647
Rvalue::BinaryOp(_bin_op, ref operand1, ref operand2) |
@@ -711,7 +742,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
711742
// skip this check in that case).
712743
}
713744
ConsumeKind::Consume => {
714-
self.check_if_path_is_moved(context, "use", lvalue_span, flow_state);
745+
self.check_if_path_is_moved(context, InitializationRequiringAction::Use,
746+
lvalue_span, flow_state);
715747
}
716748
}
717749
}
@@ -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

Lines changed: 1 addition & 1 deletion
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)