Skip to content

Commit 3a648ff

Browse files
authored
Rollup merge of rust-lang#78836 - fanzier:struct-and-slice-destructuring, r=petrochenkov
Implement destructuring assignment for structs and slices This is the second step towards implementing destructuring assignment (RFC: rust-lang/rfcs#2909, tracking issue: rust-lang#71126). This PR is the second part of rust-lang#71156, which was split up to allow for easier review. Note that the first PR (rust-lang#78748) is not merged yet, so it is included as the first commit in this one. I thought this would allow the review to start earlier because I have some time this weekend to respond to reviews. If ``@petrochenkov`` prefers to wait until the first PR is merged, I totally understand, of course. This PR implements destructuring assignment for (tuple) structs and slices. In order to do this, the following *parser change* was necessary: struct expressions are not required to have a base expression, i.e. `Struct { a: 1, .. }` becomes legal (in order to act like a struct pattern). Unfortunately, this PR slightly regresses the diagnostics implemented in rust-lang#77283. However, it is only a missing help message in `src/test/ui/issues/issue-77218.rs`. Other instances of this diagnostic are not affected. Since I don't exactly understand how this help message works and how to fix it yet, I was hoping it's OK to regress this temporarily and fix it in a follow-up PR. Thanks to ``@varkor`` who helped with the implementation, particularly around the struct rest changes. r? ``@petrochenkov``
2 parents 92ece84 + 5f310d9 commit 3a648ff

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

clippy_lints/src/utils/ast_utils.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
107107
both(l, r, |l, r| eq_expr(l, r))
108108
}
109109

110+
pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool {
111+
match (l, r) {
112+
(StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb),
113+
(StructRest::Rest(_), StructRest::Rest(_)) => true,
114+
(StructRest::None, StructRest::None) => true,
115+
_ => false,
116+
}
117+
}
118+
110119
pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
111120
use ExprKind::*;
112121
if !over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)) {
@@ -150,7 +159,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
150159
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
151160
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
152161
(Struct(lp, lfs, lb), Struct(rp, rfs, rb)) => {
153-
eq_path(lp, rp) && eq_expr_opt(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r))
162+
eq_path(lp, rp) && eq_struct_rest(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r))
154163
},
155164
_ => false,
156165
}

tests/ui/crashes/ice-6250.stderr

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
error[E0658]: destructuring assignments are unstable
2+
--> $DIR/ice-6250.rs:12:25
3+
|
4+
LL | Some(reference) = cache.data.get(key) {
5+
| --------------- ^
6+
| |
7+
| cannot assign to this expression
8+
|
9+
= note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
10+
= help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
11+
112
error[E0601]: `main` function not found in crate `ice_6250`
213
--> $DIR/ice-6250.rs:4:1
314
|
@@ -10,18 +21,22 @@ LL | | }
1021
LL | | }
1122
| |_^ consider adding a `main` function to `$DIR/ice-6250.rs`
1223

24+
error[E0308]: mismatched types
25+
--> $DIR/ice-6250.rs:12:14
26+
|
27+
LL | Some(reference) = cache.data.get(key) {
28+
| ^^^^^^^^^
29+
| |
30+
| expected integer, found `&i32`
31+
| help: consider dereferencing the borrow: `*reference`
32+
1333
error[E0308]: mismatched types
1434
--> $DIR/ice-6250.rs:12:9
1535
|
1636
LL | Some(reference) = cache.data.get(key) {
1737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
18-
|
19-
help: you might have meant to use pattern matching
20-
|
21-
LL | let Some(reference) = cache.data.get(key) {
22-
| ^^^
2338

24-
error: aborting due to 2 previous errors
39+
error: aborting due to 4 previous errors
2540

26-
Some errors have detailed explanations: E0308, E0601.
41+
Some errors have detailed explanations: E0308, E0601, E0658.
2742
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)