{"id":"71302d7f26bc8a3d","repo":"rust-lang/cargo","slug":"match-ensured-element-is-present","errorCode":null,"errorMessage":"match ensured element is present","messagePattern":"match ensured element is present","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":1050,"sourceCode":"                    anyhow::bail!(\n                        \"unexpectedly found multiple copies of crate `{dependency}` at `{source}`\"\n                    )\n                }\n            }\n        }\n    }\n}\n\nfn infer_package_for_git_source(\n    mut packages: Vec<Package>,\n    src: &dyn std::fmt::Display,\n) -> CargoResult<Package> {\n    let package = match packages.len() {\n        0 => unreachable!(\n            \"this function should only be called with packages from `GitSource::read_packages` \\\n            and that call should error for us when there are no packages\"\n        ),\n        1 => packages.pop().expect(\"match ensured element is present\"),\n        _ => {\n            let mut names: Vec<_> = packages\n                .iter()\n                .map(|p| p.name().as_str().to_owned())\n                .collect();\n            names.sort_unstable();\n            anyhow::bail!(\n                \"multiple packages found at `{src}`:\\n    {}\\nTo disambiguate, run `cargo add --git {src} <package>`\",\n                names\n                    .iter()\n                    .map(|s| s.to_string())\n                    .coalesce(|x, y| if x.len() + y.len() < 78 {\n                        Ok(format!(\"{x}, {y}\"))\n                    } else {\n                        Err((x, y))\n                    })\n                    .into_iter()\n                    .format(\"\\n    \"),","sourceCodeStart":1032,"sourceCodeEnd":1068,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L1032-L1068","documentation":"This panic is in infer_package_for_git_source(). When packages.len() == 1 (exactly one package found in the git source), the code calls packages.pop().expect(\"match ensured element is present\"). The match statement on line 1045 guarantees len is exactly 1 in this arm, so pop() must return Some. This is a pure structural invariant.","triggerScenarios":"Theoretically unreachable — if packages.len() == 1 then Vec::pop always returns Some. This could only fire due to memory corruption, a concurrent mutation of the packages vector (which is owned and not shared), or an extremely unlikely allocator bug.","commonSituations":"Effectively never fires in practice; if it does, suspect memory corruption, a faulty RAM module, or a severe allocator bug. No normal cargo usage should trigger this.","solutions":["Re-run cargo add — if it was a transient memory issue it won't recur.","Run a memory diagnostic (memtest86) if this panic recurs consistently.","Report as a cargo bug with full reproduction steps, as it indicates a deeper system issue."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// This is effectively unreachable; catch as a last resort\nuse std::panic;\nlet result = panic::catch_unwind(|| {\n    infer_package_for_git_source(packages, &src)\n});\nif result.is_err() {\n    eprintln!(\"internal error in package inference; retrying\");\n    // retry or fallback\n}","preventionTips":["This panic is effectively unreachable under normal conditions.","If it recurs, suspect memory corruption or hardware issues.","Keep cargo updated to the latest stable release."],"tags":["rust","cargo","panic","invariant","cargo-add","git-source","unreachable"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}