Replace unwrap with expect in get_module_children#158002
Conversation
|
r? @oli-obk rustbot has assigned @oli-obk. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
@bors r+ rollup |
|
⌛ Testing commit b6fc0c3 with merge e68a2da... Workflow: https://github.com/rust-lang/rust/actions/runs/27671724601 |
…li-obk Fix(ICE): Remove `unwrap` from `get_module_children` # Objective Fix 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 it seems to me that it would be appropriate to just return nothing when called on a non-module-like item (if it isn't module-like, it has zero children). ## Solution - Switched from an `Option::unwrap` to an `if let Some(...)` --- ## 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.
|
@bors r- |
|
This pull request was unapproved. Auto build was cancelled due to unapproval. Cancelled workflows: |
So the fix is to only call it on module-like items in clippy.
Yes, that's a very typical behavior for rustc_metadata APIs intended to prevent mistakes.
I disagree, calling it on something that is not a module means misunderstanding and a potential mistake. |
That's what I've done in my Clippy PR, but it took me a while to work out what was going wrong. Mostly because I was using this method indirectly via
If panicking is absolutely the preferred behaviour, I'm happy to amend this PR to add a
That's perfectly reasonable, I disagree but I can understand why that'd be the preferred strategy here. |
Also replicated documentation on `TyCtxt::module_children`
b6fc0c3 to
d8953cd
Compare
unwrap from get_module_childrenunwrap with expect in get_module_children
|
@petrochenkov I'd appreciate your feedback on this one if you wouldn't mind! I believe this now addresses yours and my concerns. |
|
The comments LGTM. |
|
@rustbot ready |
|
⌛ Testing commit d8953cd with merge d001527... Workflow: https://github.com/rust-lang/rust/actions/runs/27758529074 |
…etrochenkov,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.
|
@bors yield |
|
Auto build was cancelled. Cancelled workflows: The next pull request likely to be tested is #157309. |
…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.
…uwer Rollup of 5 pull requests Successful merges: - #157935 (Make `proc_macro::ConversionErrorKind` non exhaustive) - #158002 (Replace `unwrap` with `expect` in `get_module_children`) - #158071 (Update actions/checkout action to v6) - #158072 (Bump thin-vec to 0.2.18 to address RUSTSEC-2026-0103) - #158077 (rustdoc-json-types: Replace bincode dev-dependency with postcard)
…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.
…uwer Rollup of 10 pull requests Successful merges: - #158026 (`RegionValues`: disable unnecessary range check) - #156795 (Handle generic reborrow in expression-use adjustment walking) - #157694 (Enhance documentation on wake call memory ordering) - #157935 (Make `proc_macro::ConversionErrorKind` non exhaustive) - #158002 (Replace `unwrap` with `expect` in `get_module_children`) - #158034 (Fix reborrow source expression visits) - #158072 (Bump thin-vec to 0.2.18 to address RUSTSEC-2026-0103) - #158074 (Document transient connection errors from TcpListener::accept) - #158077 (rustdoc-json-types: Replace bincode dev-dependency with postcard) - #158086 (renovate: Loosen dashboard approval and adopt recommended config)
…uwer Rollup of 10 pull requests Successful merges: - #158026 (`RegionValues`: disable unnecessary range check) - #156795 (Handle generic reborrow in expression-use adjustment walking) - #157694 (Enhance documentation on wake call memory ordering) - #157935 (Make `proc_macro::ConversionErrorKind` non exhaustive) - #158002 (Replace `unwrap` with `expect` in `get_module_children`) - #158034 (Fix reborrow source expression visits) - #158072 (Bump thin-vec to 0.2.18 to address RUSTSEC-2026-0103) - #158074 (Document transient connection errors from TcpListener::accept) - #158077 (rustdoc-json-types: Replace bincode dev-dependency with postcard) - #158086 (renovate: Loosen dashboard approval and adopt recommended config)
Rollup of 12 pull requests Successful merges: - #156795 (Handle generic reborrow in expression-use adjustment walking) - #157694 (Enhance documentation on wake call memory ordering) - #157935 (Make `proc_macro::ConversionErrorKind` non exhaustive) - #158002 (Replace `unwrap` with `expect` in `get_module_children`) - #158009 (Reject `impl const Trait` since the right syntax is `const impl Trait` now) - #158034 (Fix reborrow source expression visits) - #158072 (Bump thin-vec to 0.2.18 to address RUSTSEC-2026-0103) - #158074 (Document transient connection errors from TcpListener::accept) - #158077 (rustdoc-json-types: Replace bincode dev-dependency with postcard) - #158086 (renovate: Loosen dashboard approval and adopt recommended config) - #158088 (codegen_ssa: no dbginfo for scalable vec local w/ `-O0`) - #158089 (Fix invalid "jump-to-def" doc link generation when an item has a `derive` proc-macro)
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.
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_childrencallingunwrap. The local valuenon_reexportscan beNonewhen the passed inDefIndexis for a non-module-like item. The documentation forget_module_childrenimplies that it must only be called with module-like item indices, however the use ofunwrapmakes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented onTyCtxt::module_children.Solution
Option::unwrapto aOption::expectget_module_childrenTyCtxt::module_childrenNotes