-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4fe4063
Implementation of RFC 2086 - Allow Irrefutable Let patterns
Nokel81 e7c7f5f
Allowing attributes to be on if let statements
Nokel81 92d4ae2
rename `irrefutable_let_pattern` to `irrefutable_let_patterns`
nikomatsakis d6f13c0
update wording, do not change parser
nikomatsakis 84de498
fix `allow(irrefutable_let_patterns)`
nikomatsakis 01fbeb5
Moving allow statemate to the function block
Nokel81 9168034
make the `while let` loop terminate
nikomatsakis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# `irrefutable_let_patterns` | ||
|
||
The tracking issue for this feature is: [#44495] | ||
|
||
[#44495]: https://github.com/rust-lang/rust/issues/44495 | ||
|
||
------------------------ | ||
|
||
This feature changes the way that "irrefutable patterns" are handled | ||
in the `if let` and `while let` forms. An *irrefutable pattern* is one | ||
that cannot fail to match -- for example, the `_` pattern matches any | ||
value, and hence it is "irrefutable". Without this feature, using an | ||
irrefutable pattern in an `if let` gives a hard error (since often | ||
this indicates programmer error). But when the feature is enabled, the | ||
error becomes a lint (since in some cases irrefutable patterns are | ||
expected). This means you can use `#[allow]` to silence the lint: | ||
|
||
```rust | ||
#![feature(irrefutable_let_patterns)] | ||
|
||
#[allow(irrefutable_let_patterns)] | ||
fn main() { | ||
// These two examples used to be errors, but now they | ||
// trigger a lint (that is allowed): | ||
if let _ = 5 {} | ||
while let _ = 5 { break; } | ||
} | ||
``` |
This file contains hidden or 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.
8000
Learn more about bidirectional Unicode characters
This file contains hidden or 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 b
8000
idirectional Unicode characters
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
src/test/compile-fail/feature-gate-without_gate_irrefutable_pattern.rs
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// gate-test-irrefutable_let_patterns | ||
|
||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[allow(irrefutable_let_patterns)] | ||
fn main() { | ||
if let _ = 5 {} | ||
//~^ ERROR 15:12: 15:13: irrefutable if-let pattern [E0162] | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/compile-fail/should-fail-no_gate_irrefutable_if_let_pattern.rs
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// should-fail-irrefutable_let_patterns | ||
fn main() { | ||
if let _ = 5 {} | ||
//~^ ERROR irrefutable if-let pattern [E0162] | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/compile-fail/should-fail-with_gate_irrefutable_pattern_deny.rs
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(irrefutable_let_patterns)] | ||
|
||
// should-fail-irrefutable_let_patterns_with_gate | ||
fn main() { | ||
if let _ = 5 {} | ||
//~^ ERROR irrefutable if-let pattern [irrefutable_let_patterns] | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(irrefutable_let_patterns)] | ||
|
||
// must-compile-successfully-irrefutable_let_patterns_with_gate | ||
#[allow(irrefutable_let_patterns)] | ||
fn main() { | ||
if let _ = 5 {} | ||
|
||
while let _ = 5 { | ||
break; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implementation of RFC 2086 - Allow Irrefutable Let patterns #49469
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