]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/nll.rs
6f8fae2de29a7994e847498c7f5414cb98fdfd77
[rust.git] / compiler / rustc_borrowck / src / nll.rs
1 //! The entry point of the NLL borrow checker.
2
3 use rustc_data_structures::vec_map::VecMap;
4 use rustc_hir::def_id::DefId;
5 use rustc_index::vec::IndexVec;
6 use rustc_infer::infer::InferCtxt;
7 use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
8 use rustc_middle::mir::{
9     BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
10     Promoted,
11 };
12 use rustc_middle::ty::{self, OpaqueHiddenType, Region, RegionVid};
13 use rustc_span::symbol::sym;
14 use std::env;
15 use std::fmt::Debug;
16 use std::io;
17 use std::path::PathBuf;
18 use std::rc::Rc;
19 use std::str::FromStr;
20
21 use polonius_engine::{Algorithm, Output};
22
23 use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
24 use rustc_mir_dataflow::move_paths::{InitKind, InitLocation, MoveData};
25 use rustc_mir_dataflow::ResultsCursor;
26
27 use crate::{
28     borrow_set::BorrowSet,
29     constraint_generation,
30     diagnostics::RegionErrors,
31     facts::{AllFacts, AllFactsExt, RustcFacts},
32     invalidation,
33     location::LocationTable,
34     region_infer::{values::RegionValueElements, RegionInferenceContext},
35     renumber,
36     type_check::{self, MirTypeckRegionConstraints, MirTypeckResults},
37     universal_regions::UniversalRegions,
38     Upvar,
39 };
40
41 pub type PoloniusOutput = Output<RustcFacts>;
42
43 /// The output of `nll::compute_regions`. This includes the computed `RegionInferenceContext`, any
44 /// closure requirements to propagate, and any generated errors.
45 crate struct NllOutput<'tcx> {
46     pub regioncx: RegionInferenceContext<'tcx>,
47     pub opaque_type_values: VecMap<DefId, OpaqueHiddenType<'tcx>>,
48     pub polonius_input: Option<Box<AllFacts>>,
49     pub polonius_output: Option<Rc<PoloniusOutput>>,
50     pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
51     pub nll_errors: RegionErrors<'tcx>,
52 }
53
54 /// Rewrites the regions in the MIR to use NLL variables, also scraping out the set of universal
55 /// regions (e.g., region parameters) declared on the function. That set will need to be given to
56 /// `compute_regions`.
57 #[instrument(skip(infcx, param_env, body, promoted), level = "debug")]
58 pub(crate) fn replace_regions_in_mir<'cx, 'tcx>(
59     infcx: &InferCtxt<'cx, 'tcx>,
60     param_env: ty::ParamEnv<'tcx>,
61     body: &mut Body<'tcx>,
62     promoted: &mut IndexVec<Promoted, Body<'tcx>>,
63 ) -> UniversalRegions<'tcx> {
64     let def = body.source.with_opt_param().as_local().unwrap();
65
66     debug!(?def);
67
68     // Compute named region information. This also renumbers the inputs/outputs.
69     let universal_regions = UniversalRegions::new(infcx, def, param_env);
70
71     // Replace all remaining regions with fresh inference variables.
72     renumber::renumber_mir(infcx, body, promoted);
73
74     dump_mir(infcx.tcx, None, "renumber", &0, body, |_, _| Ok(()));
75
76     universal_regions
77 }
78
79 // This function populates an AllFacts instance with base facts related to
80 // MovePaths and needed for the move analysis.
81 fn populate_polonius_move_facts(
82     all_facts: &mut AllFacts,
83     move_data: &MoveData<'_>,
84     location_table: &LocationTable,
85     body: &Body<'_>,
86 ) {
87     all_facts
88         .path_is_var
89         .extend(move_data.rev_lookup.iter_locals_enumerated().map(|(l, r)| (r, l)));
90
91     for (child, move_path) in move_data.move_paths.iter_enumerated() {
92         if let Some(parent) = move_path.parent {
93             all_facts.child_path.push((child, parent));
94         }
95     }
96
97     let fn_entry_start = location_table
98         .start_index(Location { block: BasicBlock::from_u32(0u32), statement_index: 0 });
99
100     // initialized_at
101     for init in move_data.inits.iter() {
102         match init.location {
103             InitLocation::Statement(location) => {
104                 let block_data = &body[location.block];
105                 let is_terminator = location.statement_index == block_data.statements.len();
106
107                 if is_terminator && init.kind == InitKind::NonPanicPathOnly {
108                     // We are at the terminator of an init that has a panic path,
109                     // and where the init should not happen on panic
110
111                     for successor in block_data.terminator().successors() {
112                         if body[successor].is_cleanup {
113                             continue;
114                         }
115
116                         // The initialization happened in (or rather, when arriving at)
117                         // the successors, but not in the unwind block.
118                         let first_statement = Location { block: successor, statement_index: 0 };
119                         all_facts
120                             .path_assigned_at_base
121                             .push((init.path, location_table.start_index(first_statement)));
122                     }
123                 } else {
124                     // In all other cases, the initialization just happens at the
125                     // midpoint, like any other effect.
126                     all_facts
127                         .path_assigned_at_base
128                         .push((init.path, location_table.mid_index(location)));
129                 }
130             }
131             // Arguments are initialized on function entry
132             InitLocation::Argument(local) => {
133                 assert!(body.local_kind(local) == LocalKind::Arg);
134                 all_facts.path_assigned_at_base.push((init.path, fn_entry_start));
135             }
136         }
137     }
138
139     for (local, path) in move_data.rev_lookup.iter_locals_enumerated() {
140         if body.local_kind(local) != LocalKind::Arg {
141             // Non-arguments start out deinitialised; we simulate this with an
142             // initial move:
143             all_facts.path_moved_at_base.push((path, fn_entry_start));
144         }
145     }
146
147     // moved_out_at
148     // deinitialisation is assumed to always happen!
149     all_facts
150         .path_moved_at_base
151         .extend(move_data.moves.iter().map(|mo| (mo.path, location_table.mid_index(mo.source))));
152 }
153
154 /// Computes the (non-lexical) regions from the input MIR.
155 ///
156 /// This may result in errors being reported.
157 pub(crate) fn compute_regions<'cx, 'tcx>(
158     infcx: &InferCtxt<'cx, 'tcx>,
159     universal_regions: UniversalRegions<'tcx>,
160     body: &Body<'tcx>,
161     promoted: &IndexVec<Promoted, Body<'tcx>>,
162     location_table: &LocationTable,
163     param_env: ty::ParamEnv<'tcx>,
164     flow_inits: &mut ResultsCursor<'cx, 'tcx, MaybeInitializedPlaces<'cx, 'tcx>>,
165     move_data: &MoveData<'tcx>,
166     borrow_set: &BorrowSet<'tcx>,
167     upvars: &[Upvar<'tcx>],
168     use_polonius: bool,
169 ) -> NllOutput<'tcx> {
170     let mut all_facts =
171         (use_polonius || AllFacts::enabled(infcx.tcx)).then_some(AllFacts::default());
172
173     let universal_regions = Rc::new(universal_regions);
174
175     let elements = &Rc::new(RegionValueElements::new(&body));
176
177     // Run the MIR type-checker.
178     let MirTypeckResults { constraints, universal_region_relations, opaque_type_values } =
179         type_check::type_check(
180             infcx,
181             param_env,
182             body,
183             promoted,
184             &universal_regions,
185             location_table,
186             borrow_set,
187             &mut all_facts,
188             flow_inits,
189             move_data,
190             elements,
191             upvars,
192             use_polonius,
193         );
194
195     if let Some(all_facts) = &mut all_facts {
196         let _prof_timer = infcx.tcx.prof.generic_activity("polonius_fact_generation");
197         all_facts.universal_region.extend(universal_regions.universal_regions());
198         populate_polonius_move_facts(all_facts, move_data, location_table, &body);
199
200         // Emit universal regions facts, and their relations, for Polonius.
201         //
202         // 1: universal regions are modeled in Polonius as a pair:
203         // - the universal region vid itself.
204         // - a "placeholder loan" associated to this universal region. Since they don't exist in
205         //   the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
206         //   added to the existing number of loans, as if they succeeded them in the set.
207         //
208         let borrow_count = borrow_set.len();
209         debug!(
210             "compute_regions: polonius placeholders, num_universals={}, borrow_count={}",
211             universal_regions.len(),
212             borrow_count
213         );
214
215         for universal_region in universal_regions.universal_regions() {
216             let universal_region_idx = universal_region.index();
217             let placeholder_loan_idx = borrow_count + universal_region_idx;
218             all_facts.placeholder.push((universal_region, placeholder_loan_idx.into()));
219         }
220
221         // 2: the universal region relations `outlives` constraints are emitted as
222         //  `known_placeholder_subset` facts.
223         for (fr1, fr2) in universal_region_relations.known_outlives() {
224             if fr1 != fr2 {
225                 debug!(
226                     "compute_regions: emitting polonius `known_placeholder_subset` \
227                      fr1={:?}, fr2={:?}",
228                     fr1, fr2
229                 );
230                 all_facts.known_placeholder_subset.push((fr1, fr2));
231             }
232         }
233     }
234
235     // Create the region inference context, taking ownership of the
236     // region inference data that was contained in `infcx`, and the
237     // base constraints generated by the type-check.
238     let var_origins = infcx.take_region_var_origins();
239     let MirTypeckRegionConstraints {
240         placeholder_indices,
241         placeholder_index_to_region: _,
242         mut liveness_constraints,
243         outlives_constraints,
244         member_constraints,
245         closure_bounds_mapping,
246         universe_causes,
247         type_tests,
248     } = constraints;
249     let placeholder_indices = Rc::new(placeholder_indices);
250
251     constraint_generation::generate_constraints(
252         infcx,
253         &mut liveness_constraints,
254         &mut all_facts,
255         location_table,
256         &body,
257         borrow_set,
258     );
259
260     let mut regioncx = RegionInferenceContext::new(
261         var_origins,
262         universal_regions,
263         placeholder_indices,
264         universal_region_relations,
265         outlives_constraints,
266         member_constraints,
267         closure_bounds_mapping,
268         universe_causes,
269         type_tests,
270         liveness_constraints,
271         elements,
272     );
273
274     // Generate various additional constraints.
275     invalidation::generate_invalidates(infcx.tcx, &mut all_facts, location_table, body, borrow_set);
276
277     let def_id = body.source.def_id();
278
279     // Dump facts if requested.
280     let polonius_output = all_facts.as_ref().and_then(|all_facts| {
281         if infcx.tcx.sess.opts.debugging_opts.nll_facts {
282             let def_path = infcx.tcx.def_path(def_id);
283             let dir_path = PathBuf::from(&infcx.tcx.sess.opts.debugging_opts.nll_facts_dir)
284                 .join(def_path.to_filename_friendly_no_crate());
285             all_facts.write_to_dir(dir_path, location_table).unwrap();
286         }
287
288         if use_polonius {
289             let algorithm =
290                 env::var("POLONIUS_ALGORITHM").unwrap_or_else(|_| String::from("Hybrid"));
291             let algorithm = Algorithm::from_str(&algorithm).unwrap();
292             debug!("compute_regions: using polonius algorithm {:?}", algorithm);
293             let _prof_timer = infcx.tcx.prof.generic_activity("polonius_analysis");
294             Some(Rc::new(Output::compute(&all_facts, algorithm, false)))
295         } else {
296             None
297         }
298     });
299
300     // Solve the region constraints.
301     let (closure_region_requirements, nll_errors) =
302         regioncx.solve(infcx, &body, polonius_output.clone());
303
304     if !nll_errors.is_empty() {
305         // Suppress unhelpful extra errors in `infer_opaque_types`.
306         infcx.set_tainted_by_errors();
307     }
308
309     let remapped_opaque_tys = regioncx.infer_opaque_types(&infcx, opaque_type_values);
310
311     NllOutput {
312         regioncx,
313         opaque_type_values: remapped_opaque_tys,
314         polonius_input: all_facts.map(Box::new),
315         polonius_output,
316         opt_closure_req: closure_region_requirements,
317         nll_errors,
318     }
319 }
320
321 pub(super) fn dump_mir_results<'a, 'tcx>(
322     infcx: &InferCtxt<'a, 'tcx>,
323     body: &Body<'tcx>,
324     regioncx: &RegionInferenceContext<'tcx>,
325     closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
326 ) {
327     if !dump_enabled(infcx.tcx, "nll", body.source.def_id()) {
328         return;
329     }
330
331     dump_mir(infcx.tcx, None, "nll", &0, body, |pass_where, out| {
332         match pass_where {
333             // Before the CFG, dump out the values for each region variable.
334             PassWhere::BeforeCFG => {
335                 regioncx.dump_mir(infcx.tcx, out)?;
336                 writeln!(out, "|")?;
337
338                 if let Some(closure_region_requirements) = closure_region_requirements {
339                     writeln!(out, "| Free Region Constraints")?;
340                     for_each_region_constraint(closure_region_requirements, &mut |msg| {
341                         writeln!(out, "| {}", msg)
342                     })?;
343                     writeln!(out, "|")?;
344                 }
345             }
346
347             PassWhere::BeforeLocation(_) => {}
348
349             PassWhere::AfterTerminator(_) => {}
350
351             PassWhere::BeforeBlock(_) | PassWhere::AfterLocation(_) | PassWhere::AfterCFG => {}
352         }
353         Ok(())
354     });
355
356     // Also dump the inference graph constraints as a graphviz file.
357     let _: io::Result<()> = try {
358         let mut file =
359             create_dump_file(infcx.tcx, "regioncx.all.dot", None, "nll", &0, body.source)?;
360         regioncx.dump_graphviz_raw_constraints(&mut file)?;
361     };
362
363     // Also dump the inference graph constraints as a graphviz file.
364     let _: io::Result<()> = try {
365         let mut file =
366             create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, body.source)?;
367         regioncx.dump_graphviz_scc_constraints(&mut file)?;
368     };
369 }
370
371 pub(super) fn dump_annotation<'a, 'tcx>(
372     infcx: &InferCtxt<'a, 'tcx>,
373     body: &Body<'tcx>,
374     regioncx: &RegionInferenceContext<'tcx>,
375     closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
376     opaque_type_values: &VecMap<DefId, OpaqueHiddenType<'tcx>>,
377     errors: &mut crate::error::BorrowckErrors<'tcx>,
378 ) {
379     let tcx = infcx.tcx;
380     let base_def_id = tcx.typeck_root_def_id(body.source.def_id());
381     if !tcx.has_attr(base_def_id, sym::rustc_regions) {
382         return;
383     }
384
385     // When the enclosing function is tagged with `#[rustc_regions]`,
386     // we dump out various bits of state as warnings. This is useful
387     // for verifying that the compiler is behaving as expected.  These
388     // warnings focus on the closure region requirements -- for
389     // viewing the intraprocedural state, the -Zdump-mir output is
390     // better.
391
392     let mut err = if let Some(closure_region_requirements) = closure_region_requirements {
393         let mut err = tcx.sess.diagnostic().span_note_diag(body.span, "external requirements");
394
395         regioncx.annotate(tcx, &mut err);
396
397         err.note(&format!(
398             "number of external vids: {}",
399             closure_region_requirements.num_external_vids
400         ));
401
402         // Dump the region constraints we are imposing *between* those
403         // newly created variables.
404         for_each_region_constraint(closure_region_requirements, &mut |msg| {
405             err.note(msg);
406             Ok(())
407         })
408         .unwrap();
409
410         err
411     } else {
412         let mut err = tcx.sess.diagnostic().span_note_diag(body.span, "no external requirements");
413         regioncx.annotate(tcx, &mut err);
414
415         err
416     };
417
418     if !opaque_type_values.is_empty() {
419         err.note(&format!("Inferred opaque type values:\n{:#?}", opaque_type_values));
420     }
421
422     errors.buffer_non_error_diag(err);
423 }
424
425 fn for_each_region_constraint(
426     closure_region_requirements: &ClosureRegionRequirements<'_>,
427     with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
428 ) -> io::Result<()> {
429     for req in &closure_region_requirements.outlives_requirements {
430         let subject: &dyn Debug = match &req.subject {
431             ClosureOutlivesSubject::Region(subject) => subject,
432             ClosureOutlivesSubject::Ty(ty) => ty,
433         };
434         with_msg(&format!("where {:?}: {:?}", subject, req.outlived_free_region,))?;
435     }
436     Ok(())
437 }
438
439 /// Right now, we piggy back on the `ReVar` to store our NLL inference
440 /// regions. These are indexed with `RegionVid`. This method will
441 /// assert that the region is a `ReVar` and extract its internal index.
442 /// This is reasonable because in our MIR we replace all universal regions
443 /// with inference variables.
444 pub trait ToRegionVid {
445     fn to_region_vid(self) -> RegionVid;
446 }
447
448 impl<'tcx> ToRegionVid for Region<'tcx> {
449     fn to_region_vid(self) -> RegionVid {
450         if let ty::ReVar(vid) = *self { vid } else { bug!("region is not an ReVar: {:?}", self) }
451     }
452 }
453
454 impl ToRegionVid for RegionVid {
455     fn to_region_vid(self) -> RegionVid {
456         self
457     }
458 }
459
460 crate trait ConstraintDescription {
461     fn description(&self) -> &'static str;
462 }