2017-07-16 16:26:37 -04:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-04-07 07:11:01 -04:00
|
|
|
use borrow_check::borrow_set::BorrowSet;
|
2018-05-25 23:07:33 +00:00
|
|
|
use borrow_check::location::{LocationIndex, LocationTable};
|
2018-05-24 18:52:01 -03:00
|
|
|
use borrow_check::nll::facts::AllFactsExt;
|
2018-06-05 11:06:38 -04:00
|
|
|
use borrow_check::nll::type_check::MirTypeckRegionConstraints;
|
2018-07-09 21:26:20 +01:00
|
|
|
use borrow_check::nll::region_infer::values::RegionValueElements;
|
2018-07-20 17:30:03 +05:30
|
|
|
use borrow_check::nll::liveness_map::{NllLivenessMap, LocalWithRegion};
|
2018-05-28 13:47:20 -03:00
|
|
|
use dataflow::indexes::BorrowIndex;
|
2018-05-01 10:49:11 -04:00
|
|
|
use dataflow::move_paths::MoveData;
|
|
|
|
use dataflow::FlowAtLocation;
|
|
|
|
use dataflow::MaybeInitializedPlaces;
|
2017-11-10 19:20:35 +02:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::infer::InferCtxt;
|
2018-07-20 17:30:03 +05:30
|
|
|
use rustc::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, Mir};
|
2017-11-06 04:15:38 -05:00
|
|
|
use rustc::ty::{self, RegionKind, RegionVid};
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::util::nodemap::FxHashMap;
|
2018-07-18 18:10:08 -03:00
|
|
|
use rustc_errors::Diagnostic;
|
2017-10-24 14:20:47 -04:00
|
|
|
use std::collections::BTreeSet;
|
2017-12-04 11:37:34 -05:00
|
|
|
use std::fmt::Debug;
|
2018-05-30 17:17:37 +02:00
|
|
|
use std::env;
|
2017-11-22 17:39:46 -05:00
|
|
|
use std::io;
|
2018-05-01 10:49:11 -04:00
|
|
|
use std::path::PathBuf;
|
2018-05-28 13:47:20 -03:00
|
|
|
use std::rc::Rc;
|
2018-05-30 17:17:37 +02:00
|
|
|
use std::str::FromStr;
|
2017-11-10 19:20:35 +02:00
|
|
|
use transform::MirSource;
|
2018-07-20 17:30:03 +05:30
|
|
|
use util::liveness::{LivenessResults, LiveVarSet};
|
2017-09-28 00:14:34 -04:00
|
|
|
|
2018-05-01 10:49:11 -04:00
|
|
|
use self::mir_util::PassWhere;
|
2018-05-28 13:47:20 -03:00
|
|
|
use polonius_engine::{Algorithm, Output};
|
2017-09-28 00:14:34 -04:00
|
|
|
use util as mir_util;
|
2017-11-22 17:39:46 -05:00
|
|
|
use util::pretty::{self, ALIGN};
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
mod constraint_generation;
|
2017-12-12 17:29:37 -03:00
|
|
|
pub mod explain_borrow;
|
2018-05-01 10:49:11 -04:00
|
|
|
mod facts;
|
2018-05-28 13:47:20 -03:00
|
|
|
mod invalidation;
|
2018-05-01 10:48:04 -04:00
|
|
|
crate mod region_infer;
|
2017-12-03 12:03:44 -05:00
|
|
|
mod renumber;
|
2018-05-01 10:48:04 -04:00
|
|
|
crate mod type_check;
|
2017-11-21 13:12:24 -05:00
|
|
|
mod universal_regions;
|
2018-07-20 17:30:03 +05:30
|
|
|
crate mod liveness_map;
|
2017-10-09 22:16:57 -04:00
|
|
|
|
2018-07-01 10:29:18 -04:00
|
|
|
mod constraints;
|
2018-06-27 16:25:41 -04:00
|
|
|
|
2018-05-01 10:49:11 -04:00
|
|
|
use self::facts::AllFacts;
|
2017-10-24 14:20:47 -04:00
|
|
|
use self::region_infer::RegionInferenceContext;
|
2017-12-03 12:03:44 -05:00
|
|
|
use self::universal_regions::UniversalRegions;
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-11-17 04:34:02 -05:00
|
|
|
/// Rewrites the regions in the MIR to use NLL variables, also
|
2017-11-22 17:38:51 -05:00
|
|
|
/// scraping out the set of universal regions (e.g., region parameters)
|
2017-11-17 04:34:02 -05:00
|
|
|
/// declared on the function. That set will need to be given to
|
|
|
|
/// `compute_regions`.
|
|
|
|
pub(in borrow_check) fn replace_regions_in_mir<'cx, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
|
2017-11-10 19:20:35 +02:00
|
|
|
def_id: DefId,
|
2017-11-22 17:38:51 -05:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-10-26 09:48:20 -04:00
|
|
|
mir: &mut Mir<'tcx>,
|
2017-11-21 13:12:24 -05:00
|
|
|
) -> UniversalRegions<'tcx> {
|
2017-11-22 17:29:55 -05:00
|
|
|
debug!("replace_regions_in_mir(def_id={:?})", def_id);
|
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
// Compute named region information. This also renumbers the inputs/outputs.
|
|
|
|
let universal_regions = UniversalRegions::new(infcx, def_id, param_env);
|
2017-10-30 04:51:10 -04:00
|
|
|
|
2017-11-22 17:38:51 -05:00
|
|
|
// Replace all remaining regions with fresh inference variables.
|
2017-12-06 04:30:58 -05:00
|
|
|
renumber::renumber_mir(infcx, mir);
|
2017-11-17 04:34:02 -05:00
|
|
|
|
2017-11-22 17:29:55 -05:00
|
|
|
let source = MirSource::item(def_id);
|
|
|
|
mir_util::dump_mir(infcx.tcx, None, "renumber", &0, source, mir, |_, _| Ok(()));
|
|
|
|
|
2017-11-21 13:12:24 -05:00
|
|
|
universal_regions
|
2017-11-17 04:34:02 -05:00
|
|
|
}
|
2017-10-25 12:09:01 -04:00
|
|
|
|
2017-11-17 04:34:02 -05:00
|
|
|
/// Computes the (non-lexical) regions from the input MIR.
|
|
|
|
///
|
|
|
|
/// This may result in errors being reported.
|
|
|
|
pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
|
|
|
|
def_id: DefId,
|
2017-11-21 13:12:24 -05:00
|
|
|
universal_regions: UniversalRegions<'tcx>,
|
2017-11-17 04:34:02 -05:00
|
|
|
mir: &Mir<'tcx>,
|
2018-05-01 10:49:11 -04:00
|
|
|
location_table: &LocationTable,
|
2017-11-17 04:34:02 -05:00
|
|
|
param_env: ty::ParamEnv<'gcx>,
|
2018-01-29 01:49:29 +02:00
|
|
|
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'cx, 'gcx, 'tcx>>,
|
2017-11-17 04:34:02 -05:00
|
|
|
move_data: &MoveData<'tcx>,
|
2018-05-01 10:49:11 -04:00
|
|
|
borrow_set: &BorrowSet<'tcx>,
|
2018-07-18 18:10:08 -03:00
|
|
|
errors_buffer: &mut Vec<Diagnostic>,
|
2017-11-22 17:38:51 -05:00
|
|
|
) -> (
|
|
|
|
RegionInferenceContext<'tcx>,
|
2018-05-25 23:07:33 +00:00
|
|
|
Option<Rc<Output<RegionVid, BorrowIndex, LocationIndex>>>,
|
2017-12-04 11:37:34 -05:00
|
|
|
Option<ClosureRegionRequirements<'gcx>>,
|
2017-11-22 17:38:51 -05:00
|
|
|
) {
|
2018-06-07 05:14:32 -04:00
|
|
|
let mut all_facts = if AllFacts::enabled(infcx.tcx) {
|
2018-06-05 11:06:38 -04:00
|
|
|
Some(AllFacts::default())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2018-07-23 22:32:12 +03:00
|
|
|
let elements = &Rc::new(RegionValueElements::new(mir));
|
2018-07-09 21:26:20 +01:00
|
|
|
|
2017-11-06 07:26:34 -05:00
|
|
|
// Run the MIR type-checker.
|
2018-07-18 22:31:04 +05:30
|
|
|
let liveness_map = NllLivenessMap::compute(&mir);
|
|
|
|
let liveness = LivenessResults::compute(mir, &liveness_map);
|
2018-06-05 11:06:38 -04:00
|
|
|
let constraint_sets = type_check::type_check(
|
2017-12-03 13:14:36 -05:00
|
|
|
infcx,
|
|
|
|
param_env,
|
|
|
|
mir,
|
2017-12-10 10:23:45 -05:00
|
|
|
def_id,
|
2017-12-10 09:55:43 -05:00
|
|
|
&universal_regions,
|
2018-06-05 11:06:38 -04:00
|
|
|
location_table,
|
2018-07-01 10:22:36 -04:00
|
|
|
borrow_set,
|
2017-12-03 13:14:36 -05:00
|
|
|
&liveness,
|
2018-06-05 11:06:38 -04:00
|
|
|
&mut all_facts,
|
2017-12-03 13:14:36 -05:00
|
|
|
flow_inits,
|
|
|
|
move_data,
|
2018-07-09 21:26:20 +01:00
|
|
|
elements,
|
2018-07-18 22:34:01 +02:00
|
|
|
errors_buffer,
|
2017-12-03 13:14:36 -05:00
|
|
|
);
|
2017-11-06 07:26:34 -05:00
|
|
|
|
2018-05-01 10:49:11 -04:00
|
|
|
if let Some(all_facts) = &mut all_facts {
|
|
|
|
all_facts
|
|
|
|
.universal_region
|
|
|
|
.extend(universal_regions.universal_regions());
|
|
|
|
}
|
|
|
|
|
2018-06-05 11:06:38 -04:00
|
|
|
// Create the region inference context, taking ownership of the
|
|
|
|
// region inference data that was contained in `infcx`, and the
|
|
|
|
// base constraints generated by the type-check.
|
2017-11-06 07:26:34 -05:00
|
|
|
let var_origins = infcx.take_region_var_origins();
|
2018-06-05 11:06:38 -04:00
|
|
|
let MirTypeckRegionConstraints {
|
2018-07-23 22:10:18 +03:00
|
|
|
mut liveness_constraints,
|
2018-06-05 11:06:38 -04:00
|
|
|
outlives_constraints,
|
|
|
|
type_tests,
|
|
|
|
} = constraint_sets;
|
2018-07-23 22:10:18 +03:00
|
|
|
|
|
|
|
constraint_generation::generate_constraints(
|
|
|
|
infcx,
|
|
|
|
&mut liveness_constraints,
|
|
|
|
&mut all_facts,
|
|
|
|
location_table,
|
|
|
|
&mir,
|
|
|
|
borrow_set,
|
|
|
|
);
|
|
|
|
|
2018-06-05 11:06:38 -04:00
|
|
|
let mut regioncx = RegionInferenceContext::new(
|
|
|
|
var_origins,
|
|
|
|
universal_regions,
|
2018-05-01 10:49:11 -04:00
|
|
|
mir,
|
2018-06-05 11:06:38 -04:00
|
|
|
outlives_constraints,
|
|
|
|
type_tests,
|
2018-07-09 21:26:20 +01:00
|
|
|
liveness_constraints,
|
|
|
|
elements,
|
2018-05-01 10:49:11 -04:00
|
|
|
);
|
2018-06-05 11:06:38 -04:00
|
|
|
|
|
|
|
// Generate various additional constraints.
|
2018-05-15 22:31:29 -07:00
|
|
|
invalidation::generate_invalidates(
|
|
|
|
infcx,
|
|
|
|
&mut all_facts,
|
|
|
|
location_table,
|
|
|
|
&mir,
|
|
|
|
def_id,
|
2018-05-28 13:47:20 -03:00
|
|
|
borrow_set,
|
2018-05-15 22:31:29 -07:00
|
|
|
);
|
2017-10-25 12:09:01 -04:00
|
|
|
|
2018-05-01 10:49:11 -04:00
|
|
|
// Dump facts if requested.
|
2018-05-25 23:07:33 +00:00
|
|
|
let polonius_output = all_facts.and_then(|all_facts| {
|
2018-05-28 13:47:20 -03:00
|
|
|
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
|
|
|
|
let def_path = infcx.tcx.hir.def_path(def_id);
|
|
|
|
let dir_path =
|
|
|
|
PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate());
|
|
|
|
all_facts.write_to_dir(dir_path, location_table).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if infcx.tcx.sess.opts.debugging_opts.polonius {
|
2018-05-30 17:17:37 +02:00
|
|
|
let algorithm = env::var("POLONIUS_ALGORITHM")
|
|
|
|
.unwrap_or(String::from("DatafrogOpt"));
|
|
|
|
let algorithm = Algorithm::from_str(&algorithm).unwrap();
|
2018-06-02 14:20:04 +02:00
|
|
|
debug!("compute_regions: using polonius algorithm {:?}", algorithm);
|
2018-05-28 13:47:20 -03:00
|
|
|
Some(Rc::new(Output::compute(
|
|
|
|
&all_facts,
|
2018-05-30 17:17:37 +02:00
|
|
|
algorithm,
|
2018-05-28 13:47:20 -03:00
|
|
|
false,
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-05-25 23:07:33 +00:00
|
|
|
});
|
2017-11-06 07:26:34 -05:00
|
|
|
|
|
|
|
// Solve the region constraints.
|
2018-07-18 18:10:08 -03:00
|
|
|
let closure_region_requirements = regioncx.solve(infcx, &mir, def_id, errors_buffer);
|
2017-10-25 12:09:01 -04:00
|
|
|
|
|
|
|
// Dump MIR results into a file, if that is enabled. This let us
|
2017-11-22 17:39:46 -05:00
|
|
|
// write unit-tests, as well as helping with debugging.
|
|
|
|
dump_mir_results(
|
|
|
|
infcx,
|
2018-07-18 22:31:04 +05:30
|
|
|
&liveness,
|
2017-11-22 17:39:46 -05:00
|
|
|
MirSource::item(def_id),
|
|
|
|
&mir,
|
|
|
|
®ioncx,
|
|
|
|
&closure_region_requirements,
|
|
|
|
);
|
|
|
|
|
|
|
|
// We also have a `#[rustc_nll]` annotation that causes us to dump
|
|
|
|
// information
|
2018-07-18 18:10:08 -03:00
|
|
|
dump_annotation(infcx, &mir, def_id, ®ioncx, &closure_region_requirements, errors_buffer);
|
2017-10-25 12:09:01 -04:00
|
|
|
|
2018-05-25 23:07:33 +00:00
|
|
|
(regioncx, polonius_output, closure_region_requirements)
|
2017-10-25 12:09:01 -04:00
|
|
|
}
|
|
|
|
|
2018-07-08 02:14:57 +05:30
|
|
|
fn dump_mir_results<'a, 'gcx, 'tcx>(
|
2017-10-24 14:20:47 -04:00
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
2018-07-18 22:31:04 +05:30
|
|
|
liveness: &LivenessResults<LocalWithRegion>,
|
2017-10-24 14:20:47 -04:00
|
|
|
source: MirSource,
|
2017-10-26 09:48:20 -04:00
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
regioncx: &RegionInferenceContext,
|
2017-11-22 17:39:46 -05:00
|
|
|
closure_region_requirements: &Option<ClosureRegionRequirements>,
|
2017-10-24 14:20:47 -04:00
|
|
|
) {
|
|
|
|
if !mir_util::dump_enabled(infcx.tcx, "nll", source) {
|
|
|
|
return;
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 22:48:06 +05:30
|
|
|
let map = &NllLivenessMap::compute(mir);
|
2018-07-15 00:28:26 -04:00
|
|
|
|
2018-05-28 13:47:20 -03:00
|
|
|
let regular_liveness_per_location: FxHashMap<_, _> = mir
|
|
|
|
.basic_blocks()
|
2017-10-24 16:20:47 -04:00
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
2017-10-25 10:59:50 -04:00
|
|
|
liveness
|
|
|
|
.regular
|
2018-07-15 00:28:26 -04:00
|
|
|
.simulate_block(&mir, bb, map, |location, local_set| {
|
2017-10-25 10:59:50 -04:00
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
2017-10-24 16:20:47 -04:00
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2018-05-28 13:47:20 -03:00
|
|
|
let drop_liveness_per_location: FxHashMap<_, _> = mir
|
|
|
|
.basic_blocks()
|
2017-10-24 14:20:47 -04:00
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
2017-10-25 10:59:50 -04:00
|
|
|
liveness
|
|
|
|
.drop
|
2018-07-15 00:28:26 -04:00
|
|
|
.simulate_block(&mir, bb, map, |location, local_set| {
|
2017-10-25 10:59:50 -04:00
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
2017-10-24 14:20:47 -04:00
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2018-05-28 13:47:20 -03:00
|
|
|
mir_util::dump_mir(
|
|
|
|
infcx.tcx,
|
|
|
|
None,
|
|
|
|
"nll",
|
|
|
|
&0,
|
|
|
|
source,
|
|
|
|
mir,
|
|
|
|
|pass_where, out| {
|
|
|
|
match pass_where {
|
|
|
|
// Before the CFG, dump out the values for each region variable.
|
|
|
|
PassWhere::BeforeCFG => {
|
|
|
|
regioncx.dump_mir(out)?;
|
|
|
|
|
|
|
|
if let Some(closure_region_requirements) = closure_region_requirements {
|
|
|
|
writeln!(out, "|")?;
|
|
|
|
writeln!(out, "| Free Region Constraints")?;
|
|
|
|
for_each_region_constraint(closure_region_requirements, &mut |msg| {
|
|
|
|
writeln!(out, "| {}", msg)
|
|
|
|
})?;
|
|
|
|
}
|
2017-11-22 17:39:46 -05:00
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
|
2018-05-28 13:47:20 -03:00
|
|
|
PassWhere::BeforeLocation(location) => {
|
|
|
|
let s = live_variable_set(
|
|
|
|
®ular_liveness_per_location[&location],
|
|
|
|
&drop_liveness_per_location[&location],
|
|
|
|
);
|
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"{:ALIGN$} | Live variables on entry to {:?}: {}",
|
|
|
|
"",
|
|
|
|
location,
|
|
|
|
s,
|
|
|
|
ALIGN = ALIGN
|
|
|
|
)?;
|
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2018-06-29 06:47:12 -04:00
|
|
|
// After each basic block, dump out the values
|
|
|
|
// that are live on exit from the basic block.
|
|
|
|
PassWhere::AfterTerminator(bb) => {
|
|
|
|
let s = live_variable_set(&liveness.regular.outs[bb], &liveness.drop.outs[bb]);
|
2018-06-29 10:04:17 -04:00
|
|
|
writeln!(out, " | Live variables on exit from {:?}: {}", bb, s)?;
|
2018-06-29 06:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PassWhere::BeforeBlock(_) | PassWhere::AfterLocation(_) | PassWhere::AfterCFG => {}
|
2018-05-28 13:47:20 -03:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
);
|
2017-11-22 17:39:46 -05:00
|
|
|
|
|
|
|
// Also dump the inference graph constraints as a graphviz file.
|
2018-05-10 12:02:19 -06:00
|
|
|
let _: io::Result<()> = do catch {
|
2017-11-22 17:39:46 -05:00
|
|
|
let mut file =
|
2018-07-04 19:39:38 -04:00
|
|
|
pretty::create_dump_file(infcx.tcx, "regioncx.all.dot", None, "nll", &0, source)?;
|
2018-07-04 06:07:17 -04:00
|
|
|
regioncx.dump_graphviz_raw_constraints(&mut file)?;
|
2018-05-10 12:02:19 -06:00
|
|
|
};
|
2018-07-04 19:39:38 -04:00
|
|
|
|
|
|
|
// Also dump the inference graph constraints as a graphviz file.
|
|
|
|
let _: io::Result<()> = do catch {
|
|
|
|
let mut file =
|
|
|
|
pretty::create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, source)?;
|
|
|
|
regioncx.dump_graphviz_scc_constraints(&mut file)?;
|
|
|
|
};
|
2017-11-22 17:39:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dump_annotation<'a, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
mir_def_id: DefId,
|
|
|
|
regioncx: &RegionInferenceContext,
|
|
|
|
closure_region_requirements: &Option<ClosureRegionRequirements>,
|
2018-07-18 18:10:08 -03:00
|
|
|
errors_buffer: &mut Vec<Diagnostic>,
|
2017-11-22 17:39:46 -05:00
|
|
|
) {
|
|
|
|
let tcx = infcx.tcx;
|
|
|
|
let base_def_id = tcx.closure_base_def_id(mir_def_id);
|
|
|
|
if !tcx.has_attr(base_def_id, "rustc_regions") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the enclosing function is tagged with `#[rustc_regions]`,
|
|
|
|
// we dump out various bits of state as warnings. This is useful
|
|
|
|
// for verifying that the compiler is behaving as expected. These
|
|
|
|
// warnings focus on the closure region requirements -- for
|
|
|
|
// viewing the intraprocedural state, the -Zdump-mir output is
|
|
|
|
// better.
|
|
|
|
|
|
|
|
if let Some(closure_region_requirements) = closure_region_requirements {
|
2018-05-28 13:47:20 -03:00
|
|
|
let mut err = tcx
|
|
|
|
.sess
|
2017-11-22 17:39:46 -05:00
|
|
|
.diagnostic()
|
|
|
|
.span_note_diag(mir.span, "External requirements");
|
|
|
|
|
|
|
|
regioncx.annotate(&mut err);
|
|
|
|
|
|
|
|
err.note(&format!(
|
|
|
|
"number of external vids: {}",
|
|
|
|
closure_region_requirements.num_external_vids
|
|
|
|
));
|
|
|
|
|
|
|
|
// Dump the region constraints we are imposing *between* those
|
|
|
|
// newly created variables.
|
|
|
|
for_each_region_constraint(closure_region_requirements, &mut |msg| {
|
|
|
|
err.note(msg);
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
|
|
|
|
2018-07-18 18:10:08 -03:00
|
|
|
err.buffer(errors_buffer);
|
2017-11-22 17:39:46 -05:00
|
|
|
} else {
|
2018-05-28 13:47:20 -03:00
|
|
|
let mut err = tcx
|
|
|
|
.sess
|
2017-11-22 17:39:46 -05:00
|
|
|
.diagnostic()
|
|
|
|
.span_note_diag(mir.span, "No external requirements");
|
|
|
|
regioncx.annotate(&mut err);
|
2018-07-18 18:10:08 -03:00
|
|
|
|
|
|
|
err.buffer(errors_buffer);
|
2017-11-22 17:39:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn for_each_region_constraint(
|
|
|
|
closure_region_requirements: &ClosureRegionRequirements,
|
2018-02-23 10:15:26 -08:00
|
|
|
with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
|
2017-11-22 17:39:46 -05:00
|
|
|
) -> io::Result<()> {
|
|
|
|
for req in &closure_region_requirements.outlives_requirements {
|
2018-02-23 10:15:26 -08:00
|
|
|
let subject: &dyn Debug = match &req.subject {
|
2017-12-04 11:37:34 -05:00
|
|
|
ClosureOutlivesSubject::Region(subject) => subject,
|
|
|
|
ClosureOutlivesSubject::Ty(ty) => ty,
|
|
|
|
};
|
2017-11-22 17:39:46 -05:00
|
|
|
with_msg(&format!(
|
|
|
|
"where {:?}: {:?}",
|
2018-05-28 13:47:20 -03:00
|
|
|
subject, req.outlived_free_region,
|
2017-11-22 17:39:46 -05:00
|
|
|
))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2017-09-26 22:24:19 -04:00
|
|
|
}
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
/// Right now, we piggy back on the `ReVar` to store our NLL inference
|
2017-11-06 04:15:38 -05:00
|
|
|
/// regions. These are indexed with `RegionVid`. This method will
|
2018-02-16 15:56:50 +01:00
|
|
|
/// assert that the region is a `ReVar` and extract its internal index.
|
2017-11-22 17:38:51 -05:00
|
|
|
/// This is reasonable because in our MIR we replace all universal regions
|
2017-11-06 04:15:38 -05:00
|
|
|
/// with inference variables.
|
|
|
|
pub trait ToRegionVid {
|
2017-12-07 11:21:29 -05:00
|
|
|
fn to_region_vid(self) -> RegionVid;
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-12-07 11:21:29 -05:00
|
|
|
impl<'tcx> ToRegionVid for &'tcx RegionKind {
|
|
|
|
fn to_region_vid(self) -> RegionVid {
|
|
|
|
if let ty::ReVar(vid) = self {
|
|
|
|
*vid
|
2017-10-24 14:20:47 -04:00
|
|
|
} else {
|
|
|
|
bug!("region is not an ReVar: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 10:59:50 -04:00
|
|
|
|
2017-12-07 11:21:29 -05:00
|
|
|
impl ToRegionVid for RegionVid {
|
|
|
|
fn to_region_vid(self) -> RegionVid {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 00:22:57 +05:30
|
|
|
fn live_variable_set(
|
2018-07-20 17:30:03 +05:30
|
|
|
regular: &LiveVarSet<LocalWithRegion>,
|
|
|
|
drops: &LiveVarSet<LocalWithRegion>
|
2018-07-20 00:22:57 +05:30
|
|
|
) -> String {
|
2017-10-25 10:59:50 -04:00
|
|
|
// sort and deduplicate:
|
|
|
|
let all_locals: BTreeSet<_> = regular.iter().chain(drops.iter()).collect();
|
|
|
|
|
|
|
|
// construct a string with each local, including `(drop)` if it is
|
|
|
|
// only dropped, versus a regular use.
|
|
|
|
let mut string = String::new();
|
|
|
|
for local in all_locals {
|
|
|
|
string.push_str(&format!("{:?}", local));
|
|
|
|
|
|
|
|
if !regular.contains(&local) {
|
|
|
|
assert!(drops.contains(&local));
|
|
|
|
string.push_str(" (drop)");
|
|
|
|
}
|
|
|
|
|
|
|
|
string.push_str(", ");
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
let len = if string.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
string.len() - 2
|
|
|
|
};
|
2017-10-25 10:59:50 -04:00
|
|
|
|
|
|
|
format!("[{}]", &string[..len])
|
|
|
|
}
|