From a12982cdc2fee3d892015ee981a2f98481f831f0 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 20 Feb 2019 12:15:27 -0300 Subject: [PATCH] Run rustfmt --- .../borrow_check/nll/explain_borrow/mod.rs | 272 ++++++++++-------- 1 file changed, 155 insertions(+), 117 deletions(-) diff --git a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs index 86cab6cbfb2d3..8ea249959dd2c 100644 --- a/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs +++ b/src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs @@ -2,15 +2,14 @@ use std::collections::VecDeque; use crate::borrow_check::borrow_set::BorrowData; use crate::borrow_check::error_reporting::UseSpans; -use crate::borrow_check::nll::ConstraintDescription; use crate::borrow_check::nll::region_infer::{Cause, RegionName}; +use crate::borrow_check::nll::ConstraintDescription; use crate::borrow_check::{Context, MirBorrowckCtxt, WriteKind}; -use rustc::ty::{self, TyCtxt}; use rustc::mir::{ - CastKind, ConstraintCategory, FakeReadCause, Local, Location, Mir, Operand, - Place, Projection, ProjectionElem, Rvalue, Statement, StatementKind, - TerminatorKind + CastKind, ConstraintCategory, FakeReadCause, Local, Location, Mir, Operand, Place, Projection, + ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind, }; +use rustc::ty::{self, TyCtxt}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::DiagnosticBuilder; use syntax_pos::Span; @@ -63,50 +62,58 @@ impl BorrowExplanation { let message = match later_use_kind { LaterUseKind::TraitCapture => "borrow later captured here by trait object", LaterUseKind::ClosureCapture => "borrow later captured here by closure", - LaterUseKind::Call => "borrow later used by call", + LaterUseKind::Call => "borrow later used by call", LaterUseKind::FakeLetRead => "borrow later stored here", LaterUseKind::Other => "borrow later used here", }; err.span_label(var_or_use_span, format!("{}{}", borrow_desc, message)); - }, + } BorrowExplanation::UsedLaterInLoop(later_use_kind, var_or_use_span) => { let message = match later_use_kind { - LaterUseKind::TraitCapture => - "borrow captured here by trait object, in later iteration of loop", - LaterUseKind::ClosureCapture => - "borrow captured here by closure, in later iteration of loop", - LaterUseKind::Call => "borrow used by call, in later iteration of loop", + LaterUseKind::TraitCapture => { + "borrow captured here by trait object, in later iteration of loop" + } + LaterUseKind::ClosureCapture => { + "borrow captured here by closure, in later iteration of loop" + } + LaterUseKind::Call => "borrow used by call, in later iteration of loop", LaterUseKind::FakeLetRead => "borrow later stored here", LaterUseKind::Other => "borrow used here, in later iteration of loop", }; err.span_label(var_or_use_span, format!("{}{}", borrow_desc, message)); - }, - BorrowExplanation::UsedLaterWhenDropped { drop_loc, dropped_local, - should_note_order } => - { + } + BorrowExplanation::UsedLaterWhenDropped { + drop_loc, + dropped_local, + should_note_order, + } => { let local_decl = &mir.local_decls[dropped_local]; let (dtor_desc, type_desc) = match local_decl.ty.sty { // If type is an ADT that implements Drop, then // simplify output by reporting just the ADT name. - ty::Adt(adt, _substs) if adt.has_dtor(tcx) && !adt.is_box() => - ("`Drop` code", format!("type `{}`", tcx.item_path_str(adt.did))), + ty::Adt(adt, _substs) if adt.has_dtor(tcx) && !adt.is_box() => ( + "`Drop` code", + format!("type `{}`", tcx.item_path_str(adt.did)), + ), // Otherwise, just report the whole type (and use // the intentionally fuzzy phrase "destructor") - ty::Closure(..) => - ("destructor", "closure".to_owned()), - ty::Generator(..) => - ("destructor", "generator".to_owned()), + ty::Closure(..) => ("destructor", "closure".to_owned()), + ty::Generator(..) => ("destructor", "generator".to_owned()), _ => ("destructor", format!("type `{}`", local_decl.ty)), }; match local_decl.name { Some(local_name) => { - let message = - format!("{B}borrow might be used here, when `{LOC}` is dropped \ - and runs the {DTOR} for {TYPE}", - B=borrow_desc, LOC=local_name, TYPE=type_desc, DTOR=dtor_desc); + let message = format!( + "{B}borrow might be used here, when `{LOC}` is dropped \ + and runs the {DTOR} for {TYPE}", + B = borrow_desc, + LOC = local_name, + TYPE = type_desc, + DTOR = dtor_desc + ); err.span_label(mir.source_info(drop_loc).span, message); if should_note_order { @@ -117,15 +124,22 @@ impl BorrowExplanation { } } None => { - err.span_label(local_decl.source_info.span, - format!("a temporary with access to the {B}borrow \ - is created here ...", - B=borrow_desc)); - let message = - format!("... and the {B}borrow might be used here, \ - when that temporary is dropped \ - and runs the {DTOR} for {TYPE}", - B=borrow_desc, TYPE=type_desc, DTOR=dtor_desc); + err.span_label( + local_decl.source_info.span, + format!( + "a temporary with access to the {B}borrow \ + is created here ...", + B = borrow_desc + ), + ); + let message = format!( + "... and the {B}borrow might be used here, \ + when that temporary is dropped \ + and runs the {DTOR} for {TYPE}", + B = borrow_desc, + TYPE = type_desc, + DTOR = dtor_desc + ); err.span_label(mir.source_info(drop_loc).span, message); if let Some(info) = &local_decl.is_block_tail { @@ -149,7 +163,7 @@ impl BorrowExplanation { } } } - }, + } BorrowExplanation::MustBeValidFor { category, span, @@ -160,18 +174,28 @@ impl BorrowExplanation { region_name.highlight_region_name(err); if let Some(desc) = opt_place_desc { - err.span_label(span, format!( - "{}requires that `{}` is borrowed for `{}`", - category.description(), desc, region_name, - )); + err.span_label( + span, + format!( + "{}requires that `{}` is borrowed for `{}`", + category.description(), + desc, + region_name, + ), + ); } else { - err.span_label(span, format!( - "{}requires that {}borrow lasts for `{}`", - category.description(), borrow_desc, region_name, - )); + err.span_label( + span, + format!( + "{}requires that {}borrow lasts for `{}`", + category.description(), + borrow_desc, + region_name, + ), + ); }; - }, - _ => {}, + } + _ => {} } } } @@ -217,10 +241,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { region_sub ); - match find_use::find(mir, regioncx, tcx, region_sub, context.loc) { + match find_use::find(mir, regioncx, tcx, region_sub, context.loc) { Some(Cause::LiveVar(local, location)) => { let span = mir.source_info(location).span; - let spans = self.move_spans(&Place::Local(local), location) + let spans = self + .move_spans(&Place::Local(local), location) .or_else(|| self.borrow_spans(span, location)); let borrow_location = context.loc; @@ -236,55 +261,56 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { } } - Some(Cause::DropVar(local, location)) => { - let mut should_note_order = false; - if mir.local_decls[local].name.is_some() { - if let Some((WriteKind::StorageDeadOrDrop, place)) = kind_place { - if let Place::Local(borrowed_local) = place { - let dropped_local_scope = mir.local_decls[local].visibility_scope; - let borrowed_local_scope = - mir.local_decls[*borrowed_local].visibility_scope; - - if mir.is_sub_scope(borrowed_local_scope, dropped_local_scope) - && local != *borrowed_local - { - should_note_order = true; - } - } - } - } - - BorrowExplanation::UsedLaterWhenDropped { - drop_loc: location, - dropped_local: local, - should_note_order, - } + Some(Cause::DropVar(local, location)) => { + let mut should_note_order = false; + if mir.local_decls[local].name.is_some() { + if let Some((WriteKind::StorageDeadOrDrop, place)) = kind_place { + if let Place::Local(borrowed_local) = place { + let dropped_local_scope = mir.local_decls[local].visibility_scope; + let borrowed_local_scope = + mir.local_decls[*borrowed_local].visibility_scope; + + if mir.is_sub_scope(borrowed_local_scope, dropped_local_scope) + && local != *borrowed_local + { + should_note_order = true; + } + } + } + } + + BorrowExplanation::UsedLaterWhenDropped { + drop_loc: location, + dropped_local: local, + should_note_order, + } } - None => if let Some(region) = regioncx.to_error_region_vid(borrow_region_vid) { - let (category, from_closure, span, region_name) = self - .nonlexical_regioncx - .free_region_constraint_info( - self.mir, - self.mir_def_id, - self.infcx, - borrow_region_vid, - region, - ); - if let Some(region_name) = region_name { - let opt_place_desc = self.describe_place(&borrow.borrowed_place); - BorrowExplanation::MustBeValidFor { - category, - from_closure, - span, - region_name, - opt_place_desc, + None => { + if let Some(region) = regioncx.to_error_region_vid(borrow_region_vid) { + let (category, from_closure, span, region_name) = + self.nonlexical_regioncx.free_region_constraint_info( + self.mir, + self.mir_def_id, + self.infcx, + borrow_region_vid, + region, + ); + if let Some(region_name) = region_name { + let opt_place_desc = self.describe_place(&borrow.borrowed_place); + BorrowExplanation::MustBeValidFor { + category, + from_closure, + span, + region_name, + opt_place_desc, + } + } else { + BorrowExplanation::Unexplained } } else { BorrowExplanation::Unexplained } - } else { - BorrowExplanation::Unexplained } } } @@ -427,27 +453,31 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { &self, borrow: &BorrowData<'tcx>, use_spans: UseSpans, - location: Location + location: Location, ) -> (LaterUseKind, Span) { match use_spans { UseSpans::ClosureUse { var_span, .. } => { // Used in a closure. (LaterUseKind::ClosureCapture, var_span) - }, + } UseSpans::OtherUse(span) => { let block = &self.mir.basic_blocks()[location.block]; let kind = if let Some(&Statement { kind: StatementKind::FakeRead(FakeReadCause::ForLet, _), .. - }) = block.statements.get(location.statement_index) { + }) = block.statements.get(location.statement_index) + { LaterUseKind::FakeLetRead } else if self.was_captured_by_trait_object(borrow) { LaterUseKind::TraitCapture } else if location.statement_index == block.statements.len() { if let TerminatorKind::Call { - ref func, from_hir_call: true, .. - } = block.terminator().kind { + ref func, + from_hir_call: true, + .. + } = block.terminator().kind + { // Just point to the function, to reduce the chance of overlapping spans. let function_span = match func { Operand::Constant(c) => c.span, @@ -458,7 +488,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { } else { span } - }, + } _ => span, }; return (LaterUseKind::Call, function_span); @@ -482,7 +512,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { let location = borrow.reserve_location; let block = &self.mir[location.block]; let stmt = block.statements.get(location.statement_index); - debug!("was_captured_by_trait_object: location={:?} stmt={:?}", location, stmt); + debug!( + "was_captured_by_trait_object: location={:?} stmt={:?}", + location, stmt + ); // We make a `queue` vector that has the locations we want to visit. As of writing, this // will only ever have one item at any given time, but by using a vector, we can pop from @@ -491,13 +524,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { let mut target = if let Some(&Statement { kind: StatementKind::Assign(Place::Local(local), _), .. - }) = stmt { + }) = stmt + { local } else { return false; }; - debug!("was_captured_by_trait: target={:?} queue={:?}", target, queue); + debug!( + "was_captured_by_trait: target={:?} queue={:?}", + target, queue + ); while let Some(current_location) = queue.pop() { debug!("was_captured_by_trait: target={:?}", target); let block = &self.mir[current_location.block]; @@ -508,55 +545,55 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { debug!("was_captured_by_trait_object: stmt={:?}", stmt); // The only kind of statement that we care about is assignments... - if let StatementKind::Assign( - place, - box rvalue, - ) = &stmt.kind { + if let StatementKind::Assign(place, box rvalue) = &stmt.kind { let into = match place { Place::Local(into) => into, Place::Projection(box Projection { base: Place::Local(into), elem: ProjectionElem::Deref, }) => into, - _ => { + _ => { // Continue at the next location. queue.push(current_location.successor_within_block()); continue; - }, + } }; match rvalue { // If we see a use, we should check whether it is our data, and if so // update the place that we're looking for to that new place. Rvalue::Use(operand) => match operand { - Operand::Copy(Place::Local(from)) | - Operand::Move(Place::Local(from)) if *from == target => { + Operand::Copy(Place::Local(from)) + | Operand::Move(Place::Local(from)) + if *from == target => + { target = *into; - }, - _ => {}, + } + _ => {} }, // If we see a unsized cast, then if it is our data we should check // whether it is being cast to a trait object. Rvalue::Cast(CastKind::Unsize, operand, ty) => match operand { - Operand::Copy(Place::Local(from)) | - Operand::Move(Place::Local(from)) if *from == target => { + Operand::Copy(Place::Local(from)) + | Operand::Move(Place::Local(from)) + if *from == target => + { debug!("was_captured_by_trait_object: ty={:?}", ty); // Check the type for a trait object. return match ty.sty { // `&dyn Trait` ty::TyKind::Ref(_, ty, _) if ty.is_trait() => true, // `Box` - _ if ty.is_box() && ty.boxed_ty().is_trait() => - true, + _ if ty.is_box() && ty.boxed_ty().is_trait() => true, // `dyn Trait` _ if ty.is_trait() => true, // Anything else. _ => false, }; - }, + } _ => return false, }, - _ => {}, + _ => {} } } @@ -571,7 +608,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> { destination: Some((Place::Local(dest), block)), args, .. - } = &terminator.kind { + } = &terminator.kind + { debug!( "was_captured_by_trait_object: target={:?} dest={:?} args={:?}", target, dest, args