Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace (Body, DefId) with Body where possible
A `Body` now contains its `MirSource`, which in turn contains the
`DefId` of the item associated with the `Body`.
  • Loading branch information
ecstatic-morse committed Oct 4, 2020
1 parent 4ccf5f7 commit e72e43c
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 232 deletions.
10 changes: 4 additions & 6 deletions compiler/rustc_mir/src/borrow_check/mod.rs
Expand Up @@ -203,7 +203,7 @@ fn do_mir_borrowck<'a, 'tcx>(
let mdpe = MoveDataParamEnv { move_data, param_env };

let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint()
.into_results_cursor(&body);
Expand All @@ -221,7 +221,6 @@ fn do_mir_borrowck<'a, 'tcx>(
nll_errors,
} = nll::compute_regions(
infcx,
def.did,
free_regions,
body,
&promoted,
Expand All @@ -242,7 +241,6 @@ fn do_mir_borrowck<'a, 'tcx>(
nll::dump_annotation(
infcx,
&body,
def.did.to_def_id(),
&regioncx,
&opt_closure_req,
&opaque_type_values,
Expand All @@ -257,15 +255,15 @@ fn do_mir_borrowck<'a, 'tcx>(
let regioncx = Rc::new(regioncx);

let flow_borrows = Borrows::new(tcx, &body, regioncx.clone(), &borrow_set)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_uninits = MaybeUninitializedPlaces::new(tcx, &body, &mdpe)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint();
let flow_ever_inits = EverInitializedPlaces::new(tcx, &body, &mdpe)
.into_engine(tcx, &body, def.did.to_def_id())
.into_engine(tcx, &body)
.pass_name("borrowck")
.iterate_to_fixpoint();

Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_mir/src/borrow_check/nll.rs
Expand Up @@ -156,7 +156,6 @@ fn populate_polonius_move_facts(
/// This may result in errors being reported.
pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
infcx: &InferCtxt<'cx, 'tcx>,
def_id: LocalDefId,
universal_regions: UniversalRegions<'tcx>,
body: &Body<'tcx>,
promoted: &IndexVec<Promoted, Body<'tcx>>,
Expand All @@ -180,7 +179,6 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
param_env,
body,
promoted,
def_id,
&universal_regions,
location_table,
borrow_set,
Expand Down Expand Up @@ -270,10 +268,12 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
// Generate various additional constraints.
invalidation::generate_invalidates(infcx.tcx, &mut all_facts, location_table, body, borrow_set);

let def_id = body.source.def_id();

// Dump facts if requested.
let polonius_output = all_facts.and_then(|all_facts| {
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
let def_path = infcx.tcx.def_path(def_id.to_def_id());
let def_path = infcx.tcx.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();
Expand All @@ -293,7 +293,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(

// Solve the region constraints.
let (closure_region_requirements, nll_errors) =
regioncx.solve(infcx, &body, def_id.to_def_id(), polonius_output.clone());
regioncx.solve(infcx, &body, def_id, polonius_output.clone());

if !nll_errors.is_empty() {
// Suppress unhelpful extra errors in `infer_opaque_types`.
Expand Down Expand Up @@ -364,14 +364,13 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
pub(super) fn dump_annotation<'a, 'tcx>(
infcx: &InferCtxt<'a, 'tcx>,
body: &Body<'tcx>,
mir_def_id: DefId,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
opaque_type_values: &FxHashMap<DefId, ty::ResolvedOpaqueTy<'tcx>>,
errors_buffer: &mut Vec<Diagnostic>,
) {
let tcx = infcx.tcx;
let base_def_id = tcx.closure_base_def_id(mir_def_id);
let base_def_id = tcx.closure_base_def_id(body.source.def_id());
if !tcx.has_attr(base_def_id, sym::rustc_regions) {
return;
}
Expand Down
59 changes: 30 additions & 29 deletions compiler/rustc_mir/src/borrow_check/type_check/input_output.rs
Expand Up @@ -28,42 +28,43 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let (&normalized_output_ty, normalized_input_tys) =
normalized_inputs_and_output.split_last().unwrap();

let mir_def_id = body.source.def_id().expect_local();

// If the user explicitly annotated the input types, extract
// those.
//
// e.g., `|x: FxHashMap<_, &'static u32>| ...`
let user_provided_sig;
if !self.tcx().is_closure(self.mir_def_id.to_def_id()) {
if !self.tcx().is_closure(mir_def_id.to_def_id()) {
user_provided_sig = None;
} else {
let typeck_results = self.tcx().typeck(self.mir_def_id);
user_provided_sig =
match typeck_results.user_provided_sigs.get(&self.mir_def_id.to_def_id()) {
None => None,
Some(user_provided_poly_sig) => {
// Instantiate the canonicalized variables from
// user-provided signature (e.g., the `_` in the code
// above) with fresh variables.
let (poly_sig, _) =
self.infcx.instantiate_canonical_with_fresh_inference_vars(
let typeck_results = self.tcx().typeck(mir_def_id);
user_provided_sig = match typeck_results.user_provided_sigs.get(&mir_def_id.to_def_id())
{
None => None,
Some(user_provided_poly_sig) => {
// Instantiate the canonicalized variables from
// user-provided signature (e.g., the `_` in the code
// above) with fresh variables.
let (poly_sig, _) = self.infcx.instantiate_canonical_with_fresh_inference_vars(
body.span,
&user_provided_poly_sig,
);

// Replace the bound items in the fn sig with fresh
// variables, so that they represent the view from
// "inside" the closure.
Some(
self.infcx
.replace_bound_vars_with_fresh_vars(
body.span,
&user_provided_poly_sig,
);

// Replace the bound items in the fn sig with fresh
// variables, so that they represent the view from
// "inside" the closure.
Some(
self.infcx
.replace_bound_vars_with_fresh_vars(
body.span,
LateBoundRegionConversionTime::FnCall,
&poly_sig,
)
.0,
)
}
LateBoundRegionConversionTime::FnCall,
&poly_sig,
)
.0,
)
}
}
};

debug!(
Expand Down Expand Up @@ -122,7 +123,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
if let Err(terr) = self.eq_opaque_type_and_type(
mir_output_ty,
normalized_output_ty,
self.mir_def_id,
mir_def_id,
Locations::All(output_span),
ConstraintCategory::BoringNoLocation,
) {
Expand All @@ -145,7 +146,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
if let Err(err) = self.eq_opaque_type_and_type(
mir_output_ty,
user_provided_output_ty,
self.mir_def_id,
mir_def_id,
Locations::All(output_span),
ConstraintCategory::BoringNoLocation,
) {
Expand Down
34 changes: 7 additions & 27 deletions compiler/rustc_mir/src/borrow_check/type_check/mod.rs
Expand Up @@ -73,7 +73,7 @@ macro_rules! span_mirbug {
$context.last_span,
&format!(
"broken MIR in {:?} ({:?}): {}",
$context.mir_def_id,
$context.body.source.def_id(),
$elem,
format_args!($($message)*),
),
Expand Down Expand Up @@ -113,7 +113,6 @@ mod relate_tys;
/// - `param_env` -- parameter environment to use for trait solving
/// - `body` -- MIR body to type-check
/// - `promoted` -- map of promoted constants within `body`
/// - `mir_def_id` -- `LocalDefId` from which the MIR is derived
/// - `universal_regions` -- the universal regions from `body`s function signature
/// - `location_table` -- MIR location map of `body`
/// - `borrow_set` -- information about borrows occurring in `body`
Expand All @@ -126,7 +125,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
param_env: ty::ParamEnv<'tcx>,
body: &Body<'tcx>,
promoted: &IndexVec<Promoted, Body<'tcx>>,
mir_def_id: LocalDefId,
universal_regions: &Rc<UniversalRegions<'tcx>>,
location_table: &LocationTable,
borrow_set: &BorrowSet<'tcx>,
Expand Down Expand Up @@ -170,7 +168,6 @@ pub(crate) fn type_check<'mir, 'tcx>(

let opaque_type_values = type_check_internal(
infcx,
mir_def_id,
param_env,
body,
promoted,
Expand All @@ -192,7 +189,6 @@ pub(crate) fn type_check<'mir, 'tcx>(

fn type_check_internal<'a, 'tcx, R>(
infcx: &'a InferCtxt<'a, 'tcx>,
mir_def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
body: &'a Body<'tcx>,
promoted: &'a IndexVec<Promoted, Body<'tcx>>,
Expand All @@ -205,7 +201,6 @@ fn type_check_internal<'a, 'tcx, R>(
let mut checker = TypeChecker::new(
infcx,
body,
mir_def_id,
param_env,
region_bound_pairs,
implicit_region_bound,
Expand Down Expand Up @@ -272,7 +267,6 @@ struct TypeVerifier<'a, 'b, 'tcx> {
body: &'b Body<'tcx>,
promoted: &'b IndexVec<Promoted, Body<'tcx>>,
last_span: Span,
mir_def_id: LocalDefId,
errors_reported: bool,
}

Expand Down Expand Up @@ -460,14 +454,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
body: &'b Body<'tcx>,
promoted: &'b IndexVec<Promoted, Body<'tcx>>,
) -> Self {
TypeVerifier {
body,
promoted,
mir_def_id: cx.mir_def_id,
cx,
last_span: body.span,
errors_reported: false,
}
TypeVerifier { body, promoted, cx, last_span: body.span, errors_reported: false }
}

fn tcx(&self) -> TyCtxt<'tcx> {
Expand Down Expand Up @@ -816,7 +803,6 @@ struct TypeChecker<'a, 'tcx> {
/// User type annotations are shared between the main MIR and the MIR of
/// all of the promoted items.
user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
mir_def_id: LocalDefId,
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
implicit_region_bound: ty::Region<'tcx>,
reported_errors: FxHashSet<(Ty<'tcx>, Span)>,
Expand Down Expand Up @@ -965,7 +951,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
fn new(
infcx: &'a InferCtxt<'a, 'tcx>,
body: &'a Body<'tcx>,
mir_def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
implicit_region_bound: ty::Region<'tcx>,
Expand All @@ -975,7 +960,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let mut checker = Self {
infcx,
last_span: DUMMY_SP,
mir_def_id,
body,
user_type_annotations: &body.user_type_annotations,
param_env,
Expand Down Expand Up @@ -1145,7 +1129,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// the resulting inferend values are stored with the
// def-id of the base function.
let parent_def_id =
self.tcx().closure_base_def_id(self.mir_def_id.to_def_id()).expect_local();
self.tcx().closure_base_def_id(self.body.source.def_id()).expect_local();
return self.eq_opaque_type_and_type(sub, sup, parent_def_id, locations, category);
} else {
return Err(terr);
Expand Down Expand Up @@ -1242,7 +1226,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let concrete_opaque_types = &tcx.typeck(anon_owner_def_id).concrete_opaque_types;
let mut opaque_type_values = Vec::new();

debug!("eq_opaque_type_and_type: mir_def_id={:?}", self.mir_def_id);
debug!("eq_opaque_type_and_type: mir_def_id={:?}", body.source.def_id());
let opaque_type_map = self.fully_perform_op(
locations,
category,
Expand Down Expand Up @@ -2001,12 +1985,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
let span = body.source_info(location).span;
let ty = operand.ty(body, tcx);
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
let ccx = ConstCx::new_with_param_env(
tcx,
self.mir_def_id,
body,
self.param_env,
);
let ccx = ConstCx::new_with_param_env(tcx, body, self.param_env);
// To determine if `const_in_array_repeat_expressions` feature gate should
// be mentioned, need to check if the rvalue is promotable.
let should_suggest =
Expand All @@ -2015,11 +1994,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);
debug!("check_rvalue: should_suggest={:?}", should_suggest);

let def_id = body.source.def_id().expect_local();
self.infcx.report_selection_error(
&traits::Obligation::new(
ObligationCause::new(
span,
self.tcx().hir().local_def_id_to_hir_id(self.mir_def_id),
self.tcx().hir().local_def_id_to_hir_id(def_id),
traits::ObligationCauseCode::RepeatVec(should_suggest),
),
self.param_env,
Expand Down

0 comments on commit e72e43c

Please sign in to comment.