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

Make _ an expression, to discard values in destructuring assignments #79016

Merged
merged 1 commit into from
Nov 15, 2020

Conversation

fanzier
Copy link
Contributor

@fanzier fanzier commented Nov 13, 2020

This is the third and final step towards implementing destructuring assignment (RFC: rust-lang/rfcs#2909, tracking issue: #71126). This PR is the third and final part of #71156, which was split up to allow for easier review.

With this PR, an underscore _ is parsed as an expression but is allowed only on the left-hand side of a destructuring assignment. There it simply discards a value, similarly to the wildcard _ in patterns. For instance,

(a, _) = (1, 2)

will simply assign 1 to a and discard the 2. Note that for consistency,

_ = foo

is also allowed and equivalent to just foo.

Thanks to @varkor who helped with the implementation, particularly around pre-expansion gating.

r? @petrochenkov

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 13, 2020
compiler/rustc_parse/src/parser/expr.rs Outdated Show resolved Hide resolved
compiler/rustc_ast_lowering/src/expr.rs Outdated Show resolved Hide resolved
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2020
@petrochenkov
Copy link
Contributor

r=me after squashing commits.

@fanzier
Copy link
Contributor Author

fanzier commented Nov 14, 2020

Squashed.

@varkor
Copy link
Member

varkor commented Nov 14, 2020

@bors r=petrochenkov rollup

@bors
Copy link
Contributor

bors commented Nov 14, 2020

📌 Commit 419c515 has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 14, 2020
Co-authored-by: varkor <github@varkor.com>
@fanzier
Copy link
Contributor Author

fanzier commented Nov 14, 2020

(just added a forgotten test (a, _) = (8, 9) in src/test/ui/destructuring-assignment/tuple_destructure.rs)

@varkor
Copy link
Member

varkor commented Nov 14, 2020

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Nov 14, 2020

📌 Commit 8cf3564 has been approved by petrochenkov

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 15, 2020
…as-schievink

Rollup of 13 pull requests

Successful merges:

 - rust-lang#77802 (Allow making `RUSTC_BOOTSTRAP` conditional on the crate name)
 - rust-lang#79004 (Add `--color` support to bootstrap)
 - rust-lang#79005 (cleanup: Remove `ParseSess::injected_crate_name`)
 - rust-lang#79016 (Make `_` an expression, to discard values in destructuring assignments)
 - rust-lang#79019 (astconv: extract closures into a separate trait)
 - rust-lang#79026 (Implement BTreeMap::retain and BTreeSet::retain)
 - rust-lang#79031 (Validate that locals have a corresponding `LocalDecl`)
 - rust-lang#79034 (rustc_resolve: Make `macro_rules` scope chain compression lazy)
 - rust-lang#79036 (Move Steal to rustc_data_structures.)
 - rust-lang#79041 (Rename clean::{ItemEnum -> ItemKind}, clean::Item::{inner -> kind})
 - rust-lang#79058 (Move likely/unlikely argument outside of invisible unsafe block)
 - rust-lang#79059 (Print 'checking cranelift artifacts' to easily separate it from other artifacts)
 - rust-lang#79063 (Update rustfmt to v1.4.26)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f66af28 into rust-lang:master Nov 15, 2020
@rustbot rustbot added this to the 1.50.0 milestone Nov 15, 2020
@RalfJung
Copy link
Member

RalfJung commented Nov 15, 2020

Is drop order here consistent with let _? I recall being quite surprised by that one day, I thought let _ = ... was equivalent to let _name = ... and then never using _name, but this is actually not equivalent. _ drops immediately.

@fanzier
Copy link
Contributor Author

fanzier commented Nov 15, 2020

@RalfJung I'm not sure whether this is what you mean but I just tested the following code:

#![feature(destructuring_assignment)]

struct Bomb(&'static str);
impl Drop for Bomb {
    fn drop(&mut self) {
        println!("{}", self.0);
    }
}

fn main() {
    let _val;
    let x;
    _ = Bomb("bomb 1");
    _val = Bomb("bomb 2");
    (x, _) = (Bomb("bomb 3"), Bomb("bomb 4"));
    drop(Bomb("bomb 5"));
}

and it prints

bomb 1
bomb 4
bomb 5
bomb 3
bomb 2

just like the version with let. So I think everything is fine?

@RalfJung
Copy link
Member

Yes that sounds great. :) Would be good to have a testcase for this. Can you add one in a follow-up PR?

@fanzier
Copy link
Contributor Author

fanzier commented Nov 16, 2020

@RalfJung Sure! What's the best way of converting this to a test? Is there a way to specify the expected output of a test program? The UI tests I know just compare the compiler output to what's expected.

@RalfJung
Copy link
Member

The UI tests I know just compare the compiler output to what's expected.

Ah right, I keep forgetting that.
Here's one way to do this: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=fa4dedeb937020116f2b7123a9409a2d

@varkor varkor added the F-destructuring_assignment `#![feature(destructuring_assignment)]` label Nov 19, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Nov 19, 2020
Test drop order for (destructuring) assignments

Add a test that checks whether the drop order of `let` bindings is consistent with the drop order of the corresponding destructuring assignments.

Thanks to `@RalfJung` for the suggesting this test ([here](rust-lang#79016 (comment))) and an implementation!

r? `@RalfJung`
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Nov 19, 2020
Test drop order for (destructuring) assignments

Add a test that checks whether the drop order of `let` bindings is consistent with the drop order of the corresponding destructuring assignments.

Thanks to ``@RalfJung`` for the suggesting this test ([here](rust-lang#79016 (comment))) and an implementation!

r? ``@RalfJung``
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Nov 19, 2020
Test drop order for (destructuring) assignments

Add a test that checks whether the drop order of `let` bindings is consistent with the drop order of the corresponding destructuring assignments.

Thanks to ```@RalfJung``` for the suggesting this test ([here](rust-lang#79016 (comment))) and an implementation!

r? ```@RalfJung```
flip1995 pushed a commit to flip1995/rust that referenced this pull request Nov 20, 2020
…etrochenkov

Make `_` an expression, to discard values in destructuring assignments

This is the third and final step towards implementing destructuring assignment (RFC: rust-lang/rfcs#2909, tracking issue: rust-lang#71126). This PR is the third and final part of rust-lang#71156, which was split up to allow for easier review.

With this PR, an underscore `_` is parsed as an expression but is allowed *only* on the left-hand side of a destructuring assignment. There it simply discards a value, similarly to the wildcard `_` in patterns. For instance,
```rust
(a, _) = (1, 2)
```
will simply assign 1 to `a` and discard the 2. Note that for consistency,
```
_ = foo
```
is also allowed and equivalent to just `foo`.

Thanks to ````@varkor```` who helped with the implementation, particularly around pre-expansion gating.

r? ````@petrochenkov````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-destructuring_assignment `#![feature(destructuring_assignment)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants