-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Formally implement let chains #88642
Conversation
Some changes occurred in src/tools/clippy. cc @rust-lang/clippy |
r? @oli-obk (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
367d905
to
9721fbf
Compare
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #88950) made this pull request unmergeable. Please resolve the merge conflicts. |
Coding looks extraordinary to say the least keep up the executive chain
working towards fundamental exposure.
…On Sat, Sep 4, 2021, 9:02 AM Caio ***@***.***> wrote:
Opening early for any possible code comments.
Let chains
My longest and hardest contribution since #64010
<#64010>.
Thanks to @Centril <https://github.com/Centril> for creating the RFC and
special thanks to @matthewjasper <https://github.com/matthewjasper> for
helping me since the beginning of this journey. In fact, @matthewjasper
<https://github.com/matthewjasper> did much of the complicated MIR stuff
so it's true to say that this feature wouldn't be possible without him.
Thanks again @matthewjasper <https://github.com/matthewjasper>!
With the changes proposed in this PR, it will be possible to chain let
expressions along side local variable declarations or ordinary conditional
expressions. In other words, do much of what the if_chain crate already
does.
fn u2_beautiful_day(lyrics: &str) {
let mut lines = lyrics.trim().lines();
if let Some(see_the_world) = lines.next().map(|el| el.trim())
&& let Some(see_china) = lines.next().map(|el| el.trim())
&& let Some(see_the_canyons) = lines.next().map(|el| el.trim())
&& let Some(see_the_tuna_fleets) = lines.next().map(|el| el.trim())
&& let Some(see_the_bedouin_fires) = lines.next().map(|el| el.trim())
&& let Some(see_the_oil_fields) = lines.next().map(|el| el.trim())
&& let Some(and_see_the_bird) = lines.next().map(|el| el.trim())
&& let Some(after_the_flood) = lines.next().map(|el| el.trim())
&& see_the_world == "See the world in green and blue"
&& see_china == "See China right in front of you"
&& see_the_canyons == "See the canyons broken by cloud"
&& see_the_tuna_fleets == "See the tuna fleets clearing the sea out"
&& see_the_bedouin_fires == "See the bedouin fires at night"
&& see_the_oil_fields == "See the oil fields at first light"
&& and_see_the_bird == "And see the bird with a leaf in her mouth"
&& after_the_flood == "After the flood all the colors came out"
{
println!("It was a beautiful day, don't let it get away");
}
}
Other considerations
-
if let guard and let ... else features need special care and should be
handled in a following PR.
-
Irrefutable patterns are allowed within a let chain context
-
Three Clippy lints were already converted to start dogfooding and help
detect possible corner cases
cc #53667 <#53667>
------------------------------
You can view, comment on, or merge this pull request online at:
#88642
Commit Summary
- Formally implement let chains
File Changes
- *M* compiler/rustc_ast_lowering/src/expr.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-d93592ecc730e2061ac31cad11e78e3bb7cdc7ca3257a85f04bbd3f48c0c6521>
(20)
- *M* compiler/rustc_ast_passes/src/feature_gate.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-0e671850197410b3d51dfa88222d5b28bb9dded13f6dc7c3b85903412250d02d>
(6)
- *M* compiler/rustc_feature/src/active.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-a82bdbaaa3c79096a30755dce8896aaf2885128a7f3f3a290072b767f05b6095>
(2)
- *M* compiler/rustc_middle/src/thir.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-41e178024ba30b219fa5866c3ceb8c737795e67756facbcb4f98e5d2437e6d13>
(2)
- *M* compiler/rustc_mir_build/src/build/expr/into.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-e6ec2869375e3c21af22d0856902635da779fa6a1460f328421300d649dcd3ab>
(50)
- *M* compiler/rustc_mir_build/src/build/matches/mod.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-2c3b14b0e2f9e45ac67a2ef71d59ff7cdf4cf854fd248a785eb41882fc2b524a>
(19)
- *M* compiler/rustc_mir_build/src/thir/cx/expr.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-ad5988b23007e07f23d5459907b7aa75c9dc6ae82623be8cd4bc6acc58cb3397>
(1)
- *M* compiler/rustc_mir_build/src/thir/pattern/check_match.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-af0d549a94f69a8f6068e01f3df0cfad0bdcc2868bda71c416df6294abb241e6>
(43)
- *M* src/test/ui/expr/if/attrs/let-chains-attr.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-1a2363e45a4b048f2b8a391c970747cece6b5fe4177671c612e14232e5191fe5>
(2)
- *D* src/test/ui/expr/if/attrs/let-chains-attr.stderr
<https://github.com/rust-lang/rust/pull/88642/files#diff-57fd29c0277dc54c916ad60e06eb20dc940377bfd6a408e720d9fcfd0ab9ac45>
(11)
- *M* src/test/ui/pattern/issue-82290.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-a3906064be0cc37b7c5906241e8662e90a81f632f83f90b90ecc71bcc76e1a66>
(2)
- *M* src/test/ui/pattern/issue-82290.stderr
<https://github.com/rust-lang/rust/pull/88642/files#diff-c65af7bedc8610ac75f0c9c70a6ad9ca8e454b789c167862227b8a76174d2c00>
(11)
- *M* src/test/ui/rfc-2294-if-let-guard/feature-gate.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-d32dab3eddba912fe3daf724afebe091acb30b4c835999416222c447f3bff7a9>
(32)
- *M* src/test/ui/rfc-2294-if-let-guard/feature-gate.stderr
<https://github.com/rust-lang/rust/pull/88642/files#diff-a1da853e4c7223cfb0f551e8a2fe18dd11942517577ac394f554646b999184cf>
(48)
- *M* src/test/ui/rfc-2497-if-let-chains/ast-pretty-check.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-3feab612744a9bb3efd0910125b0f99dfeb0cca81f9822722b3896dea77cad4a>
(2)
- *M* src/test/ui/rfc-2497-if-let-chains/ast-pretty-check.stdout
<https://github.com/rust-lang/rust/pull/88642/files#diff-e015c21ec226de08b2b9189edca2d9c2e68cbc30f420d768bdd0f6352d5ea86b>
(2)
- *M* src/test/ui/rfc-2497-if-let-chains/disallowed-positions.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-211c1d6e2f4ff5f2af12400a49c018c5ecebd9e0ec9e8a15049a9e55521692bb>
(1)
- *M* src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr
<https://github.com/rust-lang/rust/pull/88642/files#diff-be7ed9bb98d46e675d578e6ca2dcdc33f993b479cb3696cb2aee5a0223b652a6>
(221)
- *M* src/test/ui/rfc-2497-if-let-chains/feature-gate.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-d88c3f9cb474c5bc86352d49ec59dabf33217d2e3fa1a1d7f61fd2c7d88c7832>
(64)
- *M* src/test/ui/rfc-2497-if-let-chains/feature-gate.stderr
<https://github.com/rust-lang/rust/pull/88642/files#diff-95f7fe3af3dc85b9699c156fbdd6afacadeb79bc450a1890481110d621d15d01>
(96)
- *A* src/test/ui/rfc-2497-if-let-chains/irrefutable-lets.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-3d0e3ebf889365a2e801771dc42dfefc8b7f4c9aebe1da61890fa91a2483dac4>
(23)
- *A* src/test/ui/rfc-2497-if-let-chains/issue-88498.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-bdb6e7ed0f897f31a425090520c953c4b3712733d1c0feec47b9a58a80f69539>
(16)
- *A* src/test/ui/rfc-2497-if-let-chains/no-double-assigments.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-1ce6220a966d8c5864dfec5330d571e98f926e3661658edfa1a5ce589b362eb0>
(9)
- *A* src/test/ui/rfc-2497-if-let-chains/then-else-blocks.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-ca8bfd3f58ce534900af7144740be7674ea6bd22a9140f4baf5ea8306c8fce7d>
(35)
- *A* src/test/ui/rfc-2497-if-let-chains/u2-beautiful-day.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-c27754999e71ee5c951fa7bf10cc6955257000e5ee0805744ac1673754e11c7a>
(31)
- *M* src/tools/clippy/clippy_lints/src/collapsible_if.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-0e7af6693b9039d4d52aa3a7faa748dfa1f32def99e1c808e60b916d05606032>
(83)
- *M* src/tools/clippy/clippy_lints/src/lib.rs
<https://github.com/rust-lang/rust/pull/88642/files#diff-87bfc7a19a52188bf03cd0c38999b9a1adf259e1631f5c493cd30704340dd602>
(1)
Patch Links:
- https://github.com/rust-lang/rust/pull/88642.patch
- https://github.com/rust-lang/rust/pull/88642.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#88642>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIK4WZUNGD3BLZWXS5BGIXDUAIKHJANCNFSM5DNIVYUQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
9721fbf
to
1b12d01
Compare
☔ The latest upstream changes (presumably #90966) made this pull request unmergeable. Please resolve the merge conflicts. |
2dc3e8b
to
801fd50
Compare
This comment has been minimized.
This comment has been minimized.
801fd50
to
ab92e42
Compare
What's the status of this PR? |
Chains with the Believe it or not, I look into this PR quite regularly trying to solve stuff but I guess I am not good enough :) |
@c410-f3r: I'd be happy to work with you on Zulip if you'd like some help |
6322250
to
5f74ef4
Compare
@bors r+ |
📌 Commit 5f74ef4 has been approved by |
@bors rollup=iffy |
…askrgr Rollup of 10 pull requests Successful merges: - rust-lang#88642 (Formally implement let chains) - rust-lang#89621 (doc: guarantee call order for sort_by_cached_key) - rust-lang#91278 (Use iterator instead of recursion in `codegen_place`) - rust-lang#92124 (Little improves in CString `new` when creating from slice) - rust-lang#92783 (Annotate dead code lint with notes about ignored derived impls) - rust-lang#92797 (Remove horizontal lines at top of page) - rust-lang#92920 (Move expr- and item-related pretty printing functions to modules) - rust-lang#93041 (Remove some unused ordering derivations based on `DefId`) - rust-lang#93051 (Add Option::is_some_with and Result::is_{ok,err}_with) - rust-lang#93062 (Update books) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
…shtriplett Stabilize `let_chains` in Rust 1.64 # Stabilization proposal This PR proposes the stabilization of `#![feature(let_chains)]` in a future-compatibility way that will allow the **possible** addition of the `EXPR is PAT` syntax. Tracking issue: rust-lang#53667 Version: 1.64 (beta => 2022-08-11, stable => 2022-10-22). ## What is stabilized The ability to chain let expressions along side local variable declarations or ordinary conditional expressions. For example: ```rust pub enum Color { Blue, Red, Violet, } pub enum Flower { Rose, Tulip, Violet, } pub fn roses_are_red_violets_are_blue_printer( (first_flower, first_flower_color): (Flower, Color), (second_flower, second_flower_color): (Flower, Color), pick_up_lines: &[&str], ) { if let Flower::Rose = first_flower && let Color::Red = first_flower_color && let Flower::Violet = second_flower && let Color::Blue = second_flower_color && let &[first_pick_up_line, ..] = pick_up_lines { println!("Roses are red, violets are blue, {}", first_pick_up_line); } } fn main() { roses_are_red_violets_are_blue_printer( (Flower::Rose, Color::Red), (Flower::Violet, Color::Blue), &["sugar is sweet and so are you"], ); } ``` ## Motivation The main motivation for this feature is improving readability, ergonomics and reducing paper cuts. For more examples, see the [RFC](https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md). ## What isn't stabilized * Let chains in match guards (`if_let_guard`) * Resolution of divergent non-terminal matchers * The `EXPR is PAT` syntax ## History * On 2017-12-24, [RFC: if- and while-let-chains](rust-lang/rfcs#2260) * On 2018-07-12, [eRFC: if- and while-let-chains, take 2](rust-lang/rfcs#2497) * On 2018-08-24, [Tracking issue for eRFC 2497, "if- and while-let-chains, take 2](rust-lang#53667) * On 2019-03-19, [Run branch cleanup after copy prop](rust-lang#59290) * On 2019-03-26, [Generalize diagnostic for x = y where bool is the expected type](rust-lang#59439) * On 2019-04-24, [Introduce hir::ExprKind::Use and employ in for loop desugaring](rust-lang#60225) * On 2019-03-19, [[let_chains, 1/6] Remove hir::ExprKind::If](rust-lang#59288) * On 2019-05-15, [[let_chains, 2/6] Introduce Let(..) in AST, remove IfLet + WhileLet and parse let chains](rust-lang#60861) * On 2019-06-20, [[let_chains, 3/6] And then there was only Loop](rust-lang#61988) * On 2020-11-22, [Reintroduce hir::ExprKind::If](rust-lang#79328) * On 2020-12-24, [Introduce hir::ExprKind::Let - Take 2](rust-lang#80357) * On 2021-02-19, [Lower condition of if expression before it's "then" block](rust-lang#82308) * On 2021-09-01, [Fix drop handling for `if let` expressions](rust-lang#88572) * On 2021-09-04, [Formally implement let chains](rust-lang#88642) * On 2022-01-19, [Add tests to ensure that let_chains works with if_let_guard](rust-lang#93086) * On 2022-01-18, [Introduce `enhanced_binary_op` feature](rust-lang#93049) * On 2022-01-22, [Fix `let_chains` and `if_let_guard` feature flags](rust-lang#93213) * On 2022-02-25, [Initiate the inner usage of `let_chains`](rust-lang#94376) * On 2022-01-28, [[WIP] Introduce ast::StmtKind::LetElse to allow the usage of `let_else` with `let_chains`](rust-lang#93437) * On 2022-02-26, [1 - Make more use of `let_chains`](rust-lang#94396) * On 2022-02-26, [2 - Make more use of `let_chains`](rust-lang#94400) * On 2022-02-27, [3 - Make more use of `let_chains`](rust-lang#94420) * On 2022-02-28, [4 - Make more use of `let_chains`](rust-lang#94445) * On 2022-02-28, [5 - Make more use of `let_chains`](rust-lang#94448) * On 2022-02-28, [6 - Make more use of `let_chains`](rust-lang#94465) * On 2022-03-01, [7 - Make more use of `let_chains`](rust-lang#94476) * On 2022-03-01, [8 - Make more use of `let_chains`](rust-lang#94484) * On 2022-03-01, [9 - Make more use of `let_chains`](rust-lang#94498) * On 2022-03-08, [Warn users about `||` in let chain expressions](rust-lang#94754) From the first RFC (2017-12-24) to the theoretical future stabilization day (2022-10-22), it can be said that this feature took 4 years, 9 months and 28 days of research, development, discussions, agreements and headaches to be settled. ## Divergent non-terminal matchers More specifically, rust-lang#86730. ```rust macro_rules! mac { ($e:expr) => { if $e { true } else { false } }; } fn main() { // OK! assert_eq!(mac!(true && let 1 = 1), true); // ERROR! Anything starting with `let` is not considered an expression assert_eq!(mac!(let 1 = 1 && true), true); } ``` To the best of my knowledge, such error or divergence is orthogonal, does not prevent stabilization and can be tackled independently in the near future or effectively in the next Rust 2024 edition. If not, then https://github.com/c410-f3r/rust/tree/let-macro-blah contains a set of changes that will consider `let` an expression. It is possible that none of the solutions above satisfies all applicable constraints but I personally don't know of any other plausible answers. ## Alternative syntax Taking into account the usefulness of this feature and the overwhelming desire to use both now and in the past, `let PAT = EXPR` will be utilized for stabilization but it doesn't or shall create any obstacle for a **possible** future addition of `EXPR is PAT`. The introductory snippet would then be written as the following. ```rust if first_flower is Flower::Rose && first_flower_color is Color::Red && second_flower is Flower::Violet && second_flower_color is Color::Blue && pick_up_lines is &[first_pick_up_line, ..] { println!("Roses are red, violets are blue, {}", first_pick_up_line); } ``` Just to reinforce, this PR only unblocks a **possible** future road for `EXPR is PAT` and does emphasize what is better or what is worse. ## Tests * [Verifies the drop order of let chains and ensures it won't change in the future in an unpredictable way](https://github.com/rust-lang/rust/blob/master/src/test/ui/mir/mir_let_chains_drop_order.rs) * [AST lowering does not wrap let chains in an `DropTemps` expression](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/ast-lowering-does-not-wrap-let-chains.rs) * [Checks pretty printing output](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/ast-pretty-check.rs) * [Verifies uninitialized variables due to MIR modifications](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/chains-without-let.rs) * [A collection of statements where `let` expressions are forbidden](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.rs) * [All or at least most of the places where let chains are allowed](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/feature-gate.rs) * [Ensures that irrefutable lets are allowed in let chains](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/irrefutable-lets.rs) * [issue-88498.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-88498.rs), [issue-90722.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-90722.rs), [issue-92145.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-92145.rs) and [issue-93150.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-93150.rs) were bugs found by third parties and fixed overtime. * [Indexing was triggering a ICE due to a wrongly constructed MIR graph](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/no-double-assigments.rs) * [Protects the precedence of `&&` in relation to other things](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/protect-precedences.rs) * [`let_chains`, as well as `if_let_guard`, has a valid MIR graph that evaluates conditional expressions correctly](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/then-else-blocks.rs) Most of the infra-structure used by let chains is also used by `if` expressions in stable compiler versions since rust-lang#80357 and rust-lang#88572. As a result, no bugs were found since the integration of rust-lang#88642. ## Possible future work * Let chains in match guards is implemented and working but stabilization is blocked by `if_let_guard`. * The usage of `let_chains` with `let_else` is possible but not implemented. Regardless, one attempt was introduced and closed in rust-lang#93437. Thanks `@Centril` for creating the RFC and huge thanks (again) to `@matthewjasper` for all the reviews, mentoring and MIR implementations. Fixes rust-lang#53667
Stabilize `let_chains` in Rust 1.64 # Stabilization proposal This PR proposes the stabilization of `#![feature(let_chains)]` in a future-compatibility way that will allow the **possible** addition of the `EXPR is PAT` syntax. Tracking issue: #53667 Version: 1.64 (beta => 2022-08-11, stable => 2022-10-22). ## What is stabilized The ability to chain let expressions along side local variable declarations or ordinary conditional expressions. For example: ```rust pub enum Color { Blue, Red, Violet, } pub enum Flower { Rose, Tulip, Violet, } pub fn roses_are_red_violets_are_blue_printer( (first_flower, first_flower_color): (Flower, Color), (second_flower, second_flower_color): (Flower, Color), pick_up_lines: &[&str], ) { if let Flower::Rose = first_flower && let Color::Red = first_flower_color && let Flower::Violet = second_flower && let Color::Blue = second_flower_color && let &[first_pick_up_line, ..] = pick_up_lines { println!("Roses are red, violets are blue, {}", first_pick_up_line); } } fn main() { roses_are_red_violets_are_blue_printer( (Flower::Rose, Color::Red), (Flower::Violet, Color::Blue), &["sugar is sweet and so are you"], ); } ``` ## Motivation The main motivation for this feature is improving readability, ergonomics and reducing paper cuts. For more examples, see the [RFC](https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md). ## What isn't stabilized * Let chains in match guards (`if_let_guard`) * Resolution of divergent non-terminal matchers * The `EXPR is PAT` syntax ## History * On 2017-12-24, [RFC: if- and while-let-chains](rust-lang/rfcs#2260) * On 2018-07-12, [eRFC: if- and while-let-chains, take 2](rust-lang/rfcs#2497) * On 2018-08-24, [Tracking issue for eRFC 2497, "if- and while-let-chains, take 2](rust-lang/rust#53667) * On 2019-03-19, [Run branch cleanup after copy prop](rust-lang/rust#59290) * On 2019-03-26, [Generalize diagnostic for x = y where bool is the expected type](rust-lang/rust#59439) * On 2019-04-24, [Introduce hir::ExprKind::Use and employ in for loop desugaring](rust-lang/rust#60225) * On 2019-03-19, [[let_chains, 1/6] Remove hir::ExprKind::If](rust-lang/rust#59288) * On 2019-05-15, [[let_chains, 2/6] Introduce Let(..) in AST, remove IfLet + WhileLet and parse let chains](rust-lang/rust#60861) * On 2019-06-20, [[let_chains, 3/6] And then there was only Loop](rust-lang/rust#61988) * On 2020-11-22, [Reintroduce hir::ExprKind::If](rust-lang/rust#79328) * On 2020-12-24, [Introduce hir::ExprKind::Let - Take 2](rust-lang/rust#80357) * On 2021-02-19, [Lower condition of if expression before it's "then" block](rust-lang/rust#82308) * On 2021-09-01, [Fix drop handling for `if let` expressions](rust-lang/rust#88572) * On 2021-09-04, [Formally implement let chains](rust-lang/rust#88642) * On 2022-01-19, [Add tests to ensure that let_chains works with if_let_guard](rust-lang/rust#93086) * On 2022-01-18, [Introduce `enhanced_binary_op` feature](rust-lang/rust#93049) * On 2022-01-22, [Fix `let_chains` and `if_let_guard` feature flags](rust-lang/rust#93213) * On 2022-02-25, [Initiate the inner usage of `let_chains`](rust-lang/rust#94376) * On 2022-01-28, [[WIP] Introduce ast::StmtKind::LetElse to allow the usage of `let_else` with `let_chains`](rust-lang/rust#93437) * On 2022-02-26, [1 - Make more use of `let_chains`](rust-lang/rust#94396) * On 2022-02-26, [2 - Make more use of `let_chains`](rust-lang/rust#94400) * On 2022-02-27, [3 - Make more use of `let_chains`](rust-lang/rust#94420) * On 2022-02-28, [4 - Make more use of `let_chains`](rust-lang/rust#94445) * On 2022-02-28, [5 - Make more use of `let_chains`](rust-lang/rust#94448) * On 2022-02-28, [6 - Make more use of `let_chains`](rust-lang/rust#94465) * On 2022-03-01, [7 - Make more use of `let_chains`](rust-lang/rust#94476) * On 2022-03-01, [8 - Make more use of `let_chains`](rust-lang/rust#94484) * On 2022-03-01, [9 - Make more use of `let_chains`](rust-lang/rust#94498) * On 2022-03-08, [Warn users about `||` in let chain expressions](rust-lang/rust#94754) From the first RFC (2017-12-24) to the theoretical future stabilization day (2022-10-22), it can be said that this feature took 4 years, 9 months and 28 days of research, development, discussions, agreements and headaches to be settled. ## Divergent non-terminal matchers More specifically, rust-lang/rust#86730. ```rust macro_rules! mac { ($e:expr) => { if $e { true } else { false } }; } fn main() { // OK! assert_eq!(mac!(true && let 1 = 1), true); // ERROR! Anything starting with `let` is not considered an expression assert_eq!(mac!(let 1 = 1 && true), true); } ``` To the best of my knowledge, such error or divergence is orthogonal, does not prevent stabilization and can be tackled independently in the near future or effectively in the next Rust 2024 edition. If not, then https://github.com/c410-f3r/rust/tree/let-macro-blah contains a set of changes that will consider `let` an expression. It is possible that none of the solutions above satisfies all applicable constraints but I personally don't know of any other plausible answers. ## Alternative syntax Taking into account the usefulness of this feature and the overwhelming desire to use both now and in the past, `let PAT = EXPR` will be utilized for stabilization but it doesn't or shall create any obstacle for a **possible** future addition of `EXPR is PAT`. The introductory snippet would then be written as the following. ```rust if first_flower is Flower::Rose && first_flower_color is Color::Red && second_flower is Flower::Violet && second_flower_color is Color::Blue && pick_up_lines is &[first_pick_up_line, ..] { println!("Roses are red, violets are blue, {}", first_pick_up_line); } ``` Just to reinforce, this PR only unblocks a **possible** future road for `EXPR is PAT` and does emphasize what is better or what is worse. ## Tests * [Verifies the drop order of let chains and ensures it won't change in the future in an unpredictable way](https://github.com/rust-lang/rust/blob/master/src/test/ui/mir/mir_let_chains_drop_order.rs) * [AST lowering does not wrap let chains in an `DropTemps` expression](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/ast-lowering-does-not-wrap-let-chains.rs) * [Checks pretty printing output](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/ast-pretty-check.rs) * [Verifies uninitialized variables due to MIR modifications](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/chains-without-let.rs) * [A collection of statements where `let` expressions are forbidden](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.rs) * [All or at least most of the places where let chains are allowed](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/feature-gate.rs) * [Ensures that irrefutable lets are allowed in let chains](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/irrefutable-lets.rs) * [issue-88498.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-88498.rs), [issue-90722.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-90722.rs), [issue-92145.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-92145.rs) and [issue-93150.rs](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/issue-93150.rs) were bugs found by third parties and fixed overtime. * [Indexing was triggering a ICE due to a wrongly constructed MIR graph](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/no-double-assigments.rs) * [Protects the precedence of `&&` in relation to other things](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/protect-precedences.rs) * [`let_chains`, as well as `if_let_guard`, has a valid MIR graph that evaluates conditional expressions correctly](https://github.com/rust-lang/rust/blob/master/src/test/ui/rfc-2497-if-let-chains/then-else-blocks.rs) Most of the infra-structure used by let chains is also used by `if` expressions in stable compiler versions since rust-lang/rust#80357 and rust-lang/rust#88572. As a result, no bugs were found since the integration of rust-lang/rust#88642. ## Possible future work * Let chains in match guards is implemented and working but stabilization is blocked by `if_let_guard`. * The usage of `let_chains` with `let_else` is possible but not implemented. Regardless, one attempt was introduced and closed in rust-lang/rust#93437. Thanks `@Centril` for creating the RFC and huge thanks (again) to `@matthewjasper` for all the reviews, mentoring and MIR implementations. Fixes #53667
Let chains
Thanks to @Centril for creating the RFC and special thanks to @matthewjasper for helping me since the beginning of this journey. In fact, @matthewjasper did much of the complicated MIR stuff so it's true to say that this feature wouldn't be possible without him. Thanks again @matthewjasper!
With the changes proposed in this PR, it will be possible to chain let expressions along side local variable declarations or ordinary conditional expressions. In other words, do much of what the
if_chain
crate already does.Other considerations
if let guard
andlet ... else
features need special care and should be handled in a following PR.Irrefutable patterns are allowed within a let chain context
Three Clippy lints were already converted to start dogfooding and help detect possible corner casescc #53667