Skip to content

rustc: Panic by default in DefIdTree::parent #96431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
{
if let ty::FnDef(id, _) = *literal.ty().kind() {
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
if self.infcx.tcx.parent(id) == self.infcx.tcx.lang_items().fn_once_trait() {
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
let closure = match args.first() {
Some(Operand::Copy(ref place)) | Some(Operand::Move(ref place))
if target == place.local_or_deref_local() =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/debuginfo/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
}

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

#[derive(Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
}

pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
let parent_id = tcx.local_parent(def_id).unwrap();
let parent_id = tcx.local_parent(def_id);
tcx.def_kind(parent_id) == DefKind::Impl
&& tcx.impl_constness(parent_id) == hir::Constness::Const
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_const_eval/src/util/call_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ pub fn call_kind<'tcx>(
} else {
None
};
let parent_self_ty = tcx
.parent(method_did)
.filter(|did| tcx.def_kind(*did) == rustc_hir::def::DefKind::Impl)
let parent_did = tcx.parent(method_did);
let parent_self_ty = (tcx.def_kind(parent_did) == rustc_hir::def::DefKind::Impl)
.then_some(parent_did)
.and_then(|did| match tcx.type_of(did).kind() {
ty::Adt(def, ..) => Some(def.did()),
_ => None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl InferenceDiagnosticsData {

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

let parent_name =
tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
Expand Down Expand Up @@ -854,10 +854,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Some((DefKind::AssocFn, def_id)) =
self.in_progress_typeck_results?.borrow().type_dependent_def(hir_id)
{
return self
.tcx
.parent(def_id)
.filter(|&parent_def_id| self.tcx.is_trait(parent_def_id));
let parent_def_id = self.tcx.parent(def_id);
return self.tcx.is_trait(parent_def_id).then_some(parent_def_id);
}

None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn find_param_with_region<'tcx>(
let (id, bound_region) = match *anon_region {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ebr) => {
(tcx.parent(ebr.def_id).unwrap(), ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name))
(tcx.parent(ebr.def_id), ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name))
}
_ => return None, // not a free region
};
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ impl InvalidAtomicOrdering {
&& let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def()
// skip extension traits, only lint functions from the standard library
&& cx.tcx.trait_id_of_impl(impl_did).is_none()
&& let Some(parent) = cx.tcx.parent(adt.did())
&& let parent = cx.tcx.parent(adt.did())
&& cx.tcx.is_diagnostic_item(sym::atomic_mod, parent)
&& ATOMIC_TYPES.contains(&cx.tcx.item_name(adt.did()))
{
Expand All @@ -1486,9 +1486,9 @@ impl InvalidAtomicOrdering {
orderings.iter().any(|ordering| {
tcx.item_name(did) == *ordering && {
let parent = tcx.parent(did);
parent == atomic_ordering
Some(parent) == atomic_ordering
// needed in case this is a ctor, not a variant
|| parent.map_or(false, |parent| tcx.parent(parent) == atomic_ordering)
|| tcx.opt_parent(parent) == atomic_ordering
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit weird since atomic_ordering could be None, which could return true accidentally I think? Anyways this is an existing logic bug. I hope no_core has an actual purpose otherwise fixing this feels useless..

}
})
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl<'hir> Map<'hir> {
let def_kind = self.tcx.def_kind(def_id);
match def_kind {
DefKind::Trait | DefKind::TraitAlias => def_id,
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id).unwrap(),
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id),
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn suggestion_for_allocator_api(
feature: Symbol,
) -> Option<(Span, String, String, Applicability)> {
if feature == sym::allocator_api {
if let Some(trait_) = tcx.parent(def_id) {
if let Some(trait_) = tcx.opt_parent(def_id) {
if tcx.is_diagnostic_item(sym::Vec, trait_) {
let sm = tcx.sess.parse_sess.source_map();
let inner_types = sm.span_extend_to_prev_char(span, '<', true);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ impl<'tcx> TyCtxt<'tcx> {
(free_region.scope.expect_local(), free_region.bound_region)
}
ty::ReEarlyBound(ref ebr) => (
self.parent(ebr.def_id).unwrap().expect_local(),
self.local_parent(ebr.def_id.expect_local()),
ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name),
),
_ => return None, // not a free region
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'tcx> Ty<'tcx> {
| Placeholder(_)
| Error(_) => false,
Opaque(did, substs) => {
let parent = tcx.parent(*did).expect("opaque types always have a parent");
let parent = tcx.parent(*did);
if let hir::def::DefKind::TyAlias | hir::def::DefKind::AssocTy = tcx.def_kind(parent)
&& let Opaque(parent_did, _) = tcx.type_of(parent).kind()
&& parent_did == did
Expand Down
36 changes: 27 additions & 9 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,28 @@ pub struct ClosureSizeProfileData<'tcx> {
}

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

#[inline]
fn local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
Some(self.parent(id.to_def_id())?.expect_local())
#[track_caller]
fn parent(self, id: DefId) -> DefId {
match self.opt_parent(id) {
Some(id) => id,
// not `unwrap_or_else` to avoid breaking caller tracking
None => bug!("{id:?} doesn't have a parent"),
}
}

#[inline]
#[track_caller]
fn opt_local_parent(self, id: LocalDefId) -> Option<LocalDefId> {
self.opt_parent(id.to_def_id()).map(DefId::expect_local)
}

#[inline]
#[track_caller]
fn local_parent(self, id: LocalDefId) -> LocalDefId {
self.parent(id.to_def_id()).expect_local()
}

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

while descendant != ancestor {
match self.parent(descendant) {
match self.opt_parent(descendant) {
Some(parent) => descendant = parent,
None => return false,
}
Expand All @@ -313,7 +330,8 @@ pub trait DefIdTree: Copy {
}

impl<'tcx> DefIdTree for TyCtxt<'tcx> {
fn parent(self, id: DefId) -> Option<DefId> {
#[inline]
fn opt_parent(self, id: DefId) -> Option<DefId> {
self.def_key(id).parent.map(|index| DefId { index, ..id })
}
}
Expand Down Expand Up @@ -2123,17 +2141,17 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn expect_variant_res(self, res: Res) -> &'tcx VariantDef {
match res {
Res::Def(DefKind::Variant, did) => {
let enum_did = self.parent(did).unwrap();
let enum_did = self.parent(did);
self.adt_def(enum_did).variant_with_id(did)
}
Res::Def(DefKind::Struct | DefKind::Union, did) => self.adt_def(did).non_enum_variant(),
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), variant_ctor_did) => {
let variant_did = self.parent(variant_ctor_did).unwrap();
let enum_did = self.parent(variant_did).unwrap();
let variant_did = self.parent(variant_ctor_did);
let enum_did = self.parent(variant_did);
self.adt_def(enum_did).variant_with_ctor_id(variant_ctor_did)
}
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), ctor_did) => {
let struct_did = self.parent(ctor_did).expect("struct ctor has no parent");
let struct_did = self.parent(ctor_did);
self.adt_def(struct_did).non_enum_variant()
}
_ => bug!("expect_variant_res used with unexpected res {:?}", res),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ pub trait Printer<'tcx>: Sized {
// as the trait.
let in_self_mod = match characteristic_def_id_of_type(self_ty) {
None => false,
Some(ty_def_id) => self.tcx().parent(ty_def_id) == Some(parent_def_id),
Some(ty_def_id) => self.tcx().parent(ty_def_id) == parent_def_id,
};
let in_trait_mod = match impl_trait_ref {
None => false,
Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == Some(parent_def_id),
Some(trait_ref) => self.tcx().parent(trait_ref.def_id) == parent_def_id,
};

if !in_self_mod && !in_trait_mod {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ pub trait PrettyPrinter<'tcx>:
return Ok((self, false));
};

let actual_parent = self.tcx().parent(def_id);
let actual_parent = self.tcx().opt_parent(def_id);
debug!(
"try_print_visible_def_path: visible_parent={:?} actual_parent={:?}",
visible_parent, actual_parent,
Expand Down Expand Up @@ -643,7 +643,7 @@ pub trait PrettyPrinter<'tcx>:
return Ok(self);
}

let parent = self.tcx().parent(def_id).expect("opaque types always have a parent");
let parent = self.tcx().parent(def_id);
match self.tcx().def_kind(parent) {
DefKind::TyAlias | DefKind::AssocTy => {
if let ty::Opaque(d, _) = *self.tcx().type_of(parent).kind() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ impl<'tcx> Region<'tcx> {
/// function might return the `DefId` of a closure.
pub fn free_region_binding_scope(self, tcx: TyCtxt<'_>) -> DefId {
match *self {
ty::ReEarlyBound(br) => tcx.parent(br.def_id).unwrap(),
ty::ReEarlyBound(br) => tcx.parent(br.def_id),
ty::ReFree(fr) => fr.scope,
_ => bug!("free_region_binding_scope invoked on inappropriate region: {:?}", self),
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
match res {
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
Some(self.parent(def_id).and_then(|def_id| self.parent(def_id)).unwrap())
Some(self.parent(self.parent(def_id)))
}
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
Some(self.parent(def_id).unwrap())
Some(self.parent(def_id))
}
// Other `DefKind`s don't have generics and would ICE when calling
// `generics_of`.
Expand Down Expand Up @@ -500,9 +500,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn typeck_root_def_id(self, def_id: DefId) -> DefId {
let mut def_id = def_id;
while self.is_typeck_child(def_id) {
def_id = self.parent(def_id).unwrap_or_else(|| {
bug!("closure {:?} has no parent", def_id);
});
def_id = self.parent(def_id);
}
def_id
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
) -> PatKind<'tcx> {
let res = match res {
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), variant_ctor_id) => {
let variant_id = self.tcx.parent(variant_ctor_id).unwrap();
let variant_id = self.tcx.parent(variant_ctor_id);
Res::Def(DefKind::Variant, variant_id)
}
res => res,
};

let mut kind = match res {
Res::Def(DefKind::Variant, variant_id) => {
let enum_id = self.tcx.parent(variant_id).unwrap();
let enum_id = self.tcx.parent(variant_id);
let adt_def = self.tcx.adt_def(enum_id);
if adt_def.is_enum() {
let substs = match ty.kind() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_monomorphize/src/partitioning/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn compute_codegen_unit_name(
cgu_def_id = None;
}

current_def_id = tcx.parent(current_def_id).unwrap();
current_def_id = tcx.parent(current_def_id);
}

let cgu_def_id = cgu_def_id.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
_ if self.in_pat => {}
Res::PrimTy(..) | Res::SelfCtor(..) | Res::Local(..) => {}
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), ctor_def_id) => {
let variant_id = self.tcx.parent(ctor_def_id).unwrap();
let enum_id = self.tcx.parent(variant_id).unwrap();
let variant_id = self.tcx.parent(ctor_def_id);
let enum_id = self.tcx.parent(variant_id);
self.check_def_id(enum_id);
if !self.ignore_variant_stack.contains(&ctor_def_id) {
self.check_def_id(variant_id);
}
}
Res::Def(DefKind::Variant, variant_id) => {
let enum_id = self.tcx.parent(variant_id).unwrap();
let enum_id = self.tcx.parent(variant_id);
self.check_def_id(enum_id);
if !self.ignore_variant_stack.contains(&variant_id) {
self.check_def_id(variant_id);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct EntryContext<'tcx> {

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

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
let def_id = local_def_id.to_def_id();

// Don't run unused pass for #[derive()]
if let Some(parent) = self.tcx.parent(def_id)
&& let DefKind::Impl = self.tcx.def_kind(parent.expect_local())
&& self.tcx.has_attr(parent, sym::automatically_derived)
let parent = self.tcx.local_parent(local_def_id);
if let DefKind::Impl = self.tcx.def_kind(parent)
&& self.tcx.has_attr(parent.to_def_id(), sym::automatically_derived)
{
return;
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_passes/src/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ impl<'tcx> ReachableContext<'tcx> {
self.visit_nested_body(body);
}
hir::ImplItemKind::Fn(_, body) => {
let impl_def_id =
self.tcx.parent(search_item.to_def_id()).unwrap().expect_local();
let impl_def_id = self.tcx.local_parent(search_item);
if method_might_be_inlined(self.tcx, impl_item, impl_def_id) {
self.visit_nested_body(body)
}
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use rustc_middle::thir::abstract_const::Node as ACNode;
use rustc_middle::ty::fold::TypeVisitor;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::{self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable};
use rustc_middle::ty::{self, Const, DefIdTree, GenericParamDefKind};
use rustc_middle::ty::{TraitRef, Ty, TyCtxt, TypeFoldable};
use rustc_session::lint;
use rustc_span::hygiene::Transparency;
use rustc_span::symbol::{kw, Ident};
Expand Down Expand Up @@ -456,9 +457,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
return;
}

let item_def_id = local_def_id.to_def_id();
let macro_module_def_id =
ty::DefIdTree::parent(self.tcx, item_def_id).unwrap().expect_local();
let macro_module_def_id = self.tcx.local_parent(local_def_id);
if self.tcx.hir().opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) {
// The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252).
return;
Expand All @@ -477,8 +476,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
if changed_reachability || module_def_id == CRATE_DEF_ID {
break;
}
module_def_id =
ty::DefIdTree::parent(self.tcx, module_def_id.to_def_id()).unwrap().expect_local();
module_def_id = self.tcx.local_parent(module_def_id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
&& self
.r
.trait_impl_items
.contains(&ty::DefIdTree::parent(&*self.r, def_id).unwrap().expect_local()))
.contains(&ty::DefIdTree::local_parent(&*self.r, local_def_id)))
{
// Trait impl item visibility is inherited from its trait when not specified
// explicitly. In that case we cannot determine it here in early resolve,
Expand Down
Loading