{"record":{"id":"d28d41c50a9ae185","repo":"gleam-lang/gleam","slug":"index-out-of-bounds","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/error.rs","lineNumber":870,"sourceCode":"        if let Some(element) = current.get_mut(0) {\n            *element = i;\n        }\n        let a_idx = i - 1;\n\n        // column by column\n        for j in 1..=b.len() {\n            let b_idx = j - 1;\n\n            // There is no cost to substitute a character with itself.\n            let substitution_cost = match (a.get(a_idx), b.get(b_idx)) {\n                (Some(&a_char), Some(&b_char)) => {\n                    if a_char == b_char {\n                        0\n                    } else {\n                        1\n                    }\n                }\n                _ => panic!(\"Index out of bounds\"),\n            };\n\n            let insertion = current.get(j - 1).map_or(usize::MAX, |&x| x + 1);\n\n            if let Some(value) = current.get_mut(j) {\n                *value = std::cmp::min(\n                    // deletion\n                    prev.get(j).map_or(usize::MAX, |&x| x + 1),\n                    std::cmp::min(\n                        // insertion\n                        insertion,\n                        // substitution\n                        prev.get(j - 1)\n                            .map_or(usize::MAX, |&x| x + substitution_cost),\n                    ),\n                );\n            }\n","sourceCodeStart":852,"sourceCodeEnd":888,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/error.rs#L852-L888","documentation":"A panic in error.rs inside the edit-distance (Levenshtein-style) matrix computation used to suggest 'did you mean' alternatives (e.g. for `gleam add` package names): the loop indexes rows of the DP matrix beyond its bounds. The Error type's command variant / command dispatch reached this helper with indices larger than the matrix, indicating a mismatch between the compared string lengths and the allocated matrix.","triggerScenarios":"Running a gleam CLI command (e.g. `gleam add <name>`, which builds Error values at compiler-cli/src/add.rs) whose name-matching path passes strings whose lengths exceed the preallocated distance matrix dimensions.","commonSituations":"Typing a package name into `gleam add`/`gleam remove` that triggers the suggestion engine; very long names or empty strings can expose an off-by-one in the distance calculation.","solutions":["Retry with a correctly spelled package name; the panic occurs while trying to suggest alternatives for a bad name.","Update gleam to the latest version where the distance-matrix bug may be fixed.","File an issue with the exact command and argument at github.com/gleam-lang/gleam."],"exampleFix":"// before (CLI)\ngleam add gleam_otp  // with a misspelled/edge-case name causing the panic\n// after\ngleam add otp  // or check spelling; upgrade gleam if the panic persists","handlingStrategy":"retry","validationCode":"// Validate the package name before invoking gleam add\nfn valid_pkg_name(name: &str) -> bool {\n    !name.is_empty() && name.len() < 64 && name.chars().all(|c| c.is_ascii_lowercase() || c == '_')\n}","typeGuard":"fn is_usable_name(s: &str) -> bool { !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') }","tryCatchPattern":"let status = Command::new(\"gleam\").args([\"add\", name]).status()?;\nif !status.success() {\n    eprintln!(\"gleam add failed; verify the package name on hex.pm\");\n}","preventionTips":["Double-check package names against hex.pm before using `gleam add`.","Avoid empty or extremely long names that stress the suggestion engine.","Update gleam; the edit-distance matrix bug may be fixed in newer releases."],"tags":["cli","internal","panic","string-matching"],"backgroundTag":"index-out-of-bounds","analyzedSha":"49f8762da595eb3c8f576656a4200a142ca188c9","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}