Rollup merge of #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: #71126). This PR is the second part of #71156, which was split up to allow for easier review. Note that the first PR (#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 #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``
This commit is contained in:
commit
755dd14e00
32 changed files with 619 additions and 108 deletions
|
@ -1061,7 +1061,7 @@ pub struct Expr {
|
|||
|
||||
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
rustc_data_structures::static_assert_size!(Expr, 112);
|
||||
rustc_data_structures::static_assert_size!(Expr, 120);
|
||||
|
||||
impl Expr {
|
||||
/// Returns `true` if this expression would be valid somewhere that expects a value;
|
||||
|
@ -1218,6 +1218,16 @@ pub enum RangeLimits {
|
|||
Closed,
|
||||
}
|
||||
|
||||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub enum StructRest {
|
||||
/// `..x`.
|
||||
Base(P<Expr>),
|
||||
/// `..`.
|
||||
Rest(Span),
|
||||
/// No trailing `..` or expression.
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub enum ExprKind {
|
||||
/// A `box x` expression.
|
||||
|
@ -1312,7 +1322,7 @@ pub enum ExprKind {
|
|||
Field(P<Expr>, Ident),
|
||||
/// An indexing operation (e.g., `foo[2]`).
|
||||
Index(P<Expr>, P<Expr>),
|
||||
/// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
|
||||
/// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`; and `..` in destructuring assingment).
|
||||
Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
|
||||
|
||||
/// Variable reference, possibly containing `::` and/or type
|
||||
|
@ -1340,9 +1350,8 @@ pub enum ExprKind {
|
|||
|
||||
/// A struct literal expression.
|
||||
///
|
||||
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`,
|
||||
/// where `base` is the `Option<Expr>`.
|
||||
Struct(Path, Vec<Field>, Option<P<Expr>>),
|
||||
/// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. rest}`.
|
||||
Struct(Path, Vec<Field>, StructRest),
|
||||
|
||||
/// An array literal constructed from one repeated element.
|
||||
///
|
||||
|
|
|
@ -1288,7 +1288,11 @@ pub fn noop_visit_expr<T: MutVisitor>(
|
|||
ExprKind::Struct(path, fields, expr) => {
|
||||
vis.visit_path(path);
|
||||
fields.flat_map_in_place(|field| vis.flat_map_field(field));
|
||||
visit_opt(expr, |expr| vis.visit_expr(expr));
|
||||
match expr {
|
||||
StructRest::Base(expr) => vis.visit_expr(expr),
|
||||
StructRest::Rest(_span) => {}
|
||||
StructRest::None => {}
|
||||
}
|
||||
}
|
||||
ExprKind::Paren(expr) => {
|
||||
vis.visit_expr(expr);
|
||||
|
|
|
@ -719,7 +719,11 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
|
|||
ExprKind::Struct(ref path, ref fields, ref optional_base) => {
|
||||
visitor.visit_path(path, expression.id);
|
||||
walk_list!(visitor, visit_field, fields);
|
||||
walk_list!(visitor, visit_expr, optional_base);
|
||||
match optional_base {
|
||||
StructRest::Base(expr) => visitor.visit_expr(expr),
|
||||
StructRest::Rest(_span) => {}
|
||||
StructRest::None => {}
|
||||
}
|
||||
}
|
||||
ExprKind::Tup(ref subexpressions) => {
|
||||
walk_list!(visitor, visit_expr, subexpressions);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue