1 //! This query borrow-checks the MIR to (further) ensure it is not broken.
2
3 #![feature(bool_to_option)]
4 #![feature(box_patterns)]
5 #![feature(crate_visibility_modifier)]
6 #![cfg_attr(bootstrap, feature(format_args_capture))]
7 #![feature(in_band_lifetimes)]
8 #![feature(iter_zip)]
9 #![feature(let_else)]
10 #![feature(min_specialization)]
11 #![feature(stmt_expr_attributes)]
12 #![feature(trusted_step)]
13 #![feature(try_blocks)]
14 #![recursion_limit = "256"]
15
16 #[macro_use]
17 extern crate rustc_middle;
18 #[macro_use]
19 extern crate tracing;
20
21 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
22 use rustc_data_structures::graph::dominators::Dominators;
23 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorReported};
24 use rustc_hir as hir;
25 use rustc_hir::def_id::LocalDefId;
26 use rustc_hir::Node;
27 use rustc_index::bit_set::BitSet;
28 use rustc_index::vec::IndexVec;
29 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
30 use rustc_middle::mir::{
31 traversal, Body, ClearCrossCrate, Local, Location, Mutability, Operand, Place, PlaceElem,
32 PlaceRef, VarDebugInfoContents,
33 };
34 use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
35 use rustc_middle::mir::{Field, ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
36 use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
37 use rustc_middle::ty::query::Providers;
38 use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
39 use rustc_session::lint::builtin::{MUTABLE_BORROW_RESERVATION_CONFLICT, UNUSED_MUT};
40 use rustc_span::{Span, Symbol, DUMMY_SP};
41
42 use either::Either;
43 use smallvec::SmallVec;
44 use std::cell::RefCell;
45 use std::collections::BTreeMap;
46 use std::iter;
47 use std::mem;
48 use std::rc::Rc;
49
50 use rustc_mir_dataflow::impls::{
51 EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
52 };
53 use rustc_mir_dataflow::move_paths::{InitIndex, MoveOutIndex, MovePathIndex};
54 use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveData, MoveError};
55 use rustc_mir_dataflow::Analysis;
56 use rustc_mir_dataflow::MoveDataParamEnv;
57
58 use self::diagnostics::{AccessKind, RegionName};
59 use self::location::LocationTable;
60 use self::prefixes::PrefixSet;
61 use self::MutateMode::{JustWrite, WriteAndRead};
62 use facts::AllFacts;
63
64 use self::path_utils::*;
65
66 pub mod borrow_set;
67 mod borrowck_errors;
68 mod constraint_generation;
69 mod constraints;
70 mod dataflow;
71 mod def_use;
72 mod diagnostics;
73 mod facts;
74 mod invalidation;
75 mod location;
76 mod member_constraints;
77 mod nll;
78 mod path_utils;
79 mod place_ext;
80 mod places_conflict;
81 mod prefixes;
82 mod region_infer;
83 mod renumber;
84 mod type_check;
85 mod universal_regions;
86 mod used_muts;
87
88 // A public API provided for the Rust compiler consumers.
89 pub mod consumers;
90
91 use borrow_set::{BorrowData, BorrowSet};
92 use dataflow::{BorrowIndex, BorrowckFlowState as Flows, BorrowckResults, Borrows};
93 use nll::{PoloniusOutput, ToRegionVid};
94 use place_ext::PlaceExt;
95 use places_conflict::{places_conflict, PlaceConflictBias};
96 use region_infer::RegionInferenceContext;
97
98 // FIXME(eddyb) perhaps move this somewhere more centrally.
99 #[derive(Debug)]
100 struct Upvar<'tcx> {
101 place: CapturedPlace<'tcx>,
102
103 /// If true, the capture is behind a reference.
104 by_ref: bool,
105 }
106
107 const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
108
provide(providers: &mut Providers)109 pub fn provide(providers: &mut Providers) {
110 *providers = Providers {
111 mir_borrowck: |tcx, did| {
112 if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
113 tcx.mir_borrowck_const_arg(def)
114 } else {
115 mir_borrowck(tcx, ty::WithOptConstParam::unknown(did))
116 }
117 },
118 mir_borrowck_const_arg: |tcx, (did, param_did)| {
119 mir_borrowck(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
120 },
121 ..*providers
122 };
123 }
124
mir_borrowck<'tcx>( tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>, ) -> &'tcx BorrowCheckResult<'tcx>125 fn mir_borrowck<'tcx>(
126 tcx: TyCtxt<'tcx>,
127 def: ty::WithOptConstParam<LocalDefId>,
128 ) -> &'tcx BorrowCheckResult<'tcx> {
129 let (input_body, promoted) = tcx.mir_promoted(def);
130 debug!("run query mir_borrowck: {}", tcx.def_path_str(def.did.to_def_id()));
131
132 let opt_closure_req = tcx.infer_ctxt().with_opaque_type_inference(def.did).enter(|infcx| {
133 let input_body: &Body<'_> = &input_body.borrow();
134 let promoted: &IndexVec<_, _> = &promoted.borrow();
135 do_mir_borrowck(&infcx, input_body, promoted, false).0
136 });
137 debug!("mir_borrowck done");
138
139 tcx.arena.alloc(opt_closure_req)
140 }
141
142 /// Perform the actual borrow checking.
143 ///
144 /// If `return_body_with_facts` is true, then return the body with non-erased
145 /// region ids on which the borrow checking was performed together with Polonius
146 /// facts.
147 #[instrument(skip(infcx, input_body, input_promoted), level = "debug")]
do_mir_borrowck<'a, 'tcx>( infcx: &InferCtxt<'a, 'tcx>, input_body: &Body<'tcx>, input_promoted: &IndexVec<Promoted, Body<'tcx>>, return_body_with_facts: bool, ) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>)148 fn do_mir_borrowck<'a, 'tcx>(
149 infcx: &InferCtxt<'a, 'tcx>,
150 input_body: &Body<'tcx>,
151 input_promoted: &IndexVec<Promoted, Body<'tcx>>,
152 return_body_with_facts: bool,
153 ) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
154 let def = input_body.source.with_opt_param().as_local().unwrap();
155
156 debug!(?def);
157
158 let tcx = infcx.tcx;
159 let param_env = tcx.param_env(def.did);
160 let id = tcx.hir().local_def_id_to_hir_id(def.did);
161
162 let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
163 for var_debug_info in &input_body.var_debug_info {
164 if let VarDebugInfoContents::Place(place) = var_debug_info.value {
165 if let Some(local) = place.as_local() {
166 if let Some(prev_name) = local_names[local] {
167 if var_debug_info.name != prev_name {
168 span_bug!(
169 var_debug_info.source_info.span,
170 "local {:?} has many names (`{}` vs `{}`)",
171 local,
172 prev_name,
173 var_debug_info.name
174 );
175 }
176 }
177 local_names[local] = Some(var_debug_info.name);
178 }
179 }
180 }
181
182 // Gather the upvars of a closure, if any.
183 let tables = tcx.typeck_opt_const_arg(def);
184 if let Some(ErrorReported) = tables.tainted_by_errors {
185 infcx.set_tainted_by_errors();
186 }
187 let upvars: Vec<_> = tables
188 .closure_min_captures_flattened(def.did.to_def_id())
189 .map(|captured_place| {
190 let capture = captured_place.info.capture_kind;
191 let by_ref = match capture {
192 ty::UpvarCapture::ByValue(_) => false,
193 ty::UpvarCapture::ByRef(..) => true,
194 };
195 Upvar { place: captured_place.clone(), by_ref }
196 })
197 .collect();
198
199 // Replace all regions with fresh inference variables. This
200 // requires first making our own copy of the MIR. This copy will
201 // be modified (in place) to contain non-lexical lifetimes. It
202 // will have a lifetime tied to the inference context.
203 let mut body_owned = input_body.clone();
204 let mut promoted = input_promoted.clone();
205 let free_regions =
206 nll::replace_regions_in_mir(infcx, param_env, &mut body_owned, &mut promoted);
207 let body = &body_owned; // no further changes
208
209 let location_table_owned = LocationTable::new(body);
210 let location_table = &location_table_owned;
211
212 let mut errors_buffer = Vec::new();
213 let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
214 match MoveData::gather_moves(&body, tcx, param_env) {
215 Ok(move_data) => (move_data, Vec::new()),
216 Err((move_data, move_errors)) => (move_data, move_errors),
217 };
218 let promoted_errors = promoted
219 .iter_enumerated()
220 .map(|(idx, body)| (idx, MoveData::gather_moves(&body, tcx, param_env)));
221
222 let mdpe = MoveDataParamEnv { move_data, param_env };
223
224 let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
225 .into_engine(tcx, &body)
226 .pass_name("borrowck")
227 .iterate_to_fixpoint()
228 .into_results_cursor(&body);
229
230 let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(id).is_fn_or_closure();
231 let borrow_set =
232 Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data));
233
234 let use_polonius = return_body_with_facts || infcx.tcx.sess.opts.debugging_opts.polonius;
235
236 // Compute non-lexical lifetimes.
237 let nll::NllOutput {
238 regioncx,
239 opaque_type_values,
240 polonius_input,
241 polonius_output,
242 opt_closure_req,
243 nll_errors,
244 } = nll::compute_regions(
245 infcx,
246 free_regions,
247 body,
248 &promoted,
249 location_table,
250 param_env,
251 &mut flow_inits,
252 &mdpe.move_data,
253 &borrow_set,
254 &upvars,
255 use_polonius,
256 );
257
258 // Dump MIR results into a file, if that is enabled. This let us
259 // write unit-tests, as well as helping with debugging.
260 nll::dump_mir_results(infcx, &body, ®ioncx, &opt_closure_req);
261
262 // We also have a `#[rustc_regions]` annotation that causes us to dump
263 // information.
264 nll::dump_annotation(
265 infcx,
266 &body,
267 ®ioncx,
268 &opt_closure_req,
269 &opaque_type_values,
270 &mut errors_buffer,
271 );
272
273 // The various `flow_*` structures can be large. We drop `flow_inits` here
274 // so it doesn't overlap with the others below. This reduces peak memory
275 // usage significantly on some benchmarks.
276 drop(flow_inits);
277
278 let regioncx = Rc::new(regioncx);
279
280 let flow_borrows = Borrows::new(tcx, body, ®ioncx, &borrow_set)
281 .into_engine(tcx, body)
282 .pass_name("borrowck")
283 .iterate_to_fixpoint();
284 let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &mdpe)
285 .into_engine(tcx, body)
286 .pass_name("borrowck")
287 .iterate_to_fixpoint();
288 let flow_ever_inits = EverInitializedPlaces::new(tcx, body, &mdpe)
289 .into_engine(tcx, body)
290 .pass_name("borrowck")
291 .iterate_to_fixpoint();
292
293 let movable_generator = !matches!(
294 tcx.hir().get(id),
295 Node::Expr(&hir::Expr {
296 kind: hir::ExprKind::Closure(.., Some(hir::Movability::Static)),
297 ..
298 })
299 );
300
301 for (idx, move_data_results) in promoted_errors {
302 let promoted_body = &promoted[idx];
303
304 if let Err((move_data, move_errors)) = move_data_results {
305 let mut promoted_mbcx = MirBorrowckCtxt {
306 infcx,
307 param_env,
308 body: promoted_body,
309 move_data: &move_data,
310 location_table, // no need to create a real one for the promoted, it is not used
311 movable_generator,
312 fn_self_span_reported: Default::default(),
313 locals_are_invalidated_at_exit,
314 access_place_error_reported: Default::default(),
315 reservation_error_reported: Default::default(),
316 reservation_warnings: Default::default(),
317 move_error_reported: BTreeMap::new(),
318 uninitialized_error_reported: Default::default(),
319 errors_buffer,
320 regioncx: regioncx.clone(),
321 used_mut: Default::default(),
322 used_mut_upvars: SmallVec::new(),
323 borrow_set: Rc::clone(&borrow_set),
324 dominators: Dominators::dummy(), // not used
325 upvars: Vec::new(),
326 local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
327 region_names: RefCell::default(),
328 next_region_name: RefCell::new(1),
329 polonius_output: None,
330 };
331 promoted_mbcx.report_move_errors(move_errors);
332 errors_buffer = promoted_mbcx.errors_buffer;
333 };
334 }
335
336 let dominators = body.dominators();
337
338 let mut mbcx = MirBorrowckCtxt {
339 infcx,
340 param_env,
341 body,
342 move_data: &mdpe.move_data,
343 location_table,
344 movable_generator,
345 locals_are_invalidated_at_exit,
346 fn_self_span_reported: Default::default(),
347 access_place_error_reported: Default::default(),
348 reservation_error_reported: Default::default(),
349 reservation_warnings: Default::default(),
350 move_error_reported: BTreeMap::new(),
351 uninitialized_error_reported: Default::default(),
352 errors_buffer,
353 regioncx: Rc::clone(®ioncx),
354 used_mut: Default::default(),
355 used_mut_upvars: SmallVec::new(),
356 borrow_set: Rc::clone(&borrow_set),
357 dominators,
358 upvars,
359 local_names,
360 region_names: RefCell::default(),
361 next_region_name: RefCell::new(1),
362 polonius_output,
363 };
364
365 // Compute and report region errors, if any.
366 mbcx.report_region_errors(nll_errors);
367
368 let results = BorrowckResults {
369 ever_inits: flow_ever_inits,
370 uninits: flow_uninits,
371 borrows: flow_borrows,
372 };
373
374 mbcx.report_move_errors(move_errors);
375
376 rustc_mir_dataflow::visit_results(
377 body,
378 traversal::reverse_postorder(body).map(|(bb, _)| bb),
379 &results,
380 &mut mbcx,
381 );
382
383 // Convert any reservation warnings into lints.
384 let reservation_warnings = mem::take(&mut mbcx.reservation_warnings);
385 for (_, (place, span, location, bk, borrow)) in reservation_warnings {
386 let mut initial_diag = mbcx.report_conflicting_borrow(location, (place, span), bk, &borrow);
387
388 let scope = mbcx.body.source_info(location).scope;
389 let lint_root = match &mbcx.body.source_scopes[scope].local_data {
390 ClearCrossCrate::Set(data) => data.lint_root,
391 _ => id,
392 };
393
394 // Span and message don't matter; we overwrite them below anyway
395 mbcx.infcx.tcx.struct_span_lint_hir(
396 MUTABLE_BORROW_RESERVATION_CONFLICT,
397 lint_root,
398 DUMMY_SP,
399 |lint| {
400 let mut diag = lint.build("");
401
402 diag.message = initial_diag.styled_message().clone();
403 diag.span = initial_diag.span.clone();
404
405 diag.buffer(&mut mbcx.errors_buffer);
406 },
407 );
408 initial_diag.cancel();
409 }
410
411 // For each non-user used mutable variable, check if it's been assigned from
412 // a user-declared local. If so, then put that local into the used_mut set.
413 // Note that this set is expected to be small - only upvars from closures
414 // would have a chance of erroneously adding non-user-defined mutable vars
415 // to the set.
416 let temporary_used_locals: FxHashSet<Local> = mbcx
417 .used_mut
418 .iter()
419 .filter(|&local| !mbcx.body.local_decls[*local].is_user_variable())
420 .cloned()
421 .collect();
422 // For the remaining unused locals that are marked as mutable, we avoid linting any that
423 // were never initialized. These locals may have been removed as unreachable code; or will be
424 // linted as unused variables.
425 let unused_mut_locals =
426 mbcx.body.mut_vars_iter().filter(|local| !mbcx.used_mut.contains(local)).collect();
427 mbcx.gather_used_muts(temporary_used_locals, unused_mut_locals);
428
429 debug!("mbcx.used_mut: {:?}", mbcx.used_mut);
430 let used_mut = mbcx.used_mut;
431 for local in mbcx.body.mut_vars_and_args_iter().filter(|local| !used_mut.contains(local)) {
432 let local_decl = &mbcx.body.local_decls[local];
433 let lint_root = match &mbcx.body.source_scopes[local_decl.source_info.scope].local_data {
434 ClearCrossCrate::Set(data) => data.lint_root,
435 _ => continue,
436 };
437
438 // Skip over locals that begin with an underscore or have no name
439 match mbcx.local_names[local] {
440 Some(name) => {
441 if name.as_str().starts_with('_') {
442 continue;
443 }
444 }
445 None => continue,
446 }
447
448 let span = local_decl.source_info.span;
449 if span.desugaring_kind().is_some() {
450 // If the `mut` arises as part of a desugaring, we should ignore it.
451 continue;
452 }
453
454 tcx.struct_span_lint_hir(UNUSED_MUT, lint_root, span, |lint| {
455 let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
456 lint.build("variable does not need to be mutable")
457 .span_suggestion_short(
458 mut_span,
459 "remove this `mut`",
460 String::new(),
461 Applicability::MachineApplicable,
462 )
463 .emit();
464 })
465 }
466
467 // Buffer any move errors that we collected and de-duplicated.
468 for (_, (_, diag)) in mbcx.move_error_reported {
469 diag.buffer(&mut mbcx.errors_buffer);
470 }
471
472 if !mbcx.errors_buffer.is_empty() {
473 mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span);
474
475 for diag in mbcx.errors_buffer.drain(..) {
476 mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag);
477 }
478 }
479
480 let result = BorrowCheckResult {
481 concrete_opaque_types: opaque_type_values,
482 closure_requirements: opt_closure_req,
483 used_mut_upvars: mbcx.used_mut_upvars,
484 };
485
486 let body_with_facts = if return_body_with_facts {
487 let output_facts = mbcx.polonius_output.expect("Polonius output was not computed");
488 Some(Box::new(BodyWithBorrowckFacts {
489 body: body_owned,
490 input_facts: *polonius_input.expect("Polonius input facts were not generated"),
491 output_facts,
492 location_table: location_table_owned,
493 }))
494 } else {
495 None
496 };
497
498 debug!("do_mir_borrowck: result = {:#?}", result);
499
500 (result, body_with_facts)
501 }
502
503 /// A `Body` with information computed by the borrow checker. This struct is
504 /// intended to be consumed by compiler consumers.
505 ///
506 /// We need to include the MIR body here because the region identifiers must
507 /// match the ones in the Polonius facts.
508 pub struct BodyWithBorrowckFacts<'tcx> {
509 /// A mir body that contains region identifiers.
510 pub body: Body<'tcx>,
511 /// Polonius input facts.
512 pub input_facts: AllFacts,
513 /// Polonius output facts.
514 pub output_facts: Rc<self::nll::PoloniusOutput>,
515 /// The table that maps Polonius points to locations in the table.
516 pub location_table: LocationTable,
517 }
518
519 struct MirBorrowckCtxt<'cx, 'tcx> {
520 infcx: &'cx InferCtxt<'cx, 'tcx>,
521 param_env: ParamEnv<'tcx>,
522 body: &'cx Body<'tcx>,
523 move_data: &'cx MoveData<'tcx>,
524
525 /// Map from MIR `Location` to `LocationIndex`; created
526 /// when MIR borrowck begins.
527 location_table: &'cx LocationTable,
528
529 movable_generator: bool,
530 /// This keeps track of whether local variables are free-ed when the function
531 /// exits even without a `StorageDead`, which appears to be the case for
532 /// constants.
533 ///
534 /// I'm not sure this is the right approach - @eddyb could you try and
535 /// figure this out?
536 locals_are_invalidated_at_exit: bool,
537 /// This field keeps track of when borrow errors are reported in the access_place function
538 /// so that there is no duplicate reporting. This field cannot also be used for the conflicting
539 /// borrow errors that is handled by the `reservation_error_reported` field as the inclusion
540 /// of the `Span` type (while required to mute some errors) stops the muting of the reservation
541 /// errors.
542 access_place_error_reported: FxHashSet<(Place<'tcx>, Span)>,
543 /// This field keeps track of when borrow conflict errors are reported
544 /// for reservations, so that we don't report seemingly duplicate
545 /// errors for corresponding activations.
546 //
547 // FIXME: ideally this would be a set of `BorrowIndex`, not `Place`s,
548 // but it is currently inconvenient to track down the `BorrowIndex`
549 // at the time we detect and report a reservation error.
550 reservation_error_reported: FxHashSet<Place<'tcx>>,
551 /// This fields keeps track of the `Span`s that we have
552 /// used to report extra information for `FnSelfUse`, to avoid
553 /// unnecessarily verbose errors.
554 fn_self_span_reported: FxHashSet<Span>,
555 /// Migration warnings to be reported for #56254. We delay reporting these
556 /// so that we can suppress the warning if there's a corresponding error
557 /// for the activation of the borrow.
558 reservation_warnings:
559 FxHashMap<BorrowIndex, (Place<'tcx>, Span, Location, BorrowKind, BorrowData<'tcx>)>,
560 /// This field keeps track of move errors that are to be reported for given move indices.
561 ///
562 /// There are situations where many errors can be reported for a single move out (see #53807)
563 /// and we want only the best of those errors.
564 ///
565 /// The `report_use_of_moved_or_uninitialized` function checks this map and replaces the
566 /// diagnostic (if there is one) if the `Place` of the error being reported is a prefix of the
567 /// `Place` of the previous most diagnostic. This happens instead of buffering the error. Once
568 /// all move errors have been reported, any diagnostics in this map are added to the buffer
569 /// to be emitted.
570 ///
571 /// `BTreeMap` is used to preserve the order of insertions when iterating. This is necessary
572 /// when errors in the map are being re-added to the error buffer so that errors with the
573 /// same primary span come out in a consistent order.
574 move_error_reported: BTreeMap<Vec<MoveOutIndex>, (PlaceRef<'tcx>, DiagnosticBuilder<'cx>)>,
575 /// This field keeps track of errors reported in the checking of uninitialized variables,
576 /// so that we don't report seemingly duplicate errors.
577 uninitialized_error_reported: FxHashSet<PlaceRef<'tcx>>,
578 /// Errors to be reported buffer
579 errors_buffer: Vec<Diagnostic>,
580 /// This field keeps track of all the local variables that are declared mut and are mutated.
581 /// Used for the warning issued by an unused mutable local variable.
582 used_mut: FxHashSet<Local>,
583 /// If the function we're checking is a closure, then we'll need to report back the list of
584 /// mutable upvars that have been used. This field keeps track of them.
585 used_mut_upvars: SmallVec<[Field; 8]>,
586 /// Region inference context. This contains the results from region inference and lets us e.g.
587 /// find out which CFG points are contained in each borrow region.
588 regioncx: Rc<RegionInferenceContext<'tcx>>,
589
590 /// The set of borrows extracted from the MIR
591 borrow_set: Rc<BorrowSet<'tcx>>,
592
593 /// Dominators for MIR
594 dominators: Dominators<BasicBlock>,
595
596 /// Information about upvars not necessarily preserved in types or MIR
597 upvars: Vec<Upvar<'tcx>>,
598
599 /// Names of local (user) variables (extracted from `var_debug_info`).
600 local_names: IndexVec<Local, Option<Symbol>>,
601
602 /// Record the region names generated for each region in the given
603 /// MIR def so that we can reuse them later in help/error messages.
604 region_names: RefCell<FxHashMap<RegionVid, RegionName>>,
605
606 /// The counter for generating new region names.
607 next_region_name: RefCell<usize>,
608
609 /// Results of Polonius analysis.
610 polonius_output: Option<Rc<PoloniusOutput>>,
611 }
612
613 // Check that:
614 // 1. assignments are always made to mutable locations (FIXME: does that still really go here?)
615 // 2. loans made in overlapping scopes do not conflict
616 // 3. assignments do not affect things loaned out as immutable
617 // 4. moves do not affect things loaned out in any way
618 impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx> {
619 type FlowState = Flows<'cx, 'tcx>;
620
visit_statement_before_primary_effect( &mut self, flow_state: &Flows<'cx, 'tcx>, stmt: &'cx Statement<'tcx>, location: Location, )621 fn visit_statement_before_primary_effect(
622 &mut self,
623 flow_state: &Flows<'cx, 'tcx>,
624 stmt: &'cx Statement<'tcx>,
625 location: Location,
626 ) {
627 debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, flow_state);
628 let span = stmt.source_info.span;
629
630 self.check_activations(location, span, flow_state);
631
632 match &stmt.kind {
633 StatementKind::Assign(box (lhs, ref rhs)) => {
634 self.consume_rvalue(location, (rhs, span), flow_state);
635
636 self.mutate_place(location, (*lhs, span), Shallow(None), JustWrite, flow_state);
637 }
638 StatementKind::FakeRead(box (_, ref place)) => {
639 // Read for match doesn't access any memory and is used to
640 // assert that a place is safe and live. So we don't have to
641 // do any checks here.
642 //
643 // FIXME: Remove check that the place is initialized. This is
644 // needed for now because matches don't have never patterns yet.
645 // So this is the only place we prevent
646 // let x: !;
647 // match x {};
648 // from compiling.
649 self.check_if_path_or_subpath_is_moved(
650 location,
651 InitializationRequiringAction::Use,
652 (place.as_ref(), span),
653 flow_state,
654 );
655 }
656 StatementKind::SetDiscriminant { place, variant_index: _ } => {
657 self.mutate_place(location, (**place, span), Shallow(None), JustWrite, flow_state);
658 }
659 StatementKind::LlvmInlineAsm(ref asm) => {
660 for (o, output) in iter::zip(&asm.asm.outputs, &*asm.outputs) {
661 if o.is_indirect {
662 // FIXME(eddyb) indirect inline asm outputs should
663 // be encoded through MIR place derefs instead.
664 self.access_place(
665 location,
666 (*output, o.span),
667 (Deep, Read(ReadKind::Copy)),
668 LocalMutationIsAllowed::No,
669 flow_state,
670 );
671 self.check_if_path_or_subpath_is_moved(
672 location,
673 InitializationRequiringAction::Use,
674 (output.as_ref(), o.span),
675 flow_state,
676 );
677 } else {
678 self.mutate_place(
679 location,
680 (*output, o.span),
681 if o.is_rw { Deep } else { Shallow(None) },
682 if o.is_rw { WriteAndRead } else { JustWrite },
683 flow_state,
684 );
685 }
686 }
687 for (_, input) in asm.inputs.iter() {
688 self.consume_operand(location, (input, span), flow_state);
689 }
690 }
691
692 StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping {
693 ..
694 }) => {
695 span_bug!(
696 span,
697 "Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",
698 )
699 }
700 StatementKind::Nop
701 | StatementKind::Coverage(..)
702 | StatementKind::AscribeUserType(..)
703 | StatementKind::Retag { .. }
704 | StatementKind::StorageLive(..) => {
705 // `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
706 // to borrow check.
707 }
708 StatementKind::StorageDead(local) => {
709 self.access_place(
710 location,
711 (Place::from(*local), span),
712 (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
713 LocalMutationIsAllowed::Yes,
714 flow_state,
715 );
716 }
717 }
718 }
719
visit_terminator_before_primary_effect( &mut self, flow_state: &Flows<'cx, 'tcx>, term: &'cx Terminator<'tcx>, loc: Location, )720 fn visit_terminator_before_primary_effect(
721 &mut self,
722 flow_state: &Flows<'cx, 'tcx>,
723 term: &'cx Terminator<'tcx>,
724 loc: Location,
725 ) {
726 debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, flow_state);
727 let span = term.source_info.span;
728
729 self.check_activations(loc, span, flow_state);
730
731 match term.kind {
732 TerminatorKind::SwitchInt { ref discr, switch_ty: _, targets: _ } => {
733 self.consume_operand(loc, (discr, span), flow_state);
734 }
735 TerminatorKind::Drop { place, target: _, unwind: _ } => {
736 debug!(
737 "visit_terminator_drop \
738 loc: {:?} term: {:?} place: {:?} span: {:?}",
739 loc, term, place, span
740 );
741
742 self.access_place(
743 loc,
744 (place, span),
745 (AccessDepth::Drop, Write(WriteKind::StorageDeadOrDrop)),
746 LocalMutationIsAllowed::Yes,
747 flow_state,
748 );
749 }
750 TerminatorKind::DropAndReplace {
751 place: drop_place,
752 value: ref new_value,
753 target: _,
754 unwind: _,
755 } => {
756 self.mutate_place(loc, (drop_place, span), Deep, JustWrite, flow_state);
757 self.consume_operand(loc, (new_value, span), flow_state);
758 }
759 TerminatorKind::Call {
760 ref func,
761 ref args,
762 ref destination,
763 cleanup: _,
764 from_hir_call: _,
765 fn_span: _,
766 } => {
767 self.consume_operand(loc, (func, span), flow_state);
768 for arg in args {
769 self.consume_operand(loc, (arg, span), flow_state);
770 }
771 if let Some((dest, _ /*bb*/)) = *destination {
772 self.mutate_place(loc, (dest, span), Deep, JustWrite, flow_state);
773 }
774 }
775 TerminatorKind::Assert { ref cond, expected: _, ref msg, target: _, cleanup: _ } => {
776 self.consume_operand(loc, (cond, span), flow_state);
777 use rustc_middle::mir::AssertKind;
778 if let AssertKind::BoundsCheck { ref len, ref index } = *msg {
779 self.consume_operand(loc, (len, span), flow_state);
780 self.consume_operand(loc, (index, span), flow_state);
781 }
782 }
783
784 TerminatorKind::Yield { ref value, resume: _, resume_arg, drop: _ } => {
785 self.consume_operand(loc, (value, span), flow_state);
786 self.mutate_place(loc, (resume_arg, span), Deep, JustWrite, flow_state);
787 }
788
789 TerminatorKind::InlineAsm {
790 template: _,
791 ref operands,
792 options: _,
793 line_spans: _,
794 destination: _,
795 } => {
796 for op in operands {
797 match *op {
798 InlineAsmOperand::In { reg: _, ref value } => {
799 self.consume_operand(loc, (value, span), flow_state);
800 }
801 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
802 if let Some(place) = place {
803 self.mutate_place(
804 loc,
805 (place, span),
806 Shallow(None),
807 JustWrite,
808 flow_state,
809 );
810 }
811 }
812 InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
813 self.consume_operand(loc, (in_value, span), flow_state);
814 if let Some(out_place) = out_place {
815 self.mutate_place(
816 loc,
817 (out_place, span),
818 Shallow(None),
819 JustWrite,
820 flow_state,
821 );
822 }
823 }
824 InlineAsmOperand::Const { value: _ }
825 | InlineAsmOperand::SymFn { value: _ }
826 | InlineAsmOperand::SymStatic { def_id: _ } => {}
827 }
828 }
829 }
830
831 TerminatorKind::Goto { target: _ }
832 | TerminatorKind::Abort
833 | TerminatorKind::Unreachable
834 | TerminatorKind::Resume
835 | TerminatorKind::Return
836 | TerminatorKind::GeneratorDrop
837 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
838 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
839 // no data used, thus irrelevant to borrowck
840 }
841 }
842 }
843
visit_terminator_after_primary_effect( &mut self, flow_state: &Flows<'cx, 'tcx>, term: &'cx Terminator<'tcx>, loc: Location, )844 fn visit_terminator_after_primary_effect(
845 &mut self,
846 flow_state: &Flows<'cx, 'tcx>,
847 term: &'cx Terminator<'tcx>,
848 loc: Location,
849 ) {
850 let span = term.source_info.span;
851
852 match term.kind {
853 TerminatorKind::Yield { value: _, resume: _, resume_arg: _, drop: _ } => {
854 if self.movable_generator {
855 // Look for any active borrows to locals
856 let borrow_set = self.borrow_set.clone();
857 for i in flow_state.borrows.iter() {
858 let borrow = &borrow_set[i];
859 self.check_for_local_borrow(borrow, span);
860 }
861 }
862 }
863
864 TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::GeneratorDrop => {
865 // Returning from the function implicitly kills storage for all locals and statics.
866 // Often, the storage will already have been killed by an explicit
867 // StorageDead, but we don't always emit those (notably on unwind paths),
868 // so this "extra check" serves as a kind of backup.
869 let borrow_set = self.borrow_set.clone();
870 for i in flow_state.borrows.iter() {
871 let borrow = &borrow_set[i];
872 self.check_for_invalidation_at_exit(loc, borrow, span);
873 }
874 }
875
876 TerminatorKind::Abort
877 | TerminatorKind::Assert { .. }
878 | TerminatorKind::Call { .. }
879 | TerminatorKind::Drop { .. }
880 | TerminatorKind::DropAndReplace { .. }
881 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
882 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ }
883 | TerminatorKind::Goto { .. }
884 | TerminatorKind::SwitchInt { .. }
885 | TerminatorKind::Unreachable
886 | TerminatorKind::InlineAsm { .. } => {}
887 }
888 }
889 }
890
891 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
892 enum MutateMode {
893 JustWrite,
894 WriteAndRead,
895 }
896
897 use self::AccessDepth::{Deep, Shallow};
898 use self::ReadOrWrite::{Activation, Read, Reservation, Write};
899
900 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
901 enum ArtificialField {
902 ArrayLength,
903 ShallowBorrow,
904 }
905
906 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
907 enum AccessDepth {
908 /// From the RFC: "A *shallow* access means that the immediate
909 /// fields reached at P are accessed, but references or pointers
910 /// found within are not dereferenced. Right now, the only access
911 /// that is shallow is an assignment like `x = ...;`, which would
912 /// be a *shallow write* of `x`."
913 Shallow(Option<ArtificialField>),
914
915 /// From the RFC: "A *deep* access means that all data reachable
916 /// through the given place may be invalidated or accesses by
917 /// this action."
918 Deep,
919
920 /// Access is Deep only when there is a Drop implementation that
921 /// can reach the data behind the reference.
922 Drop,
923 }
924
925 /// Kind of access to a value: read or write
926 /// (For informational purposes only)
927 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
928 enum ReadOrWrite {
929 /// From the RFC: "A *read* means that the existing data may be
930 /// read, but will not be changed."
931 Read(ReadKind),
932
933 /// From the RFC: "A *write* means that the data may be mutated to
934 /// new values or otherwise invalidated (for example, it could be
935 /// de-initialized, as in a move operation).
936 Write(WriteKind),
937
938 /// For two-phase borrows, we distinguish a reservation (which is treated
939 /// like a Read) from an activation (which is treated like a write), and
940 /// each of those is furthermore distinguished from Reads/Writes above.
941 Reservation(WriteKind),
942 Activation(WriteKind, BorrowIndex),
943 }
944
945 /// Kind of read access to a value
946 /// (For informational purposes only)
947 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
948 enum ReadKind {
949 Borrow(BorrowKind),
950 Copy,
951 }
952
953 /// Kind of write access to a value
954 /// (For informational purposes only)
955 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
956 enum WriteKind {
957 StorageDeadOrDrop,
958 MutableBorrow(BorrowKind),
959 Mutate,
960 Move,
961 }
962
963 /// When checking permissions for a place access, this flag is used to indicate that an immutable
964 /// local place can be mutated.
965 //
966 // FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:
967 // - Merge `check_access_permissions()` and `check_if_reassignment_to_immutable_state()`.
968 // - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
969 // `is_declared_mutable()`.
970 // - Take flow state into consideration in `is_assignable()` for local variables.
971 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
972 enum LocalMutationIsAllowed {
973 Yes,
974 /// We want use of immutable upvars to cause a "write to immutable upvar"
975 /// error, not an "reassignment" error.
976 ExceptUpvars,
977 No,
978 }
979
980 #[derive(Copy, Clone, Debug)]
981 enum InitializationRequiringAction {
982 Update,
983 Borrow,
984 MatchOn,
985 Use,
986 Assignment,
987 PartialAssignment,
988 }
989
990 struct RootPlace<'tcx> {
991 place_local: Local,
992 place_projection: &'tcx [PlaceElem<'tcx>],
993 is_local_mutation_allowed: LocalMutationIsAllowed,
994 }
995
996 impl InitializationRequiringAction {
as_noun(self) -> &'static str997 fn as_noun(self) -> &'static str {
998 match self {
999 InitializationRequiringAction::Update => "update",
1000 InitializationRequiringAction::Borrow => "borrow",
1001 InitializationRequiringAction::MatchOn => "use", // no good noun
1002 InitializationRequiringAction::Use => "use",
1003 InitializationRequiringAction::Assignment => "assign",
1004 InitializationRequiringAction::PartialAssignment => "assign to part",
1005 }
1006 }
1007
as_verb_in_past_tense(self) -> &'static str1008 fn as_verb_in_past_tense(self) -> &'static str {
1009 match self {
1010 InitializationRequiringAction::Update => "updated",
1011 InitializationRequiringAction::Borrow => "borrowed",
1012 InitializationRequiringAction::MatchOn => "matched on",
1013 InitializationRequiringAction::Use => "used",
1014 InitializationRequiringAction::Assignment => "assigned",
1015 InitializationRequiringAction::PartialAssignment => "partially assigned",
1016 }
1017 }
1018 }
1019
1020 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
body(&self) -> &'cx Body<'tcx>1021 fn body(&self) -> &'cx Body<'tcx> {
1022 self.body
1023 }
1024
1025 /// Checks an access to the given place to see if it is allowed. Examines the set of borrows
1026 /// that are in scope, as well as which paths have been initialized, to ensure that (a) the
1027 /// place is initialized and (b) it is not borrowed in some way that would prevent this
1028 /// access.
1029 ///
1030 /// Returns `true` if an error is reported.
access_place( &mut self, location: Location, place_span: (Place<'tcx>, Span), kind: (AccessDepth, ReadOrWrite), is_local_mutation_allowed: LocalMutationIsAllowed, flow_state: &Flows<'cx, 'tcx>, )1031 fn access_place(
1032 &mut self,
1033 location: Location,
1034 place_span: (Place<'tcx>, Span),
1035 kind: (AccessDepth, ReadOrWrite),
1036 is_local_mutation_allowed: LocalMutationIsAllowed,
1037 flow_state: &Flows<'cx, 'tcx>,
1038 ) {
1039 let (sd, rw) = kind;
1040
1041 if let Activation(_, borrow_index) = rw {
1042 if self.reservation_error_reported.contains(&place_span.0) {
1043 debug!(
1044 "skipping access_place for activation of invalid reservation \
1045 place: {:?} borrow_index: {:?}",
1046 place_span.0, borrow_index
1047 );
1048 return;
1049 }
1050 }
1051
1052 // Check is_empty() first because it's the common case, and doing that
1053 // way we avoid the clone() call.
1054 if !self.access_place_error_reported.is_empty()
1055 && self.access_place_error_reported.contains(&(place_span.0, place_span.1))
1056 {
1057 debug!(
1058 "access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
1059 place_span, kind
1060 );
1061 return;
1062 }
1063
1064 let mutability_error = self.check_access_permissions(
1065 place_span,
1066 rw,
1067 is_local_mutation_allowed,
1068 flow_state,
1069 location,
1070 );
1071 let conflict_error =
1072 self.check_access_for_conflict(location, place_span, sd, rw, flow_state);
1073
1074 if let (Activation(_, borrow_idx), true) = (kind.1, conflict_error) {
1075 // Suppress this warning when there's an error being emitted for the
1076 // same borrow: fixing the error is likely to fix the warning.
1077 self.reservation_warnings.remove(&borrow_idx);
1078 }
1079
1080 if conflict_error || mutability_error {
1081 debug!("access_place: logging error place_span=`{:?}` kind=`{:?}`", place_span, kind);
1082
1083 self.access_place_error_reported.insert((place_span.0, place_span.1));
1084 }
1085 }
1086
check_access_for_conflict( &mut self, location: Location, place_span: (Place<'tcx>, Span), sd: AccessDepth, rw: ReadOrWrite, flow_state: &Flows<'cx, 'tcx>, ) -> bool1087 fn check_access_for_conflict(
1088 &mut self,
1089 location: Location,
1090 place_span: (Place<'tcx>, Span),
1091 sd: AccessDepth,
1092 rw: ReadOrWrite,
1093 flow_state: &Flows<'cx, 'tcx>,
1094 ) -> bool {
1095 debug!(
1096 "check_access_for_conflict(location={:?}, place_span={:?}, sd={:?}, rw={:?})",
1097 location, place_span, sd, rw,
1098 );
1099
1100 let mut error_reported = false;
1101 let tcx = self.infcx.tcx;
1102 let body = self.body;
1103 let borrow_set = self.borrow_set.clone();
1104
1105 // Use polonius output if it has been enabled.
1106 let polonius_output = self.polonius_output.clone();
1107 let borrows_in_scope = if let Some(polonius) = &polonius_output {
1108 let location = self.location_table.start_index(location);
1109 Either::Left(polonius.errors_at(location).iter().copied())
1110 } else {
1111 Either::Right(flow_state.borrows.iter())
1112 };
1113
1114 each_borrow_involving_path(
1115 self,
1116 tcx,
1117 body,
1118 location,
1119 (sd, place_span.0),
1120 &borrow_set,
1121 borrows_in_scope,
1122 |this, borrow_index, borrow| match (rw, borrow.kind) {
1123 // Obviously an activation is compatible with its own
1124 // reservation (or even prior activating uses of same
1125 // borrow); so don't check if they interfere.
1126 //
1127 // NOTE: *reservations* do conflict with themselves;
1128 // thus aren't injecting unsoundenss w/ this check.)
1129 (Activation(_, activating), _) if activating == borrow_index => {
1130 debug!(
1131 "check_access_for_conflict place_span: {:?} sd: {:?} rw: {:?} \
1132 skipping {:?} b/c activation of same borrow_index",
1133 place_span,
1134 sd,
1135 rw,
1136 (borrow_index, borrow),
1137 );
1138 Control::Continue
1139 }
1140
1141 (Read(_), BorrowKind::Shared | BorrowKind::Shallow)
1142 | (
1143 Read(ReadKind::Borrow(BorrowKind::Shallow)),
1144 BorrowKind::Unique | BorrowKind::Mut { .. },
1145 ) => Control::Continue,
1146
1147 (Write(WriteKind::Move), BorrowKind::Shallow) => {
1148 // Handled by initialization checks.
1149 Control::Continue
1150 }
1151
1152 (Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
1153 // Reading from mere reservations of mutable-borrows is OK.
1154 if !is_active(&this.dominators, borrow, location) {
1155 assert!(allow_two_phase_borrow(borrow.kind));
1156 return Control::Continue;
1157 }
1158
1159 error_reported = true;
1160 match kind {
1161 ReadKind::Copy => {
1162 this.report_use_while_mutably_borrowed(location, place_span, borrow)
1163 .buffer(&mut this.errors_buffer);
1164 }
1165 ReadKind::Borrow(bk) => {
1166 this.report_conflicting_borrow(location, place_span, bk, borrow)
1167 .buffer(&mut this.errors_buffer);
1168 }
1169 }
1170 Control::Break
1171 }
1172
1173 (
1174 Reservation(WriteKind::MutableBorrow(bk)),
1175 BorrowKind::Shallow | BorrowKind::Shared,
1176 ) if { tcx.migrate_borrowck() && this.borrow_set.contains(&location) } => {
1177 let bi = this.borrow_set.get_index_of(&location).unwrap();
1178 debug!(
1179 "recording invalid reservation of place: {:?} with \
1180 borrow index {:?} as warning",
1181 place_span.0, bi,
1182 );
1183 // rust-lang/rust#56254 - This was previously permitted on
1184 // the 2018 edition so we emit it as a warning. We buffer
1185 // these sepately so that we only emit a warning if borrow
1186 // checking was otherwise successful.
1187 this.reservation_warnings
1188 .insert(bi, (place_span.0, place_span.1, location, bk, borrow.clone()));
1189
1190 // Don't suppress actual errors.
1191 Control::Continue
1192 }
1193
1194 (Reservation(kind) | Activation(kind, _) | Write(kind), _) => {
1195 match rw {
1196 Reservation(..) => {
1197 debug!(
1198 "recording invalid reservation of \
1199 place: {:?}",
1200 place_span.0
1201 );
1202 this.reservation_error_reported.insert(place_span.0);
1203 }
1204 Activation(_, activating) => {
1205 debug!(
1206 "observing check_place for activation of \
1207 borrow_index: {:?}",
1208 activating
1209 );
1210 }
1211 Read(..) | Write(..) => {}
1212 }
1213
1214 error_reported = true;
1215 match kind {
1216 WriteKind::MutableBorrow(bk) => {
1217 this.report_conflicting_borrow(location, place_span, bk, borrow)
1218 .buffer(&mut this.errors_buffer);
1219 }
1220 WriteKind::StorageDeadOrDrop => this
1221 .report_borrowed_value_does_not_live_long_enough(
1222 location,
1223 borrow,
1224 place_span,
1225 Some(kind),
1226 ),
1227 WriteKind::Mutate => {
1228 this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
1229 }
1230 WriteKind::Move => {
1231 this.report_move_out_while_borrowed(location, place_span, borrow)
1232 }
1233 }
1234 Control::Break
1235 }
1236 },
1237 );
1238
1239 error_reported
1240 }
1241
mutate_place( &mut self, location: Location, place_span: (Place<'tcx>, Span), kind: AccessDepth, mode: MutateMode, flow_state: &Flows<'cx, 'tcx>, )1242 fn mutate_place(
1243 &mut self,
1244 location: Location,
1245 place_span: (Place<'tcx>, Span),
1246 kind: AccessDepth,
1247 mode: MutateMode,
1248 flow_state: &Flows<'cx, 'tcx>,
1249 ) {
1250 // Write of P[i] or *P, or WriteAndRead of any P, requires P init'd.
1251 match mode {
1252 MutateMode::WriteAndRead => {
1253 self.check_if_path_or_subpath_is_moved(
1254 location,
1255 InitializationRequiringAction::Update,
1256 (place_span.0.as_ref(), place_span.1),
1257 flow_state,
1258 );
1259 }
1260 MutateMode::JustWrite => {
1261 self.check_if_assigned_path_is_moved(location, place_span, flow_state);
1262 }
1263 }
1264
1265 // Special case: you can assign an immutable local variable
1266 // (e.g., `x = ...`) so long as it has never been initialized
1267 // before (at this point in the flow).
1268 if let Some(local) = place_span.0.as_local() {
1269 if let Mutability::Not = self.body.local_decls[local].mutability {
1270 // check for reassignments to immutable local variables
1271 self.check_if_reassignment_to_immutable_state(
1272 location, local, place_span, flow_state,
1273 );
1274 return;
1275 }
1276 }
1277
1278 // Otherwise, use the normal access permission rules.
1279 self.access_place(
1280 location,
1281 place_span,
1282 (kind, Write(WriteKind::Mutate)),
1283 LocalMutationIsAllowed::No,
1284 flow_state,
1285 );
1286 }
1287
consume_rvalue( &mut self, location: Location, (rvalue, span): (&'cx Rvalue<'tcx>, Span), flow_state: &Flows<'cx, 'tcx>, )1288 fn consume_rvalue(
1289 &mut self,
1290 location: Location,
1291 (rvalue, span): (&'cx Rvalue<'tcx>, Span),
1292 flow_state: &Flows<'cx, 'tcx>,
1293 ) {
1294 match *rvalue {
1295 Rvalue::Ref(_ /*rgn*/, bk, place) => {
1296 let access_kind = match bk {
1297 BorrowKind::Shallow => {
1298 (Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
1299 }
1300 BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
1301 BorrowKind::Unique | BorrowKind::Mut { .. } => {
1302 let wk = WriteKind::MutableBorrow(bk);
1303 if allow_two_phase_borrow(bk) {
1304 (Deep, Reservation(wk))
1305 } else {
1306 (Deep, Write(wk))
1307 }
1308 }
1309 };
1310
1311 self.access_place(
1312 location,
1313 (place, span),
1314 access_kind,
1315 LocalMutationIsAllowed::No,
1316 flow_state,
1317 );
1318
1319 let action = if bk == BorrowKind::Shallow {
1320 InitializationRequiringAction::MatchOn
1321 } else {
1322 InitializationRequiringAction::Borrow
1323 };
1324
1325 self.check_if_path_or_subpath_is_moved(
1326 location,
1327 action,
1328 (place.as_ref(), span),
1329 flow_state,
1330 );
1331 }
1332
1333 Rvalue::AddressOf(mutability, place) => {
1334 let access_kind = match mutability {
1335 Mutability::Mut => (
1336 Deep,
1337 Write(WriteKind::MutableBorrow(BorrowKind::Mut {
1338 allow_two_phase_borrow: false,
1339 })),
1340 ),
1341 Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1342 };
1343
1344 self.access_place(
1345 location,
1346 (place, span),
1347 access_kind,
1348 LocalMutationIsAllowed::No,
1349 flow_state,
1350 );
1351
1352 self.check_if_path_or_subpath_is_moved(
1353 location,
1354 InitializationRequiringAction::Borrow,
1355 (place.as_ref(), span),
1356 flow_state,
1357 );
1358 }
1359
1360 Rvalue::ThreadLocalRef(_) => {}
1361
1362 Rvalue::Use(ref operand)
1363 | Rvalue::Repeat(ref operand, _)
1364 | Rvalue::UnaryOp(_ /*un_op*/, ref operand)
1365 | Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
1366 | Rvalue::ShallowInitBox(ref operand, _ /*ty*/) => {
1367 self.consume_operand(location, (operand, span), flow_state)
1368 }
1369
1370 Rvalue::Len(place) | Rvalue::Discriminant(place) => {
1371 let af = match *rvalue {
1372 Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1373 Rvalue::Discriminant(..) => None,
1374 _ => unreachable!(),
1375 };
1376 self.access_place(
1377 location,
1378 (place, span),
1379 (Shallow(af), Read(ReadKind::Copy)),
1380 LocalMutationIsAllowed::No,
1381 flow_state,
1382 );
1383 self.check_if_path_or_subpath_is_moved(
1384 location,
1385 InitializationRequiringAction::Use,
1386 (place.as_ref(), span),
1387 flow_state,
1388 );
1389 }
1390
1391 Rvalue::BinaryOp(_bin_op, box (ref operand1, ref operand2))
1392 | Rvalue::CheckedBinaryOp(_bin_op, box (ref operand1, ref operand2)) => {
1393 self.consume_operand(location, (operand1, span), flow_state);
1394 self.consume_operand(location, (operand2, span), flow_state);
1395 }
1396
1397 Rvalue::NullaryOp(_op, _ty) => {
1398 // nullary ops take no dynamic input; no borrowck effect.
1399 //
1400 // FIXME: is above actually true? Do we want to track
1401 // the fact that uninitialized data can be created via
1402 // `NullOp::Box`?
1403 }
1404
1405 Rvalue::Aggregate(ref aggregate_kind, ref operands) => {
1406 // We need to report back the list of mutable upvars that were
1407 // moved into the closure and subsequently used by the closure,
1408 // in order to populate our used_mut set.
1409 match **aggregate_kind {
1410 AggregateKind::Closure(def_id, _) | AggregateKind::Generator(def_id, _, _) => {
1411 let BorrowCheckResult { used_mut_upvars, .. } =
1412 self.infcx.tcx.mir_borrowck(def_id.expect_local());
1413 debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);
1414 for field in used_mut_upvars {
1415 self.propagate_closure_used_mut_upvar(&operands[field.index()]);
1416 }
1417 }
1418 AggregateKind::Adt(..)
1419 | AggregateKind::Array(..)
1420 | AggregateKind::Tuple { .. } => (),
1421 }
1422
1423 for operand in operands {
1424 self.consume_operand(location, (operand, span), flow_state);
1425 }
1426 }
1427 }
1428 }
1429
propagate_closure_used_mut_upvar(&mut self, operand: &Operand<'tcx>)1430 fn propagate_closure_used_mut_upvar(&mut self, operand: &Operand<'tcx>) {
1431 let propagate_closure_used_mut_place = |this: &mut Self, place: Place<'tcx>| {
1432 // We have three possibilities here:
1433 // a. We are modifying something through a mut-ref
1434 // b. We are modifying something that is local to our parent
1435 // c. Current body is a nested closure, and we are modifying path starting from
1436 // a Place captured by our parent closure.
1437
1438 // Handle (c), the path being modified is exactly the path captured by our parent
1439 if let Some(field) = this.is_upvar_field_projection(place.as_ref()) {
1440 this.used_mut_upvars.push(field);
1441 return;
1442 }
1443
1444 for (place_ref, proj) in place.iter_projections().rev() {
1445 // Handle (a)
1446 if proj == ProjectionElem::Deref {
1447 match place_ref.ty(this.body(), this.infcx.tcx).ty.kind() {
1448 // We aren't modifying a variable directly
1449 ty::Ref(_, _, hir::Mutability::Mut) => return,
1450
1451 _ => {}
1452 }
1453 }
1454
1455 // Handle (c)
1456 if let Some(field) = this.is_upvar_field_projection(place_ref) {
1457 this.used_mut_upvars.push(field);
1458 return;
1459 }
1460 }
1461
1462 // Handle(b)
1463 this.used_mut.insert(place.local);
1464 };
1465
1466 // This relies on the current way that by-value
1467 // captures of a closure are copied/moved directly
1468 // when generating MIR.
1469 match *operand {
1470 Operand::Move(place) | Operand::Copy(place) => {
1471 match place.as_local() {
1472 Some(local) if !self.body.local_decls[local].is_user_variable() => {
1473 if self.body.local_decls[local].ty.is_mutable_ptr() {
1474 // The variable will be marked as mutable by the borrow.
1475 return;
1476 }
1477 // This is an edge case where we have a `move` closure
1478 // inside a non-move closure, and the inner closure
1479 // contains a mutation:
1480 //
1481 // let mut i = 0;
1482 // || { move || { i += 1; }; };
1483 //
1484 // In this case our usual strategy of assuming that the
1485 // variable will be captured by mutable reference is
1486 // wrong, since `i` can be copied into the inner
1487 // closure from a shared reference.
1488 //
1489 // As such we have to search for the local that this
1490 // capture comes from and mark it as being used as mut.
1491
1492 let temp_mpi = self.move_data.rev_lookup.find_local(local);
1493 let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {
1494 &self.move_data.inits[init_index]
1495 } else {
1496 bug!("temporary should be initialized exactly once")
1497 };
1498
1499 let loc = match init.location {
1500 InitLocation::Statement(stmt) => stmt,
1501 _ => bug!("temporary initialized in arguments"),
1502 };
1503
1504 let body = self.body;
1505 let bbd = &body[loc.block];
1506 let stmt = &bbd.statements[loc.statement_index];
1507 debug!("temporary assigned in: stmt={:?}", stmt);
1508
1509 if let StatementKind::Assign(box (_, Rvalue::Ref(_, _, source))) = stmt.kind
1510 {
1511 propagate_closure_used_mut_place(self, source);
1512 } else {
1513 bug!(
1514 "closures should only capture user variables \
1515 or references to user variables"
1516 );
1517 }
1518 }
1519 _ => propagate_closure_used_mut_place(self, place),
1520 }
1521 }
1522 Operand::Constant(..) => {}
1523 }
1524 }
1525
consume_operand( &mut self, location: Location, (operand, span): (&'cx Operand<'tcx>, Span), flow_state: &Flows<'cx, 'tcx>, )1526 fn consume_operand(
1527 &mut self,
1528 location: Location,
1529 (operand, span): (&'cx Operand<'tcx>, Span),
1530 flow_state: &Flows<'cx, 'tcx>,
1531 ) {
1532 match *operand {
1533 Operand::Copy(place) => {
1534 // copy of place: check if this is "copy of frozen path"
1535 // (FIXME: see check_loans.rs)
1536 self.access_place(
1537 location,
1538 (place, span),
1539 (Deep, Read(ReadKind::Copy)),
1540 LocalMutationIsAllowed::No,
1541 flow_state,
1542 );
1543
1544 // Finally, check if path was already moved.
1545 self.check_if_path_or_subpath_is_moved(
1546 location,
1547 InitializationRequiringAction::Use,
1548 (place.as_ref(), span),
1549 flow_state,
1550 );
1551 }
1552 Operand::Move(place) => {
1553 // move of place: check if this is move of already borrowed path
1554 self.access_place(
1555 location,
1556 (place, span),
1557 (Deep, Write(WriteKind::Move)),
1558 LocalMutationIsAllowed::Yes,
1559 flow_state,
1560 );
1561
1562 // Finally, check if path was already moved.
1563 self.check_if_path_or_subpath_is_moved(
1564 location,
1565 InitializationRequiringAction::Use,
1566 (place.as_ref(), span),
1567 flow_state,
1568 );
1569 }
1570 Operand::Constant(_) => {}
1571 }
1572 }
1573
1574 /// Checks whether a borrow of this place is invalidated when the function
1575 /// exits
check_for_invalidation_at_exit( &mut self, location: Location, borrow: &BorrowData<'tcx>, span: Span, )1576 fn check_for_invalidation_at_exit(
1577 &mut self,
1578 location: Location,
1579 borrow: &BorrowData<'tcx>,
1580 span: Span,
1581 ) {
1582 debug!("check_for_invalidation_at_exit({:?})", borrow);
1583 let place = borrow.borrowed_place;
1584 let mut root_place = PlaceRef { local: place.local, projection: &[] };
1585
1586 // FIXME(nll-rfc#40): do more precise destructor tracking here. For now
1587 // we just know that all locals are dropped at function exit (otherwise
1588 // we'll have a memory leak) and assume that all statics have a destructor.
1589 //
1590 // FIXME: allow thread-locals to borrow other thread locals?
1591
1592 let (might_be_alive, will_be_dropped) =
1593 if self.body.local_decls[root_place.local].is_ref_to_thread_local() {
1594 // Thread-locals might be dropped after the function exits
1595 // We have to dereference the outer reference because
1596 // borrows don't conflict behind shared references.
1597 root_place.projection = DEREF_PROJECTION;
1598 (true, true)
1599 } else {
1600 (false, self.locals_are_invalidated_at_exit)
1601 };
1602
1603 if !will_be_dropped {
1604 debug!("place_is_invalidated_at_exit({:?}) - won't be dropped", place);
1605 return;
1606 }
1607
1608 let sd = if might_be_alive { Deep } else { Shallow(None) };
1609
1610 if places_conflict::borrow_conflicts_with_place(
1611 self.infcx.tcx,
1612 &self.body,
1613 place,
1614 borrow.kind,
1615 root_place,
1616 sd,
1617 places_conflict::PlaceConflictBias::Overlap,
1618 ) {
1619 debug!("check_for_invalidation_at_exit({:?}): INVALID", place);
1620 // FIXME: should be talking about the region lifetime instead
1621 // of just a span here.
1622 let span = self.infcx.tcx.sess.source_map().end_point(span);
1623 self.report_borrowed_value_does_not_live_long_enough(
1624 location,
1625 borrow,
1626 (place, span),
1627 None,
1628 )
1629 }
1630 }
1631
1632 /// Reports an error if this is a borrow of local data.
1633 /// This is called for all Yield expressions on movable generators
check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span)1634 fn check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span) {
1635 debug!("check_for_local_borrow({:?})", borrow);
1636
1637 if borrow_of_local_data(borrow.borrowed_place) {
1638 let err = self.cannot_borrow_across_generator_yield(
1639 self.retrieve_borrow_spans(borrow).var_or_use(),
1640 yield_span,
1641 );
1642
1643 err.buffer(&mut self.errors_buffer);
1644 }
1645 }
1646
check_activations(&mut self, location: Location, span: Span, flow_state: &Flows<'cx, 'tcx>)1647 fn check_activations(&mut self, location: Location, span: Span, flow_state: &Flows<'cx, 'tcx>) {
1648 // Two-phase borrow support: For each activation that is newly
1649 // generated at this statement, check if it interferes with
1650 // another borrow.
1651 let borrow_set = self.borrow_set.clone();
1652 for &borrow_index in borrow_set.activations_at_location(location) {
1653 let borrow = &borrow_set[borrow_index];
1654
1655 // only mutable borrows should be 2-phase
1656 assert!(match borrow.kind {
1657 BorrowKind::Shared | BorrowKind::Shallow => false,
1658 BorrowKind::Unique | BorrowKind::Mut { .. } => true,
1659 });
1660
1661 self.access_place(
1662 location,
1663 (borrow.borrowed_place, span),
1664 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
1665 LocalMutationIsAllowed::No,
1666 flow_state,
1667 );
1668 // We do not need to call `check_if_path_or_subpath_is_moved`
1669 // again, as we already called it when we made the
1670 // initial reservation.
1671 }
1672 }
1673
check_if_reassignment_to_immutable_state( &mut self, location: Location, local: Local, place_span: (Place<'tcx>, Span), flow_state: &Flows<'cx, 'tcx>, )1674 fn check_if_reassignment_to_immutable_state(
1675 &mut self,
1676 location: Location,
1677 local: Local,
1678 place_span: (Place<'tcx>, Span),
1679 flow_state: &Flows<'cx, 'tcx>,
1680 ) {
1681 debug!("check_if_reassignment_to_immutable_state({:?})", local);
1682
1683 // Check if any of the initializiations of `local` have happened yet:
1684 if let Some(init_index) = self.is_local_ever_initialized(local, flow_state) {
1685 // And, if so, report an error.
1686 let init = &self.move_data.inits[init_index];
1687 let span = init.span(&self.body);
1688 self.report_illegal_reassignment(location, place_span, span, place_span.0);
1689 }
1690 }
1691
check_if_full_path_is_moved( &mut self, location: Location, desired_action: InitializationRequiringAction, place_span: (PlaceRef<'tcx>, Span), flow_state: &Flows<'cx, 'tcx>, )1692 fn check_if_full_path_is_moved(
1693 &mut self,
1694 location: Location,
1695 desired_action: InitializationRequiringAction,
1696 place_span: (PlaceRef<'tcx>, Span),
1697 flow_state: &Flows<'cx, 'tcx>,
1698 ) {
1699 let maybe_uninits = &flow_state.uninits;
1700
1701 // Bad scenarios:
1702 //
1703 // 1. Move of `a.b.c`, use of `a.b.c`
1704 // 2. Move of `a.b.c`, use of `a.b.c.d` (without first reinitializing `a.b.c.d`)
1705 // 3. Uninitialized `(a.b.c: &_)`, use of `*a.b.c`; note that with
1706 // partial initialization support, one might have `a.x`
1707 // initialized but not `a.b`.
1708 //
1709 // OK scenarios:
1710 //
1711 // 4. Move of `a.b.c`, use of `a.b.d`
1712 // 5. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
1713 // 6. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
1714 // must have been initialized for the use to be sound.
1715 // 7. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
1716
1717 // The dataflow tracks shallow prefixes distinctly (that is,
1718 // field-accesses on P distinctly from P itself), in order to
1719 // track substructure initialization separately from the whole
1720 // structure.
1721 //
1722 // E.g., when looking at (*a.b.c).d, if the closest prefix for
1723 // which we have a MovePath is `a.b`, then that means that the
1724 // initialization state of `a.b` is all we need to inspect to
1725 // know if `a.b.c` is valid (and from that we infer that the
1726 // dereference and `.d` access is also valid, since we assume
1727 // `a.b.c` is assigned a reference to an initialized and
1728 // well-formed record structure.)
1729
1730 // Therefore, if we seek out the *closest* prefix for which we
1731 // have a MovePath, that should capture the initialization
1732 // state for the place scenario.
1733 //
1734 // This code covers scenarios 1, 2, and 3.
1735
1736 debug!("check_if_full_path_is_moved place: {:?}", place_span.0);
1737 let (prefix, mpi) = self.move_path_closest_to(place_span.0);
1738 if maybe_uninits.contains(mpi) {
1739 self.report_use_of_moved_or_uninitialized(
1740 location,
1741 desired_action,
1742 (prefix, place_span.0, place_span.1),
1743 mpi,
1744 );
1745 } // Only query longest prefix with a MovePath, not further
1746 // ancestors; dataflow recurs on children when parents
1747 // move (to support partial (re)inits).
1748 //
1749 // (I.e., querying parents breaks scenario 7; but may want
1750 // to do such a query based on partial-init feature-gate.)
1751 }
1752
1753 /// Subslices correspond to multiple move paths, so we iterate through the
1754 /// elements of the base array. For each element we check
1755 ///
1756 /// * Does this element overlap with our slice.
1757 /// * Is any part of it uninitialized.
check_if_subslice_element_is_moved( &mut self, location: Location, desired_action: InitializationRequiringAction, place_span: (PlaceRef<'tcx>, Span), maybe_uninits: &BitSet<MovePathIndex>, from: u64, to: u64, )1758 fn check_if_subslice_element_is_moved(
1759 &mut self,
1760 location: Location,
1761 desired_action: InitializationRequiringAction,
1762 place_span: (PlaceRef<'tcx>, Span),
1763 maybe_uninits: &BitSet<MovePathIndex>,
1764 from: u64,
1765 to: u64,
1766 ) {
1767 if let Some(mpi) = self.move_path_for_place(place_span.0) {
1768 let move_paths = &self.move_data.move_paths;
1769
1770 let root_path = &move_paths[mpi];
1771 for (child_mpi, child_move_path) in root_path.children(move_paths) {
1772 let last_proj = child_move_path.place.projection.last().unwrap();
1773 if let ProjectionElem::ConstantIndex { offset, from_end, .. } = last_proj {
1774 debug_assert!(!from_end, "Array constant indexing shouldn't be `from_end`.");
1775
1776 if (from..to).contains(offset) {
1777 let uninit_child =
1778 self.move_data.find_in_move_path_or_its_descendants(child_mpi, |mpi| {
1779 maybe_uninits.contains(mpi)
1780 });
1781
1782 if let Some(uninit_child) = uninit_child {
1783 self.report_use_of_moved_or_uninitialized(
1784 location,
1785 desired_action,
1786 (place_span.0, place_span.0, place_span.1),
1787 uninit_child,
1788 );
1789 return; // don't bother finding other problems.
1790 }
1791 }
1792 }
1793 }
1794 }
1795 }
1796
check_if_path_or_subpath_is_moved( &mut self, location: Location, desired_action: InitializationRequiringAction, place_span: (PlaceRef<'tcx>, Span), flow_state: &Flows<'cx, 'tcx>, )1797 fn check_if_path_or_subpath_is_moved(
1798 &mut self,
1799 location: Location,
1800 desired_action: InitializationRequiringAction,
1801 place_span: (PlaceRef<'tcx>, Span),
1802 flow_state: &Flows<'cx, 'tcx>,
1803 ) {
1804 let maybe_uninits = &flow_state.uninits;
1805
1806 // Bad scenarios:
1807 //
1808 // 1. Move of `a.b.c`, use of `a` or `a.b`
1809 // partial initialization support, one might have `a.x`
1810 // initialized but not `a.b`.
1811 // 2. All bad scenarios from `check_if_full_path_is_moved`
1812 //
1813 // OK scenarios:
1814 //
1815 // 3. Move of `a.b.c`, use of `a.b.d`
1816 // 4. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
1817 // 5. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
1818 // must have been initialized for the use to be sound.
1819 // 6. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
1820
1821 self.check_if_full_path_is_moved(location, desired_action, place_span, flow_state);
1822
1823 if let Some((place_base, ProjectionElem::Subslice { from, to, from_end: false })) =
1824 place_span.0.last_projection()
1825 {
1826 let place_ty = place_base.ty(self.body(), self.infcx.tcx);
1827 if let ty::Array(..) = place_ty.ty.kind() {
1828 self.check_if_subslice_element_is_moved(
1829 location,
1830 desired_action,
1831 (place_base, place_span.1),
1832 maybe_uninits,
1833 from,
1834 to,
1835 );
1836 return;
1837 }
1838 }
1839
1840 // A move of any shallow suffix of `place` also interferes
1841 // with an attempt to use `place`. This is scenario 3 above.
1842 //
1843 // (Distinct from handling of scenarios 1+2+4 above because
1844 // `place` does not interfere with suffixes of its prefixes,
1845 // e.g., `a.b.c` does not interfere with `a.b.d`)
1846 //
1847 // This code covers scenario 1.
1848
1849 debug!("check_if_path_or_subpath_is_moved place: {:?}", place_span.0);
1850 if let Some(mpi) = self.move_path_for_place(place_span.0) {
1851 let uninit_mpi = self
1852 .move_data
1853 .find_in_move_path_or_its_descendants(mpi, |mpi| maybe_uninits.contains(mpi));
1854
1855 if let Some(uninit_mpi) = uninit_mpi {
1856 self.report_use_of_moved_or_uninitialized(
1857 location,
1858 desired_action,
1859 (place_span.0, place_span.0, place_span.1),
1860 uninit_mpi,
1861 );
1862 return; // don't bother finding other problems.
1863 }
1864 }
1865 }
1866
1867 /// Currently MoveData does not store entries for all places in
1868 /// the input MIR. For example it will currently filter out
1869 /// places that are Copy; thus we do not track places of shared
1870 /// reference type. This routine will walk up a place along its
1871 /// prefixes, searching for a foundational place that *is*
1872 /// tracked in the MoveData.
1873 ///
1874 /// An Err result includes a tag indicated why the search failed.
1875 /// Currently this can only occur if the place is built off of a
1876 /// static variable, as we do not track those in the MoveData.
move_path_closest_to(&mut self, place: PlaceRef<'tcx>) -> (PlaceRef<'tcx>, MovePathIndex)1877 fn move_path_closest_to(&mut self, place: PlaceRef<'tcx>) -> (PlaceRef<'tcx>, MovePathIndex) {
1878 match self.move_data.rev_lookup.find(place) {
1879 LookupResult::Parent(Some(mpi)) | LookupResult::Exact(mpi) => {
1880 (self.move_data.move_paths[mpi].place.as_ref(), mpi)
1881 }
1882 LookupResult::Parent(None) => panic!("should have move path for every Local"),
1883 }
1884 }
1885
move_path_for_place(&mut self, place: PlaceRef<'tcx>) -> Option<MovePathIndex>1886 fn move_path_for_place(&mut self, place: PlaceRef<'tcx>) -> Option<MovePathIndex> {
1887 // If returns None, then there is no move path corresponding
1888 // to a direct owner of `place` (which means there is nothing
1889 // that borrowck tracks for its analysis).
1890
1891 match self.move_data.rev_lookup.find(place) {
1892 LookupResult::Parent(_) => None,
1893 LookupResult::Exact(mpi) => Some(mpi),
1894 }
1895 }
1896
check_if_assigned_path_is_moved( &mut self, location: Location, (place, span): (Place<'tcx>, Span), flow_state: &Flows<'cx, 'tcx>, )1897 fn check_if_assigned_path_is_moved(
1898 &mut self,
1899 location: Location,
1900 (place, span): (Place<'tcx>, Span),
1901 flow_state: &Flows<'cx, 'tcx>,
1902 ) {
1903 debug!("check_if_assigned_path_is_moved place: {:?}", place);
1904
1905 // None case => assigning to `x` does not require `x` be initialized.
1906 for (place_base, elem) in place.iter_projections().rev() {
1907 match elem {
1908 ProjectionElem::Index(_/*operand*/) |
1909 ProjectionElem::ConstantIndex { .. } |
1910 // assigning to P[i] requires P to be valid.
1911 ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
1912 // assigning to (P->variant) is okay if assigning to `P` is okay
1913 //
1914 // FIXME: is this true even if P is an adt with a dtor?
1915 { }
1916
1917 // assigning to (*P) requires P to be initialized
1918 ProjectionElem::Deref => {
1919 self.check_if_full_path_is_moved(
1920 location, InitializationRequiringAction::Use,
1921 (place_base, span), flow_state);
1922 // (base initialized; no need to
1923 // recur further)
1924 break;
1925 }
1926
1927 ProjectionElem::Subslice { .. } => {
1928 panic!("we don't allow assignments to subslices, location: {:?}",
1929 location);
1930 }
1931
1932 ProjectionElem::Field(..) => {
1933 // if type of `P` has a dtor, then
1934 // assigning to `P.f` requires `P` itself
1935 // be already initialized
1936 let tcx = self.infcx.tcx;
1937 let base_ty = place_base.ty(self.body(), tcx).ty;
1938 match base_ty.kind() {
1939 ty::Adt(def, _) if def.has_dtor(tcx) => {
1940 self.check_if_path_or_subpath_is_moved(
1941 location, InitializationRequiringAction::Assignment,
1942 (place_base, span), flow_state);
1943
1944 // (base initialized; no need to
1945 // recur further)
1946 break;
1947 }
1948
1949 // Once `let s; s.x = V; read(s.x);`,
1950 // is allowed, remove this match arm.
1951 ty::Adt(..) | ty::Tuple(..) => {
1952 check_parent_of_field(self, location, place_base, span, flow_state);
1953
1954 // rust-lang/rust#21232, #54499, #54986: during period where we reject
1955 // partial initialization, do not complain about unnecessary `mut` on
1956 // an attempt to do a partial initialization.
1957 self.used_mut.insert(place.local);
1958 }
1959
1960 _ => {}
1961 }
1962 }
1963 }
1964 }
1965
1966 fn check_parent_of_field<'cx, 'tcx>(
1967 this: &mut MirBorrowckCtxt<'cx, 'tcx>,
1968 location: Location,
1969 base: PlaceRef<'tcx>,
1970 span: Span,
1971 flow_state: &Flows<'cx, 'tcx>,
1972 ) {
1973 // rust-lang/rust#21232: Until Rust allows reads from the
1974 // initialized parts of partially initialized structs, we
1975 // will, starting with the 2018 edition, reject attempts
1976 // to write to structs that are not fully initialized.
1977 //
1978 // In other words, *until* we allow this:
1979 //
1980 // 1. `let mut s; s.x = Val; read(s.x);`
1981 //
1982 // we will for now disallow this:
1983 //
1984 // 2. `let mut s; s.x = Val;`
1985 //
1986 // and also this:
1987 //
1988 // 3. `let mut s = ...; drop(s); s.x=Val;`
1989 //
1990 // This does not use check_if_path_or_subpath_is_moved,
1991 // because we want to *allow* reinitializations of fields:
1992 // e.g., want to allow
1993 //
1994 // `let mut s = ...; drop(s.x); s.x=Val;`
1995 //
1996 // This does not use check_if_full_path_is_moved on
1997 // `base`, because that would report an error about the
1998 // `base` as a whole, but in this scenario we *really*
1999 // want to report an error about the actual thing that was
2000 // moved, which may be some prefix of `base`.
2001
2002 // Shallow so that we'll stop at any dereference; we'll
2003 // report errors about issues with such bases elsewhere.
2004 let maybe_uninits = &flow_state.uninits;
2005
2006 // Find the shortest uninitialized prefix you can reach
2007 // without going over a Deref.
2008 let mut shortest_uninit_seen = None;
2009 for prefix in this.prefixes(base, PrefixSet::Shallow) {
2010 let mpi = match this.move_path_for_place(prefix) {
2011 Some(mpi) => mpi,
2012 None => continue,
2013 };
2014
2015 if maybe_uninits.contains(mpi) {
2016 debug!(
2017 "check_parent_of_field updating shortest_uninit_seen from {:?} to {:?}",
2018 shortest_uninit_seen,
2019 Some((prefix, mpi))
2020 );
2021 shortest_uninit_seen = Some((prefix, mpi));
2022 } else {
2023 debug!("check_parent_of_field {:?} is definitely initialized", (prefix, mpi));
2024 }
2025 }
2026
2027 if let Some((prefix, mpi)) = shortest_uninit_seen {
2028 // Check for a reassignment into an uninitialized field of a union (for example,
2029 // after a move out). In this case, do not report an error here. There is an
2030 // exception, if this is the first assignment into the union (that is, there is
2031 // no move out from an earlier location) then this is an attempt at initialization
2032 // of the union - we should error in that case.
2033 let tcx = this.infcx.tcx;
2034 if base.ty(this.body(), tcx).ty.is_union() {
2035 if this.move_data.path_map[mpi].iter().any(|moi| {
2036 this.move_data.moves[*moi].source.is_predecessor_of(location, this.body)
2037 }) {
2038 return;
2039 }
2040 }
2041
2042 this.report_use_of_moved_or_uninitialized(
2043 location,
2044 InitializationRequiringAction::PartialAssignment,
2045 (prefix, base, span),
2046 mpi,
2047 );
2048 }
2049 }
2050 }
2051
2052 /// Checks the permissions for the given place and read or write kind
2053 ///
2054 /// Returns `true` if an error is reported.
check_access_permissions( &mut self, (place, span): (Place<'tcx>, Span), kind: ReadOrWrite, is_local_mutation_allowed: LocalMutationIsAllowed, flow_state: &Flows<'cx, 'tcx>, location: Location, ) -> bool2055 fn check_access_permissions(
2056 &mut self,
2057 (place, span): (Place<'tcx>, Span),
2058 kind: ReadOrWrite,
2059 is_local_mutation_allowed: LocalMutationIsAllowed,
2060 flow_state: &Flows<'cx, 'tcx>,
2061 location: Location,
2062 ) -> bool {
2063 debug!(
2064 "check_access_permissions({:?}, {:?}, is_local_mutation_allowed: {:?})",
2065 place, kind, is_local_mutation_allowed
2066 );
2067
2068 let error_access;
2069 let the_place_err;
2070
2071 match kind {
2072 Reservation(WriteKind::MutableBorrow(
2073 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
2074 ))
2075 | Write(WriteKind::MutableBorrow(
2076 borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
2077 )) => {
2078 let is_local_mutation_allowed = match borrow_kind {
2079 BorrowKind::Unique => LocalMutationIsAllowed::Yes,
2080 BorrowKind::Mut { .. } => is_local_mutation_allowed,
2081 BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
2082 };
2083 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
2084 Ok(root_place) => {
2085 self.add_used_mut(root_place, flow_state);
2086 return false;
2087 }
2088 Err(place_err) => {
2089 error_access = AccessKind::MutableBorrow;
2090 the_place_err = place_err;
2091 }
2092 }
2093 }
2094 Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
2095 match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
2096 Ok(root_place) => {
2097 self.add_used_mut(root_place, flow_state);
2098 return false;
2099 }
2100 Err(place_err) => {
2101 error_access = AccessKind::Mutate;
2102 the_place_err = place_err;
2103 }
2104 }
2105 }
2106
2107 Reservation(
2108 WriteKind::Move
2109 | WriteKind::StorageDeadOrDrop
2110 | WriteKind::MutableBorrow(BorrowKind::Shared)
2111 | WriteKind::MutableBorrow(BorrowKind::Shallow),
2112 )
2113 | Write(
2114 WriteKind::Move
2115 | WriteKind::StorageDeadOrDrop
2116 | WriteKind::MutableBorrow(BorrowKind::Shared)
2117 | WriteKind::MutableBorrow(BorrowKind::Shallow),
2118 ) => {
2119 if let (Err(_), true) = (
2120 self.is_mutable(place.as_ref(), is_local_mutation_allowed),
2121 self.errors_buffer.is_empty(),
2122 ) {
2123 // rust-lang/rust#46908: In pure NLL mode this code path should be
2124 // unreachable, but we use `delay_span_bug` because we can hit this when
2125 // dereferencing a non-Copy raw pointer *and* have `-Ztreat-err-as-bug`
2126 // enabled. We don't want to ICE for that case, as other errors will have
2127 // been emitted (#52262).
2128 self.infcx.tcx.sess.delay_span_bug(
2129 span,
2130 &format!(
2131 "Accessing `{:?}` with the kind `{:?}` shouldn't be possible",
2132 place, kind,
2133 ),
2134 );
2135 }
2136 return false;
2137 }
2138 Activation(..) => {
2139 // permission checks are done at Reservation point.
2140 return false;
2141 }
2142 Read(
2143 ReadKind::Borrow(
2144 BorrowKind::Unique
2145 | BorrowKind::Mut { .. }
2146 | BorrowKind::Shared
2147 | BorrowKind::Shallow,
2148 )
2149 | ReadKind::Copy,
2150 ) => {
2151 // Access authorized
2152 return false;
2153 }
2154 }
2155
2156 // rust-lang/rust#21232, #54986: during period where we reject
2157 // partial initialization, do not complain about mutability
2158 // errors except for actual mutation (as opposed to an attempt
2159 // to do a partial initialization).
2160 let previously_initialized =
2161 self.is_local_ever_initialized(place.local, flow_state).is_some();
2162
2163 // at this point, we have set up the error reporting state.
2164 if previously_initialized {
2165 self.report_mutability_error(place, span, the_place_err, error_access, location);
2166 true
2167 } else {
2168 false
2169 }
2170 }
2171
is_local_ever_initialized( &self, local: Local, flow_state: &Flows<'cx, 'tcx>, ) -> Option<InitIndex>2172 fn is_local_ever_initialized(
2173 &self,
2174 local: Local,
2175 flow_state: &Flows<'cx, 'tcx>,
2176 ) -> Option<InitIndex> {
2177 let mpi = self.move_data.rev_lookup.find_local(local);
2178 let ii = &self.move_data.init_path_map[mpi];
2179 for &index in ii {
2180 if flow_state.ever_inits.contains(index) {
2181 return Some(index);
2182 }
2183 }
2184 None
2185 }
2186
2187 /// Adds the place into the used mutable variables set
add_used_mut(&mut self, root_place: RootPlace<'tcx>, flow_state: &Flows<'cx, 'tcx>)2188 fn add_used_mut(&mut self, root_place: RootPlace<'tcx>, flow_state: &Flows<'cx, 'tcx>) {
2189 match root_place {
2190 RootPlace { place_local: local, place_projection: [], is_local_mutation_allowed } => {
2191 // If the local may have been initialized, and it is now currently being
2192 // mutated, then it is justified to be annotated with the `mut`
2193 // keyword, since the mutation may be a possible reassignment.
2194 if is_local_mutation_allowed != LocalMutationIsAllowed::Yes
2195 && self.is_local_ever_initialized(local, flow_state).is_some()
2196 {
2197 self.used_mut.insert(local);
2198 }
2199 }
2200 RootPlace {
2201 place_local: _,
2202 place_projection: _,
2203 is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2204 } => {}
2205 RootPlace {
2206 place_local,
2207 place_projection: place_projection @ [.., _],
2208 is_local_mutation_allowed: _,
2209 } => {
2210 if let Some(field) = self.is_upvar_field_projection(PlaceRef {
2211 local: place_local,
2212 projection: place_projection,
2213 }) {
2214 self.used_mut_upvars.push(field);
2215 }
2216 }
2217 }
2218 }
2219
2220 /// Whether this value can be written or borrowed mutably.
2221 /// Returns the root place if the place passed in is a projection.
is_mutable( &self, place: PlaceRef<'tcx>, is_local_mutation_allowed: LocalMutationIsAllowed, ) -> Result<RootPlace<'tcx>, PlaceRef<'tcx>>2222 fn is_mutable(
2223 &self,
2224 place: PlaceRef<'tcx>,
2225 is_local_mutation_allowed: LocalMutationIsAllowed,
2226 ) -> Result<RootPlace<'tcx>, PlaceRef<'tcx>> {
2227 debug!("is_mutable: place={:?}, is_local...={:?}", place, is_local_mutation_allowed);
2228 match place.last_projection() {
2229 None => {
2230 let local = &self.body.local_decls[place.local];
2231 match local.mutability {
2232 Mutability::Not => match is_local_mutation_allowed {
2233 LocalMutationIsAllowed::Yes => Ok(RootPlace {
2234 place_local: place.local,
2235 place_projection: place.projection,
2236 is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
2237 }),
2238 LocalMutationIsAllowed::ExceptUpvars => Ok(RootPlace {
2239 place_local: place.local,
2240 place_projection: place.projection,
2241 is_local_mutation_allowed: LocalMutationIsAllowed::ExceptUpvars,
2242 }),
2243 LocalMutationIsAllowed::No => Err(place),
2244 },
2245 Mutability::Mut => Ok(RootPlace {
2246 place_local: place.local,
2247 place_projection: place.projection,
2248 is_local_mutation_allowed,
2249 }),
2250 }
2251 }
2252 Some((place_base, elem)) => {
2253 match elem {
2254 ProjectionElem::Deref => {
2255 let base_ty = place_base.ty(self.body(), self.infcx.tcx).ty;
2256
2257 // Check the kind of deref to decide
2258 match base_ty.kind() {
2259 ty::Ref(_, _, mutbl) => {
2260 match mutbl {
2261 // Shared borrowed data is never mutable
2262 hir::Mutability::Not => Err(place),
2263 // Mutably borrowed data is mutable, but only if we have a
2264 // unique path to the `&mut`
2265 hir::Mutability::Mut => {
2266 let mode = match self.is_upvar_field_projection(place) {
2267 Some(field) if self.upvars[field.index()].by_ref => {
2268 is_local_mutation_allowed
2269 }
2270 _ => LocalMutationIsAllowed::Yes,
2271 };
2272
2273 self.is_mutable(place_base, mode)
2274 }
2275 }
2276 }
2277 ty::RawPtr(tnm) => {
2278 match tnm.mutbl {
2279 // `*const` raw pointers are not mutable
2280 hir::Mutability::Not => Err(place),
2281 // `*mut` raw pointers are always mutable, regardless of
2282 // context. The users have to check by themselves.
2283 hir::Mutability::Mut => Ok(RootPlace {
2284 place_local: place.local,
2285 place_projection: place.projection,
2286 is_local_mutation_allowed,
2287 }),
2288 }
2289 }
2290 // `Box<T>` owns its content, so mutable if its location is mutable
2291 _ if base_ty.is_box() => {
2292 self.is_mutable(place_base, is_local_mutation_allowed)
2293 }
2294 // Deref should only be for reference, pointers or boxes
2295 _ => bug!("Deref of unexpected type: {:?}", base_ty),
2296 }
2297 }
2298 // All other projections are owned by their base path, so mutable if
2299 // base path is mutable
2300 ProjectionElem::Field(..)
2301 | ProjectionElem::Index(..)
2302 | ProjectionElem::ConstantIndex { .. }
2303 | ProjectionElem::Subslice { .. }
2304 | ProjectionElem::Downcast(..) => {
2305 let upvar_field_projection = self.is_upvar_field_projection(place);
2306 if let Some(field) = upvar_field_projection {
2307 let upvar = &self.upvars[field.index()];
2308 debug!(
2309 "is_mutable: upvar.mutability={:?} local_mutation_is_allowed={:?} \
2310 place={:?}, place_base={:?}",
2311 upvar, is_local_mutation_allowed, place, place_base
2312 );
2313 match (upvar.place.mutability, is_local_mutation_allowed) {
2314 (
2315 Mutability::Not,
2316 LocalMutationIsAllowed::No
2317 | LocalMutationIsAllowed::ExceptUpvars,
2318 ) => Err(place),
2319 (Mutability::Not, LocalMutationIsAllowed::Yes)
2320 | (Mutability::Mut, _) => {
2321 // Subtle: this is an upvar
2322 // reference, so it looks like
2323 // `self.foo` -- we want to double
2324 // check that the location `*self`
2325 // is mutable (i.e., this is not a
2326 // `Fn` closure). But if that
2327 // check succeeds, we want to
2328 // *blame* the mutability on
2329 // `place` (that is,
2330 // `self.foo`). This is used to
2331 // propagate the info about
2332 // whether mutability declarations
2333 // are used outwards, so that we register
2334 // the outer variable as mutable. Otherwise a
2335 // test like this fails to record the `mut`
2336 // as needed:
2337 //
2338 // ```
2339 // fn foo<F: FnOnce()>(_f: F) { }
2340 // fn main() {
2341 // let var = Vec::new();
2342 // foo(move || {
2343 // var.push(1);
2344 // });
2345 // }
2346 // ```
2347 let _ =
2348 self.is_mutable(place_base, is_local_mutation_allowed)?;
2349 Ok(RootPlace {
2350 place_local: place.local,
2351 place_projection: place.projection,
2352 is_local_mutation_allowed,
2353 })
2354 }
2355 }
2356 } else {
2357 self.is_mutable(place_base, is_local_mutation_allowed)
2358 }
2359 }
2360 }
2361 }
2362 }
2363 }
2364
2365 /// If `place` is a field projection, and the field is being projected from a closure type,
2366 /// then returns the index of the field being projected. Note that this closure will always
2367 /// be `self` in the current MIR, because that is the only time we directly access the fields
2368 /// of a closure type.
is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field>2369 fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<Field> {
2370 path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
2371 }
2372 }
2373
2374 /// The degree of overlap between 2 places for borrow-checking.
2375 enum Overlap {
2376 /// The places might partially overlap - in this case, we give
2377 /// up and say that they might conflict. This occurs when
2378 /// different fields of a union are borrowed. For example,
2379 /// if `u` is a union, we have no way of telling how disjoint
2380 /// `u.a.x` and `a.b.y` are.
2381 Arbitrary,
2382 /// The places have the same type, and are either completely disjoint
2383 /// or equal - i.e., they can't "partially" overlap as can occur with
2384 /// unions. This is the "base case" on which we recur for extensions
2385 /// of the place.
2386 EqualOrDisjoint,
2387 /// The places are disjoint, so we know all extensions of them
2388 /// will also be disjoint.
2389 Disjoint,
2390 }
2391