Skip to content

Commit d066582

Browse files
Rollup merge of #158002 - bushrat011899:get_module_children_unwrap, r=petrochenkov,oli-obk
Replace `unwrap` with `expect` in `get_module_children` # Objective I ran into an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however the use of `unwrap` makes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented on `TyCtxt::module_children`. ## Solution - Switched from an `Option::unwrap` to a `Option::expect` - Added panic documentation to `get_module_children` - Replicated documentation for `TyCtxt::module_children` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
2 parents 97e7f77 + d8953cd commit d066582

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,10 @@ impl CrateMetadata {
12741274
/// including both proper items and reexports.
12751275
/// Module here is understood in name resolution sense - it can be a `mod` item,
12761276
/// or a crate root, or an enum, or a trait.
1277+
///
1278+
/// # Panics
1279+
///
1280+
/// May panic if the provided `id` does not refer to a module.
12771281
fn get_module_children(&self, tcx: TyCtxt<'_>, id: DefIndex) -> impl Iterator<Item = ModChild> {
12781282
gen move {
12791283
if let Some(data) = &self.root.proc_macro_data {
@@ -1287,7 +1291,9 @@ impl CrateMetadata {
12871291
} else {
12881292
// Iterate over all children.
12891293
let non_reexports = self.root.tables.module_children_non_reexports.get(self, id);
1290-
for child_index in non_reexports.unwrap().decode((self, tcx)) {
1294+
let non_reexports =
1295+
non_reexports.expect("provided `DefIndex` must refer to a module-like item");
1296+
for child_index in non_reexports.decode((self, tcx)) {
12911297
yield self.get_mod_child(tcx, child_index);
12921298
}
12931299

compiler/rustc_middle/src/queries.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,15 @@ rustc_queries! {
21612161
desc { "fetching what a crate is named" }
21622162
separate_provide_extern
21632163
}
2164+
2165+
/// Iterates over all named children of the given module,
2166+
/// including both proper items and reexports.
2167+
/// Module here is understood in name resolution sense - it can be a `mod` item,
2168+
/// or a crate root, or an enum, or a trait.
2169+
///
2170+
/// # Panics
2171+
///
2172+
/// May panic if the provided `id` does not refer to a module.
21642173
query module_children(def_id: DefId) -> &'tcx [ModChild] {
21652174
desc { "collecting child items of module `{}`", tcx.def_path_str(def_id) }
21662175
separate_provide_extern

0 commit comments

Comments
 (0)