Skip to content

Commit 5b5964f

Browse files
committed
rustc: Panic by default in DefIdTree::parent
Only crate root def-ids don't have a parent, and in majority of cases the argument of `DefIdTree::parent` cannot be a crate root. So we now panic by default in `parent` and introduce a new non-panicing function `opt_parent` for cases where the argument can be a crate root. Same applies to `local_parent`/`opt_local_parent`.
1 parent a933de8 commit 5b5964f

File tree

50 files changed

+162
-176
lines changed

Some content is hidden

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

50 files changed

+162
-176
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
9090
{
9191
if let ty::FnDef(id, _) = *literal.ty().kind() {
9292
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
93-
if self.infcx.tcx.parent(id) == self.infcx.tcx.lang_items().fn_once_trait() {
93+
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
9494
let closure = match args.first() {
9595
Some(Operand::Copy(ref place)) | Some(Operand::Move(ref place))
9696
if target == place.local_or_deref_local() =>

compiler/rustc_codegen_llvm/src/debuginfo/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
4646
}
4747

4848
pub fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
49-
item_namespace(cx, cx.tcx.parent(def_id).expect("get_namespace_for_item: missing parent?"))
49+
item_namespace(cx, cx.tcx.parent(def_id))
5050
}
5151

5252
#[derive(Debug, PartialEq, Eq)]

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
1717
}
1818

1919
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
20-
let parent_id = tcx.local_parent(def_id).unwrap();
20+
let parent_id = tcx.local_parent(def_id);
2121
tcx.def_kind(parent_id) == DefKind::Impl
2222
&& tcx.impl_constness(parent_id) == hir::Constness::Const
2323
}

