rust-lang/cargo · error

len() == 1 above

Error message

len() == 1 above

What it means

This panic is in the --config argument validator (validate_config_arg_dotted_key). It walks a parsed TOML document's nested tables, checking that each level has exactly one key (a dotted-key chain). The while loop on line 2436 breaks if table.len() != 1 (line 2438), so reaching line 2441 guarantees exactly one entry. The .expect("len() == 1 above") asserts table.iter().next() returns that single entry.

Source

Thrown at src/context/mod.rs:2441

    fn non_empty(d: Option<&toml_edit::RawString>) -> bool {
        d.map_or(false, |p| !p.as_str().unwrap_or_default().trim().is_empty())
    }
    fn non_empty_decor(d: &toml_edit::Decor) -> bool {
        non_empty(d.prefix()) || non_empty(d.suffix())
    }
    fn non_empty_key_decor(k: &toml_edit::Key) -> bool {
        non_empty_decor(k.leaf_decor()) || non_empty_decor(k.dotted_decor())
    }
    let ok = {
        let mut got_to_value = false;
        let mut table = doc.as_table();
        let mut is_root = true;
        while table.is_dotted() || is_root {
            is_root = false;
            if table.len() != 1 {
                break;
            }
            let (k, n) = table.iter().next().expect("len() == 1 above");
            match n {
                Item::Table(nt) => {
                    if table.key(k).map_or(false, non_empty_key_decor)
                        || non_empty_decor(nt.decor())
                    {
                        bail!(
                            "--config argument `{arg}` \
                                includes non-whitespace decoration"
                        )
                    }
                    table = nt;
                }
                Item::Value(v) if v.is_inline_table() => {
                    bail!(
                        "--config argument `{arg}` \
                        sets a value to an inline table, which is not accepted"
                    );
                }

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Simplify the --config argument to a straightforward dotted-key expression.
  2. Use a config file instead of --config for complex overrides.
  3. Update cargo — if this is a toml_edit version-compatibility bug, it will be fixed in a patch release.
Defensive patterns

Strategy: validation

Validate before calling

// Validate --config argument is a simple dotted-key expression before passing to cargo
fn validate_config_arg(arg: &str) -> Result<(), String> {
    let doc: toml_edit::DocumentMut = arg.parse()
        .map_err(|e| format!("invalid TOML: {}", e))?;
    let mut table = doc.as_table();
    let mut is_root = true;
    while table.is_dotted() || is_root {
        is_root = false;
        if table.len() != 1 { break; }
        if let Some((_, n)) = table.iter().next() {
            match n {
                toml_edit::Item::Table(t) => table = t,
                toml_edit::Item::Value(v) if v.is_inline_table() => {
                    return Err("inline tables not allowed in --config".into());
                }
                _ => break,
            }
        }
    }
    Ok(())
}

Prevention

When it happens

Trigger: Passing a --config argument to cargo whose TOML representation passes the len()==1 check but then yields no iterator element — theoretically impossible with a correct TOML parser, indicating either a toml_edit internal bug or a memory corruption issue.

Common situations: Passing complex or malformed --config arguments like cargo build --config 'foo.bar="baz"' that stress the dotted-key parser; using a cargo build with a mismatched toml_edit version; corrupted argument passing on the command line.

Related errors


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