Skip to content

Commit 936a09a

Browse files
committed
Reduce use of local_def_id_to_hir_id.
1 parent f8e2b9a commit 936a09a

File tree

25 files changed

+99
-130
lines changed

25 files changed

+99
-130
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
407407
let generics = tcx.generics_of(self.mir_def_id());
408408
let param = generics.type_param(&param_ty, tcx);
409409
if let Some(generics) = tcx
410-
.hir()
411-
.get_generics(tcx.closure_base_def_id(self.mir_def_id().to_def_id()))
410+
.closure_base_def_id(self.mir_def_id().to_def_id())
411+
.as_local()
412+
.and_then(|def_id| tcx.hir().get_generics(def_id))
412413
{
413414
suggest_constraining_type_param(
414415
tcx,

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_hir as hir;
2-
use rustc_hir::def_id::DefId;
2+
use rustc_hir::def_id::{DefId, LocalDefId};
33
use rustc_middle::ty::query::Providers;
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::symbol::Symbol;
@@ -15,7 +15,8 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
1515
}
1616
}
1717

18-
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
18+
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
19+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1920
let parent_id = tcx.hir().get_parent_node(hir_id);
2021
matches!(
2122
tcx.hir().get(parent_id),
@@ -29,7 +30,8 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
2930
/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
3031
/// said intrinsic has a `rustc_const_{un,}stable` attribute.
3132
fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
32-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
33+
let def_id = def_id.expect_local();
34+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
3335

3436
let node = tcx.hir().get(hir_id);
3537

@@ -50,7 +52,7 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
5052

5153
// If the function itself is not annotated with `const`, it may still be a `const fn`
5254
// if it resides in a const trait impl.
53-
is_parent_const_impl_raw(tcx, hir_id)
55+
is_parent_const_impl_raw(tcx, def_id)
5456
} else if let hir::Node::Ctor(_) = node {
5557
true
5658
} else {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
33
use rustc_errors::{Applicability, Diagnostic, ErrorReported};
4-
use rustc_hir::def_id::DefId;
5-
use rustc_hir::{self as hir, HirId, LangItem};
4+
use rustc_hir::def_id::{DefId, LocalDefId};
5+
use rustc_hir::{self as hir, LangItem};
66
use rustc_index::bit_set::BitSet;
77
use rustc_infer::infer::TyCtxtInferExt;
88
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
@@ -259,8 +259,7 @@ impl Checker<'mir, 'tcx> {
259259
// Prevent const trait methods from being annotated as `stable`.
260260
// FIXME: Do this as part of stability checking.
261261
if self.is_const_stable_const_fn() {
262-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
263-
if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) {
262+
if crate::const_eval::is_parent_const_impl_raw(tcx, def_id) {
264263
self.ccx
265264
.tcx
266265
.sess
@@ -298,8 +297,7 @@ impl Checker<'mir, 'tcx> {
298297
&& !tcx.is_thread_local_static(def_id.to_def_id());
299298

300299
if should_check_for_sync {
301-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
302-
check_return_ty_is_sync(tcx, &body, hir_id);
300+
check_return_ty_is_sync(tcx, &body, def_id);
303301
}
304302

305303
// If we got through const-checking without emitting any "primary" errors, emit any
@@ -1088,9 +1086,10 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
10881086
}
10891087
}
10901088

1091-
fn check_return_ty_is_sync(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, hir_id: HirId) {
1089+
fn check_return_ty_is_sync(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: LocalDefId) {
10921090
let ty = body.return_ty();
10931091
tcx.infer_ctxt().enter(|infcx| {
1092+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
10941093
let cause = traits::ObligationCause::new(body.span, hir_id, traits::SharedStatic);
10951094
let mut fulfillment_cx = traits::FulfillmentContext::new();
10961095
let sync_def_id = tcx.require_lang_item(LangItem::Sync, Some(body.span));

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ impl Qualif for CustomEq {
198198
// because that component may be part of an enum variant (e.g.,
199199
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
200200
// structural-match (`Option::None`).
201-
let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id());
202-
traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some()
201+
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
203202
}
204203

205204
fn in_adt_inherently(

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ fn msg_span_from_early_bound_and_free_regions(
151151
) -> (String, Span) {
152152
let sm = tcx.sess.source_map();
153153

154-
let scope = region.free_region_binding_scope(tcx);
155-
let node = tcx.hir().local_def_id_to_hir_id(scope.expect_local());
154+
let scope = region.free_region_binding_scope(tcx).expect_local();
156155
match *region {
157156
ty::ReEarlyBound(ref br) => {
158-
let mut sp = sm.guess_head_span(tcx.hir().span(node));
157+
let mut sp = sm.guess_head_span(tcx.def_span(scope));
159158
if let Some(param) =
160159
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
161160
{
@@ -166,7 +165,7 @@ fn msg_span_from_early_bound_and_free_regions(
166165
ty::ReFree(ty::FreeRegion {
167166
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
168167
}) => {
169-
let mut sp = sm.guess_head_span(tcx.hir().span(node));
168+
let mut sp = sm.guess_head_span(tcx.def_span(scope));
170169
if let Some(param) =
171170
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
172171
{
@@ -181,13 +180,13 @@ fn msg_span_from_early_bound_and_free_regions(
181180
} else {
182181
(
183182
format!("the anonymous lifetime #{} defined here", idx + 1),
184-
tcx.hir().span(node),
183+
tcx.def_span(scope),
185184
)
186185
}
187186
}
188187
_ => (
189188
format!("the lifetime `{}` as defined here", region),
190-
sm.guess_head_span(tcx.hir().span(node)),
189+
sm.guess_head_span(tcx.def_span(scope)),
191190
),
192191
},
193192
_ => bug!(),
@@ -1688,8 +1687,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16881687
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values {
16891688
if let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind() {
16901689
if let Some(def_id) = def_id.as_local() {
1691-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1692-
let span = self.tcx.hir().span(hir_id);
1690+
let span = self.tcx.def_span(def_id);
16931691
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
16941692
}
16951693
}
@@ -2129,7 +2127,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21292127
if let Node::GenericParam(param) = hir.get(id) {
21302128
has_bounds = !param.bounds.is_empty();
21312129
}
2132-
let sp = hir.span(id);
2130+
let sp = self.tcx.def_span(def_id);
21332131
// `sp` only covers `T`, change it so that it covers
21342132
// `T:` when appropriate
21352133
let is_impl_trait = bound_kind.to_string().starts_with("impl ");
@@ -2176,12 +2174,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21762174
.as_ref()
21772175
.and_then(|(_, g)| g.params.first())
21782176
.and_then(|param| param.def_id.as_local())
2179-
.map(|def_id| {
2180-
(
2181-
hir.span(hir.local_def_id_to_hir_id(def_id)).shrink_to_lo(),
2182-
format!("{}, ", new_lt),
2183-
)
2184-
});
2177+
.map(|def_id| (self.tcx.def_span(def_id).shrink_to_lo(), format!("{}, ", new_lt)));
21852178

21862179
let labeled_user_string = match bound_kind {
21872180
GenericKind::Param(ref p) => format!("the parameter type `{}`", p),

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
12121212
check_no_mangle_on_generic_fn(
12131213
no_mangle_attr,
12141214
Some(generics),
1215-
cx.tcx.hir().get_generics(it.id.def_id.to_def_id()).unwrap(),
1215+
cx.tcx.hir().get_generics(it.id.def_id).unwrap(),
12161216
it.span,
12171217
);
12181218
}

compiler/rustc_middle/src/hir/map/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,7 @@ impl<'hir> Map<'hir> {
358358
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
359359
}
360360

361-
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
362-
let id = id.as_local()?;
361+
pub fn get_generics(&self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
363362
let node = self.tcx.hir_owner(id)?;
364363
match node.node {
365364
OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics),

compiler/rustc_middle/src/mir/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2405,15 +2405,14 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24052405

24062406
AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| {
24072407
if let Some(def_id) = def_id.as_local() {
2408-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
24092408
let name = if tcx.sess.opts.debugging_opts.span_free_formats {
24102409
let substs = tcx.lift(substs).unwrap();
24112410
format!(
24122411
"[closure@{}]",
24132412
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
24142413
)
24152414
} else {
2416-
let span = tcx.hir().span(hir_id);
2415+
let span = tcx.def_span(def_id);
24172416
format!(
24182417
"[closure@{}]",
24192418
tcx.sess.source_map().span_to_diagnostic_string(span)
@@ -2437,8 +2436,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24372436

24382437
AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| {
24392438
if let Some(def_id) = def_id.as_local() {
2440-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2441-
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
2439+
let name = format!("[generator@{:?}]", tcx.def_span(def_id));
24422440
let mut struct_fmt = fmt.debug_struct(&name);
24432441

24442442
// FIXME(project-rfc-2229#48): This should be a list of capture names/places

compiler/rustc_middle/src/mir/mono.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use rustc_data_structures::base_n;
55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8-
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
9-
use rustc_hir::{HirId, ItemId};
8+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
9+
use rustc_hir::ItemId;
1010
use rustc_query_system::ich::{NodeIdHashingMode, StableHashingContext};
1111
use rustc_session::config::OptLevel;
1212
use rustc_span::source_map::Span;
@@ -179,15 +179,11 @@ impl<'tcx> MonoItem<'tcx> {
179179

180180
pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> {
181181
match *self {
182-
MonoItem::Fn(Instance { def, .. }) => {
183-
def.def_id().as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
184-
}
185-
MonoItem::Static(def_id) => {
186-
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
187-
}
188-
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
182+
MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(),
183+
MonoItem::Static(def_id) => def_id.as_local(),
184+
MonoItem::GlobalAsm(item_id) => Some(item_id.def_id),
189185
}
190-
.map(|hir_id| tcx.hir().span(hir_id))
186+
.map(|def_id| tcx.def_span(def_id))
191187
}
192188

193189
// Only used by rustc_codegen_cranelift
@@ -355,7 +351,7 @@ impl<'tcx> CodegenUnit<'tcx> {
355351
// The codegen tests rely on items being process in the same order as
356352
// they appear in the file, so for local items, we sort by node_id first
357353
#[derive(PartialEq, Eq, PartialOrd, Ord)]
358-
pub struct ItemSortKey<'tcx>(Option<HirId>, SymbolName<'tcx>);
354+
pub struct ItemSortKey<'tcx>(Option<LocalDefId>, SymbolName<'tcx>);
359355

360356
fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
361357
ItemSortKey(
@@ -366,10 +362,7 @@ impl<'tcx> CodegenUnit<'tcx> {
366362
// instances into account. The others don't matter for
367363
// the codegen tests and can even make item order
368364
// unstable.
369-
InstanceDef::Item(def) => def
370-
.did
371-
.as_local()
372-
.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)),
365+
InstanceDef::Item(def) => def.did.as_local(),
373366
InstanceDef::VtableShim(..)
374367
| InstanceDef::ReifyShim(..)
375368
| InstanceDef::Intrinsic(..)
@@ -380,10 +373,8 @@ impl<'tcx> CodegenUnit<'tcx> {
380373
| InstanceDef::CloneShim(..) => None,
381374
}
382375
}
383-
MonoItem::Static(def_id) => {
384-
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
385-
}
386-
MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()),
376+
MonoItem::Static(def_id) => def_id.as_local(),
377+
MonoItem::GlobalAsm(item_id) => Some(item_id.def_id),
387378
},
388379
item.symbol_name(tcx),
389380
)

compiler/rustc_middle/src/mir/spanview.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
665665
}
666666

667667
fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
668-
let hir_id =
669-
tcx.hir().local_def_id_to_hir_id(def_id.as_local().expect("expected DefId is local"));
670-
let fn_decl_span = tcx.hir().span(hir_id);
668+
let fn_decl_span = tcx.def_span(def_id);
671669
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
672670
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
673671
} else {

compiler/rustc_middle/src/ty/inhabitedness/def_id_forest.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ty::context::TyCtxt;
22
use crate::ty::{DefId, DefIdTree};
3-
use rustc_hir::CRATE_HIR_ID;
3+
use rustc_span::def_id::CRATE_DEF_ID;
44
use smallvec::SmallVec;
55
use std::mem;
66
use std::sync::Arc;
@@ -43,8 +43,8 @@ impl<'tcx> DefIdForest {
4343
/// Creates a forest consisting of a single tree representing the entire
4444
/// crate.
4545
#[inline]
46-
pub fn full(tcx: TyCtxt<'tcx>) -> DefIdForest {
47-
DefIdForest::from_id(tcx.hir().local_def_id(CRATE_HIR_ID).to_def_id())
46+
pub fn full() -> DefIdForest {
47+
DefIdForest::from_id(CRATE_DEF_ID.to_def_id())
4848
}
4949

5050
/// Creates a forest containing a `DefId` and all its descendants.
@@ -96,7 +96,7 @@ impl<'tcx> DefIdForest {
9696
let mut ret: SmallVec<[_; 1]> = if let Some(first) = iter.next() {
9797
SmallVec::from_slice(first.as_slice())
9898
} else {
99-
return DefIdForest::full(tcx);
99+
return DefIdForest::full();
100100
};
101101

102102
let mut next_ret: SmallVec<[_; 1]> = SmallVec::new();

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub(crate) fn type_uninhabited_from<'tcx>(
205205
match *ty.kind() {
206206
Adt(def, substs) => def.uninhabited_from(tcx, substs, param_env),
207207

208-
Never => DefIdForest::full(tcx),
208+
Never => DefIdForest::full(),
209209

210210
Tuple(ref tys) => DefIdForest::union(
211211
tcx,

compiler/rustc_middle/src/ty/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1998,8 +1998,7 @@ impl<'tcx> TyCtxt<'tcx> {
19981998
/// with the name of the crate containing the impl.
19991999
pub fn span_of_impl(self, impl_did: DefId) -> Result<Span, Symbol> {
20002000
if let Some(impl_did) = impl_did.as_local() {
2001-
let hir_id = self.hir().local_def_id_to_hir_id(impl_did);
2002-
Ok(self.hir().span(hir_id))
2001+
Ok(self.def_span(impl_did))
20032002
} else {
20042003
Err(self.crate_name(impl_did.krate))
20052004
}

compiler/rustc_middle/src/ty/print/pretty.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,7 @@ pub trait PrettyPrinter<'tcx>:
688688
p!("generator");
689689
// FIXME(eddyb) should use `def_span`.
690690
if let Some(did) = did.as_local() {
691-
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did);
692-
let span = self.tcx().hir().span(hir_id);
691+
let span = self.tcx().def_span(did);
693692
p!(write(
694693
"@{}",
695694
// This may end up in stderr diagnostics but it may also be emitted
@@ -725,11 +724,10 @@ pub trait PrettyPrinter<'tcx>:
725724
p!(write("closure"));
726725
// FIXME(eddyb) should use `def_span`.
727726
if let Some(did) = did.as_local() {
728-
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did);
729727
if self.tcx().sess.opts.debugging_opts.span_free_formats {
730728
p!("@", print_def_path(did.to_def_id(), substs));
731729
} else {
732-
let span = self.tcx().hir().span(hir_id);
730+
let span = self.tcx().def_span(did);
733731
p!(write(
734732
"@{}",
735733
// This may end up in stderr diagnostics but it may also be emitted

compiler/rustc_mir_build/src/build/expr/as_place.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,14 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
217217
ty::ClosureKind::FnOnce => {}
218218
}
219219

220-
// We won't be building MIR if the closure wasn't local
221-
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
222-
let closure_span = tcx.hir().span(closure_hir_id);
223-
224220
let Some((capture_index, capture)) =
225221
find_capture_matching_projections(
226222
typeck_results,
227223
var_hir_id,
228224
closure_def_id,
229225
&from_builder.projection,
230226
) else {
227+
let closure_span = tcx.def_span(closure_def_id);
231228
if !enable_precise_capture(tcx, closure_span) {
232229
bug!(
233230
"No associated capture found for {:?}[{:#?}] even though \
@@ -244,6 +241,8 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
244241
return Err(from_builder);
245242
};
246243

244+
// We won't be building MIR if the closure wasn't local
245+
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
247246
let closure_ty = typeck_results.node_type(closure_hir_id);
248247

249248
let substs = match closure_ty.kind() {

0 commit comments

Comments
 (0)