Skip to content

Commit 5a59527

Browse files
committed
Auto merge of #71215 - marmeladema:issue70853/librustc_middle-local-def-id-2, r=eddyb
Simplify `local_def_id` and `as_local_hir_id` See #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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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
{

0 commit comments

Comments
 (0)