From 4890ae254f39b1721724e8f663531e53245a347f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 25 May 2019 13:13:51 -0700 Subject: [PATCH 1/4] Do not ICE on missing access place description during mutability error reporting --- .../borrow_check/mutability_errors.rs | 16 +++++++--------- src/test/ui/issues/issue-61187.rs | 9 +++++++++ src/test/ui/issues/issue-61187.stderr | 11 +++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/test/ui/issues/issue-61187.rs create mode 100644 src/test/ui/issues/issue-61187.stderr diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index 16fbc8d6bb299..aa78545989955 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -41,14 +41,17 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { ); let mut err; - let item_msg; + let mut item_msg; let reason; let access_place_desc = self.describe_place(access_place); debug!("report_mutability_error: access_place_desc={:?}", access_place_desc); + item_msg = match &access_place_desc { + Some(desc) => format!("`{}`", desc), + None => "temporary place".to_string(), + }; match the_place_err { Place::Base(PlaceBase::Local(local)) => { - item_msg = format!("`{}`", access_place_desc.unwrap()); if let Place::Base(PlaceBase::Local(_)) = access_place { reason = ", as it is not declared as mutable".to_string(); } else { @@ -67,7 +70,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { base.ty(self.mir, self.infcx.tcx).ty )); - item_msg = format!("`{}`", access_place_desc.unwrap()); if self.is_upvar_field_projection(access_place).is_some() { reason = ", as it is not declared as mutable".to_string(); } else { @@ -82,7 +84,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { }) => { if *base == Place::Base(PlaceBase::Local(Local::new(1))) && !self.upvars.is_empty() { - item_msg = format!("`{}`", access_place_desc.unwrap()); debug_assert!(self.mir.local_decls[Local::new(1)].ty.is_region_ptr()); debug_assert!(is_closure_or_generator( the_place_err.ty(self.mir, self.infcx.tcx).ty @@ -105,7 +106,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { false } } { - item_msg = format!("`{}`", access_place_desc.unwrap()); reason = ", as it is immutable for the pattern guard".to_string(); } else { let pointer_type = @@ -114,8 +114,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { } else { "`*const` pointer" }; - if let Some(desc) = access_place_desc { - item_msg = format!("`{}`", desc); + if access_place_desc.is_some() { reason = match error_access { AccessKind::Move | AccessKind::Mutate => format!(" which is behind a {}", pointer_type), @@ -135,10 +134,9 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })) => { if let Place::Base(PlaceBase::Static(_)) = access_place { - item_msg = format!("immutable static item `{}`", access_place_desc.unwrap()); + item_msg = format!("immutable static item {}", item_msg); reason = String::new(); } else { - item_msg = format!("`{}`", access_place_desc.unwrap()); let static_name = &self.infcx.tcx.item_name(*def_id); reason = format!(", as `{}` is an immutable static item", static_name); } diff --git a/src/test/ui/issues/issue-61187.rs b/src/test/ui/issues/issue-61187.rs new file mode 100644 index 0000000000000..8b939b43b8bd4 --- /dev/null +++ b/src/test/ui/issues/issue-61187.rs @@ -0,0 +1,9 @@ +// edition:2018 +#![feature(async_await)] + +fn main() { +} + +async fn response(data: Vec) { + data.reverse(); //~ ERROR E0596 +} diff --git a/src/test/ui/issues/issue-61187.stderr b/src/test/ui/issues/issue-61187.stderr new file mode 100644 index 0000000000000..1019999a4bf30 --- /dev/null +++ b/src/test/ui/issues/issue-61187.stderr @@ -0,0 +1,11 @@ +error[E0596]: cannot borrow temporary place as mutable, as it is not declared as mutable + --> $DIR/issue-61187.rs:8:5 + | +LL | async fn response(data: Vec) { + | ---- help: consider changing this to be mutable: `mut data` +LL | data.reverse(); + | ^^^^ cannot borrow as mutable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0596`. From 285064ca21b8a96687a96dfc5ece713b38170d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 25 May 2019 20:42:00 -0700 Subject: [PATCH 2/4] Probe for CompilerDesugaringKind::Async argument --- src/librustc_mir/borrow_check/mutability_errors.rs | 12 +++++++----- src/test/ui/issues/issue-61187.stderr | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index aa78545989955..e90795c964691 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -7,7 +7,7 @@ use rustc::mir::{ use rustc::mir::{Terminator, TerminatorKind}; use rustc::ty::{self, Const, DefIdTree, Ty, TyS, TyCtxt}; use rustc_data_structures::indexed_vec::Idx; -use syntax_pos::Span; +use syntax_pos::{Span, CompilerDesugaringKind}; use syntax_pos::symbol::kw; use crate::dataflow::move_paths::InitLocation; @@ -41,14 +41,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { ); let mut err; - let mut item_msg; let reason; let access_place_desc = self.describe_place(access_place); debug!("report_mutability_error: access_place_desc={:?}", access_place_desc); - item_msg = match &access_place_desc { - Some(desc) => format!("`{}`", desc), - None => "temporary place".to_string(), + let mut item_msg = match (&access_place_desc, &the_place_err) { + (Some(desc), _) => format!("`{}`", desc), + (None, Place::Base(PlaceBase::Local(local))) if self.mir.local_decls[*local] + .source_info.span.is_compiler_desugaring(CompilerDesugaringKind::Async) + => "async `fn` parameter".to_string(), + (None, _) => "temporary place".to_string(), }; match the_place_err { Place::Base(PlaceBase::Local(local)) => { diff --git a/src/test/ui/issues/issue-61187.stderr b/src/test/ui/issues/issue-61187.stderr index 1019999a4bf30..6f6f32a9ca781 100644 --- a/src/test/ui/issues/issue-61187.stderr +++ b/src/test/ui/issues/issue-61187.stderr @@ -1,4 +1,4 @@ -error[E0596]: cannot borrow temporary place as mutable, as it is not declared as mutable +error[E0596]: cannot borrow async `fn` parameter as mutable, as it is not declared as mutable --> $DIR/issue-61187.rs:8:5 | LL | async fn response(data: Vec) { From 025a5595e6230c50fe84c1a35215afdd96370655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 25 May 2019 20:45:00 -0700 Subject: [PATCH 3/4] Move tests to async-await directory --- src/test/ui/{ => async-await}/issues/issue-61187.rs | 0 src/test/ui/{ => async-await}/issues/issue-61187.stderr | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/test/ui/{ => async-await}/issues/issue-61187.rs (100%) rename src/test/ui/{ => async-await}/issues/issue-61187.stderr (100%) diff --git a/src/test/ui/issues/issue-61187.rs b/src/test/ui/async-await/issues/issue-61187.rs similarity index 100% rename from src/test/ui/issues/issue-61187.rs rename to src/test/ui/async-await/issues/issue-61187.rs diff --git a/src/test/ui/issues/issue-61187.stderr b/src/test/ui/async-await/issues/issue-61187.stderr similarity index 100% rename from src/test/ui/issues/issue-61187.stderr rename to src/test/ui/async-await/issues/issue-61187.stderr From d72f97dfa90db5757d58689180939d574c5ab259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 28 May 2019 13:33:57 -0700 Subject: [PATCH 4/4] review comment: tweak error wording --- src/librustc_mir/borrow_check/mutability_errors.rs | 2 +- src/test/ui/async-await/issues/issue-61187.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index e90795c964691..9bb359a8911e4 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -49,7 +49,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { (Some(desc), _) => format!("`{}`", desc), (None, Place::Base(PlaceBase::Local(local))) if self.mir.local_decls[*local] .source_info.span.is_compiler_desugaring(CompilerDesugaringKind::Async) - => "async `fn` parameter".to_string(), + => "`async fn` parameter".to_string(), (None, _) => "temporary place".to_string(), }; match the_place_err { diff --git a/src/test/ui/async-await/issues/issue-61187.stderr b/src/test/ui/async-await/issues/issue-61187.stderr index 6f6f32a9ca781..52fe15e8cf7c7 100644 --- a/src/test/ui/async-await/issues/issue-61187.stderr +++ b/src/test/ui/async-await/issues/issue-61187.stderr @@ -1,4 +1,4 @@ -error[E0596]: cannot borrow async `fn` parameter as mutable, as it is not declared as mutable +error[E0596]: cannot borrow `async fn` parameter as mutable, as it is not declared as mutable --> $DIR/issue-61187.rs:8:5 | LL | async fn response(data: Vec) {