Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions compiler/rustc_mir/src/borrow_check/diagnostics/move_errors.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_span::source_map::DesugaringKind; use rustc_span::source_map::DesugaringKind;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span, DUMMY_SP};
use rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions;


use crate::borrow_check::diagnostics::UseSpans; use crate::borrow_check::diagnostics::UseSpans;
use crate::borrow_check::prefixes::PrefixSet; use crate::borrow_check::prefixes::PrefixSet;
Expand Down Expand Up @@ -384,8 +385,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
} }
} }
}; };
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) { let ty = move_place.ty(self.body, self.infcx.tcx).ty;
let def_id = match *move_place.ty(self.body, self.infcx.tcx).ty.kind() { let def_id = match *ty.kind() {
ty::Adt(self_def, _) => self_def.did, ty::Adt(self_def, _) => self_def.did,
ty::Foreign(def_id) ty::Foreign(def_id)
| ty::FnDef(def_id, _) | ty::FnDef(def_id, _)
Expand All @@ -397,23 +398,31 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let is_option = self.infcx.tcx.is_diagnostic_item(sym::option_type, def_id); let is_option = self.infcx.tcx.is_diagnostic_item(sym::option_type, def_id);
let is_result = self.infcx.tcx.is_diagnostic_item(sym::result_type, def_id); let is_result = self.infcx.tcx.is_diagnostic_item(sym::result_type, def_id);
if (is_option || is_result) && use_spans.map_or(true, |v| !v.for_closure()) { if (is_option || is_result) && use_spans.map_or(true, |v| !v.for_closure()) {
err.span_suggestion( err.span_suggestion_verbose(
span, span.shrink_to_hi(),
&format!( &format!(
"consider borrowing the `{}`'s content", "consider borrowing the `{}`'s content",
if is_option { "Option" } else { "Result" } if is_option { "Option" } else { "Result" }
), ),
format!("{}.as_ref()", snippet), ".as_ref()".to_string(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} else if matches!(span.desugaring_kind(), Some(DesugaringKind::ForLoop(_))) } else if matches!(span.desugaring_kind(), Some(DesugaringKind::ForLoop(_))) {
&& self.infcx.tcx.is_diagnostic_item(sym::vec_type, def_id) let suggest = match self.infcx.tcx.get_diagnostic_item(sym::IntoIterator) {
{ Some(def_id) => type_known_to_meet_bound_modulo_regions(
// FIXME: suggest for anything that implements `IntoIterator`. &self.infcx,
err.span_suggestion( self.param_env,
span, self.infcx.tcx.mk_imm_ref(self.infcx.tcx.lifetimes.re_erased, ty),
"consider iterating over a slice of the `Vec<_>`'s content", def_id,
format!("&{}", snippet), DUMMY_SP,
),
_ => false,
};
if suggest {
err.span_suggestion_verbose(
span.shrink_to_lo(),
&format!("consider iterating over a slice of the `{}`'s content", ty),
"&".to_string(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.fixed
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@


struct Foo { struct Foo {
v: Vec<u32>, v: Vec<u32>,
h: std::collections::HashMap<i32, i32>,
} }


impl Foo { impl Foo {
fn bar(&self) { fn bar(&self) {
for _ in &self.v { //~ ERROR cannot move out of `self.v` which is behind a shared reference for _ in &self.v { //~ ERROR cannot move out of `self.v` which is behind a shared reference
} }
for _ in &self.h { //~ ERROR cannot move out of `self.h` which is behind a shared reference
}
} }
} }


Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.rs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@


struct Foo { struct Foo {
v: Vec<u32>, v: Vec<u32>,
h: std::collections::HashMap<i32, i32>,
} }


impl Foo { impl Foo {
fn bar(&self) { fn bar(&self) {
for _ in self.v { //~ ERROR cannot move out of `self.v` which is behind a shared reference for _ in self.v { //~ ERROR cannot move out of `self.v` which is behind a shared reference
} }
for _ in self.h { //~ ERROR cannot move out of `self.h` which is behind a shared reference
}
} }
} }


Expand Down
25 changes: 19 additions & 6 deletions src/test/ui/suggestions/for-i-in-vec.stderr
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,25 @@
error[E0507]: cannot move out of `self.v` which is behind a shared reference error[E0507]: cannot move out of `self.v` which is behind a shared reference
--> $DIR/for-i-in-vec.rs:10:18 --> $DIR/for-i-in-vec.rs:11:18
| |
LL | for _ in self.v { LL | for _ in self.v {
| ^^^^^^ | ^^^^^^ move occurs because `self.v` has type `Vec<u32>`, which does not implement the `Copy` trait
| | |
| move occurs because `self.v` has type `Vec<u32>`, which does not implement the `Copy` trait help: consider iterating over a slice of the `Vec<u32>`'s content
| help: consider iterating over a slice of the `Vec<_>`'s content: `&self.v` |
LL | for _ in &self.v {
| ^

error[E0507]: cannot move out of `self.h` which is behind a shared reference
--> $DIR/for-i-in-vec.rs:13:18
|
LL | for _ in self.h {
| ^^^^^^ move occurs because `self.h` has type `HashMap<i32, i32>`, which does not implement the `Copy` trait
|
help: consider iterating over a slice of the `HashMap<i32, i32>`'s content
|
LL | for _ in &self.h {
| ^


error: aborting due to previous error error: aborting due to 2 previous errors


For more information about this error, try `rustc --explain E0507`. For more information about this error, try `rustc --explain E0507`.
20 changes: 12 additions & 8 deletions src/test/ui/suggestions/option-content-move.stderr
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ error[E0507]: cannot move out of `selection.1` which is behind a shared referenc
--> $DIR/option-content-move.rs:11:20 --> $DIR/option-content-move.rs:11:20
| |
LL | if selection.1.unwrap().contains(selection.0) { LL | if selection.1.unwrap().contains(selection.0) {
| ^^^^^^^^^^^ | ^^^^^^^^^^^ move occurs because `selection.1` has type `Option<String>`, which does not implement the `Copy` trait
| | |
| move occurs because `selection.1` has type `Option<String>`, which does not implement the `Copy` trait help: consider borrowing the `Option`'s content
| help: consider borrowing the `Option`'s content: `selection.1.as_ref()` |
LL | if selection.1.as_ref().unwrap().contains(selection.0) {
| ^^^^^^^^^


error[E0507]: cannot move out of `selection.1` which is behind a shared reference error[E0507]: cannot move out of `selection.1` which is behind a shared reference
--> $DIR/option-content-move.rs:29:20 --> $DIR/option-content-move.rs:29:20
| |
LL | if selection.1.unwrap().contains(selection.0) { LL | if selection.1.unwrap().contains(selection.0) {
| ^^^^^^^^^^^ | ^^^^^^^^^^^ move occurs because `selection.1` has type `Result<String, String>`, which does not implement the `Copy` trait
| | |
| move occurs because `selection.1` has type `Result<String, String>`, which does not implement the `Copy` trait help: consider borrowing the `Result`'s content
| help: consider borrowing the `Result`'s content: `selection.1.as_ref()` |
LL | if selection.1.as_ref().unwrap().contains(selection.0) {
| ^^^^^^^^^


error: aborting due to 2 previous errors error: aborting due to 2 previous errors


Expand Down