Make _ an expression, to discard values in destructuring assignments#79016
Make _ an expression, to discard values in destructuring assignments#79016bors merged 1 commit intorust-lang:masterfrom
_ an expression, to discard values in destructuring assignments#79016Conversation
|
r=me after squashing commits. |
b0ba98a to
419c515
Compare
|
Squashed. |
|
@bors r=petrochenkov rollup |
|
📌 Commit 419c515 has been approved by |
Co-authored-by: varkor <github@varkor.com>
419c515 to
8cf3564
Compare
|
(just added a forgotten test |
|
@bors r=petrochenkov |
|
📌 Commit 8cf3564 has been approved by |
…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
|
Is drop order here consistent with |
|
@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 just like the version with |
|
Yes that sounds great. :) Would be good to have a testcase for this. Can you add one in a follow-up PR? |
|
@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. |
Ah right, I keep forgetting that. |
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`
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``
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```
…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````
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,will simply assign 1 to
aand discard the 2. Note that for consistency,is also allowed and equivalent to just
foo.Thanks to @varkor who helped with the implementation, particularly around pre-expansion gating.
r? @petrochenkov