1
Fork 0

librustc_middle: return LocalDefId instead of DefId in body_owner_def_id

This commit is contained in:
marmeladema 2020-04-08 14:53:06 +01:00
parent 1dc363bce1
commit f62c6e1c76
13 changed files with 30 additions and 32 deletions

View file

@ -59,7 +59,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, struct_span_err}; use rustc_errors::{pluralize, struct_span_err};
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString}; use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::DefId;
use rustc_hir::Node; use rustc_hir::Node;
use rustc_middle::middle::region; use rustc_middle::middle::region;
use rustc_middle::ty::error::TypeError; use rustc_middle::ty::error::TypeError;
@ -1589,16 +1589,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// it's a actual definition. According to the comments (e.g. in // it's a actual definition. According to the comments (e.g. in
// librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter // librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter
// is relied upon by some other code. This might (or might not) need cleanup. // is relied upon by some other code. This might (or might not) need cleanup.
let body_owner_def_id = self let body_owner_def_id =
.tcx self.tcx.hir().opt_local_def_id(cause.body_id).unwrap_or_else(|| {
.hir()
.opt_local_def_id(cause.body_id)
.map(LocalDefId::to_def_id)
.unwrap_or_else(|| {
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id }) self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
}); });
self.check_and_note_conflicting_crates(diag, terr); self.check_and_note_conflicting_crates(diag, terr);
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id); self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id.to_def_id());
// It reads better to have the error origin as the final // It reads better to have the error origin as the final
// thing. // thing.

View file

