diff --git a/src/librustc_mir/borrow_check/conflict_errors.rs b/src/librustc_mir/borrow_check/conflict_errors.rs index 7b5fce4abbc4b..8986e87627e5e 100644 --- a/src/librustc_mir/borrow_check/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/conflict_errors.rs @@ -22,7 +22,7 @@ use super::{InitializationRequiringAction, PrefixSet}; use super::error_reporting::{IncludingDowncast, UseSpans}; use crate::dataflow::drop_flag_effects; use crate::dataflow::indexes::{MovePathIndex, MoveOutIndex}; -use crate::util::borrowck_errors::{BorrowckErrors, Origin}; +use crate::util::borrowck_errors; #[derive(Debug)] struct MoveSite { @@ -89,12 +89,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { Some(name) => format!("`{}`", name), None => "value".to_owned(), }; - let mut err = self.infcx.tcx.cannot_act_on_uninitialized_variable( + let mut err = self.cannot_act_on_uninitialized_variable( span, desired_action.as_noun(), &self.describe_place_with_options(moved_place, IncludingDowncast(true)) .unwrap_or_else(|| "_".to_owned()), - Origin::Mir, ); err.span_label(span, format!("use of possibly uninitialized {}", item_msg)); @@ -120,12 +119,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let msg = ""; //FIXME: add "partially " or "collaterally " - let mut err = self.infcx.tcx.cannot_act_on_moved_value( + let mut err = self.cannot_act_on_moved_value( span, desired_action.as_noun(), msg, self.describe_place_with_options(&moved_place, IncludingDowncast(true)), - Origin::Mir, ); self.add_moved_or_invoked_closure_note( @@ -267,7 +265,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}", location, place, span, borrow ); - let tcx = self.infcx.tcx; let value_msg = match self.describe_place(place) { Some(name) => format!("`{}`", name), None => "value".to_owned(), @@ -283,10 +280,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let move_spans = self.move_spans(place, location); let span = move_spans.args_or_use(); - let mut err = tcx.cannot_move_when_borrowed( + let mut err = self.cannot_move_when_borrowed( span, &self.describe_place(place).unwrap_or_else(|| "_".to_owned()), - Origin::Mir, ); err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg)); err.span_label(span, format!("move out of {} occurs here", value_msg)); @@ -315,8 +311,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { (place, _span): (&Place<'tcx>, Span), borrow: &BorrowData<'tcx>, ) -> DiagnosticBuilder<'cx> { - let tcx = self.infcx.tcx; - let borrow_spans = self.retrieve_borrow_spans(borrow); let borrow_span = borrow_spans.args_or_use(); @@ -325,13 +319,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let use_spans = self.move_spans(place, location); let span = use_spans.var_or_use(); - let mut err = tcx.cannot_use_when_mutably_borrowed( + let mut err = self.cannot_use_when_mutably_borrowed( span, &self.describe_place(place).unwrap_or_else(|| "_".to_owned()), borrow_span, &self.describe_place(&borrow.borrowed_place) .unwrap_or_else(|| "_".to_owned()), - Origin::Mir, ); borrow_spans.var_span_label(&mut err, { @@ -376,7 +369,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { }; // FIXME: supply non-"" `opt_via` when appropriate - let tcx = self.infcx.tcx; let first_borrow_desc; let mut err = match ( gen_borrow_kind, @@ -388,7 +380,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ) { (BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt) => { first_borrow_desc = "mutable "; - tcx.cannot_reborrow_already_borrowed( + self.cannot_reborrow_already_borrowed( span, &desc_place, &msg_place, @@ -398,12 +390,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { rgt, &msg_borrow, None, - Origin::Mir, ) } (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => { first_borrow_desc = "immutable "; - tcx.cannot_reborrow_already_borrowed( + self.cannot_reborrow_already_borrowed( span, &desc_place, &msg_place, @@ -413,42 +404,38 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { rgt, &msg_borrow, None, - Origin::Mir, ) } (BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => { first_borrow_desc = "first "; - tcx.cannot_mutably_borrow_multiply( + self.cannot_mutably_borrow_multiply( span, &desc_place, &msg_place, issued_span, &msg_borrow, None, - Origin::Mir, ) } (BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => { first_borrow_desc = "first "; - tcx.cannot_uniquely_borrow_by_two_closures( + self.cannot_uniquely_borrow_by_two_closures( span, &desc_place, issued_span, None, - Origin::Mir, ) } (BorrowKind::Mut { .. }, _, _, BorrowKind::Shallow, _, _) | (BorrowKind::Unique, _, _, BorrowKind::Shallow, _, _) => { - let mut err = tcx.cannot_mutate_in_match_guard( + let mut err = self.cannot_mutate_in_match_guard( span, issued_span, &desc_place, "mutably borrow", - Origin::Mir, ); borrow_spans.var_span_label( &mut err, @@ -462,7 +449,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { (BorrowKind::Unique, _, _, _, _, _) => { first_borrow_desc = "first "; - tcx.cannot_uniquely_borrow_by_one_closure( + self.cannot_uniquely_borrow_by_one_closure( span, container_name, &desc_place, @@ -471,13 +458,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "it", "", None, - Origin::Mir, ) }, (BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => { first_borrow_desc = "first "; - tcx.cannot_reborrow_already_uniquely_borrowed( + self.cannot_reborrow_already_uniquely_borrowed( span, container_name, &desc_place, @@ -487,13 +473,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "", None, second_borrow_desc, - Origin::Mir, ) } (BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => { first_borrow_desc = "first "; - tcx.cannot_reborrow_already_uniquely_borrowed( + self.cannot_reborrow_already_uniquely_borrowed( span, container_name, &desc_place, @@ -503,7 +488,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "", None, second_borrow_desc, - Origin::Mir, ) } @@ -833,10 +817,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { } } - let mut err = self.infcx.tcx.path_does_not_live_long_enough( + let mut err = self.path_does_not_live_long_enough( borrow_span, &format!("`{}`", name), - Origin::Mir, ); if let Some(annotation) = self.annotate_argument_and_return_for_borrow(borrow) { @@ -925,9 +908,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let borrow_spans = self.retrieve_borrow_spans(borrow); let borrow_span = borrow_spans.var_or_use(); - let mut err = self.infcx - .tcx - .cannot_borrow_across_destructor(borrow_span, Origin::Mir); + let mut err = self.cannot_borrow_across_destructor(borrow_span); let what_was_dropped = match self.describe_place(place) { Some(name) => format!("`{}`", name.as_str()), @@ -978,9 +959,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { drop_span, borrow_span ); - let mut err = self.infcx - .tcx - .thread_local_value_does_not_live_long_enough(borrow_span, Origin::Mir); + let mut err = self.thread_local_value_does_not_live_long_enough(borrow_span); err.span_label( borrow_span, @@ -1024,8 +1003,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { } } - let tcx = self.infcx.tcx; - let mut err = tcx.temporary_value_borrowed_for_too_long(proper_span, Origin::Mir); + let mut err = self.temporary_value_borrowed_for_too_long(proper_span); err.span_label( proper_span, "creates a temporary which is freed while still in use", @@ -1068,8 +1046,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { category: ConstraintCategory, opt_place_desc: Option<&String>, ) -> Option> { - let tcx = self.infcx.tcx; - let return_kind = match category { ConstraintCategory::Return => "return", ConstraintCategory::Yield => "yield", @@ -1132,12 +1108,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { } }; - let mut err = tcx.cannot_return_reference_to_local( + let mut err = self.cannot_return_reference_to_local( return_span, return_kind, reference_desc, &place_desc, - Origin::Mir, ); if return_span != borrow_span { @@ -1158,11 +1133,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ) -> DiagnosticBuilder<'cx> { let tcx = self.infcx.tcx; - let mut err = tcx.cannot_capture_in_long_lived_closure( + let mut err = self.cannot_capture_in_long_lived_closure( args_span, captured_var, var_span, - Origin::Mir, ); let suggestion = match tcx.sess.source_map().span_to_snippet(args_span) { @@ -1218,7 +1192,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "function" }; - let mut err = tcx.borrowed_data_escapes_closure(escape_span, escapes_from, Origin::Mir); + let mut err = borrowck_errors::borrowed_data_escapes_closure( + tcx, + escape_span, + escapes_from, + ); err.span_label( upvar_span, @@ -1360,14 +1338,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let loan_spans = self.retrieve_borrow_spans(loan); let loan_span = loan_spans.args_or_use(); - let tcx = self.infcx.tcx; if loan.kind == BorrowKind::Shallow { - let mut err = tcx.cannot_mutate_in_match_guard( + let mut err = self.cannot_mutate_in_match_guard( span, loan_span, &self.describe_place(place).unwrap_or_else(|| "_".to_owned()), "assign", - Origin::Mir, ); loan_spans.var_span_label( &mut err, @@ -1379,11 +1355,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { return; } - let mut err = tcx.cannot_assign_to_borrowed( + let mut err = self.cannot_assign_to_borrowed( span, loan_span, &self.describe_place(place).unwrap_or_else(|| "_".to_owned()), - Origin::Mir, ); loan_spans.var_span_label( @@ -1444,11 +1419,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { Some(decl) => (self.describe_place(err_place), decl.source_info.span), }; - let mut err = self.infcx.tcx.cannot_reassign_immutable( + let mut err = self.cannot_reassign_immutable( span, place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"), from_arg, - Origin::Mir, ); let msg = if from_arg { "cannot assign to immutable argument" diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 31a9766af303a..5851cd8178878 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -41,7 +41,6 @@ use crate::dataflow::MoveDataParamEnv; use crate::dataflow::{do_dataflow, DebugFormatted}; use crate::dataflow::EverInitializedPlaces; use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces}; -use crate::util::borrowck_errors::{BorrowckErrors, Origin}; use self::borrow_set::{BorrowData, BorrowSet}; use self::flows::Flows; @@ -422,8 +421,8 @@ fn downgrade_if_error(diag: &mut Diagnostic) { } } -pub struct MirBorrowckCtxt<'cx, 'tcx> { - infcx: &'cx InferCtxt<'cx, 'tcx>, +crate struct MirBorrowckCtxt<'cx, 'tcx> { + crate infcx: &'cx InferCtxt<'cx, 'tcx>, body: &'cx Body<'tcx>, mir_def_id: DefId, move_data: &'cx MoveData<'tcx>, @@ -1499,11 +1498,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { debug!("check_for_local_borrow({:?})", borrow); if borrow_of_local_data(&borrow.borrowed_place) { - let err = self.infcx.tcx - .cannot_borrow_across_generator_yield( + let err = self.cannot_borrow_across_generator_yield( self.retrieve_borrow_spans(borrow).var_or_use(), yield_span, - Origin::Mir, ); err.buffer(&mut self.errors_buffer); diff --git a/src/librustc_mir/borrow_check/move_errors.rs b/src/librustc_mir/borrow_check/move_errors.rs index 3130b7cec5b4a..5939adc5528d9 100644 --- a/src/librustc_mir/borrow_check/move_errors.rs +++ b/src/librustc_mir/borrow_check/move_errors.rs @@ -12,7 +12,6 @@ use crate::dataflow::move_paths::{ IllegalMoveOrigin, IllegalMoveOriginKind, LookupResult, MoveError, MovePathIndex, }; -use crate::util::borrowck_errors::{BorrowckErrors, Origin}; // 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 @@ -254,12 +253,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { ) } IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => { - self.infcx.tcx - .cannot_move_out_of_interior_of_drop(span, ty, Origin::Mir) + self.cannot_move_out_of_interior_of_drop(span, ty) } IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => - self.infcx.tcx.cannot_move_out_of_interior_noncopy( - span, ty, Some(*is_index), Origin::Mir + self.cannot_move_out_of_interior_noncopy( + span, ty, Some(*is_index), ), }, span, @@ -293,7 +291,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { ) }; - self.infcx.tcx.cannot_move_out_of(span, &description, Origin::Mir) + self.cannot_move_out_of(span, &description) } fn report_cannot_move_from_borrowed_content( @@ -302,8 +300,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { deref_target_place: &Place<'tcx>, span: Span, ) -> DiagnosticBuilder<'a> { - let origin = Origin::Mir; - // Inspect the type of the content behind the // borrow to provide feedback about why this // was a move rather than a copy. @@ -319,10 +315,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { if let Place::Base(PlaceBase::Local(local)) = *deref_base { let decl = &self.body.local_decls[local]; if decl.is_ref_for_guard() { - let mut err = self.infcx.tcx.cannot_move_out_of( + let mut err = self.cannot_move_out_of( span, &format!("`{}` in pattern guard", decl.name.unwrap()), - origin, ); err.note( "variables bound in patterns cannot be moved from \ @@ -334,9 +329,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { debug!("report: ty={:?}", ty); let mut err = match ty.sty { ty::Array(..) | ty::Slice(..) => - self.infcx.tcx.cannot_move_out_of_interior_noncopy( - span, ty, None, origin - ), + self.cannot_move_out_of_interior_noncopy(span, ty, None), ty::Closure(def_id, closure_substs) if def_id == self.mir_def_id && upvar_field.is_some() => { @@ -378,7 +371,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { closure_kind_ty, closure_kind, place_description, ); - let mut diag = self.infcx.tcx.cannot_move_out_of(span, &place_description, origin); + let mut diag = self.cannot_move_out_of(span, &place_description); diag.span_label(upvar_span, "captured outer variable"); @@ -388,17 +381,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let source = self.borrowed_content_source(deref_base); match (self.describe_place(move_place), source.describe_for_named_place()) { (Some(place_desc), Some(source_desc)) => { - self.infcx.tcx.cannot_move_out_of( + self.cannot_move_out_of( span, &format!("`{}` which is behind a {}", place_desc, source_desc), - origin, ) } (_, _) => { - self.infcx.tcx.cannot_move_out_of( + self.cannot_move_out_of( span, &source.describe_for_unnamed_place(), - origin, ) } } diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index 04814a15c626b..59a3354f9c52f 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -1,3 +1,4 @@ +use core::unicode::property::Pattern_White_Space; use rustc::hir; use rustc::hir::Node; use rustc::mir::{self, BindingForm, ClearCrossCrate, Local, Location, Body}; @@ -9,9 +10,7 @@ use syntax_pos::symbol::kw; use crate::borrow_check::MirBorrowckCtxt; use crate::borrow_check::error_reporting::BorrowedContentSource; -use crate::util::borrowck_errors::{BorrowckErrors, Origin}; use crate::util::collect_writes::FindAssignments; -use crate::util::suggest_ref_mut; use rustc_errors::Applicability; #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -161,15 +160,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let span = match error_access { AccessKind::Move => { - err = self.infcx.tcx - .cannot_move_out_of(span, &(item_msg + &reason), Origin::Mir); + err = self.cannot_move_out_of(span, &(item_msg + &reason)); err.span_label(span, "cannot move"); err.buffer(&mut self.errors_buffer); return; } AccessKind::Mutate => { - err = self.infcx.tcx - .cannot_assign(span, &(item_msg + &reason), Origin::Mir); + err = self.cannot_assign(span, &(item_msg + &reason)); act = "assign"; acted_on = "written"; span @@ -180,11 +177,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let borrow_spans = self.borrow_spans(span, location); let borrow_span = borrow_spans.args_or_use(); - err = self.infcx.tcx.cannot_borrow_path_as_mutable_because( + err = self.cannot_borrow_path_as_mutable_because( borrow_span, &item_msg, &reason, - Origin::Mir, ); borrow_spans.var_span_label( &mut err, @@ -632,3 +628,16 @@ fn annotate_struct_field( None } + +/// If possible, suggest replacing `ref` with `ref mut`. +fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<(String)> { + let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).unwrap(); + if hi_src.starts_with("ref") + && hi_src["ref".len()..].starts_with(Pattern_White_Space) + { + let replacement = format!("ref mut{}", &hi_src["ref".len()..]); + Some(replacement) + } else { + None + } +} diff --git a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs index 9e08961f440f2..efa18587b7ddb 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs @@ -4,8 +4,8 @@ use crate::borrow_check::nll::region_infer::RegionInferenceContext; use crate::borrow_check::nll::type_check::Locations; use crate::borrow_check::nll::universal_regions::DefiningTy; use crate::borrow_check::nll::ConstraintDescription; -use crate::util::borrowck_errors::{BorrowckErrors, Origin}; use crate::borrow_check::Upvar; +use crate::util::borrowck_errors; use rustc::hir::def_id::DefId; use rustc::infer::error_reporting::nice_region_error::NiceRegionError; use rustc::infer::InferCtxt; @@ -487,9 +487,11 @@ impl<'tcx> RegionInferenceContext<'tcx> { ); } - let mut diag = infcx - .tcx - .borrowed_data_escapes_closure(span, escapes_from, Origin::Mir); + let mut diag = borrowck_errors::borrowed_data_escapes_closure( + infcx.tcx, + span, + escapes_from, + ); if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span { diag.span_label( diff --git a/src/librustc_mir/error_codes.rs b/src/librustc_mir/error_codes.rs index 618c047e77359..83d441b90be72 100644 --- a/src/librustc_mir/error_codes.rs +++ b/src/librustc_mir/error_codes.rs @@ -2462,12 +2462,12 @@ register_diagnostics! { // E0298, // cannot compare constants // E0299, // mismatched types between arms // E0471, // constant evaluation error (in pattern) -// E0385, // {} in an aliasable location +// E0385, // {} in an aliasable location E0493, // destructors cannot be evaluated at compile-time - E0521, // borrowed data escapes outside of closure + E0521, // borrowed data escapes outside of closure E0524, // two closures require unique access to `..` at the same time E0526, // shuffle indices are not constant E0594, // cannot assign to {} - E0598, // lifetime of {} is too short to guarantee its contents can be... +// E0598, // lifetime of {} is too short to guarantee its contents can be... E0625, // thread-local statics cannot be accessed at compile-time } diff --git a/src/librustc_mir/util/borrowck_errors.rs b/src/librustc_mir/util/borrowck_errors.rs index bce6761e2caf7..3359d1b3bbfe1 100644 --- a/src/librustc_mir/util/borrowck_errors.rs +++ b/src/librustc_mir/util/borrowck_errors.rs @@ -2,27 +2,11 @@ use rustc::ty::{self, Ty, TyCtxt}; use rustc_errors::{DiagnosticBuilder, DiagnosticId}; use syntax_pos::{MultiSpan, Span}; -// FIXME(chrisvittal) remove Origin entirely -#[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub enum Origin { - Mir, -} - -pub trait BorrowckErrors<'cx>: Sized + Copy { - fn struct_span_err_with_code>( - self, - sp: S, - msg: &str, - code: DiagnosticId, - ) -> DiagnosticBuilder<'cx>; - - fn struct_span_err>(self, sp: S, msg: &str) -> DiagnosticBuilder<'cx>; - - fn cannot_move_when_borrowed( - self, +impl<'cx, 'tcx> crate::borrow_check::MirBorrowckCtxt<'cx, 'tcx> { + crate fn cannot_move_when_borrowed( + &self, span: Span, desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -33,13 +17,12 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn cannot_use_when_mutably_borrowed( - self, + crate fn cannot_use_when_mutably_borrowed( + &self, span: Span, desc: &str, borrow_span: Span, borrow_desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -57,12 +40,11 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_act_on_uninitialized_variable( - self, + crate fn cannot_act_on_uninitialized_variable( + &self, span: Span, verb: &str, desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -74,15 +56,14 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn cannot_mutably_borrow_multiply( - self, + crate fn cannot_mutably_borrow_multiply( + &self, new_loan_span: Span, desc: &str, opt_via: &str, old_loan_span: Span, old_opt_via: &str, old_load_end_span: Option, - _: Origin, ) -> DiagnosticBuilder<'cx> { let via = |msg: &str| if msg.is_empty() { msg.to_string() } else { format!(" (via `{}`)", msg) }; @@ -124,13 +105,12 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_uniquely_borrow_by_two_closures( - self, + crate fn cannot_uniquely_borrow_by_two_closures( + &self, new_loan_span: Span, desc: &str, old_loan_span: Span, old_load_end_span: Option, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -154,8 +134,8 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_uniquely_borrow_by_one_closure( - self, + crate fn cannot_uniquely_borrow_by_one_closure( + &self, new_loan_span: Span, container_name: &str, desc_new: &str, @@ -164,7 +144,6 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { noun_old: &str, old_opt_via: &str, previous_end_span: Option, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -186,8 +165,8 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_reborrow_already_uniquely_borrowed( - self, + crate fn cannot_reborrow_already_uniquely_borrowed( + &self, new_loan_span: Span, container_name: &str, desc_new: &str, @@ -197,7 +176,6 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { old_opt_via: &str, previous_end_span: Option, second_borrow_desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -223,8 +201,8 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_reborrow_already_borrowed( - self, + crate fn cannot_reborrow_already_borrowed( + &self, span: Span, desc_new: &str, msg_new: &str, @@ -234,7 +212,6 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { kind_old: &str, msg_old: &str, old_load_end_span: Option, - _: Origin, ) -> DiagnosticBuilder<'cx> { let via = |msg: &str| if msg.is_empty() { msg.to_string() } else { format!(" (via `{}`)", msg) }; @@ -277,12 +254,11 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_assign_to_borrowed( - self, + crate fn cannot_assign_to_borrowed( + &self, span: Span, borrow_span: Span, desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -300,22 +276,11 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_move_into_closure(self, span: Span, desc: &str, _: Origin) -> DiagnosticBuilder<'cx> { - struct_span_err!( - self, - span, - E0504, - "cannot move `{}` into closure because it is borrowed", - desc, - ) - } - - fn cannot_reassign_immutable( - self, + crate fn cannot_reassign_immutable( + &self, span: Span, desc: &str, is_arg: bool, - _: Origin, ) -> DiagnosticBuilder<'cx> { let msg = if is_arg { "to immutable argument" @@ -332,19 +297,14 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn cannot_assign(self, span: Span, desc: &str, _: Origin) -> DiagnosticBuilder<'cx> { + crate fn cannot_assign(&self, span: Span, desc: &str) -> DiagnosticBuilder<'cx> { struct_span_err!(self, span, E0594, "cannot assign to {}", desc) } - fn cannot_assign_static(self, span: Span, desc: &str, o: Origin) -> DiagnosticBuilder<'cx> { - self.cannot_assign(span, &format!("immutable static item `{}`", desc), o) - } - - fn cannot_move_out_of( - self, + crate fn cannot_move_out_of( + &self, move_from_span: Span, move_from_desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -358,12 +318,11 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { /// Signal an error due to an attempt to move out of the interior /// of an array or slice. `is_index` is None when error origin /// didn't capture whether there was an indexing operation or not. - fn cannot_move_out_of_interior_noncopy( - self, + crate fn cannot_move_out_of_interior_noncopy( + &self, move_from_span: Span, ty: Ty<'_>, is_index: Option, - _: Origin, ) -> DiagnosticBuilder<'cx> { let type_name = match (&ty.sty, is_index) { (&ty::Array(_, _), Some(true)) | (&ty::Array(_, _), None) => "array", @@ -382,11 +341,10 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_move_out_of_interior_of_drop( - self, + crate fn cannot_move_out_of_interior_of_drop( + &self, move_from_span: Span, container_ty: Ty<'_>, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -399,13 +357,12 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_act_on_moved_value( - self, + crate fn cannot_act_on_moved_value( + &self, use_span: Span, verb: &str, optional_adverb_for_moved: &str, moved_path: Option, - _: Origin, ) -> DiagnosticBuilder<'cx> { let moved_path = moved_path .map(|mp| format!(": `{}`", mp)) @@ -422,42 +379,11 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn cannot_partially_reinit_an_uninit_struct( - self, - span: Span, - uninit_path: &str, - _: Origin, - ) -> DiagnosticBuilder<'cx> { - struct_span_err!( - self, - span, - E0383, - "partial reinitialization of uninitialized structure `{}`", - uninit_path, - ) - } - - fn closure_cannot_assign_to_borrowed( - self, - span: Span, - descr: &str, - _: Origin, - ) -> DiagnosticBuilder<'cx> { - struct_span_err!( - self, - span, - E0595, - "closure cannot assign to {}", - descr, - ) - } - - fn cannot_borrow_path_as_mutable_because( - self, + crate fn cannot_borrow_path_as_mutable_because( + &self, span: Span, path: &str, reason: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -469,22 +395,12 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn cannot_borrow_path_as_mutable( - self, - span: Span, - path: &str, - o: Origin, - ) -> DiagnosticBuilder<'cx> { - self.cannot_borrow_path_as_mutable_because(span, path, "", o) - } - - fn cannot_mutate_in_match_guard( - self, + crate fn cannot_mutate_in_match_guard( + &self, mutate_span: Span, match_span: Span, match_place: &str, action: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -499,11 +415,10 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_borrow_across_generator_yield( - self, + crate fn cannot_borrow_across_generator_yield( + &self, span: Span, yield_span: Span, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -515,10 +430,9 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn cannot_borrow_across_destructor( - self, + crate fn cannot_borrow_across_destructor( + &self, borrow_span: Span, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -528,11 +442,10 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn path_does_not_live_long_enough( - self, + crate fn path_does_not_live_long_enough( + &self, span: Span, path: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -543,13 +456,12 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn cannot_return_reference_to_local( - self, + crate fn cannot_return_reference_to_local( + &self, span: Span, return_kind: &str, reference_desc: &str, path_desc: &str, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -569,64 +481,11 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn lifetime_too_short_for_reborrow( - self, - span: Span, - path: &str, - _: Origin, - ) -> DiagnosticBuilder<'cx> { - struct_span_err!( - self, - span, - E0598, - "lifetime of {} is too short to guarantee \ - its contents can be safely reborrowed", - path, - ) - } - - fn cannot_act_on_capture_in_sharable_fn( - self, - span: Span, - bad_thing: &str, - help: (Span, &str), - _: Origin, - ) -> DiagnosticBuilder<'cx> { - let (help_span, help_msg) = help; - let mut err = struct_span_err!( - self, - span, - E0387, - "{} in a captured outer variable in an `Fn` closure", - bad_thing, - ); - err.span_help(help_span, help_msg); - err - } - - fn cannot_assign_into_immutable_reference( - self, - span: Span, - bad_thing: &str, - _: Origin, - ) -> DiagnosticBuilder<'cx> { - let mut err = struct_span_err!( - self, - span, - E0389, - "{} in a `&` reference", - bad_thing, - ); - err.span_label(span, "assignment into an immutable reference"); - err - } - - fn cannot_capture_in_long_lived_closure( - self, + crate fn cannot_capture_in_long_lived_closure( + &self, closure_span: Span, borrowed_path: &str, capture_span: Span, - _: Origin, ) -> DiagnosticBuilder<'cx> { let mut err = struct_span_err!( self, @@ -645,25 +504,9 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { err } - fn borrowed_data_escapes_closure( - self, - escape_span: Span, - escapes_from: &str, - _: Origin, - ) -> DiagnosticBuilder<'cx> { - struct_span_err!( - self, - escape_span, - E0521, - "borrowed data escapes outside of {}", - escapes_from, - ) - } - - fn thread_local_value_does_not_live_long_enough( - self, + crate fn thread_local_value_does_not_live_long_enough( + &self, span: Span, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -673,10 +516,9 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { ) } - fn temporary_value_borrowed_for_too_long( - self, + crate fn temporary_value_borrowed_for_too_long( + &self, span: Span, - _: Origin, ) -> DiagnosticBuilder<'cx> { struct_span_err!( self, @@ -685,19 +527,27 @@ pub trait BorrowckErrors<'cx>: Sized + Copy { "temporary value dropped while borrowed", ) } -} -impl BorrowckErrors<'tcx> for TyCtxt<'tcx> { fn struct_span_err_with_code>( - self, + &self, sp: S, msg: &str, code: DiagnosticId, ) -> DiagnosticBuilder<'tcx> { - self.sess.struct_span_err_with_code(sp, msg, code) + self.infcx.tcx.sess.struct_span_err_with_code(sp, msg, code) } +} - fn struct_span_err>(self, sp: S, msg: &str) -> DiagnosticBuilder<'tcx> { - self.sess.struct_span_err(sp, msg) - } +crate fn borrowed_data_escapes_closure<'tcx>( + tcx: TyCtxt<'tcx>, + escape_span: Span, + escapes_from: &str, +) -> DiagnosticBuilder<'tcx> { + struct_span_err!( + tcx.sess, + escape_span, + E0521, + "borrowed data escapes outside of {}", + escapes_from, + ) } diff --git a/src/librustc_mir/util/mod.rs b/src/librustc_mir/util/mod.rs index fd2a5e452ce4b..c8a90a989142f 100644 --- a/src/librustc_mir/util/mod.rs +++ b/src/librustc_mir/util/mod.rs @@ -1,7 +1,3 @@ -use core::unicode::property::Pattern_White_Space; -use rustc::ty::TyCtxt; -use syntax_pos::Span; - pub mod aggregate; pub mod borrowck_errors; pub mod elaborate_drops; @@ -19,16 +15,3 @@ pub use self::alignment::is_disaligned; pub use self::pretty::{dump_enabled, dump_mir, write_mir_pretty, PassWhere}; pub use self::graphviz::{graphviz_safe_def_name, write_mir_graphviz}; pub use self::graphviz::write_node_label as write_graphviz_node_label; - -/// If possible, suggest replacing `ref` with `ref mut`. -pub fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<(String)> { - let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).unwrap(); - if hi_src.starts_with("ref") - && hi_src["ref".len()..].starts_with(Pattern_White_Space) - { - let replacement = format!("ref mut{}", &hi_src["ref".len()..]); - Some(replacement) - } else { - None - } -}