pydantic/monty · error

test namespace fits in u16

Error message

test namespace fits in u16

What it means

This panic is an `expect` in the test-only fault injector `corrupt_metadata_for_tests`, raised when `NamespaceId::new(self.namespace_size)` returns `None` while setting up the `FreeVarSlotOutOfRange` fault. It means the current `namespace_size` cannot be converted into a valid `NamespaceId`, i.e. the size exceeds `u16::MAX` or is otherwise invalid, so the test cannot construct an out-of-range slot value. It signals bad test fixture state, not a runtime bug.

Source

Thrown at crates/monty/src/function.rs:325

    /// Injects a metadata fault for dump validation tests.
    #[cfg(feature = "test-hooks")]
    pub(crate) fn corrupt_metadata_for_tests(&mut self, fault: FunctionMetadataFault) {
        match fault {
            FunctionMetadataFault::SignatureSlotsBeyondNamespace => {
                self.namespace_size = self.signature.total_slots() - 1;
            }
            FunctionMetadataFault::NamespaceTooLarge => {
                self.namespace_size = usize::from(u16::MAX) + 1;
            }
            FunctionMetadataFault::FreeVarLengthMismatch => {
                self.free_var_slots.pop().expect("test function has a free variable");
            }
            FunctionMetadataFault::CellVarLengthMismatch => {
                self.cell_param_indices.pop().expect("test function has an owned cell");
            }
            FunctionMetadataFault::FreeVarSlotOutOfRange => {
                let slot = NamespaceId::new(self.namespace_size).expect("test namespace fits in u16");
                *self
                    .free_var_slots
                    .first_mut()
                    .expect("test function has a free variable") = slot;
            }
            FunctionMetadataFault::CellVarSlotOutOfRange => {
                let slot = NamespaceId::new(self.namespace_size).expect("test namespace fits in u16");
                *self
                    .cell_var_slots
                    .first_mut()
                    .expect("test function has an owned cell") = slot;
            }
            FunctionMetadataFault::CellParamIndexOutOfRange => {
                *self
                    .cell_param_indices
                    .first_mut()
                    .expect("test function has an owned cell") = Some(self.signature.total_slots());
            }

View on GitHub (pinned to adc986b362)

Solutions

  1. Reorder faults so slot-range faults run before any fault that corrupts `namespace_size`.
  2. Rebuild fresh metadata for each fault instead of stacking faults on the same object.
  3. Use a fixed known-valid slot constant for the fault instead of deriving it from the (possibly corrupted) `namespace_size`.

Example fix

// before
let mut m = metadata.clone();
m.corrupt_metadata_for_tests(FunctionMetadataFault::NamespaceSizeTooLarge);
m.corrupt_metadata_for_tests(FunctionMetadataFault::FreeVarSlotOutOfRange); // panics
// after
let mut m = metadata.clone();
m.corrupt_metadata_for_tests(FunctionMetadataFault::FreeVarSlotOutOfRange); // valid namespace_size here
m = metadata.clone();
m.corrupt_metadata_for_tests(FunctionMetadataFault::NamespaceSizeTooLarge);
Defensive patterns

Strategy: validation

Validate before calling

assert!(metadata.namespace_size > 0 && metadata.namespace_size <= usize::from(u16::MAX), 'namespace_size must be a valid NamespaceId before slot faults');

Prevention

When it happens

Trigger: Calling `corrupt_metadata_for_tests(FunctionMetadataFault::FreeVarSlotOutOfRange)` (or `CellVarSlotOutOfRange`) on metadata whose `namespace_size` is 0 or above `u16::MAX`, so `NamespaceId::new` fails.

Common situations: A prior fault step (e.g. `NamespaceSizeTooLarge`) already corrupted `namespace_size` before the slot fault is applied; composing faults in an order the injector does not support.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of pydantic/monty@adc986b362 (2026-09-13). Data as JSON: /api/errors/0594b43f55368659. Report an issue: GitHub.