Skip to content

Commit 5a59527

Browse files
committed
Auto merge of rust-lang#71215 - marmeladema:issue70853/librustc_middle-local-def-id-2, r=eddyb
Simplify `local_def_id` and `as_local_hir_id` See rust-lang#70853
2 parents 45c7838 + b9ba521 commit 5a59527

File tree

119 files changed

+1082
-987
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1082
-987
lines changed

src/librustc_codegen_llvm/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value
116116
if cx.tcx.sess.opts.share_generics() {
117117
// We are in share_generics mode.
118118

119-
if instance_def_id.is_local() {
119+
if let Some(instance_def_id) = instance_def_id.as_local() {
120120
// This is a definition from the current crate. If the
121121
// definition is unreachable for downstream crates or
122122
// the current crate does not re-export generics, the

src/librustc_codegen_llvm/consts.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ impl CodegenCx<'ll, 'tcx> {
209209

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

212-
let g = if let Some(id) = self.tcx.hir().as_local_hir_id(def_id) {
212+
let g = if let Some(def_id) = def_id.as_local() {
213+
let id = self.tcx.hir().as_local_hir_id(def_id);
213214
let llty = self.layout_of(ty).llvm_type(self);
214215
let (g, attrs) = match self.tcx.hir().get(id) {
215216
Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => {

src/librustc_codegen_ssa/back/symbol_export.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn reachable_non_generics_provider(
9696
if !generics.requires_monomorphization(tcx) &&
9797
// Functions marked with #[inline] are only ever codegened
9898
// with "internal" linkage and are never exported.
99-
!Instance::mono(tcx, def_id).def.generates_cgu_internal_copy(tcx)
99+
!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
100100
{
101101
Some(def_id)
102102
} else {
@@ -109,7 +109,7 @@ fn reachable_non_generics_provider(
109109
})
110110
.map(|def_id| {
111111
let export_level = if special_runtime_crate {
112-
let name = tcx.symbol_name(Instance::mono(tcx, def_id)).name.as_str();
112+
let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name.as_str();
113113
// We can probably do better here by just ensuring that
114114
// it has hidden visibility rather than public
115115
// visibility, as this is primarily here to ensure it's
@@ -126,14 +126,14 @@ fn reachable_non_generics_provider(
126126
SymbolExportLevel::Rust
127127
}
128128
} else {
129-
symbol_export_level(tcx, def_id)
129+
symbol_export_level(tcx, def_id.to_def_id())
130130
};
131131
debug!(
132132
"EXPORTED SYMBOL (local): {} ({:?})",
133-
tcx.symbol_name(Instance::mono(tcx, def_id)),
133+
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
134134
export_level
135135
);
136-
(def_id, export_level)
136+
(def_id.to_def_id(), export_level)
137137
})
138138
.collect();
139139

@@ -361,8 +361,8 @@ fn upstream_drop_glue_for_provider<'tcx>(
361361
}
362362

363363
fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
364-
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
365-
!tcx.reachable_set(LOCAL_CRATE).contains(&hir_id)
364+
if let Some(def_id) = def_id.as_local() {
365+
!tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().as_local_hir_id(def_id))
366366
} else {
367367
bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id)
368368
}

src/librustc_driver/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
322322
}
323323

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

src/librustc_hir/definitions.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,8 @@ impl Definitions {
342342
}
343343

344344
#[inline]
345-
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<hir::HirId> {
346-
if let Some(def_id) = def_id.as_local() {
347-
Some(self.local_def_id_to_hir_id(def_id))
348-
} else {
349-
None
350-
}
345+
pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId {
346+
self.local_def_id_to_hir_id(def_id)
351347
}
352348

353349
#[inline]

src/librustc_incremental/assert_dep_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl IfThisChanged<'tcx> {
115115

116116
fn process_attrs(&mut self, hir_id: hir::HirId, attrs: &[ast::Attribute]) {
117117
let def_id = self.tcx.hir().local_def_id(hir_id);
118-
let def_path_hash = self.tcx.def_path_hash(def_id);
118+
let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
119119
for attr in attrs {
120120
if attr.check_name(sym::rustc_if_this_changed) {
121121
let dep_node_interned = self.argument(attr);
@@ -131,7 +131,7 @@ impl IfThisChanged<'tcx> {
131131
}
132132
},
133133
};
134-
self.if_this_changed.push((attr.span, def_id, dep_node));
134+
self.if_this_changed.push((attr.span, def_id.to_def_id(), dep_node));
135135
} else if attr.check_name(sym::rustc_then_this_would_need) {
136136
let dep_node_interned = self.argument(attr);
137137
let dep_node = match dep_node_interned {

src/librustc_incremental/persist/dirty_clean.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,16 @@ impl DirtyCleanVisitor<'tcx> {
434434

435435
fn check_item(&mut self, item_id: hir::HirId, item_span: Span) {
436436
let def_id = self.tcx.hir().local_def_id(item_id);
437-
for attr in self.tcx.get_attrs(def_id).iter() {
437+
for attr in self.tcx.get_attrs(def_id.to_def_id()).iter() {
438438
let assertion = match self.assertion_maybe(item_id, attr) {
439439
Some(a) => a,
440440
None => continue,
441441
};
442442
self.checked_attrs.insert(attr.id);
443-
for dep_node in self.dep_nodes(&assertion.clean, def_id) {
443+
for dep_node in self.dep_nodes(&assertion.clean, def_id.to_def_id()) {
444444
self.assert_clean(item_span, dep_node);
445445
}
446-
for dep_node in self.dep_nodes(&assertion.dirty, def_id) {
446+
for dep_node in self.dep_nodes(&assertion.dirty, def_id.to_def_id()) {
447447
self.assert_dirty(item_span, dep_node);
448448
}
449449
}

src/librustc_infer/infer/error_reporting/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn msg_span_from_early_bound_and_free_regions(
191191
let sm = tcx.sess.source_map();
192192

193193
let scope = region.free_region_binding_scope(tcx);
194-
let node = tcx.hir().as_local_hir_id(scope).unwrap();
194+
let node = tcx.hir().as_local_hir_id(scope.expect_local());
195195
let tag = match tcx.hir().find(node) {
196196
Some(Node::Block(_) | Node::Expr(_)) => "body",
197197
Some(Node::Item(it)) => item_scope_tag(&it),
@@ -1782,10 +1782,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17821782
if !(generics.has_self && param.index == 0) {
17831783
let type_param = generics.type_param(param, self.tcx);
17841784
let hir = &self.tcx.hir();
1785-
hir.as_local_hir_id(type_param.def_id).map(|id| {
1785+
type_param.def_id.as_local().map(|def_id| {
17861786
// Get the `hir::Param` to verify whether it already has any bounds.
17871787
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
17881788
// instead we suggest `T: 'a + 'b` in that case.
1789+
let id = hir.as_local_hir_id(def_id);
17891790
let mut has_bounds = false;
17901791
if let Node::GenericParam(param) = hir.get(id) {
17911792
has_bounds = !param.bounds.is_empty();

src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2929
) -> Option<(&hir::Ty<'_>, &hir::FnDecl<'_>)> {
3030
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
3131
let def_id = anon_reg.def_id;
32-
if let Some(hir_id) = self.tcx().hir().as_local_hir_id(def_id) {
32+
if let Some(def_id) = def_id.as_local() {
33+
let hir_id = self.tcx().hir().as_local_hir_id(def_id);
3334
let fndecl = match self.tcx().hir().get(hir_id) {
3435
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
3536
| Node::TraitItem(&hir::TraitItem {

src/librustc_infer/infer/error_reporting/nice_region_error/outlives_closure.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4646
) = (&sub_origin, sup_region)
4747
{
4848
let hir = &self.tcx().hir();
49-
if let Some(hir_id) = hir.as_local_hir_id(free_region.scope) {
49+
if let Some(def_id) = free_region.scope.as_local() {
50+
let hir_id = hir.as_local_hir_id(def_id);
5051
if let Node::Expr(Expr { kind: Closure(_, _, _, closure_span, None), .. }) =
5152
hir.get(hir_id)
5253
{

src/librustc_infer/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5151
};
5252

5353
let hir = &self.tcx().hir();
54-
let hir_id = hir.as_local_hir_id(id)?;
54+
let hir_id = hir.as_local_hir_id(id.as_local()?);
5555
let body_id = hir.maybe_body_owned_by(hir_id)?;
5656
let body = hir.body(body_id);
5757
let owner_id = hir.body_owner(body_id);

src/librustc_interface/proc_macro_decls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn proc_macro_decls_static(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<DefId> {
1616
let mut finder = Finder { decls: None };
1717
tcx.hir().krate().visit_all_item_likes(&mut finder);
1818

19-
finder.decls.map(|id| tcx.hir().local_def_id(id))
19+
finder.decls.map(|id| tcx.hir().local_def_id(id).to_def_id())
2020
}
2121

2222
struct Finder {

src/librustc_lint/builtin.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
436436
// If the trait is private, add the impl items to `private_traits` so they don't get
437437
// reported for missing docs.
438438
let real_trait = trait_ref.path.res.def_id();
439-
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(real_trait) {
439+
if let Some(def_id) = real_trait.as_local() {
440+
let hir_id = cx.tcx.hir().as_local_hir_id(def_id);
440441
if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) {
441442
if let hir::VisibilityKind::Inherited = item.vis.node {
442443
for impl_item_ref in items {
@@ -461,7 +462,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
461462
};
462463

463464
let def_id = cx.tcx.hir().local_def_id(it.hir_id);
464-
let (article, desc) = cx.tcx.article_and_description(def_id);
465+
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
465466

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

474475
let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id);
475-
let (article, desc) = cx.tcx.article_and_description(def_id);
476+
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
476477

477478
self.check_missing_docs_attrs(
478479
cx,
@@ -491,7 +492,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
491492
}
492493

493494
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
494-
let (article, desc) = cx.tcx.article_and_description(def_id);
495+
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
495496
self.check_missing_docs_attrs(
496497
cx,
497498
Some(impl_item.hir_id),
@@ -609,8 +610,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
609610
let mut impls = HirIdSet::default();
610611
cx.tcx.for_each_impl(debug, |d| {
611612
if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() {
612-
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(ty_def.did) {
613-
impls.insert(hir_id);
613+
if let Some(def_id) = ty_def.did.as_local() {
614+
impls.insert(cx.tcx.hir().as_local_hir_id(def_id));
614615
}
615616
}
616617
});
@@ -1531,7 +1532,8 @@ impl ExplicitOutlivesRequirements {
15311532
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15321533
ty_generics: &'tcx ty::Generics,
15331534
) -> Vec<ty::Region<'tcx>> {
1534-
let index = ty_generics.param_def_id_to_index[&tcx.hir().local_def_id(param.hir_id)];
1535+
let index =
1536+
ty_generics.param_def_id_to_index[&tcx.hir().local_def_id(param.hir_id).to_def_id()];
15351537

15361538
match param.kind {
15371539
hir::GenericParamKind::Lifetime { .. } => {

src/librustc_lint/late.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_ast::ast;
1919
use rustc_ast::walk_list;
2020
use rustc_data_structures::sync::{join, par_iter, ParallelIterator};
2121
use rustc_hir as hir;
22-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
22+
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
2323
use rustc_hir::intravisit as hir_visit;
2424
use rustc_hir::intravisit::Visitor;
2525
use rustc_middle::hir::map::Map;
@@ -353,7 +353,7 @@ crate::late_lint_methods!(late_lint_pass_impl, [], ['tcx]);
353353

354354
fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
355355
tcx: TyCtxt<'tcx>,
356-
module_def_id: DefId,
356+
module_def_id: LocalDefId,
357357
pass: T,
358358
) {
359359
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
@@ -364,7 +364,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
364364
param_env: ty::ParamEnv::empty(),
365365
access_levels,
366366
lint_store: unerased_lint_store(tcx),
367-
last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(),
367+
last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id),
368368
generics: None,
369369
only_module: true,
370370
};
@@ -382,7 +382,7 @@ fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
382382

383383
pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
384384
tcx: TyCtxt<'tcx>,
385-
module_def_id: DefId,
385+
module_def_id: LocalDefId,
386386
builtin_lints: T,
387387
) {
388388
if tcx.sess.opts.debugging_opts.no_interleave_lints {

src/librustc_lint/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ pub fn provide(providers: &mut Providers<'_>) {
9191
}
9292

9393
fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
94-
late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
94+
late::late_lint_mod(
95+
tcx,
96+
module_def_id.expect_local(),
97+
BuiltinCombinedModuleLateLintPass::new(),
98+
);
9599
}
96100

97101
macro_rules! pre_expansion_lint_passes {

src/librustc_metadata/foreign_modules.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
2222
};
2323

2424
let foreign_items =
25-
fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id)).collect();
26-
self.modules
27-
.push(ForeignModule { foreign_items, def_id: self.tcx.hir().local_def_id(it.hir_id) });
25+
fm.items.iter().map(|it| self.tcx.hir().local_def_id(it.hir_id).to_def_id()).collect();
26+
self.modules.push(ForeignModule {
27+
foreign_items,
28+
def_id: self.tcx.hir().local_def_id(it.hir_id).to_def_id(),
29+
});
2830
}
2931

3032
fn visit_trait_item(&mut self, _it: &'tcx hir::TraitItem<'tcx>) {}

src/librustc_metadata/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
5151
name: None,
5252
kind: cstore::NativeUnknown,
5353
cfg: None,
54-
foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id)),
54+
foreign_module: Some(self.tcx.hir().local_def_id(it.hir_id).to_def_id()),
5555
wasm_import_module: None,
5656
};
5757
let mut kind_specified = false;

0 commit comments

Comments
 (0)