Skip to content

Commit 9b38750

Browse files
varkorfanzier
authored andcommitted
Adjust comments and diagnostics
1 parent 2415a64 commit 9b38750

20 files changed

+108
-61
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,9 +1313,9 @@ pub enum ExprKind {
13131313
Field(P<Expr>, Ident),
13141314
/// An indexing operation (e.g., `foo[2]`).
13151315
Index(P<Expr>, P<Expr>),
1316-
/// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
1316+
/// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`; and `..` in destructuring assingment).
13171317
Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
1318-
/// An underscore.
1318+
/// An underscore, used in destructuring assignment to ignore a value.
13191319
Underscore,
13201320

13211321
/// Variable reference, possibly containing `::` and/or type

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
861861
}
862862

863863
/// Destructure the LHS of complex assignments.
864-
/// For instance, lower `(a,b) = t` to `{ let (lhs1,lhs2) = t; a = lhs1; b = lhs2; }`.
864+
/// For instance, lower `(a, b) = t` to `{ let (lhs1, lhs2) = t; a = lhs1; b = lhs2; }`.
865865
fn lower_expr_assign(
866866
&mut self,
867867
lhs: &Expr,
@@ -902,13 +902,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
902902
return hir::ExprKind::Assign(self.lower_expr(lhs), self.lower_expr(rhs), eq_sign_span);
903903
}
904904

905-
let mut assignments = Vec::new();
905+
let mut assignments = vec![];
906906

907-
// The LHS becomes a pattern: `(lhs1, lhs2)`
907+
// The LHS becomes a pattern: `(lhs1, lhs2)`.
908908
let pat = self.destructure_assign(lhs, eq_sign_span, &mut assignments);
909909
let rhs = self.lower_expr(rhs);
910910

911-
// Introduce a let for destructuring: `let (lhs1,lhs2) = t`.
911+
// Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
912912
let destructure_let = self.stmt_let_pat(
913913
ThinVec::new(),
914914
whole_span,
@@ -927,18 +927,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
927927
}
928928

