rust-lang/cargo · error

in the non-empty branch

Error message

in the non-empty branch

What it means

Invariant in resolver error formatting: when reporting `ConflictReason::MissingFeature`, the code calls `candidates.last().expect("in the non-empty branch")`. This branch is only entered when there is at least one candidate for the dependency; the expect guards that assumption.

Source

Thrown at src/resolver/errors.rs:167

                    msg.push_str("` as well:\n");
                    msg.push_str(&describe_path_in_context(resolver_ctx, p));
                    msg.push_str("\nnote: only one package in the dependency graph may specify the same links value to ensure that only one copy of a native library is linked in the final binary");
                    msg.push_str("\nfor more information, see https://doc.rust-lang.org/cargo/reference/resolver.html#links");
                    msg.push_str("\nhelp: try to adjust your dependencies so that only one package uses the `links = \"");
                    msg.push_str(link);
                    msg.push_str("\"` value");
                }
                ConflictReason::MissingFeature(feature) => {
                    msg.push_str("\n\npackage `");
                    msg.push_str(&*p.name());
                    msg.push_str("` depends on `");
                    msg.push_str(&*dep.package_name());
                    msg.push_str("` with feature `");
                    msg.push_str(feature);
                    msg.push_str("` but `");
                    msg.push_str(&*dep.package_name());
                    msg.push_str("` does not have that feature.\n");
                    let latest = candidates.last().expect("in the non-empty branch");
                    if let Some(closest) = closest(feature, latest.features().keys(), |k| k) {
                        msg.push_str("help: there is a feature `");
                        msg.push_str(closest);
                        msg.push_str("` with a similar name\n");
                    } else if !latest.features().is_empty() {
                        let mut features: Vec<_> =
                            latest.features().keys().map(|f| f.as_str()).collect();
                        features.sort();
                        msg.push_str("help: available features: ");
                        msg.push_str(&features.join(", "));
                        msg.push_str("\n");
                    }
                    // p == parent so the full path is redundant.
                }
                ConflictReason::RequiredDependencyAsFeature(feature) => {
                    msg.push_str("\n\npackage `");
                    msg.push_str(&*p.name());
                    msg.push_str("` depends on `");

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Report a cargo bug with the resolution failure context (the missing-feature message would have preceded it).
  2. Loosen constraints (rust-version, target, feature) that may be eliminating all candidates and retry.
  3. Update cargo — resolver error-reporting regressions are patched quickly.

Example fix

// before
let latest = candidates.last().expect("in the non-empty branch");

// after
let latest = candidates.last().ok_or_else(|| anyhow::anyhow!(
    "reached missing-feature report with no candidates for `{}`", dep.package_name()))?;
Defensive patterns

Strategy: validation

Validate before calling

// If you mirror resolver error formatting, guard the candidates lookup.
let latest = match candidates.last() {
    Some(c) => c,
    None => return format!("missing feature (no candidates available for `{}`)", dep.package_name()),
};

Prevention

When it happens

Trigger: Fires only if the missing-feature error path is reached with an empty `candidates` list — i.e. the resolver is formatting a "missing feature" conflict for a dependency that has zero candidate versions, contradicting the branch's non-empty precondition.

Common situations: Cargo bug in conflict/error formatting where `candidates` is unexpectedly empty; a dependency whose all versions were filtered out (e.g. by `rust-version`/platform) but still reached the missing-feature reporter. End users see this only as a panic during an already-failing resolution.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/d95c7b33e895653d.json. Report an issue: GitHub.