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

librustc: Disallow mutation and assignment in pattern guards, and modify #15989

Closed
wants to merge 1 commit into from

Commits on Jul 25, 2014

  1. librustc: Disallow mutation and assignment in pattern guards, and modify

    the CFG for match statements.
    
    There were two bugs in issue rust-lang#14684. One was simply that the borrow
    check didn't know about the correct CFG for match statements: the
    pattern must be a predecessor of the guard. This disallows the bad
    behavior if there are bindings in the pattern. But it isn't enough to
    prevent the memory safety problem, because of wildcards; thus, this
    patch introduces a more restrictive rule, which disallows assignments
    and mutable borrows inside guards outright.
    
    I discussed this with Niko and we decided this was the best plan of
    action.
    
    This breaks code that performs mutable borrows in pattern guards. Most
    commonly, the code looks like this:
    
        impl Foo {
            fn f(&mut self, ...) {}
            fn g(&mut self, ...) {
                match bar {
                    Baz if self.f(...) => { ... }
                    _ => { ... }
                }
            }
        }
    
    Change this code to not use a guard. For example:
    
        impl Foo {
            fn f(&mut self, ...) {}
            fn g(&mut self, ...) {
                match bar {
                    Baz => {
                        if self.f(...) {
                            ...
                        } else {
                            ...
                        }
                    }
                    _ => { ... }
                }
            }
        }
    
    Sometimes this can result in code duplication, but often it illustrates
    a hidden memory safety problem.
    
    Closes rust-lang#14684.
    
    [breaking-change]
    pcwalton committed Jul 25, 2014
    5 Configuration menu
    Copy the full SHA
    b2eb888 View commit details
    Browse the repository at this point in the history