rust-lang / rust-lang/rust

"error[E0596]: cannot borrow data in a `&` reference as mutable" error doesn't say what's wrong or where it's actually wrong; same code diagnoses OK when the deductions are simpler

Open
#151,064 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-diagnostics C-enhancement T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
/*
[package]
name = "b"
version = "0.1.0"
edition = "2024"

[dependencies]
rusqlite = "0.38"
*/
use rusqlite::{Statement, Connection};
struct Statements<'conn> {
    known: Option<Statement<'conn>>,
}
impl Statements<'_> {
    fn prepare<'s, 'conn>(_: &'s mut Option<Statement<'conn>>, _: &'conn Connection, _: &str)
                          -> Result<&'s Statement<'conn>, String> {
        unimplemented!();
    }
}
fn main() -> Result<(), String> {
    let sqlite = Connection::open_in_memory().unwrap();
    let sqlite = &sqlite;
    let path_sql = "";
    let mut statements = Statements{known:None};

    if Statements::prepare(&mut statements.known, sqlite, "SELECT EXISTS(SELECT 1 FROM history WHERE file=?1);")?
        .query_row(&[&path_sql], |row| row.get(0))
        .map_err(|e| format!("known: {}", e))? {
        return Ok(());
    }

    Ok(())
}
Current output
Compiling b v0.1.0 (/home/nabijaczleweli/uwu/b)
error[E0596]: cannot borrow data in a `&` reference as mutable
  --> lib.rs:17:8
   |
17 |     if Statements::prepare(&mut statements.known, sqlite, "SELECT EXISTS(SELECT 1 FROM history WHERE file=?1);")?
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable

For more information about this error, try `rustc --explain E0596`.
error: could not compile `b` (lib) due to 1 previous error
Desired output
error[E0599]: no method named `query_row` found for reference `&Statement<'_>` in the current scope
  --> a.rs:24:10
   |
23 |        if Statements::prepare(&mut statements.known, sqlite, "SELECT EXISTS(SELECT 1 FROM history WHERE file=?1);")?
   |   ________-
   |  |________|
24 | ||         .query_row(&[&path_sql], |row| row.get(0))
   | ||_________-^^^^^^^^^_______________________________- help: use associated function syntax instead: `Statement::<'_>::query_row(&[&path_sql], |row| row.get(0))`
   | |          ||
   | |__________|this is an associated function, not a method
   |
   |
   = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `Statement<'conn>`
  --> a.rs:4:5
   |
 4 |     fn query_row(_: &[&()], _: fn(&[()]) -> Option<&()>) -> Result<bool, String> {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Rationale and extra context

I obtained the output above by mocking up an equivalent scenario without any deductive complications:

type Connection = ();
struct Statement<'conn>(&'conn ());
impl<'conn> Statement<'conn> {
    fn query_row(_: &[&()], _: fn(&[()]) -> Option<&()>) -> Result<bool, String> {
        unimplemented!();
    }
}
struct Statements<'conn> {
    known: Option<Statement<'conn>>,
}
impl Statements<'_> {
    fn prepare<'s, 'conn>(_: &'s mut Option<Statement<'conn>>, _: &'conn Connection, _: &str)
                          -> Result<&'s Statement<'conn>, String> {
        unimplemented!();
    }
}
fn main() -> Result<(), String> {
    let sqlite = ();
    let sqlite = &sqlite;
    let path_sql = ();
    let mut statements = Statements{known:None};

    if Statements::prepare(&mut statements.known, sqlite, "SELECT EXISTS(SELECT 1 FROM history WHERE file=?1);")?
        .query_row(&[&path_sql], |row| row.get(0))
        .map_err(|e| format!("known: {}", e))? {
        return Ok(());
    }

    Ok(())
}
Other cases

Rust Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3
Anything else?

No response

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 reported diagnostics from the Rust example in lib.rs, then compare them with the simplified equivalent in a.rs. Trace why the compiler selects E0596 instead of the more specific E0599 diagnostic and identify the relevant diagnostic behavior. The work is done when the original example produces the expected method-resolution explanation without regressing the simpler case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.