Skip to content

Commit d57d5f3

Browse files
committed
librustc_borrowck: refactor common code for reporting moves
1 parent 6cd0710 commit d57d5f3

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/librustc_borrowck/borrowck/check_loans.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ enum UseError<'tcx> {
220220
UseWhileBorrowed(/*loan*/Rc<LoanPath<'tcx>>, /*loan*/Span)
221221
}
222222

223+
#[derive(Debug)]
224+
enum AssignError {
225+
Moved,
226+
PartialReinit
227+
}
228+
223229
fn compatible_borrow_kinds(borrow_kind1: ty::BorrowKind,
224230
borrow_kind2: ty::BorrowKind)
225231
-> bool {
@@ -690,26 +696,41 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
690696
return ret;
691697
}
692698

699+
fn report_if_path_is_moved(&self,
700+
id: ast::NodeId,
701+
span: Span,
702+
use_kind: MovedValueUseKind,
703+
lp: &Rc<LoanPath<'tcx>>,
704+
err_kind: AssignError) {
705+
use self::AssignError::*;
706+
debug!("report_if_moved(id={}, use_kind={:?}, lp={}, err_kind={:?})",
707+
id, use_kind, lp.repr(self.bccx.tcx), err_kind);
708+
709+
let base_lp = owned_ptr_base_path_rc(lp);
710+
self.move_data.each_move_of(id, &base_lp, |the_move, moved_lp| {
711+
match err_kind {
712+
Moved => self.bccx.report_use_of_moved_value(
713+
span, use_kind, &**lp, the_move, moved_lp, self.param_env),
714+
PartialReinit => {
715+
self.bccx
716+
.report_partial_reinitialization_of_uninitialized_structure(
717+
span, &*lp)
718+
}
719+
};
720+
721+
false
722+
});
723+
724+
}
725+
693726
/// Reports an error if `expr` (which should be a path)
694727
/// is using a moved/uninitialized value
695728
fn check_if_path_is_moved(&self,
696729
id: ast::NodeId,
697730
span: Span,
698731
use_kind: MovedValueUseKind,
699732
lp: &Rc<LoanPath<'tcx>>) {
700-
debug!("check_if_path_is_moved(id={}, use_kind={:?}, lp={})",
701-
id, use_kind, lp.repr(self.bccx.tcx));
702-
let base_lp = owned_ptr_base_path_rc(lp);
703-
self.move_data.each_move_of(id, &base_lp, |the_move, moved_lp| {
704-
self.bccx.report_use_of_moved_value(
705-
span,
706-
use_kind,
707-
&**lp,
708-
the_move,
709-
moved_lp,
710-
self.param_env);
711-
false
712-
});
733+
self.report_if_path_is_moved(id, span, use_kind, lp, AssignError::Moved);
713734
}
714735

715736
/// Reports an error if assigning to `lp` will use a
@@ -747,19 +768,13 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
747768
LpExtend(ref lp_base, _, LpInterior(InteriorField(_))) => {
748769
match lp_base.to_type().sty {
749770
ty::ty_struct(def_id, _) | ty::ty_enum(def_id, _) => {
771+
// In the case where the owner implements drop, then
772+
// the path must be initialized to prevent a case of
773+
// partial reinitialization
750774
if ty::has_dtor(self.tcx(), def_id) {
751-
// In the case where the owner implements drop, then
752-
// the path must be initialized to prevent a case of
753-
// partial reinitialization
754-
let loan_path = owned_ptr_base_path_rc(lp_base);
755-
self.move_data.each_move_of(id, &loan_path, |_, _| {
756-
self.bccx
757-
.report_partial_reinitialization_of_uninitialized_structure(
758-
span,
759-
&*loan_path);
760-
false
761-
});
762-
return;
775+
return self.report_if_path_is_moved(id, span,
776+
use_kind, lp_base,
777+
AssignError::PartialReinit);
763778
}
764779
},
765780
_ => {},

0 commit comments

Comments
 (0)