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

Đang mở
#166,180 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Rust
Star
122k
Fork
16.7k
Merge trung bình
2 ngày 16 giờ
Pull request đã merge (30 ngày)
515

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/rust

Tất cả issue của rust-lang/rust

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.