Skip to content

Commit 879fb42

Browse files
committed
Auto merge of #96431 - petrochenkov:parent, r=cjgillot
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`.
2 parents 5428983 + 5b5964f commit 879fb42

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)