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

Cannot mutably borrow in a pattern guard false positive #37510

Closed
matklad opened this issue Nov 1, 2016 · 3 comments
Closed

Cannot mutably borrow in a pattern guard false positive #37510

matklad opened this issue Nov 1, 2016 · 3 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@matklad
Copy link
Member

matklad commented Nov 1, 2016

This code fails to compile:

fn foo(_: &mut i32) -> bool { true }

fn main() {
    let opt = Some(92);
    let mut x = 62;

    if let Some(_) = opt {

    } else if foo(&mut x) {

    }
}
error
error[E0301]: cannot mutably borrow in a pattern guard
 --> main.rs:9:19
  |
9 |     } else if foo(&mut x) {
  |                   ^^^^^^ borrowed mutably in pattern guard

error[E0301]: cannot mutably borrow in a pattern guard
 --> main.rs:9:24
  |
9 |     } else if foo(&mut x) {
  |                        ^ borrowed mutably in pattern guard

error: aborting due to 2 previous errors

I believe this is a bug, because you can just add a pair of braces to "fix" it:

if let Some(_) = opt {

} else { if foo(&mut x) {

} }
@TimNN
Copy link
Contributor

TimNN commented Nov 1, 2016

Duplicate of #28449, I think.

@roblabla
Copy link
Contributor

roblabla commented Nov 6, 2016

I'm hitting this as well. #28449 was closed without much reason. There's only some talk about why the current behavior is what it is, but no mention about why the current behavior is correct.

Is there any reason the above code doesn't desugar to something like

match opt {
    Some(_) => {},
    _ => if foo(&mut x) {
    }
}

It seems like that would be the correct implementation :|.

@Mark-Simulacrum
Copy link
Member

This code compiles today as far as I can tell. I'm going to mark as E-needstest.

@Mark-Simulacrum Mark-Simulacrum added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label May 15, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 22, 2017
Add tests for a few issues.

Fixes rust-lang#41998
Fixes rust-lang#38381
Fixes rust-lang#37515
Fixes rust-lang#37510
Fixes rust-lang#37508
Fixes rust-lang#37366
Fixes rust-lang#37323
Fixes rust-lang#37051
Fixes rust-lang#36839
Fixes rust-lang#35570
Fixes rust-lang#34373
Fixes rust-lang#34222

Certainly not all of the E-needstest issues right now, but I started to get bored.
bors added a commit that referenced this issue Jun 24, 2017
Add tests for a few issues.

Fixes #41998
Fixes #38381
Fixes #37515
Fixes #37510
Fixes #37366
Fixes #37323
Fixes #37051
Fixes #36839
Fixes #35570
Fixes #34373
Fixes #34222

Certainly not all of the E-needstest issues right now, but I started to get bored.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

4 participants