-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
librustc: Disallow mutation and assignment in pattern guards, and modify
the CFG for match statements. There were two bugs in issue #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 #14684. [breaking-change]
- Loading branch information
Showing
10 changed files
with
383 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b2eb888
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from pnkfelix
at pcwalton@b2eb888
b2eb888
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging pcwalton/rust/borrowck-pattern-guards = b2eb888 into auto
b2eb888
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pcwalton/rust/borrowck-pattern-guards = b2eb888 merged ok, testing candidate = 6635fe7
b2eb888
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/562
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/561
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-c/builds/560
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/561
success: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/561
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/562
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/561
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/567
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-c/builds/562
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/561
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android-t/builds/564
success: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/562
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/562
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/563
b2eb888
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = 6635fe7