rust-lang/cargo · error
match ensured element is present
Error message
match ensured element is present
What it means
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.
Source
Thrown at src/ops/cargo_add/mod.rs:1050
anyhow::bail!(
"unexpectedly found multiple copies of crate `{dependency}` at `{source}`"
)
}
}
}
}
}
fn infer_package_for_git_source(
mut packages: Vec<Package>,
src: &dyn std::fmt::Display,
) -> CargoResult<Package> {
let package = match packages.len() {
0 => unreachable!(
"this function should only be called with packages from `GitSource::read_packages` \
and that call should error for us when there are no packages"
),
1 => packages.pop().expect("match ensured element is present"),
_ => {
let mut names: Vec<_> = packages
.iter()
.map(|p| p.name().as_str().to_owned())
.collect();
names.sort_unstable();
anyhow::bail!(
"multiple packages found at `{src}`:\n {}\nTo disambiguate, run `cargo add --git {src} <package>`",
names
.iter()
.map(|s| s.to_string())
.coalesce(|x, y| if x.len() + y.len() < 78 {
Ok(format!("{x}, {y}"))
} else {
Err((x, y))
})
.into_iter()
.format("\n "),View on GitHub (pinned to 0e07a15537)
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.
Defensive patterns
Strategy: try-catch
Try / catch
// This is effectively unreachable; catch as a last resort
use std::panic;
let result = panic::catch_unwind(|| {
infer_package_for_git_source(packages, &src)
});
if result.is_err() {
eprintln!("internal error in package inference; retrying");
// retry or fallback
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- manifest validated
- latest always has a source
- as `None` are compatible, we can't be here
- source should be resolved before here
- only none when there is 1
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/71302d7f26bc8a3d.json.
Report an issue: GitHub.