louis-e/arnis · error

internal error: entered unreachable code

Error message

internal error: entered unreachable code

What it means

`Tree::random_type` maps a random number to a `TreeType` across arms like 71..=77 => Pine, ..., 99..=100 => Mangrove, with `_ => unreachable!()`. The panic fires when the random source yields a value outside the covered range (1..=100 here), meaning the RNG call and the match table disagree.

Source

Thrown at src/element_processing/tree.rs:477

    /// The species mix for a scattered tree, keyed on the column.
    fn random_type(x: i32, z: i32) -> TreeType {
        let mut rng = coord_rng(x, z, 0);
        match rng.random_range(1..=100) {
            1..=20 => TreeType::Oak,
            21..=32 => TreeType::Spruce,
            33..=44 => TreeType::Birch,
            45..=50 => TreeType::DarkOak,
            51..=56 => TreeType::Jungle,
            57..=62 => TreeType::Acacia,
            63..=64 => TreeType::Cherry,
            65..=70 => TreeType::TallOak,
            71..=77 => TreeType::Pine,
            78..=84 => TreeType::Bush,
            85..=88 => TreeType::AzaleaBush,
            89..=92 => TreeType::Willow,
            93..=98 => TreeType::FloweringOak,
            99..=100 => TreeType::Mangrove,
            _ => unreachable!(),
        }
    }

    /// Creates a tree at the specified coordinates.
    pub fn create(
        editor: &mut WorldEditor,
        (x, y, z): Coord,
        building_footprints: Option<&BuildingFootprintBitmap>,
        bridge_surface: Option<&BridgeSurfaceMap>,
    ) {
        let tree_type = Self::random_type(x, z);
        Self::build(
            editor,
            (x, y, z),
            tree_type,
            building_footprints,
            bridge_surface,
            false,

View on GitHub (pinned to 34048924d9)

Solutions

  1. Check the generator call and make its range exactly cover the arms (e.g. 1..=100)
  2. Add a catch-all arm returning a default `TreeType` (e.g. Oak) instead of `unreachable!()`
  3. Add a debug_assert/unit test that all values in the generator range are covered

Example fix

// before
_ => unreachable!(),
// after
_ => TreeType::Oak,
// upstream: let n = rng.random_range(1..=100);
Defensive patterns

Strategy: fallback

Validate before calling

debug_assert!((1..=100).contains(&n), "tree roll {n} outside 1..=100");

Type guard

fn valid_tree_roll(n: i32) -> bool { (1..=100).contains(&n) }

Prevention

When it happens

Trigger: The RNG range used to produce the value was changed (e.g. `random_range(0..=100)`, which includes 0 if arms start at 1, or a wider range), or an arm was removed/renumbered leaving a gap such as exclusive `99..100` vs inclusive `99..=100`.

Common situations: Refactoring the tree-type distribution; switching RNG providers with different range semantics (inclusive vs exclusive upper bounds); copy-pasted match tables drifting apart.

Related errors


AI-assisted analysis of louis-e/arnis@34048924d9 (2026-09-03). Data as JSON: /api/errors/e4587d682d596adf. Report an issue: GitHub.