Confusing report for `let guard` borrows of owned values used in match expression
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
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
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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di rust-lang/rust
-
Remove unnecessary parentheses when suggesting to remove '&' from a patternForse già presa @BartSimpson001 l’ha presa 1 giorno fa. ApertaA-diagnostics D-invalid-suggestion T-compiler
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Windows `File::set_permissions` silently does nothing when clearing `readonly` leaves `FileAttributes == 0`Forse già presa @ChrisDenton l’ha presa 11 giorni fa. ApertaA-filesystem C-bug O-windows T-libs
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
A-run-make relnotes relnotes-needs-review relnotes-tracking-issue T-lang T-libs
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
A-LLVM O-Arm O-UEFI O-x86_64 relnotes relnotes-needs-review relnotes-tracking-issue T-compiler
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
rust-lang/rust#165504 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
relnotes relnotes-tracking-issue T-libs
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di rust-lang/rust
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
antithesishq/bombadil#473 ·
I maintainer di solito rispondono entro 2 giorni
-
bench/api.sh exec test fails with curl exit 52, and --ignore-failure still records the timingsApertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
libracoder/kiri#669 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
I maintainer di solito rispondono entro 5 giorni
-
autofixer: follow-up
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
tursodatabase/turso#12217 ·
I maintainer di solito rispondono entro 1 giorno
-
Declare the CMS schema library in the optional recipients extraForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaarea:python in-progress
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
yhay81/pyropdf#1005 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno