From 4fe40635ef3c2cdbc2e3f62ca71f0e2235e70639 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Sun, 3 Jun 2018 23:31:49 -0400 Subject: Implementation of RFC 2086 - Allow Irrefutable Let patterns --- src/libsyntax/feature_gate.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/libsyntax') diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 51788b6063a..f08a404a02a 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -467,6 +467,9 @@ declare_features! ( // Scoped attributes (active, tool_attributes, "1.25.0", Some(44690), None), + // allow irrefutable patterns in if-let and while-let statements (RFC 2086) + (active, irrefutable_let_pattern, "1.27.0", Some(44495), None), + // Allows use of the :literal macro fragment specifier (RFC 1576) (active, macro_literal_matcher, "1.27.0", Some(35625), None), -- cgit 1.4.1-3-g733a5 From e7c7f5f07139cd4afe6e8db13942a0ec250ded70 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 6 Jun 2018 22:03:42 -0400 Subject: Allowing attributes to be on if let statements --- src/libsyntax/parse/parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 95d519eae58..4fdfc74ca5d 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2597,7 +2597,7 @@ impl<'a> Parser<'a> { attrs.extend::>(expr.attrs.into()); expr.attrs = attrs; match expr.node { - ExprKind::If(..) | ExprKind::IfLet(..) => { + ExprKind::If(..) => { if !expr.attrs.is_empty() { // Just point to the first attribute in there... let span = expr.attrs[0].span; -- cgit 1.4.1-3-g733a5 From 92d4ae2be2e9428c9ab523f04f917a41ac0f760e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 8 Jun 2018 11:25:35 -0400 Subject: rename `irrefutable_let_pattern` to `irrefutable_let_patterns` --- .../language-features/irrefutable-let-pattern.md | 23 ---------------------- .../language-features/irrefutable-let-patterns.md | 23 ++++++++++++++++++++++ src/librustc_mir/hair/pattern/check_match.rs | 4 ++-- src/libsyntax/feature_gate.rs | 2 +- ...eature-gate-without_gate_irrefutable_pattern.rs | 4 ++-- ...ould-fail-no_gate_irrefutable_if_let_pattern.rs | 2 +- ...ould-fail-with_gate_irrefutable_pattern_deny.rs | 6 +++--- .../run-pass/allow_irrefutable_let_patterns.rs | 8 ++++---- 8 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 src/doc/unstable-book/src/language-features/irrefutable-let-pattern.md create mode 100644 src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md (limited to 'src/libsyntax') diff --git a/src/doc/unstable-book/src/language-features/irrefutable-let-pattern.md b/src/doc/unstable-book/src/language-features/irrefutable-let-pattern.md deleted file mode 100644 index 13681c96811..00000000000 --- a/src/doc/unstable-book/src/language-features/irrefutable-let-pattern.md +++ /dev/null @@ -1,23 +0,0 @@ -# `irrefutable_let_pattern` - -The tracking issue for this feature is: [#44495] - -[#44495]: https://github.com/rust-lang/rust/issues/44495 - ------------------------- - -This feature changes the way that the irrefutable pattern is handled -in the `if let` and `while let` forms. The old way was to always error -but now with a tag the error-by-default lint can be switched off. - -```rust -#![feature(irrefutable_let_pattern)] - -fn main() { - #[allow(irrefutable_let_pattern)] - if let _ = 5 {} - - #[allow(irrefutable_let_pattern)] - while let _ = 5 {} -} -``` diff --git a/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md b/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md new file mode 100644 index 00000000000..0d782e1c539 --- /dev/null +++ b/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md @@ -0,0 +1,23 @@ +# `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 the irrefutable pattern is handled +in the `if let` and `while let` forms. The old way was to always error +but now with a tag the error-by-default lint can be switched off. + +```rust +#![feature(irrefutable_let_patterns)] + +fn main() { + #[allow(irrefutable_let_patterns)] + if let _ = 5 {} + + #[allow(irrefutable_let_patterns)] + while let _ = 5 {} +} +``` diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index d5d69bf7f2b..895f050d399 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -369,7 +369,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>, NotUseful => { match source { hir::MatchSource::IfLetDesugar { .. } => { - if cx.tcx.features().irrefutable_let_pattern { + if cx.tcx.features().irrefutable_let_patterns { cx.tcx.lint_node( lint::builtin::IRREFUTABLE_LET_PATTERNS, hir_pat.id, pat.span, @@ -404,7 +404,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>, }, // The arm with the wildcard pattern. 1 => { - if cx.tcx.features().irrefutable_let_pattern { + if cx.tcx.features().irrefutable_let_patterns { cx.tcx.lint_node( lint::builtin::IRREFUTABLE_LET_PATTERNS, hir_pat.id, pat.span, diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index f08a404a02a..01820379c96 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -468,7 +468,7 @@ declare_features! ( (active, tool_attributes, "1.25.0", Some(44690), None), // allow irrefutable patterns in if-let and while-let statements (RFC 2086) - (active, irrefutable_let_pattern, "1.27.0", Some(44495), None), + (active, irrefutable_let_patterns, "1.27.0", Some(44495), None), // Allows use of the :literal macro fragment specifier (RFC 1576) (active, macro_literal_matcher, "1.27.0", Some(35625), None), diff --git a/src/test/compile-fail/feature-gate-without_gate_irrefutable_pattern.rs b/src/test/compile-fail/feature-gate-without_gate_irrefutable_pattern.rs index 1facb6b152a..7cd026e21d6 100644 --- a/src/test/compile-fail/feature-gate-without_gate_irrefutable_pattern.rs +++ b/src/test/compile-fail/feature-gate-without_gate_irrefutable_pattern.rs @@ -1,4 +1,4 @@ -// gate-test-irrefutable_let_pattern +// 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 @@ -11,7 +11,7 @@ // except according to those terms. fn main() { - #[allow(irrefutable_let_pattern)] + #[allow(irrefutable_let_patterns)] if let _ = 5 {} //~^ ERROR 15:12: 15:13: irrefutable if-let pattern [E0162] } diff --git a/src/test/compile-fail/should-fail-no_gate_irrefutable_if_let_pattern.rs b/src/test/compile-fail/should-fail-no_gate_irrefutable_if_let_pattern.rs index 71dcbf329c7..8c9a24f4e72 100644 --- a/src/test/compile-fail/should-fail-no_gate_irrefutable_if_let_pattern.rs +++ b/src/test/compile-fail/should-fail-no_gate_irrefutable_if_let_pattern.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// should-fail-irrefutable_let_pattern +// should-fail-irrefutable_let_patterns fn main() { if let _ = 5 {} //~^ ERROR irrefutable if-let pattern [E0162] diff --git a/src/test/compile-fail/should-fail-with_gate_irrefutable_pattern_deny.rs b/src/test/compile-fail/should-fail-with_gate_irrefutable_pattern_deny.rs index 2f9b7f0628d..6f95f10c0d9 100644 --- a/src/test/compile-fail/should-fail-with_gate_irrefutable_pattern_deny.rs +++ b/src/test/compile-fail/should-fail-with_gate_irrefutable_pattern_deny.rs @@ -8,10 +8,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(irrefutable_let_pattern)] +#![feature(irrefutable_let_patterns)] -// should-fail-irrefutable_let_pattern_with_gate +// should-fail-irrefutable_let_patterns_with_gate fn main() { if let _ = 5 {} - //~^ ERROR irrefutable if-let pattern [irrefutable_let_pattern] + //~^ ERROR irrefutable if-let pattern [irrefutable_let_patterns] } diff --git a/src/test/run-pass/allow_irrefutable_let_patterns.rs b/src/test/run-pass/allow_irrefutable_let_patterns.rs index 3a4f226dfe6..689a54d60f3 100644 --- a/src/test/run-pass/allow_irrefutable_let_patterns.rs +++ b/src/test/run-pass/allow_irrefutable_let_patterns.rs @@ -8,14 +8,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(irrefutable_let_pattern)] +#![feature(irrefutable_let_patterns)] -// must-compile-successfully-irrefutable_let_pattern_with_gate +// must-compile-successfully-irrefutable_let_patterns_with_gate fn main() { - #[allow(irrefutable_let_pattern)] + #[allow(irrefutable_let_patterns)] if let _ = 5 {} - #[allow(irrefutable_let_pattern)] + #[allow(irrefutable_let_patterns)] while let _ = 5 { break; } -- cgit 1.4.1-3-g733a5 From d6f13c0e207bff8b09eded9bd62d61f9d6d04b4c Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 12 Jun 2018 14:49:17 -0400 Subject: update wording, do not change parser --- .../src/language-features/irrefutable-let-patterns.md | 17 +++++++++++------ src/libsyntax/parse/parser.rs | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'src/libsyntax') diff --git a/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md b/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md index 0d782e1c539..716deaba01e 100644 --- a/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md +++ b/src/doc/unstable-book/src/language-features/irrefutable-let-patterns.md @@ -6,18 +6,23 @@ The tracking issue for this feature is: [#44495] ------------------------ -This feature changes the way that the irrefutable pattern is handled -in the `if let` and `while let` forms. The old way was to always error -but now with a tag the error-by-default lint can be switched off. +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() { - #[allow(irrefutable_let_patterns)] + // These two examples used to be errors, but now they + // trigger a lint (that is allowed): if let _ = 5 {} - - #[allow(irrefutable_let_patterns)] while let _ = 5 {} } ``` diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 4fdfc74ca5d..95d519eae58 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2597,7 +2597,7 @@ impl<'a> Parser<'a> { attrs.extend::>(expr.attrs.into()); expr.attrs = attrs; match expr.node { - ExprKind::If(..) => { + ExprKind::If(..) | ExprKind::IfLet(..) => { if !expr.attrs.is_empty() { // Just point to the first attribute in there... let span = expr.attrs[0].span; -- cgit 1.4.1-3-g733a5