Skip to content

Commit

Permalink
rustc: push LocalDefId and/or assert_local calls farther back.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Nov 8, 2019
1 parent 9c1f88e commit b3c2aeb
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 92 deletions.
12 changes: 6 additions & 6 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use self::ConsumeMode::*;
use self::OverloadedCallType::*;

use crate::hir::def::Res;
use crate::hir::def_id::DefId;
use crate::hir::def_id::{DefId, LocalDefId};
use crate::hir::ptr::P;
use crate::infer::InferCtxt;
use crate::middle::mem_categorization as mc;
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
pub fn new(
delegate: &'a mut (dyn Delegate<'tcx> + 'a),
tcx: TyCtxt<'tcx>,
body_owner: DefId,
body_owner: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
region_scope_tree: &'a region::ScopeTree,
tables: &'a ty::TypeckTables<'tcx>,
Expand All @@ -140,7 +140,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
pub fn with_infer(
delegate: &'a mut (dyn Delegate<'tcx> + 'a),
infcx: &'a InferCtxt<'a, 'tcx>,
body_owner: DefId,
body_owner: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
region_scope_tree: &'a region::ScopeTree,
tables: &'a ty::TypeckTables<'tcx>,
Expand Down Expand Up @@ -617,12 +617,12 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
fn walk_captures(&mut self, closure_expr: &hir::Expr, fn_decl_span: Span) {
debug!("walk_captures({:?})", closure_expr);

let closure_def_id = self.tcx().hir().local_def_id(closure_expr.hir_id);
if let Some(upvars) = self.tcx().upvars(closure_def_id) {
let closure_def_id = self.tcx().hir().local_def_id(closure_expr.hir_id).assert_local();
if let Some(upvars) = self.tcx().upvars(closure_def_id.to_def_id()) {
for &var_id in upvars.keys() {
let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath { hir_id: var_id },
closure_expr_id: closure_def_id.assert_local(),
closure_expr_id: closure_def_id,
};
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.hir_id,
Expand Down
18 changes: 8 additions & 10 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub use self::Note::*;
use self::Aliasability::*;

use crate::middle::region;
use crate::hir::def_id::DefId;
use crate::hir::def_id::{DefId, LocalDefId};
use crate::hir::Node;
use crate::infer::InferCtxt;
use crate::hir::def::{CtorOf, Res, DefKind, CtorKind};
Expand Down Expand Up @@ -214,7 +214,7 @@ impl HirNode for hir::Pat {
pub struct MemCategorizationContext<'a, 'tcx> {
pub tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
pub body_owner: DefId,
pub body_owner: LocalDefId,
pub upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
pub region_scope_tree: &'a region::ScopeTree,
pub tables: &'a ty::TypeckTables<'tcx>,
Expand Down Expand Up @@ -330,14 +330,14 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
pub fn new(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
body_owner: DefId,
body_owner: LocalDefId,
region_scope_tree: &'a region::ScopeTree,
tables: &'a ty::TypeckTables<'tcx>,
) -> MemCategorizationContext<'a, 'tcx> {
MemCategorizationContext {
tcx,
body_owner,
upvars: tcx.upvars(body_owner),
upvars: tcx.upvars(body_owner.to_def_id()),
region_scope_tree,
tables,
infcx: None,
Expand All @@ -359,7 +359,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
pub fn with_infer(
infcx: &'a InferCtxt<'a, 'tcx>,
param_env: ty::ParamEnv<'tcx>,
body_owner: DefId,
body_owner: LocalDefId,
region_scope_tree: &'a region::ScopeTree,
tables: &'a ty::TypeckTables<'tcx>,
) -> MemCategorizationContext<'a, 'tcx> {
Expand All @@ -368,7 +368,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
MemCategorizationContext {
tcx,
body_owner,
upvars: tcx.upvars(body_owner),
upvars: tcx.upvars(body_owner.to_def_id()),
region_scope_tree,
tables,
infcx: Some(infcx),
Expand Down Expand Up @@ -722,9 +722,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
// FnOnce | copied | upvar -> &'up bk

let closure_expr_def_id = self.body_owner;
let fn_hir_id = self.tcx.hir().local_def_id_to_hir_id(
closure_expr_def_id.assert_local(),
);
let fn_hir_id = self.tcx.hir().local_def_id_to_hir_id(closure_expr_def_id);
let ty = self.node_ty(fn_hir_id)?;
let kind = match ty.kind {
ty::Generator(..) => ty::ClosureKind::FnOnce,
Expand All @@ -747,7 +745,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {

let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath { hir_id: var_id },
closure_expr_id: closure_expr_def_id.assert_local(),
closure_expr_id: closure_expr_def_id,
};

let var_ty = self.node_ty(var_id)?;
Expand Down
5 changes: 5 additions & 0 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ rustc_queries! {
desc { "type-checking all item bodies" }
}

// FIXME(eddyb) take `LocalDefId` instead of `DefId` as key.
query typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> {
cache_on_disk_if { key.is_local() }
load_cached(tcx, id) {
Expand All @@ -387,6 +388,7 @@ rustc_queries! {
}

TypeChecking {
// FIXME(eddyb) take `LocalDefId` instead of `DefId` as key.
query has_typeck_tables(_: DefId) -> bool {}

query coherent_trait(def_id: DefId) -> () {
Expand All @@ -397,6 +399,7 @@ rustc_queries! {
BorrowChecking {
/// Borrow-checks the function body. If this is a closure, returns
/// additional requirements that the closure's creator must verify.
// FIXME(eddyb) take `LocalDefId` instead of `DefId` as key.
query mir_borrowck(key: DefId) -> mir::BorrowCheckResult<'tcx> {
cache_on_disk_if(tcx, _) { key.is_local() && tcx.is_closure(key) }
}
Expand Down Expand Up @@ -489,6 +492,7 @@ rustc_queries! {

/// Per-body `region::ScopeTree`. The `DefId` should be the owner `DefId` for the body;
/// in the case of closures, this will be redirected to the enclosing function.
// FIXME(eddyb) take `LocalDefId` instead of `DefId` as key.
query region_scope_tree(_: DefId) -> &'tcx region::ScopeTree {}

query mir_shims(key: ty::InstanceDef<'tcx>) -> &'tcx mir::Body<'tcx> {
Expand Down Expand Up @@ -879,6 +883,7 @@ rustc_queries! {
desc { "generating a postorder list of CrateNums" }
}

// FIXME(eddyb) take `LocalDefId` instead of `DefId` as key.
query upvars(_: DefId) -> Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>> {
eval_always
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ pub struct UpvarBorrow<'tcx> {
pub region: ty::Region<'tcx>,
}

pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>;
pub type UpvarListMap = FxHashMap<LocalDefId, FxIndexMap<hir::HirId, UpvarId>>;
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;

#[derive(Copy, Clone)]
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ fn do_mir_borrowck<'a, 'tcx>(

// Gather the upvars of a closure, if any.
let tables = tcx.typeck_tables_of(def_id);
// FIXME(eddyb) move this `assert_local` call further back,
// replacing the `DefId` type (of `def_id`) with `LocalDefId`.
let upvars: Vec<_> = tables
.upvar_list
.get(&def_id)
.get(&def_id.assert_local())
.into_iter()
.flat_map(|v| v.values())
.map(|upvar_id| {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/borrow_check/nll/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::borrow_check::nll::{
};
use crate::borrow_check::Upvar;

use rustc::hir::def_id::DefId;
use rustc::hir::def_id::{DefId, LocalDefId};
use rustc::infer::canonical::QueryOutlivesConstraint;
use rustc::infer::opaque_types;
use rustc::infer::region_constraints::{GenericKind, VarInfos, VerifyBound};
Expand Down Expand Up @@ -1623,7 +1623,7 @@ pub trait ClosureRegionRequirementsExt<'tcx> {
fn apply_requirements(
&self,
tcx: TyCtxt<'tcx>,
closure_def_id: DefId,
closure_def_id: LocalDefId,
closure_substs: SubstsRef<'tcx>,
) -> Vec<QueryOutlivesConstraint<'tcx>>;

Expand Down Expand Up @@ -1653,7 +1653,7 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
fn apply_requirements(
&self,
tcx: TyCtxt<'tcx>,
closure_def_id: DefId,
closure_def_id: LocalDefId,
closure_substs: SubstsRef<'tcx>,
) -> Vec<QueryOutlivesConstraint<'tcx>> {
debug!(
Expand All @@ -1668,7 +1668,7 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
tcx,
closure_substs,
self.num_external_vids,
tcx.closure_base_def_id(closure_def_id),
tcx.closure_base_def_id(closure_def_id.to_def_id()).assert_local(),
);
debug!("apply_requirements: closure_mapping={:?}", closure_mapping);

Expand Down
11 changes: 6 additions & 5 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::dataflow::FlowAtLocation;
use crate::dataflow::MaybeInitializedPlaces;
use either::Either;
use rustc::hir;
use rustc::hir::def_id::DefId;
use rustc::hir::def_id::{DefId, LocalDefId};
use rustc::infer::canonical::QueryRegionConstraints;
use rustc::infer::outlives::env::RegionBoundPairs;
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin};
Expand Down Expand Up @@ -2543,7 +2543,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// clauses on the struct.
AggregateKind::Closure(def_id, substs)
| AggregateKind::Generator(def_id, substs, _) => {
self.prove_closure_bounds(tcx, *def_id, substs, location)
self.prove_closure_bounds(tcx, def_id.assert_local(), substs, location)
}

AggregateKind::Array(_) | AggregateKind::Tuple => ty::InstantiatedPredicates::empty(),
Expand All @@ -2558,11 +2558,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
fn prove_closure_bounds(
&mut self,
tcx: TyCtxt<'tcx>,
def_id: DefId,
def_id: LocalDefId,
substs: SubstsRef<'tcx>,
location: Location,
) -> ty::InstantiatedPredicates<'tcx> {
if let Some(closure_region_requirements) = tcx.mir_borrowck(def_id).closure_requirements {
let borrowck_results = tcx.mir_borrowck(def_id.to_def_id());
if let Some(closure_region_requirements) = borrowck_results.closure_requirements {
let closure_constraints = QueryRegionConstraints {
outlives: closure_region_requirements.apply_requirements(tcx, def_id, substs),

Expand Down Expand Up @@ -2621,7 +2622,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);
}

tcx.predicates_of(def_id).instantiate(tcx, substs)
tcx.predicates_of(def_id.to_def_id()).instantiate(tcx, substs)
}

fn prove_trait_ref(
Expand Down
18 changes: 9 additions & 9 deletions src/librustc_mir/borrow_check/nll/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! just returns them for other code to use.
use either::Either;
use rustc::hir::def_id::DefId;
use rustc::hir::def_id::{DefId, LocalDefId};
use rustc::hir::{self, BodyOwnerKind, HirId};
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
use rustc::middle::lang_items;
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'tcx> UniversalRegions<'tcx> {
tcx: TyCtxt<'tcx>,
closure_substs: SubstsRef<'tcx>,
expected_num_vars: usize,
closure_base_def_id: DefId,
closure_base_def_id: LocalDefId,
) -> IndexVec<RegionVid, ty::Region<'tcx>> {
let mut region_mapping = IndexVec::with_capacity(expected_num_vars);
region_mapping.push(tcx.lifetimes.re_static);
Expand Down Expand Up @@ -322,7 +322,7 @@ impl<'tcx> UniversalRegions<'tcx> {
// tests, and the resulting print-outs include def-ids
// and other things that are not stable across tests!
// So we just include the region-vid. Annoying.
let closure_base_def_id = tcx.closure_base_def_id(def_id);
let closure_base_def_id = tcx.closure_base_def_id(def_id).assert_local();
for_each_late_bound_region_defined_on(tcx, closure_base_def_id, |r| {
err.note(&format!(
"late-bound region is {:?}",
Expand All @@ -340,7 +340,7 @@ impl<'tcx> UniversalRegions<'tcx> {
// FIXME: As above, we'd like to print out the region
// `r` but doing so is not stable across architectures
// and so forth.
let closure_base_def_id = tcx.closure_base_def_id(def_id);
let closure_base_def_id = tcx.closure_base_def_id(def_id).assert_local();
for_each_late_bound_region_defined_on(tcx, closure_base_def_id, |r| {
err.note(&format!(
"late-bound region is {:?}",
Expand Down Expand Up @@ -725,7 +725,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
"replace_late_bound_regions_with_nll_infer_vars(mir_def_id={:?})",
mir_def_id
);
let closure_base_def_id = self.tcx.closure_base_def_id(mir_def_id);
let closure_base_def_id = self.tcx.closure_base_def_id(mir_def_id).assert_local();
for_each_late_bound_region_defined_on(self.tcx, closure_base_def_id, |r| {
debug!("replace_late_bound_regions_with_nll_infer_vars: r={:?}", r);
if !indices.indices.contains_key(&r) {
Expand Down Expand Up @@ -781,19 +781,19 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
/// invokes `f` with the liberated form of each one.
fn for_each_late_bound_region_defined_on<'tcx>(
tcx: TyCtxt<'tcx>,
fn_def_id: DefId,
fn_def_id: LocalDefId,
mut f: impl FnMut(ty::Region<'tcx>),
) {
if let Some(late_bounds) = tcx.is_late_bound_map(fn_def_id.assert_local()) {
if let Some(late_bounds) = tcx.is_late_bound_map(fn_def_id) {
for late_bound in late_bounds.iter() {
let hir_id = HirId {
owner: fn_def_id.assert_local(),
owner: fn_def_id,
local_id: *late_bound,
};
let name = tcx.hir().name(hir_id);
let region_def_id = tcx.hir().local_def_id(hir_id);
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {
scope: fn_def_id,
scope: fn_def_id.to_def_id(),
bound_region: ty::BoundRegion::BrNamed(region_def_id, name),
}));
f(liberated_region);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ where
let span = tcx_hir.span(fn_id);

let hir_tables = hir.tables();
let fn_def_id = tcx_hir.local_def_id(fn_id);
let fn_def_id = tcx_hir.local_def_id(fn_id).assert_local();

// Gather the upvars of a closure, if any.
let mut upvar_mutbls = vec![];
Expand Down Expand Up @@ -613,7 +613,7 @@ where
let source_info = builder.source_info(span);
let call_site_s = (call_site_scope, source_info);
unpack!(block = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
if should_abort_on_panic(tcx, fn_def_id, abi) {
if should_abort_on_panic(tcx, fn_def_id.to_def_id(), abi) {
builder.schedule_abort();
}

Expand Down Expand Up @@ -653,7 +653,7 @@ where
spread_arg = Some(Local::new(arguments.len()));
}
info!("fn_id {:?} has attrs {:?}", fn_def_id,
tcx.get_attrs(fn_def_id));
tcx.get_attrs(fn_def_id.to_def_id()));

let mut body = builder.finish(yield_ty);
body.spread_arg = spread_arg;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ fn convert_var(
let closure_def_id = cx.body_owner;
let upvar_id = ty::UpvarId {
var_path: ty::UpvarPath {hir_id: var_hir_id},
closure_expr_id: closure_def_id.assert_local(),
closure_expr_id: closure_def_id,
};
let var_ty = cx.tables().node_type(var_hir_id);

Expand All @@ -996,13 +996,13 @@ fn convert_var(
// signature will be &self or &mut self and hence will
// have a bound region with number 0
let region = ty::ReFree(ty::FreeRegion {
scope: closure_def_id,
scope: closure_def_id.to_def_id(),
bound_region: ty::BoundRegion::BrAnon(0),
});
let region = cx.tcx.mk_region(region);

let self_expr = if let ty::Closure(_, closure_substs) = closure_ty.kind {
match cx.infcx.closure_kind(closure_def_id, closure_substs).unwrap() {
match cx.infcx.closure_kind(closure_def_id.to_def_id(), closure_substs).unwrap() {
ty::ClosureKind::Fn => {
let ref_closure_ty = cx.tcx.mk_ref(region,
ty::TypeAndMut {
Expand Down
Loading

0 comments on commit b3c2aeb

Please sign in to comment.