@ -812,7 +812,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
{ {
sess.time("match_checking", || { sess.time("match_checking", || {
tcx.par_body_owners(|def_id| { tcx.par_body_owners(|def_id| {
tcx.ensure().check_match(def_id); tcx.ensure().check_match(def_id.to_def_id());
}); });
}); });
}, },
@ -834,7 +834,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
}); });
sess.time("MIR_borrow_checking", || { sess.time("MIR_borrow_checking", || {
tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id)); tcx.par_body_owners(|def_id| tcx.ensure().mir_borrowck(def_id.to_def_id()));
}); });
sess.time("dumping_chalk_like_clauses", || { sess.time("dumping_chalk_like_clauses", || {
@ -843,7 +843,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
sess.time("MIR_effect_checking", || { sess.time("MIR_effect_checking", || {
for def_id in tcx.body_owners() { for def_id in tcx.body_owners() {
mir::transform::check_unsafety::check_unsafety(tcx, def_id) mir::transform::check_unsafety::check_unsafety(tcx, def_id.to_def_id())
} }
}); });

View file

@ -1166,7 +1166,7 @@ declare_lint_pass!(
); );
fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) { fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
let def_id = cx.tcx.hir().body_owner_def_id(body_id); let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
// trigger the query once for all constants since that will already report the errors // trigger the query once for all constants since that will already report the errors
// FIXME: Use ensure here // FIXME: Use ensure here
let _ = cx.tcx.const_eval_poly(def_id); let _ = cx.tcx.const_eval_poly(def_id);

View file

@ -370,9 +370,8 @@ impl<'hir> Map<'hir> {
parent parent
} }
// FIXME(eddyb) this function can and should return `LocalDefId`. pub fn body_owner_def_id(&self, id: BodyId) -> LocalDefId {
pub fn body_owner_def_id(&self, id: BodyId) -> DefId { self.local_def_id(self.body_owner(id)).expect_local()
self.local_def_id(self.body_owner(id))
} }
/// Given a `HirId`, returns the `BodyId` associated with it, /// Given a `HirId`, returns the `BodyId` associated with it,

View file

@ -2678,13 +2678,13 @@ pub enum ImplOverlapKind {
impl<'tcx> TyCtxt<'tcx> { impl<'tcx> TyCtxt<'tcx> {
pub fn body_tables(self, body: hir::BodyId) -> &'tcx TypeckTables<'tcx> { pub fn body_tables(self, body: hir::BodyId) -> &'tcx TypeckTables<'tcx> {
self.typeck_tables_of(self.hir().body_owner_def_id(body)) self.typeck_tables_of(self.hir().body_owner_def_id(body).to_def_id())
} }
/// Returns an iterator of the `DefId`s for all body-owners in this /// Returns an iterator of the `DefId`s for all body-owners in this
/// crate. If you would prefer to iterate over the bodies /// crate. If you would prefer to iterate over the bodies
/// themselves, you can do `self.hir().krate().body_ids.iter()`. /// themselves, you can do `self.hir().krate().body_ids.iter()`.
pub fn body_owners(self) -> impl Iterator<Item = DefId> + Captures<'tcx> + 'tcx { pub fn body_owners(self) -> impl Iterator<Item = LocalDefId> + Captures<'tcx> + 'tcx {
self.hir() self.hir()
.krate() .krate()
.body_ids .body_ids
@ -2692,7 +2692,7 @@ impl<'tcx> TyCtxt<'tcx> {
.map(move |&body_id| self.hir().body_owner_def_id(body_id)) .map(move |&body_id| self.hir().body_owner_def_id(body_id))
} }
pub fn par_body_owners<F: Fn(DefId) + sync::Sync + sync::Send>(self, f: F) { pub fn par_body_owners<F: Fn(LocalDefId) + sync::Sync + sync::Send>(self, f: F) {
par_iter(&self.hir().krate().body_ids) par_iter(&self.hir().krate().body_ids)
.for_each(|&body_id| f(self.hir().body_owner_def_id(body_id))); .for_each(|&body_id| f(self.hir().body_owner_def_id(body_id)));
} }

View file

@ -1,7 +1,7 @@
use crate::{shim, util}; use crate::{shim, util};
use rustc_ast::ast; use rustc_ast::ast;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE}; use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::mir::{BodyAndCache, ConstQualifs, MirPhase, Promoted}; use rustc_middle::mir::{BodyAndCache, ConstQualifs, MirPhase, Promoted};
@ -62,7 +62,7 @@ fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &DefIdSet {
let mut set = DefIdSet::default(); let mut set = DefIdSet::default();
// All body-owners have MIR associated with them. // All body-owners have MIR associated with them.
set.extend(tcx.body_owners()); set.extend(tcx.body_owners().map(LocalDefId::to_def_id));
// Additionally, tuple struct/variant constructors have MIR, but // Additionally, tuple struct/variant constructors have MIR, but
// they don't have a BodyId, so we need to build them separately. // they don't have a BodyId, so we need to build them separately.

View file

@ -131,8 +131,8 @@ impl Visitor<'tcx> for ItemVisitor<'tcx> {
fn visit_nested_body(&mut self, body_id: hir::BodyId) { fn visit_nested_body(&mut self, body_id: hir::BodyId) {
let owner_def_id = self.tcx.hir().body_owner_def_id(body_id); let owner_def_id = self.tcx.hir().body_owner_def_id(body_id);
let body = self.tcx.hir().body(body_id); let body = self.tcx.hir().body(body_id);
let param_env = self.tcx.param_env(owner_def_id); let param_env = self.tcx.param_env(owner_def_id.to_def_id());
let tables = self.tcx.typeck_tables_of(owner_def_id); let tables = self.tcx.typeck_tables_of(owner_def_id.to_def_id());
ExprVisitor { tcx: self.tcx, param_env, tables }.visit_body(body); ExprVisitor { tcx: self.tcx, param_env, tables }.visit_body(body);
self.visit_body(body); self.visit_body(body);
} }

View file

@ -14,7 +14,7 @@ use crate::infer::{self, InferCtxt, TyCtxtInferExt};
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder}; use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::{Node, QPath, TyKind, WhereBoundPredicate, WherePredicate}; use rustc_hir::{Node, QPath, TyKind, WhereBoundPredicate, WherePredicate};
use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::error::ExpectedFound;
@ -354,12 +354,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let enclosing_scope_span = tcx.def_span( let enclosing_scope_span = tcx.def_span(
tcx.hir() tcx.hir()
.opt_local_def_id(obligation.cause.body_id) .opt_local_def_id(obligation.cause.body_id)
.map(LocalDefId::to_def_id)
.unwrap_or_else(|| { .unwrap_or_else(|| {
tcx.hir().body_owner_def_id(hir::BodyId { tcx.hir().body_owner_def_id(hir::BodyId {
hir_id: obligation.cause.body_id, hir_id: obligation.cause.body_id,
}) })
}), })
.to_def_id(),
); );
err.span_label(enclosing_scope_span, s.as_str()); err.span_label(enclosing_scope_span, s.as_str());

View file

@ -751,7 +751,7 @@ fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: DefId) {
fn typeck_item_bodies(tcx: TyCtxt<'_>, crate_num: CrateNum) { fn typeck_item_bodies(tcx: TyCtxt<'_>, crate_num: CrateNum) {
debug_assert!(crate_num == LOCAL_CRATE); debug_assert!(crate_num == LOCAL_CRATE);
tcx.par_body_owners(|body_owner_def_id| { tcx.par_body_owners(|body_owner_def_id| {
tcx.ensure().typeck_tables_of(body_owner_def_id); tcx.ensure().typeck_tables_of(body_owner_def_id.to_def_id());
}); });
} }

View file

@ -109,7 +109,7 @@ macro_rules! ignore_err {
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn regionck_expr(&self, body: &'tcx hir::Body<'tcx>) { pub fn regionck_expr(&self, body: &'tcx hir::Body<'tcx>) {
let subject = self.tcx.hir().body_owner_def_id(body.id()); let subject = self.tcx.hir().body_owner_def_id(body.id()).to_def_id();
let id = body.value.hir_id; let id = body.value.hir_id;
let mut rcx = let mut rcx =
RegionCtxt::new(self, RepeatingScope(id), id, Subject(subject), self.param_env); RegionCtxt::new(self, RepeatingScope(id), id, Subject(subject), self.param_env);
@ -154,7 +154,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// constraints to add. /// constraints to add.
pub fn regionck_fn(&self, fn_id: hir::HirId, body: &'tcx hir::Body<'tcx>) { pub fn regionck_fn(&self, fn_id: hir::HirId, body: &'tcx hir::Body<'tcx>) {
debug!("regionck_fn(id={})", fn_id); debug!("regionck_fn(id={})", fn_id);
let subject = self.tcx.hir().body_owner_def_id(body.id()); let subject = self.tcx.hir().body_owner_def_id(body.id()).to_def_id();
let hir_id = body.value.hir_id; let hir_id = body.value.hir_id;
let mut rcx = let mut rcx =
RegionCtxt::new(self, RepeatingScope(hir_id), hir_id, Subject(subject), self.param_env); RegionCtxt::new(self, RepeatingScope(hir_id), hir_id, Subject(subject), self.param_env);
@ -290,7 +290,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
let body_id = body.id(); let body_id = body.id();
self.body_id = body_id.hir_id; self.body_id = body_id.hir_id;
self.body_owner = self.tcx.hir().body_owner_def_id(body_id); self.body_owner = self.tcx.hir().body_owner_def_id(body_id).to_def_id();
let call_site = let call_site =
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite }; region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite };

View file

@ -146,7 +146,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
} }
let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id()); let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id()).to_def_id();
assert_eq!(body_owner_def_id, closure_def_id); assert_eq!(body_owner_def_id, closure_def_id);
let mut delegate = InferBorrowKind { let mut delegate = InferBorrowKind {
fcx: self, fcx: self,

View file

@ -12,7 +12,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
let mut used_trait_imports = DefIdSet::default(); let mut used_trait_imports = DefIdSet::default();
for &body_id in tcx.hir().krate().bodies.keys() { for &body_id in tcx.hir().krate().bodies.keys() {
let item_def_id = tcx.hir().body_owner_def_id(body_id); let item_def_id = tcx.hir().body_owner_def_id(body_id);
let imports = tcx.used_trait_imports(item_def_id); let imports = tcx.used_trait_imports(item_def_id.to_def_id());
debug!("GatherVisitor: item_def_id={:?} with imports {:#?}", item_def_id, imports); debug!("GatherVisitor: item_def_id={:?} with imports {:#?}", item_def_id, imports);
used_trait_imports.extend(imports.iter()); used_trait_imports.extend(imports.iter());
} }

View file

@ -419,7 +419,10 @@ impl Clean<Lifetime> for hir::GenericParam<'_> {
impl Clean<Constant> for hir::ConstArg { impl Clean<Constant> for hir::ConstArg {
fn clean(&self, cx: &DocContext<'_>) -> Constant { fn clean(&self, cx: &DocContext<'_>) -> Constant {
Constant { Constant {
type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx), type_: cx
.tcx
.type_of(cx.tcx.hir().body_owner_def_id(self.value.body).to_def_id())
.clean(cx),
expr: print_const_expr(cx, self.value.body), expr: print_const_expr(cx, self.value.body),
value: None, value: None,
is_literal: is_literal_expr(cx, self.value.body.hir_id), is_literal: is_literal_expr(cx, self.value.body.hir_id),