Confusing report for `let guard` borrows of owned values used in match expression

Aperta
#166,180 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

Start by reproducing the supplied example with rustc and inspect the borrow-checker diagnostic code that reports E0505, especially how it labels borrows and moves in if let match guards. Compare the current spans with the expected output and check nearby diagnostic tests for similar cases. Done means the diagnostic points to both relevant locations and includes the suggested borrowing help without applying it automatically.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-borrow-checker A-diagnostics A-patterns T-compiler
Code
pub fn foo(v: Option<Vec<u8>>) {
    match v {
        Some(v) if let Some(r) = v.strip_prefix(b"foo ") => {
            println!("{:?}", r);
        }
        _ => ()
    }
}
Current output
error[E0505]: cannot move out of `v.0` because it is borrowed
 --> src/lib.rs:3:14
  |
3 |         Some(v) if let Some(r) = v.strip_prefix(b"foo ") => {
  |              ^
  |              |
  |              move out of `v.0` occurs here
  |              borrow of `v.0` occurs here
4 |             println!("{:?}", r);
  |                              - borrow later used here
Desired output
error[E0505]: cannot move out of `v.0` because it is borrowed
 --> src/lib.rs:3:14
  |
3 |         Some(v) if let Some(r) = v.strip_prefix(b"foo ") => {
  |                                  ^                       ^
  |                                  |                       |
  |                   borrow of `v.0` occurs here     move out of `v.0` occurs here
4 |             println!("{:?}", r);
  |                              - borrow later used here
help: try borrowing the value here:
  |         Some(ref v) if let Some(r) = v.strip_prefix(b"foo ") => {
Rationale and extra context

Currently the error is quite opaque and points to entirely the wrong location.

What's doing the borrow is pretty easy to infer but why the value is being moved out is not as easy, especially since having to think about the data flow of patterns has become a lot less routine since match ergonomics.

I would assume before if_let_guard the only (easy?) way to get this error was using at-patterns which mixed moving things and creating overlapping references, then trying to use those references in the expression, which would be why the compiler points everything into the pattern.

The suggestion would not be machine applicable, as there could also be a legitimate borrow error (e.g. the expression tries to use both the owned value and its sub-borrow), in which case the suggestion is incorrect

Rust Version
rustc 1.100.0 (0ef1bc386 2026-11-09)
binary: rustc
commit-hash: 0ef1bc386059596dada8fb65b50425aab2ea637a
commit-date: 2026-11-09
host: x86_64-unknown-linux-gnu
release: 1.100.0
LLVM version: 23.1.5
Lingua principale
Rust
Stelle
122k
Fork
16.7k
Merge medio
2g 16h
PR unite (30g)
515

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rust

Tutte le issue di rust-lang/rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.