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
19 changes: 12 additions & 7 deletions compiler/rustc_mir/src/borrow_check/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::mir::*;
use rustc_middle::ty;
use rustc_span::source_map::DesugaringKind;
Expand Down Expand Up @@ -409,13 +410,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
} else if matches!(span.desugaring_kind(), Some(DesugaringKind::ForLoop(_))) {
let suggest = match self.infcx.tcx.get_diagnostic_item(sym::IntoIterator) {
Some(def_id) => type_known_to_meet_bound_modulo_regions(
&self.infcx,
self.param_env,
self.infcx.tcx.mk_imm_ref(self.infcx.tcx.lifetimes.re_erased, ty),
def_id,
DUMMY_SP,
),
Some(def_id) => self.infcx.tcx.infer_ctxt().enter(|infcx| {
type_known_to_meet_bound_modulo_regions(
&infcx,
self.param_env,
infcx
.tcx
.mk_imm_ref(infcx.tcx.lifetimes.re_erased, infcx.tcx.erase_regions(ty)),
def_id,
DUMMY_SP,
)
}),
_ => false,
};
if suggest {
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ impl Foo {
}
}

const LOADERS: &Vec<&'static u8> = &Vec::new();

pub fn break_code() -> Option<&'static u8> {
for loader in &*LOADERS { //~ ERROR cannot move out of a shared reference
return Some(loader);
}
None
}

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ impl Foo {
}
}

const LOADERS: &Vec<&'static u8> = &Vec::new();

pub fn break_code() -> Option<&'static u8> {
for loader in *LOADERS { //~ ERROR cannot move out of a shared reference
return Some(loader);
}
None
}

fn main() {}
13 changes: 12 additions & 1 deletion src/test/ui/suggestions/for-i-in-vec.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ help: consider iterating over a slice of the `HashMap<i32, i32>`'s content
LL | for _ in &self.h {
| +

error: aborting due to 2 previous errors
error[E0507]: cannot move out of a shared reference
--> $DIR/for-i-in-vec.rs:21:19
|
LL | for loader in *LOADERS {
| ^^^^^^^^ move occurs because value has type `Vec<&u8>`, which does not implement the `Copy` trait
|
help: consider iterating over a slice of the `Vec<&u8>`'s content
|
LL | for loader in &*LOADERS {
| +

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0507`.