929929
/// Convert the LHS of a destructuring assignment to a pattern.
930-
/// Along the way, introduce additional assignments in the parameter assignments.
930+
/// Each sub-assignment is recorded in `assignments`.
931931
fn destructure_assign(
932932
&mut self,
933933
lhs: &Expr,
934934
eq_sign_span: Span,
935935
assignments: &mut Vec<hir::Stmt<'hir>>,
936936
) -> &'hir hir::Pat<'hir> {
937937
match &lhs.kind {
938+
// Underscore pattern.
938939
ExprKind::Underscore => {
939940
return self.pat(lhs.span, hir::PatKind::Wild);
940941
}
941-
// slices:
942+
// Slice patterns.
942943
ExprKind::Array(elements) => {
943944
let (pats, rest) =
944945
self.destructure_sequence(elements, "slice", eq_sign_span, assignments);
@@ -950,7 +951,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
950951
};
951952
return self.pat(lhs.span, slice_pat);
952953
}
953-
// tuple structs:
954+
// Tuple structs.
954955
ExprKind::Call(callee, args) => {
955956
if let ExprKind::Path(qself, path) = &callee.kind {
956957
let (pats, rest) =
@@ -979,7 +980,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
979980
}
980981
}
981982
}
982-
// structs:
983+
// Structs.
983984
ExprKind::Struct(path, fields, base) => {
984985
let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| {
985986
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
@@ -1005,10 +1006,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
10051006
ExprKind::Underscore => {}
10061007
_ => self
10071008
.sess
1008-
.struct_span_err(e.span, "base expression not allowed here")
1009+
.struct_span_err(
1010+
e.span,
1011+
"functional record updates are not allowed in destructuring \
1012+
assignments",
1013+
)
10091014
.span_suggestion(
10101015
e.span,
1011-
"consider removing this",
1016+
"consider removing the trailing pattern",
10121017
String::new(),
10131018
rustc_errors::Applicability::MachineApplicable,
10141019
)
@@ -1020,7 +1025,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10201025
let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
10211026
return self.pat(lhs.span, struct_pat);
10221027
}
1023-
// tuples:
1028+
// Tuples.
10241029
ExprKind::Tup(elements) => {
10251030
let (pats, rest) =
10261031
self.destructure_sequence(elements, "tuple", eq_sign_span, assignments);
@@ -1041,8 +1046,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
10411046

10421047
/// Destructure a sequence of expressions occurring on the LHS of an assignment.
10431048
/// Such a sequence occurs in a tuple (struct)/slice.
1044-
/// Return a sequence of corresponding patterns and the index and span of `..`, if any.
1045-
/// Along the way, introduce additional assignments in the parameter `assignments`.
1049+
/// Return a sequence of corresponding patterns, and the index and the span of `..` if it
1050+
/// exists.
1051+
/// Each sub-assignment is recorded in `assignments`.
10461052
fn destructure_sequence(
10471053
&mut self,
10481054
elements: &[AstP<Expr>],

compiler/rustc_feature/src/active.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ declare_features! (
611611
(active, unsized_fn_params, "1.49.0", Some(48055), None),
612612

613613
/// Allows the use of destructuring assignments.
614+
/// The current issue reference refers to the rust-lang/rfcs issue. This will be changed to a
615+
/// valid rust-lang/rust issue when the accompanying RFC is accepted.
614616
(active, destructuring_assignment, "1.49.0", Some(372), None),
615617

616618
// -------------------------------------------------------------------------

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,8 +1680,8 @@ pub enum LocalSource {
16801680
AsyncFn,
16811681
/// A desugared `<expr>.await`.
16821682
AwaitDesugar,
1683-
/// A desugared expr = expr where the LHS is a tuple, struct or array.
1684-
/// The span is for the `=` sign.
1683+
/// A desugared `expr = expr`, where the LHS is a tuple, struct or array.
1684+
/// The span is that of the `=` sign.
16851685
AssignDesugar(Span),
16861686
}
16871687

src/test/ui/destructuring-assignment/note-unsupported.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ fn main() {
1717
S { x: a, y: b } += s; //~ ERROR invalid left-hand side of assignment
1818
//~| ERROR binary assignment operation `+=` cannot be applied
1919

20-
S { x: a, ..s } = S { x: 3, y: 4 }; //~ ERROR base expression not allowed
20+
S { x: a, ..s } = S { x: 3, y: 4 };
21+
//~^ ERROR functional record updates are not allowed in destructuring assignments
2122
//~| ERROR destructuring assignments are unstable
2223

2324
let c = 3;

src/test/ui/destructuring-assignment/note-unsupported.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: base expression not allowed here
1+
error: functional record updates are not allowed in destructuring assignments
22
--> $DIR/note-unsupported.rs:20:17
33
|
44
LL | S { x: a, ..s } = S { x: 3, y: 4 };
5-
| ^ help: consider removing this
5+
| ^ help: consider removing the trailing pattern
66

77
error[E0658]: destructuring assignments are unstable
88
--> $DIR/note-unsupported.rs:6:12
@@ -99,7 +99,7 @@ LL | S { x: a, ..s } = S { x: 3, y: 4 };
9999
= help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
100100

101101
error[E0658]: destructuring assignments are unstable
102-
--> $DIR/note-unsupported.rs:25:17
102+
--> $DIR/note-unsupported.rs:26:17
103103
|
104104
LL | ((a, b), c) = ((3, 4), 5);
105105
| ----------- ^

src/test/ui/destructuring-assignment/slice_destructure.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
fn main() {
66
let (mut a, mut b);
77
[a, b] = [0, 1];
8-
assert_eq!((a,b), (0,1));
9-
[a, .., b] = [1,2];
10-
assert_eq!((a,b), (1,2));
11-
[_, a] = [1,2];
12-
assert_eq!((a,b), (2,2));
8+
assert_eq!((a, b), (0, 1));
9+
let c;
10+
[a, .., b, c] = [1, 2, 3, 4, 5];
11+
assert_eq!((a, b, c), (1, 4, 5));
12+
[_, a, _] = [1, 2, 3];
13+
assert_eq!((a, b), (2, 4));
14+
[..] = [1, 2, 3];
1315
}

src/test/ui/destructuring-assignment/slice_destructure_fail.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
fn main() {
44
let (mut a, mut b);
55
[a, .., b, ..] = [0, 1]; //~ ERROR `..` can only be used once per slice pattern
6-
[a, a, b] = [1,2]; //~ ERROR pattern requires 3 elements but array has 2
7-
[_] = [1,2]; //~ ERROR pattern requires 1 element but array has 2
6+
[a, a, b] = [1, 2]; //~ ERROR pattern requires 3 elements but array has 2
7+
[_] = [1, 2]; //~ ERROR pattern requires 1 element but array has 2
88
}

src/test/ui/destructuring-assignment/slice_destructure_fail.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ LL | [a, .., b, ..] = [0, 1];
99
error[E0527]: pattern requires 3 elements but array has 2
1010
--> $DIR/slice_destructure_fail.rs:6:3
1111
|
12-
LL | [a, a, b] = [1,2];
12+
LL | [a, a, b] = [1, 2];
1313
| ^^^^^^^^^ expected 2 elements
1414

1515
error[E0527]: pattern requires 1 element but array has 2
1616
--> $DIR/slice_destructure_fail.rs:7:3
1717
|
18-
LL | [_] = [1,2];
18+
LL | [_] = [1, 2];
1919
| ^^^ expected 2 elements
2020

2121
error: aborting due to 3 previous errors

src/test/ui/destructuring-assignment/struct_destructure.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ fn main() {
1010
let (mut a, mut b);
1111
Struct { a, b } = Struct { a: 0, b: 1 };
1212
assert_eq!((a, b), (0, 1));
13-
Struct { a: b, b: a } = Struct { a: 1, b: 2};
14-
assert_eq!((a,b), (2,1));
13+
Struct { a: b, b: a } = Struct { a: 1, b: 2 };
14+
assert_eq!((a,b), (2, 1));
1515
Struct { a: _, b } = Struct { a: 1, b: 2 };
16-
assert_eq!((a,b), (2,2));
17-
Struct { a, .. } = Struct { a: 1, b: 3};
18-
assert_eq!((a,b), (1,2));
16+
assert_eq!((a, b), (2, 2));
17+
Struct { a, .. } = Struct { a: 1, b: 3 };
18+
assert_eq!((a, b), (1, 2));
19+
Struct { .. } = Struct { a: 1, b: 4 };
20+
assert_eq!((a, b), (1, 2));
1921
}

0 commit comments

Comments
 (0)