Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance match implied borrow #56185

Open
colinfang opened this issue Nov 23, 2018 · 1 comment
Open

Enhance match implied borrow #56185

colinfang opened this issue Nov 23, 2018 · 1 comment
Labels
A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@colinfang
Copy link

struct Foo {
    bar: Option<String>
}

fn f(foo: &Foo) -> i32 {
    match &foo.bar {
        None => 1,
        Some(s) => 2,
    }
}

The above works because of the implied borrow introduced in v1.26

However, if we don't put & before foo.bar, the ref is mandatory.

fn f(foo: &Foo) -> i32 {
    match foo.bar {
        None => 1,
        Some(ref s) => 2,
    }

Is it a good idea in this case that the compiler also implicitly adds ref for us, since we cannot move foo.bar anyway.

@estebank estebank added the A-borrow-checker Area: The borrow checker label Jan 11, 2019
@estebank
Copy link
Contributor

The diagnostic does suggest the appropriate code (borrowing foo.bar), but leaving open as a match ergonomics enhancement request:

error[E0507]: cannot move out of borrowed content
  --> src/lib.rs:8:11
   |
8  |     match foo.bar {
   |           ^^^^^^^
   |           |
   |           cannot move out of borrowed content
   |           help: consider borrowing here: `&foo.bar`
9  |         None => 1,
10 |         Some(s) => 2,
   |              - data moved here
   |
note: move occurs because `s` has type `std::string::String`, which does not implement the `Copy` trait
  --> src/lib.rs:10:14
   |
10 |         Some(s) => 2,
   |              ^

@estebank estebank added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jan 11, 2019
@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants