rust-lang / rust-lang/rust-analyzer

expected string literal error

Open
#18,686 11 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-proc-macro C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Just posting it here to let you know about bug in macro expansion. Basically, rust-analyzer reports expected string literal error for completely valid code. More information can be found here https://github.com/kyrias/sqlx-conditional-queries/issues/18

rust-analyzer version: 0.3.2212
rustc version: rustc 1.83.0 (90b35a623 2024-11-26)
editor or extension: VSCode 2024-12-11, 22:49:33
code snippet to reproduce:

#[derive(Debug)]
struct OutputType {
    id: i32,
    name: String,
}

async fn query_conditional(db: &MySqlPool) -> Result<(), sqlx::Error> {
    let limit = Some(5);
    let rows: Vec<OutputType> = conditional_query_as!(
        OutputType,
        r#"
        select id, name from users
        {#limit}
    "#,
    #limit = match limit {
        Some(_) => "limit {limit}",
        None => ""
    },
    )
    .fetch_all(db)
    .await?;

    for row in rows {
        debug!("-------------------");
        debug!("{}", row.id);
        debug!("{}", row.name);
    }

    Ok(())
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic with the provided Rust snippet, rust-analyzer 0.3.2212, and the listed rustc and VSCode versions. Start by tracing macro expansion for conditional_query_as! and read the linked sqlx-conditional-queries issue; done means valid code no longer reports expected string literal.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.