Skip to content

Commit

Permalink
Auto merge of #71215 - marmeladema:issue70853/librustc_middle-local-d…
Browse files Browse the repository at this point in the history
…ef-id-2, r=eddyb

Simplify `local_def_id` and `as_local_hir_id`

See #70853
  • Loading branch information
bors committed Apr 24, 2020
2 parents 45c7838 + b9ba521 commit 5a59527
Show file tree
Hide file tree
Showing 119 changed files with 1,082 additions and 987 deletions.
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value
if cx.tcx.sess.opts.share_generics() {
// We are in share_generics mode.

if instance_def_id.is_local() {
if let Some(instance_def_id) = instance_def_id.as_local() {
// This is a definition from the current crate. If the
// definition is unreachable for downstream crates or
// the current crate does not re-export generics, the
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_llvm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ impl CodegenCx<'ll, 'tcx> {

debug!("get_static: sym={} instance={:?}", sym, instance);

let g = if let Some(id) = self.tcx.hir().as_local_hir_id(def_id) {
let g = if let Some(def_id) = def_id.as_local() {
let id = self.tcx.hir().as_local_hir_id(def_id);
let llty = self.layout_of(ty).llvm_type(self);
let (g, attrs) = match self.tcx.hir().get(id) {
Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => {
Expand Down
14 changes: 7 additions & 7 deletions src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn reachable_non_generics_provider(
if !generics.requires_monomorphization(tcx) &&
// Functions marked with #[inline] are only ever codegened
// with "internal" linkage and are never exported.
!Instance::mono(tcx, def_id).def.generates_cgu_internal_copy(tcx)
!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
{
Some(def_id)
} else {
Expand All @@ -109,7 +109,7 @@ fn reachable_non_generics_provider(
})
.map(|def_id| {
let export_level = if special_runtime_crate {
let name = tcx.symbol_name(Instance::mono(tcx, def_id)).name.as_str();
let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name.as_str();
// We can probably do better here by just ensuring that
// it has hidden visibility rather than public
// visibility, as this is primarily here to ensure it's
Expand All @@ -126,14 +126,14 @@ fn reachable_non_generics_provider(
SymbolExportLevel::Rust
}
} else {
symbol_export_level(tcx, def_id)
symbol_export_level(tcx, def_id.to_def_id())
};
debug!(
"EXPORTED SYMBOL (local): {} ({:?})",
tcx.symbol_name(Instance::mono(tcx, def_id)),
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
export_level
);
(def_id, export_level)
(def_id.to_def_id(), export_level)
})
.collect();

Expand Down Expand Up @@ -361,8 +361,8 @@ fn upstream_drop_glue_for_provider<'tcx>(
}

fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
!tcx.reachable_set(LOCAL_CRATE).contains(&hir_id)
if let Some(def_id) = def_id.as_local() {
!tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().as_local_hir_id(def_id))
} else {
bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
}

fn node_path(&self, id: hir::HirId) -> Option<String> {
Some(self.tcx.def_path_str(self.tcx.hir().local_def_id(id)))
Some(self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()))
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,8 @@ impl Definitions {
}

#[inline]
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
if let Some(def_id) = def_id.as_local() {
Some(self.local_def_id_to_hir_id(def_id))
} else {
None
}
pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId {
self.local_def_id_to_hir_id(def_id)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_incremental/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl IfThisChanged<'tcx> {

fn process_attrs(&mut self, hir_id: hir::HirId, attrs: &[ast::Attribute]) {
let def_id = self.tcx.hir().local_def_id(hir_id);
let def_path_hash = self.tcx.def_path_hash(def_id);
let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
for attr in attrs {
if attr.check_name(sym::rustc_if_this_changed) {
let dep_node_interned = self.argument(attr);
Expand All @@ -131,7 +131,7 @@ impl IfThisChanged<'tcx> {
}
},
};
self.if_this_changed.push((attr.span, def_id, dep_node));
self.if_this_changed.push((attr.span, def_id.to_def_id(), dep_node));
} else if attr.check_name(sym::rustc_then_this_would_need) {
let dep_node_interned = self.argument(attr);
let dep_node = match dep_node_interned {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,16 @@ impl DirtyCleanVisitor<'tcx> {

fn check_item(&mut self, item_id: hir::HirId, item_span: Span) {
let def_id = self.tcx.hir().local_def_id(item_id);
for attr in self.tcx.get_attrs(def_id).iter() {
for attr in self.tcx.get_attrs(def_id.to_def_id()).iter() {
let assertion = match self.assertion_maybe(item_id, attr) {
Some(a) => a,
None => continue,
};
self.checked_attrs.insert(attr.id);
for dep_node in self.dep_nodes(&assertion.clean, def_id) {
for dep_node in self.dep_nodes(&assertion.clean, def_id.to_def_id()) {
self.assert_clean(item_span, dep_node);
}
for dep_node in self.dep_nodes(&assertion.dirty, def_id) {
for dep_node in self.dep_nodes(&assertion.dirty, def_id.to_def_id()) {
self.assert_dirty(item_span, dep_node);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_infer/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions(
let sm = tcx.sess.source_map();

let scope = region.free_region_binding_scope(tcx);
let node = tcx.hir().as_local_hir_id(scope).unwrap();
let node = tcx.hir().as_local_hir_id(scope.expect_local());
let tag = match tcx.hir().find(node) {
Some(Node::Block(_) | Node::Expr(_)) => "body",
Some(Node::Item(it)) => item_scope_tag(&it),
Expand Down Expand Up @@ -1782,10 +1782,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if !(generics.has_self && param.index == 0) {
let type_param = generics.type_param(param, self.tcx);
let hir = &self.tcx.hir();
hir.as_local_hir_id(type_param.def_id).map(|id| {
type_param.def_id.as_local().map(|def_id| {
// Get the `hir::Param` to verify whether it already has any bounds.
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
// instead we suggest `T: 'a + 'b` in that case.
let id = hir.as_local_hir_id(def_id);
let mut has_bounds = false;
if let Node::GenericParam(param) = hir.get(id) {
has_bounds = !param.bounds.is_empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
) -> Option<(&hir::Ty<'_>, &hir::FnDecl<'_>)> {
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
let def_id = anon_reg.def_id;
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(def_id) {
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx().hir().as_local_hir_id(def_id);
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
) = (&sub_origin, sup_region)
{
let hir = &self.tcx().hir();
if let Some(hir_id) = hir.as_local_hir_id(free_region.scope) {
if let Some(def_id) = free_region.scope.as_local() {
let hir_id = hir.as_local_hir_id(def_id);
if let Node::Expr(Expr { kind: Closure(_, _, _, closure_span, None), .. }) =
hir.get(hir_id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
};

let hir = &self.tcx().hir();
let hir_id = hir.as_local_hir_id(id)?;
let hir_id = hir.as_local_hir_id(id.as_local()?);
let body_id = hir.maybe_body_owned_by(hir_id)?;
let body = hir.body(body_id);
let owner_id = hir.body_owner(body_id);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_interface/proc_macro_decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn proc_macro_decls_static(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<DefId> {
let mut finder = Finder { decls: None };
tcx.hir().krate().visit_all_item_likes(&mut finder);

finder.decls.map(|id| tcx.hir().local_def_id(id))
finder.decls.map(|id| tcx.hir().local_def_id(id).to_def_id())
}

struct Finder {
Expand Down
16 changes: 9 additions & 7 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
// If the trait is private, add the impl items to `private_traits` so they don't get
// reported for missing docs.
let real_trait = trait_ref.path.res.def_id();
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(real_trait) {
if let Some(def_id) = real_trait.as_local() {
let hir_id = cx.tcx.hir().as_local_hir_id(def_id);
if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) {
if let hir::VisibilityKind::Inherited = item.vis.node {
for impl_item_ref in items {
Expand All @@ -461,7 +462,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
};

let def_id = cx.tcx.hir().local_def_id(it.hir_id);
let (article, desc) = cx.tcx.article_and_description(def_id);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());

self.check_missing_docs_attrs(cx, Some(it.hir_id), &it.attrs, it.span, article, desc);
}
Expand All @@ -472,7 +473,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
}

let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id);
let (article, desc) = cx.tcx.article_and_description(def_id);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());

self.check_missing_docs_attrs(
cx,
Expand All @@ -491,7 +492,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
}

let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
let (article, desc) = cx.tcx.article_and_description(def_id);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
self.check_missing_docs_attrs(
cx,
Some(impl_item.hir_id),
Expand Down Expand Up @@ -609,8 +610,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
let mut impls = HirIdSet::default();
cx.tcx.for_each_impl(debug, |d| {
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() {
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(ty_def.did) {
impls.insert(hir_id);
if let Some(def_id) = ty_def.did.as_local() {
impls.insert(cx.tcx.hir().as_local_hir_id(def_id));
}
}
});
Expand Down Expand Up @@ -1531,7 +1532,8 @@ impl ExplicitOutlivesRequirements {
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
ty_generics: &'tcx ty::Generics,
) -> Vec<ty::Region<'tcx>> {
let index = ty_generics.param_def_id_to_index[&tcx.hir().local_def_id(param.hir_id)];
let index =
ty_generics.param_def_id_to_index[&tcx.hir().local_def_id(param.hir_id).to_def_id()];

match param.kind {
hir::GenericParamKind::Lifetime { .. } => {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_lint/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_ast::ast;
use rustc_ast::walk_list;
use rustc_data_structures::sync::{join, par_iter, ParallelIterator};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit as hir_visit;
use rustc_hir::intravisit::Visitor;
use rustc_middle::hir::map::Map;
Expand Down Expand Up @@ -353,7 +353,7 @@ crate::late_lint_methods!(late_lint_pass_impl, [], ['tcx]);

fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
tcx: TyCtxt<'tcx>,
module_def_id: DefId,
module_def_id: LocalDefId,
pass: T,
) {
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
Expand All @@ -364,7 +364,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
param_env: ty::ParamEnv::empty(),
access_levels,
lint_store: unerased_lint_store(tcx),
last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(),
last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id),
generics: None,
only_module: true,
};
Expand All @@ -382,7 +382,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(

pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
tcx: TyCtxt<'tcx>,
module_def_id: DefId,
module_def_id: LocalDefId,
builtin_lints: T,
) {
if tcx.sess.opts.debugging_opts.no_interleave_lints {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ pub fn provide(providers: &mut Providers<'_>) {
}

fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
late::late_lint_mod(
tcx,
module_def_id.expect_local(),
BuiltinCombinedModuleLateLintPass::new(),
);
}

macro_rules! pre_expansion_lint_passes {
Expand Down
8 changes: 5 additions & 3 deletions src/librustc_metadata/foreign_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
};

let foreign_items =
fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id)).collect();
self.modules
.push(ForeignModule { foreign_items, def_id: self.tcx.hir().local_def_id(it.hir_id) });
fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id).to_def_id()).collect();
self.modules.push(ForeignModule {
foreign_items,
def_id: self.tcx.hir().local_def_id(it.hir_id).to_def_id(),
});
}

fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
name: None,
kind: cstore::NativeUnknown,
cfg: None,
foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id)),
foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id).to_def_id()),
wasm_import_module: None,
};
let mut kind_specified = false;
Expand Down
Loading

0 comments on commit 5a59527

Please sign in to comment.