Skip to content

Commit

Permalink
Use MirBorrowckCtxt while reporting move errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Jul 21, 2018
1 parent 942b384 commit 341e5e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
19 changes: 10 additions & 9 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -34,7 +34,7 @@ use std::rc::Rc;
use syntax_pos::Span;

use dataflow::indexes::BorrowIndex;
use dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
use dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MoveError, MovePathIndex};
use dataflow::Borrows;
use dataflow::DataflowResultsConsumer;
use dataflow::FlowAtLocation;
Expand Down Expand Up @@ -148,13 +148,11 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
let mir = &mir; // no further changes
let location_table = &LocationTable::new(mir);

let move_data: MoveData<'tcx> = match MoveData::gather_moves(mir, tcx) {
Ok(move_data) => move_data,
Err((move_data, move_errors)) => {
move_errors::report_move_errors(&mir, tcx, move_errors, &move_data);
move_data
}
};
let (move_data, move_errors): (MoveData<'tcx>, Option<Vec<MoveError<'tcx>>>) =
match MoveData::gather_moves(mir, tcx) {
Ok(move_data) => (move_data, None),
Err((move_data, move_errors)) => (move_data, Some(move_errors)),
};

let mdpe = MoveDataParamEnv {
move_data: move_data,
Expand Down Expand Up @@ -271,6 +269,9 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
polonius_output,
);

if let Some(errors) = move_errors {
mbcx.report_move_errors(errors);
}
mbcx.analyze_results(&mut state); // entry point for DataflowResultsConsumer

// For each non-user used mutable variable, check if it's been assigned from
Expand Down Expand Up @@ -1975,7 +1976,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
ProjectionElem::Field(field, _ty) => {
let base_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx);

if base_ty.is_closure() || base_ty.is_generator() {
if base_ty.is_closure() || base_ty.is_generator() {
Some(field)
} else {
None
Expand Down
41 changes: 11 additions & 30 deletions src/librustc_mir/borrow_check/move_errors.rs
Expand Up @@ -10,34 +10,15 @@

use rustc::hir;
use rustc::mir::*;
use rustc::ty::{self, TyCtxt};
use rustc::ty;
use rustc_errors::DiagnosticBuilder;
use syntax_pos::Span;

use dataflow::move_paths::{IllegalMoveOrigin, IllegalMoveOriginKind, MoveData};
use borrow_check::MirBorrowckCtxt;
use dataflow::move_paths::{IllegalMoveOrigin, IllegalMoveOriginKind};
use dataflow::move_paths::{LookupResult, MoveError, MovePathIndex};
use util::borrowck_errors::{BorrowckErrors, Origin};

pub(crate) fn report_move_errors<'gcx, 'tcx>(
mir: &Mir<'tcx>,
tcx: TyCtxt<'_, 'gcx, 'tcx>,
move_errors: Vec<MoveError<'tcx>>,
move_data: &MoveData<'tcx>,
) {
MoveErrorCtxt {
mir,
tcx,
move_data,
}.report_errors(move_errors);
}

#[derive(Copy, Clone)]
struct MoveErrorCtxt<'a, 'gcx: 'tcx, 'tcx: 'a> {
mir: &'a Mir<'tcx>,
tcx: TyCtxt<'a, 'gcx, 'tcx>,
move_data: &'a MoveData<'tcx>,
}

// Often when desugaring a pattern match we may have many individual moves in
// MIR that are all part of one operation from the user's point-of-view. For
// example:
Expand Down Expand Up @@ -76,15 +57,15 @@ enum GroupedMoveError<'tcx> {
},
}

impl<'a, 'gcx, 'tcx> MoveErrorCtxt<'a, 'gcx, 'tcx> {
fn report_errors(self, move_errors: Vec<MoveError<'tcx>>) {
impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
pub(crate) fn report_move_errors(&self, move_errors: Vec<MoveError<'tcx>>) {
let grouped_errors = self.group_move_errors(move_errors);
for error in grouped_errors {
self.report(error);
}
}

fn group_move_errors(self, errors: Vec<MoveError<'tcx>>) -> Vec<GroupedMoveError<'tcx>> {
fn group_move_errors(&self, errors: Vec<MoveError<'tcx>>) -> Vec<GroupedMoveError<'tcx>> {
let mut grouped_errors = Vec::new();
for error in errors {
self.append_to_grouped_errors(&mut grouped_errors, error);
Expand All @@ -93,7 +74,7 @@ impl<'a, 'gcx, 'tcx> MoveErrorCtxt<'a, 'gcx, 'tcx> {
}

fn append_to_grouped_errors(
self,
&self,
grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
error: MoveError<'tcx>,
) {
Expand Down Expand Up @@ -158,7 +139,7 @@ impl<'a, 'gcx, 'tcx> MoveErrorCtxt<'a, 'gcx, 'tcx> {
}

fn append_binding_error(
self,
&self,
grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
kind: IllegalMoveOriginKind<'tcx>,
move_from: &Place<'tcx>,
Expand Down Expand Up @@ -236,7 +217,7 @@ impl<'a, 'gcx, 'tcx> MoveErrorCtxt<'a, 'gcx, 'tcx> {
};
}

fn report(self, error: GroupedMoveError<'tcx>) {
fn report(&self, error: GroupedMoveError<'tcx>) {
let (mut err, err_span) = {
let (span, kind): (Span, &IllegalMoveOriginKind) = match error {
GroupedMoveError::MovesFromMatchPlace { span, ref kind, .. }
Expand Down Expand Up @@ -279,7 +260,7 @@ impl<'a, 'gcx, 'tcx> MoveErrorCtxt<'a, 'gcx, 'tcx> {
}

fn add_move_hints(
self,
&self,
error: GroupedMoveError<'tcx>,
err: &mut DiagnosticBuilder<'a>,
span: Span,
Expand Down Expand Up @@ -365,7 +346,7 @@ impl<'a, 'gcx, 'tcx> MoveErrorCtxt<'a, 'gcx, 'tcx> {
}
}

fn suitable_to_remove_deref(self, proj: &PlaceProjection<'tcx>, snippet: &str) -> bool {
fn suitable_to_remove_deref(&self, proj: &PlaceProjection<'tcx>, snippet: &str) -> bool {
let is_shared_ref = |ty: ty::Ty| match ty.sty {
ty::TypeVariants::TyRef(.., hir::Mutability::MutImmutable) => true,
_ => false,
Expand Down

0 comments on commit 341e5e3

Please sign in to comment.