Skip to content

refactor(payload): mark in-function invariants as unreachable!()#1890

Draft
goxberry wants to merge 1 commit into
goxberry/expect-cleanup-payload-result-propfrom
goxberry/expect-cleanup-payload-local-invariants
Draft

refactor(payload): mark in-function invariants as unreachable!()#1890
goxberry wants to merge 1 commit into
goxberry/expect-cleanup-payload-result-propfrom
goxberry/expect-cleanup-payload-local-invariants

Conversation

@goxberry
Copy link
Copy Markdown
Contributor

@goxberry goxberry commented May 22, 2026

What does this PR do?

Converts 6 .expect() sites in lading_payload to .unwrap_or_else(... unreachable!("...")). Each site is guarded by an invariant established earlier in the same function or at type/pool construction.

Sites

File:line Site Local invariant
block.rs:752 bytes.len().try_into() → u32 in construct_block bytes is sized by chunk_size: u32; bytes.len() <= chunk_size
common/strings/random_string_pool.rs:125 lower_idx.try_into() → u32 lower_idx < self.inner.len(); pool length asserted ≤ u32::MAX at with_size line 35
common/strings/random_string_pool.rs:127 bytes.try_into() → u32 bytes < self.inner.len(); same assert
common/strings/string_list_pool.rs:253 u32::try_from(idx) in range_value_at idx bounded by range length, which fits in u32 by parser construction
common/strings/string_list_pool.rs:256 char::from_u32(*start as u32 + offset) *start..=*end is a parser-validated Unicode scalar range
common/strings/string_list_pool.rs:260 u32::try_from(idx) (Number variant) same as line 253

Why this PR is separate from #1885

Both PRs convert .expect() to unwrap_or_else(... unreachable!()). The split is by where the invariant lives: #1885 (Group A) handles five sites inside Result-returning functions where the agent initially recommended ?-propagation; this PR (Group B) handles six sites where the surrounding function does not return Result, so the only options were .expect() or unreachable!(). The treatment ends up identical, but reviewing them in one batch made the inventory harder to follow.

Motivation

Fourth per-crate cleanup PR in the stack started by #1882. Remaining sub-stack:

  • Group C (next): ~16 .expect() sites where the message is the contract (handle-table lookups, documented API panics) — annotated with #[expect(clippy::expect_used, reason = "...")].
  • Group D: real bug fix at block.rs:123 (the arbitrary::Arbitrary impl for Block can panic when u32::arbitrary returns 0).
  • Final: drop the lading_payload quarantine.

Related issues

Stacked on #1885.

Additional Notes

  • cargo build --all-targets --all-features
  • cargo clippy --all-targets --all-features
  • cargo test -p lading-payload --lib ✓ (244 passed)
  • No public-API change. Diff: 4 files (3 src + CHANGELOG), +26 −6.

Copy link
Copy Markdown
Contributor Author

goxberry commented May 22, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@datadog-prod-us1-6

This comment has been minimized.

Six `usize → u32` and `Option<char>` `.expect()` sites in
`lading_payload` are guarded by invariants established earlier in the
same function (or at pool construction):

- `block.rs:752` — `bytes.len().try_into()` to u32 inside
  `construct_block`; `bytes` is sized by `chunk_size: u32`.
- `random_string_pool.rs:125,127` — `lower_idx.try_into()` and
  `bytes.try_into()` for `Handle::PosAndLength(u32, u32)`. The pool's
  inner length is asserted to fit in u32 at construction (`with_size`
  line 35); `lower_idx` and `bytes` are both bounded by it.
- `string_list_pool.rs:253,260` — `u32::try_from(idx)` inside
  `range_value_at`; `idx` is bounded by the range length, which fits
  in u32 by construction.
- `string_list_pool.rs:256` — `char::from_u32(*start as u32 + offset)`
  inside the same function; `*start..=*end` is a valid Unicode scalar
  range by parser invariant.

Same pattern as #1884 and #1885. No runtime behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant