Inconsistent error from rust-analyzer

⚓ Rust    📅 2025-11-06    👤 surdeus    👁️ 6      

surdeus

I have an error from rust-analyzer I'm confused by: it appears to be a contradiction of my explicit type specification. Also, if I run cargo build in my project everything compiles just fine, so this does appear to be a problem specific to my setup of rust-analyzer.

Here's an image highlighting the problem (text of this code has been pasted below as well)

image

fn string_to_expression(x: String, span: Option<Range<usize>>) -> Result<Value> {
    let exprs = EXPRESSION.captures_iter(&x);
    // there are multiple expressions interpolated into the string
    let mut interps = Vec::new();
    // NOTE: rust-analyzer raises some spurious errors here because it infers the wrong
    // type even though its explicitly set ???
    let mut last_match: std::ops::Range<usize> = 0..0;
    // push rest
    for expr in exprs {
        let r: std::ops::Range<usize> = expr.get(0).expect("full match").range();
        if r.len() == x.len() {
            return match_to_expression(&span, expr.get(1).expect("variable name"));
        }
        if last_match.end < r.start {
            interps.push(Value::String(x[last_match.end..r.start].into()));
        }
        last_match = r;

        interps.push(match_to_expression(
            &span,
            expr.get(1).expect("variable name"),
        )?);
    }
    if last_match.start == 0 && last_match.end == 0 {
        return Ok(check_unmatched_braces(x, span));
    }
    if last_match.end < x.len() {
        interps.push(check_unmatched_braces(x[last_match.end..].into(), span));
    }
    return Ok(Value::Interp(interps));
}

I'm looking for any insight the community might have about what I'm seeing. (I'm happy to provide further information).

Further, possibly relevant context: I'm been having some trouble because I'm using mise.toml to setup my toolchain (two different versions of rust: one globally that rust-analyzer was picking up and one project-specific). I've recently added a toolchain.toml file and I have these vscode settings specified in my workspace:

    "rust-analyzer.server.extraEnv": {
        "RUSTUP_TOOLCHAIN": "1.90.0"
    },
    "rust-analyzer.cargo.sysroot": "${userHome}/.rustup/toolchains/1.90.0-aarch64-apple-darwin",
    "rust-analyzer.cargo.sysrootSrc": "${userHome}/.rustup/toolchains/1.90.0-aarch64-apple-darwin/lib/rustlib/src/rust",

This seems to have removed some of the earlier errors I was having with rust-analyzer, but I'm still seeing this problem. I've removed all of target and recompiled after making those changes just to verify there wasn't some issue with older build artifacts.

This project is public (but still WIP, and not yet easy to read through as an outsider as I make a mess of things); as such the interped reader may choose venture here.

1 post - 1 participant

Read full topic

🏷️ Rust_feed