diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2022-04-25 22:08:45 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2022-05-02 01:56:50 +0300 |
| commit | 5b5964f569ca07ba54710cb440aacaa1cf1e5c1a (patch) | |
| tree | 2bfa7457d9fde6a513c41e9e7a7d10282e92bf39 /compiler/rustc_save_analysis/src | |
| parent | a933de83989471ac444a13d62996d30621542654 (diff) | |
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`.
Diffstat (limited to 'compiler/rustc_save_analysis/src')
| -rw-r--r-- | compiler/rustc_save_analysis/src/dump_visitor.rs | 15 | ||||
| -rw-r--r-- | compiler/rustc_save_analysis/src/lib.rs | 2 |
2 files changed, 7 insertions, 10 deletions
diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index b4230a144f8..e1c9ecc055f 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -1138,8 +1138,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { let access = access_from!(self.save_ctxt, item.def_id); let ref_id = self.lookup_def_id(item.hir_id()).map(id_from_def_id); let span = self.span_from_span(sub_span); - let parent = - self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id); + let parent = self.save_ctxt.tcx.local_parent(item.def_id); self.dumper.import( &access, Import { @@ -1149,7 +1148,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { alias_span: None, name: item.ident.to_string(), value: String::new(), - parent, + parent: Some(id_from_def_id(parent.to_def_id())), }, ); self.write_sub_paths_truncated(&path); @@ -1166,8 +1165,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { if !self.span.filter_generated(item.span) { let access = access_from!(self.save_ctxt, item.def_id); let span = self.span_from_span(sub_span); - let parent = - self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id); + let parent = self.save_ctxt.tcx.local_parent(item.def_id); self.dumper.import( &access, Import { @@ -1177,7 +1175,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { alias_span: None, name: "*".to_owned(), value: names.join(", "), - parent, + parent: Some(id_from_def_id(parent.to_def_id())), }, ); self.write_sub_paths(&path); @@ -1188,8 +1186,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { let name_span = item.ident.span; if !self.span.filter_generated(name_span) { let span = self.span_from_span(name_span); - let parent = - self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id); + let parent = self.save_ctxt.tcx.local_parent(item.def_id); self.dumper.import( &Access { public: false, reachable: false }, Import { @@ -1199,7 +1196,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { alias_span: None, name: item.ident.to_string(), value: String::new(), - parent, + parent: Some(id_from_def_id(parent.to_def_id())), }, ); } diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index 582186cbd1f..5d94884e0f6 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -690,7 +690,7 @@ impl<'tcx> SaveContext<'tcx> { // This is a reference to a tuple struct or an enum variant where the def_id points // to an invisible constructor function. That is not a very useful // def, so adjust to point to the tuple struct or enum variant itself. - let parent_def_id = self.tcx.parent(def_id).unwrap(); + let parent_def_id = self.tcx.parent(def_id); Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(parent_def_id) }) } Res::Def(HirDefKind::Static(_) | HirDefKind::Const | HirDefKind::AssocConst, _) => { |