compiler/rustc_const_eval/src/util/call_kind.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ pub fn call_kind<'tcx>(
128128
} else {
129129
None
130130
};
131-
let parent_self_ty = tcx
132-
.parent(method_did)
133-
.filter(|did| tcx.def_kind(*did) == rustc_hir::def::DefKind::Impl)
131+
let parent_did = tcx.parent(method_did);
132+
let parent_self_ty = (tcx.def_kind(parent_did) == rustc_hir::def::DefKind::Impl)
133+
.then_some(parent_did)
134134
.and_then(|did| match tcx.type_of(did).kind() {
135135
ty::Adt(def, ..) => Some(def.did()),
136136
_ => None,

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl InferenceDiagnosticsData {
341341

342342
impl InferenceDiagnosticsParentData {
343343
fn for_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<InferenceDiagnosticsParentData> {
344-
let parent_def_id = tcx.parent(def_id)?;
344+
let parent_def_id = tcx.parent(def_id);
345345

346346
let parent_name =
347347
tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
@@ -854,10 +854,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
854854
if let Some((DefKind::AssocFn, def_id)) =
855855
self.in_progress_typeck_results?.borrow().type_dependent_def(hir_id)
856856
{
857-
return self
858-
.tcx
859-
.parent(def_id)
860-
.filter(|&parent_def_id| self.tcx.is_trait(parent_def_id));
857+
let parent_def_id = self.tcx.parent(def_id);
858+
return self.tcx.is_trait(parent_def_id).then_some(parent_def_id);
861859
}
862860

863861
None

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn find_param_with_region<'tcx>(
4242
let (id, bound_region) = match *anon_region {
4343
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
4444
ty::ReEarlyBound(ebr) => {
45-
(tcx.parent(ebr.def_id).unwrap(), ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name))
45+
(tcx.parent(ebr.def_id), ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name))
4646
}
4747
_ => return None, // not a free region
4848
};

compiler/rustc_lint/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ impl InvalidAtomicOrdering {
14711471
&& let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def()
14721472
// skip extension traits, only lint functions from the standard library
14731473
&& cx.tcx.trait_id_of_impl(impl_did).is_none()
1474-
&& let Some(parent) = cx.tcx.parent(adt.did())
1474+
&& let parent = cx.tcx.parent(adt.did())
14751475
&& cx.tcx.is_diagnostic_item(sym::atomic_mod, parent)
14761476
&& ATOMIC_TYPES.contains(&cx.tcx.item_name(adt.did()))
14771477
{
@@ -1486,9 +1486,9 @@ impl InvalidAtomicOrdering {
14861486
orderings.iter().any(|ordering| {
14871487
tcx.item_name(did) == *ordering && {
14881488
let parent = tcx.parent(did);
1489-
parent == atomic_ordering
1489+
Some(parent) == atomic_ordering
14901490
// needed in case this is a ctor, not a variant
1491-
|| parent.map_or(false, |parent| tcx.parent(parent) == atomic_ordering)
1491+
|| tcx.opt_parent(parent) == atomic_ordering
14921492
}
14931493
})
14941494
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ impl<'hir> Map<'hir> {
546546
let def_kind = self.tcx.def_kind(def_id);
547547
match def_kind {
548548
DefKind::Trait | DefKind::TraitAlias => def_id,
549-
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id).unwrap(),
549+
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id),
550550
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
551551
}
552552
}

compiler/rustc_middle/src/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ fn suggestion_for_allocator_api(
289289
feature: Symbol,
290290
) -> Option<(Span, String, String, Applicability)> {
291291
if feature == sym::allocator_api {
292-
if let Some(trait_) = tcx.parent(def_id) {
292+
if let Some(trait_) = tcx.opt_parent(def_id) {
293293
if tcx.is_diagnostic_item(sym::Vec, trait_) {
294294
let sm = tcx.sess.parse_sess.source_map();
295295
let inner_types = sm.span_extend_to_prev_char(span, '<', true);

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ impl<'tcx> TyCtxt<'tcx> {
14911491
(free_region.scope.expect_local(), free_region.bound_region)
14921492
}
14931493
ty::ReEarlyBound(ref ebr) => (
1494-
self.parent(ebr.def_id).unwrap().expect_local(),
1494+
self.local_parent(ebr.def_id.expect_local()),
14951495
ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name),
14961496
),
14971497
_ => return None, // not a free region

compiler/rustc_middle/src/ty/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'tcx> Ty<'tcx> {
108108
| Placeholder(_)
109109
| Error(_) => false,
110110
Opaque(did, substs) => {
111-
let parent = tcx.parent(*did).expect("opaque types always have a parent");
111+
let parent = tcx.parent(*did);
112112
if let hir::def::DefKind::TyAlias | hir::def::DefKind::AssocTy = tcx.def_kind(parent)
113113
&& let Opaque(parent_did, _) = tcx.type_of(parent).kind()
114114
&& parent_did == did

compiler/rustc_middle/src/ty/mod.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,28 @@ pub struct ClosureSizeProfileData<'tcx> {
290290
}
291291

292292
pub trait DefIdTree: Copy {
293-
fn parent(self, id: DefId) -> Option<DefId>;
293+
fn opt_parent(self, id: DefId) -> Option<DefId>;
294294

295295
#[inline]
296-
fn local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
297-
Some(self.parent(id.to_def_id())?.expect_local())
296+
#[track_caller]
297+
fn parent(self, id: DefId) -> DefId {
298+
match self.opt_parent(id) {
299+
Some(id) => id,
300+
// not `unwrap_or_else` to avoid breaking caller tracking
301+
None => bug!("{id:?} doesn't have a parent"),
302+
}
303+
}
304+
305+
#[inline]
306+
#[track_caller]
307+
fn opt_local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
308+
self.opt_parent(id.to_def_id()).map(DefId::expect_local)
309+
}
310+
311+
#[inline]
312+
#[track_caller]
313+
fn local_parent(self, id: LocalDefId) -> LocalDefId {
314+
self.parent(id.to_def_id()).expect_local()
298315
}
299316

300317
fn is_descendant_of(self, mut descendant: DefId, ancestor: DefId) -> bool {
@@ -303,7 +320,7 @@ pub trait DefIdTree: Copy {
303320
}
304321

305322
while descendant != ancestor {
306-
match self.parent(descendant) {
323+
match self.opt_parent(descendant) {
307324
Some(parent) => descendant = parent,
308325
None => return false,
309326
}
@@ -313,7 +330,8 @@ pub trait DefIdTree: Copy {
313330
}
314331

315332
impl<'tcx> DefIdTree for TyCtxt<'tcx> {
316-
fn parent(self, id: DefId) -> Option<DefId> {
333+
#[inline]
334+
fn opt_parent(self, id: DefId) -> Option<DefId> {
317335
self.def_key(id).parent.map(|index| DefId { index, ..id })
318336
}
319337
}
@@ -2123,17 +2141,17 @@ impl<'tcx> TyCtxt<'tcx> {
21232141
pub fn expect_variant_res(self, res: Res) -> &'tcx VariantDef {
21242142
match res {
21252143
Res::Def(DefKind::Variant, did) => {
2126-
let enum_did = self.parent(did).unwrap();
2144+
let enum_did = self.parent(did);
21272145
self.adt_def(enum_did).variant_with_id(did)
21282146
}
21292147
Res::Def(DefKind::Struct | DefKind::Union, did) => self.adt_def(did).non_enum_variant(),
21302148
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), variant_ctor_did) => {
2131-
let variant_did = self.parent(variant_ctor_did).unwrap();
2132-
let enum_did = self.parent(variant_did).unwrap();
2149+
let variant_did = self.parent(variant_ctor_did);
2150+
let enum_did = self.parent(variant_did);
21332151
self.adt_def(enum_did).variant_with_ctor_id(variant_ctor_did)
21342152
}
21352153
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), ctor_did) => {
2136-
let struct_did = self.parent(ctor_did).expect("struct ctor has no parent");
2154+
let struct_did = self.parent(ctor_did);
21372155
self.adt_def(struct_did).non_enum_variant()
21382156
}
21392157
_ => bug!("expect_variant_res used with unexpected res {:?}", res),

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ pub trait Printer<'tcx>: Sized {
235235
// as the trait.
236236
let in_self_mod = match characteristic_def_id_of_type(self_ty) {
237237
None => false,
238-
Some(ty_def_id) => self.tcx().parent(ty_def_id) == Some(parent_def_id),
238+
Some(ty_def_id) => self.tcx().parent(ty_def_id) == parent_def_id,
239239
};
240240
let in_trait_mod = match impl_trait_ref {
241241
None => false,
242-
Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == Some(parent_def_id),
242+
Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == parent_def_id,
243243
};
244244

245245
if !in_self_mod && !in_trait_mod {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub trait PrettyPrinter<'tcx>:
408408
return Ok((self, false));
409409
};
410410

411-
let actual_parent = self.tcx().parent(def_id);
411+
let actual_parent = self.tcx().opt_parent(def_id);
412412
debug!(
413413
"try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",
414414
visible_parent, actual_parent,
@@ -643,7 +643,7 @@ pub trait PrettyPrinter<'tcx>:
643643
return Ok(self);
644644
}
645645

646-
let parent = self.tcx().parent(def_id).expect("opaque types always have a parent");
646+
let parent = self.tcx().parent(def_id);
647647
match self.tcx().def_kind(parent) {
648648
DefKind::TyAlias | DefKind::AssocTy => {
649649
if let ty::Opaque(d, _) = *self.tcx().type_of(parent).kind() {

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ impl<'tcx> Region<'tcx> {
17981798
/// function might return the `DefId` of a closure.
17991799
pub fn free_region_binding_scope(self, tcx: TyCtxt<'_>) -> DefId {
18001800
match *self {
1801-
ty::ReEarlyBound(br) => tcx.parent(br.def_id).unwrap(),
1801+
ty::ReEarlyBound(br) => tcx.parent(br.def_id),
18021802
ty::ReFree(fr) => fr.scope,
18031803
_ => bug!("free_region_binding_scope invoked on inappropriate region: {:?}", self),
18041804
}

compiler/rustc_middle/src/ty/util.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ impl<'tcx> TyCtxt<'tcx> {
142142
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
143143
match res {
144144
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
145-
Some(self.parent(def_id).and_then(|def_id| self.parent(def_id)).unwrap())
145+
Some(self.parent(self.parent(def_id)))
146146
}
147147
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
148-
Some(self.parent(def_id).unwrap())
148+
Some(self.parent(def_id))
149149
}
150150
// Other `DefKind`s don't have generics and would ICE when calling
151151
// `generics_of`.
@@ -500,9 +500,7 @@ impl<'tcx> TyCtxt<'tcx> {
500500
pub fn typeck_root_def_id(self, def_id: DefId) -> DefId {
501501
let mut def_id = def_id;
502502
while self.is_typeck_child(def_id) {
503-
def_id = self.parent(def_id).unwrap_or_else(|| {
504-
bug!("closure {:?} has no parent", def_id);
505-
});
503+
def_id = self.parent(def_id);
506504
}
507505
def_id
508506
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
377377
) -> PatKind<'tcx> {
378378
let res = match res {
379379
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), variant_ctor_id) => {
380-
let variant_id = self.tcx.parent(variant_ctor_id).unwrap();
380+
let variant_id = self.tcx.parent(variant_ctor_id);
381381
Res::Def(DefKind::Variant, variant_id)
382382
}
383383
res => res,
384384
};
385385

386386
let mut kind = match res {
387387
Res::Def(DefKind::Variant, variant_id) => {
388-
let enum_id = self.tcx.parent(variant_id).unwrap();
388+
let enum_id = self.tcx.parent(variant_id);
389389
let adt_def = self.tcx.adt_def(enum_id);
390390
if adt_def.is_enum() {
391391
let substs = match ty.kind() {

compiler/rustc_monomorphize/src/partitioning/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn compute_codegen_unit_name(
352352
cgu_def_id = None;
353353
}
354354

355-
current_def_id = tcx.parent(current_def_id).unwrap();
355+
current_def_id = tcx.parent(current_def_id);
356356
}
357357

358358
let cgu_def_id = cgu_def_id.unwrap();

compiler/rustc_passes/src/dead.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
8888
_ if self.in_pat => {}
8989
Res::PrimTy(..) | Res::SelfCtor(..) | Res::Local(..) => {}
9090
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), ctor_def_id) => {
91-
let variant_id = self.tcx.parent(ctor_def_id).unwrap();
92-
let enum_id = self.tcx.parent(variant_id).unwrap();
91+
let variant_id = self.tcx.parent(ctor_def_id);
92+
let enum_id = self.tcx.parent(variant_id);
9393
self.check_def_id(enum_id);
9494
if !self.ignore_variant_stack.contains(&ctor_def_id) {
9595
self.check_def_id(variant_id);
9696
}
9797
}
9898
Res::Def(DefKind::Variant, variant_id) => {
99-
let enum_id = self.tcx.parent(variant_id).unwrap();
99+
let enum_id = self.tcx.parent(variant_id);
100100
self.check_def_id(enum_id);
101101
if !self.ignore_variant_stack.contains(&variant_id) {
102102
self.check_def_id(variant_id);

compiler/rustc_passes/src/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct EntryContext<'tcx> {
2727

2828
impl<'tcx> ItemLikeVisitor<'tcx> for EntryContext<'tcx> {
2929
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
30-
let at_root = self.tcx.local_parent(item.def_id) == Some(CRATE_DEF_ID);
30+
let at_root = self.tcx.opt_local_parent(item.def_id) == Some(CRATE_DEF_ID);
3131
find_item(item, self, at_root);
3232
}
3333

compiler/rustc_passes/src/liveness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,9 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
332332
let def_id = local_def_id.to_def_id();
333333

334334
// Don't run unused pass for #[derive()]
335-
if let Some(parent) = self.tcx.parent(def_id)
336-
&& let DefKind::Impl = self.tcx.def_kind(parent.expect_local())
337-
&& self.tcx.has_attr(parent, sym::automatically_derived)
335+
let parent = self.tcx.local_parent(local_def_id);
336+
if let DefKind::Impl = self.tcx.def_kind(parent)
337+
&& self.tcx.has_attr(parent.to_def_id(), sym::automatically_derived)
338338
{
339339
return;
340340
}

compiler/rustc_passes/src/reachable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ impl<'tcx> ReachableContext<'tcx> {
280280
self.visit_nested_body(body);
281281
}
282282
hir::ImplItemKind::Fn(_, body) => {
283-
let impl_def_id =
284-
self.tcx.parent(search_item.to_def_id()).unwrap().expect_local();
283+
let impl_def_id = self.tcx.local_parent(search_item);
285284
if method_might_be_inlined(self.tcx, impl_item, impl_def_id) {
286285
self.visit_nested_body(body)
287286
}

compiler/rustc_privacy/src/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use rustc_middle::thir::abstract_const::Node as ACNode;
2424
use rustc_middle::ty::fold::TypeVisitor;
2525
use rustc_middle::ty::query::Providers;
2626
use rustc_middle::ty::subst::InternalSubsts;
27-
use rustc_middle::ty::{self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable};
27+
use rustc_middle::ty::{self, Const, DefIdTree, GenericParamDefKind};
28+
use rustc_middle::ty::{TraitRef, Ty, TyCtxt, TypeFoldable};
2829
use rustc_session::lint;
2930
use rustc_span::hygiene::Transparency;
3031
use rustc_span::symbol::{kw, Ident};
@@ -456,9 +457,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
456457
return;
457458
}
458459

459-
let item_def_id = local_def_id.to_def_id();
460-
let macro_module_def_id =
461-
ty::DefIdTree::parent(self.tcx, item_def_id).unwrap().expect_local();
460+
let macro_module_def_id = self.tcx.local_parent(local_def_id);
462461
if self.tcx.hir().opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) {
463462
// The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252).
464463
return;
@@ -477,8 +476,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
477476
if changed_reachability || module_def_id == CRATE_DEF_ID {
478477
break;
479478
}
480-
module_def_id =
481-
ty::DefIdTree::parent(self.tcx, module_def_id.to_def_id()).unwrap().expect_local();
479+
module_def_id = self.tcx.local_parent(module_def_id);
482480
}
483481
}
484482

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
13881388
&& self
13891389
.r
13901390
.trait_impl_items
1391-
.contains(&ty::DefIdTree::parent(&*self.r, def_id).unwrap().expect_local()))
1391+
.contains(&ty::DefIdTree::local_parent(&*self.r, local_def_id)))
13921392
{
13931393
// Trait impl item visibility is inherited from its trait when not specified
13941394
// explicitly. In that case we cannot determine it here in early resolve,

0 commit comments

Comments
 (0)