]> git.lizzy.rs Git - rust.git/commitdiff
Add underscore expressions for destructuring assignments
authorFabian Zaiser <fabian.zaiser@gmail.com>
Wed, 11 Nov 2020 13:15:15 +0000 (13:15 +0000)
committerFabian Zaiser <fabian.zaiser@gmail.com>
Sat, 14 Nov 2020 13:53:12 +0000 (13:53 +0000)
Co-authored-by: varkor <github@varkor.com>
27 files changed:
compiler/rustc_ast/src/ast.rs
compiler/rustc_ast/src/mut_visit.rs
compiler/rustc_ast/src/visit.rs
compiler/rustc_ast_lowering/src/expr.rs
compiler/rustc_ast_passes/src/feature_gate.rs
compiler/rustc_ast_pretty/src/pprust/state.rs
compiler/rustc_parse/src/parser/expr.rs
src/test/ui/cross/cross-file-errors/main.rs
src/test/ui/cross/cross-file-errors/main.stderr
src/test/ui/destructuring-assignment/nested_destructure.rs
src/test/ui/destructuring-assignment/slice_destructure.rs
src/test/ui/destructuring-assignment/slice_destructure_fail.rs
src/test/ui/destructuring-assignment/slice_destructure_fail.stderr
src/test/ui/destructuring-assignment/struct_destructure.rs
src/test/ui/destructuring-assignment/struct_destructure_fail.rs
src/test/ui/destructuring-assignment/struct_destructure_fail.stderr
src/test/ui/destructuring-assignment/tuple_destructure.rs
src/test/ui/destructuring-assignment/tuple_destructure_fail.rs
src/test/ui/destructuring-assignment/tuple_destructure_fail.stderr
src/test/ui/destructuring-assignment/tuple_struct_destructure.rs
src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.rs
src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr
src/test/ui/destructuring-assignment/underscore-range-expr-gating.rs
src/test/ui/destructuring-assignment/underscore-range-expr-gating.stderr
src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.rs
src/test/ui/suggestions/fn-or-tuple-struct-with-underscore-args.stderr
src/tools/clippy/clippy_lints/src/utils/sugg.rs

index 3e953729aabecaabac7e5c6b87fc583b5746bd1c..328086af183d6076b171fcd270f4a5b5aae446b1 100644 (file)
@@ -1192,6 +1192,7 @@ pub fn precedence(&self) -> ExprPrecedence {
             ExprKind::Field(..) => ExprPrecedence::Field,
             ExprKind::Index(..) => ExprPrecedence::Index,
             ExprKind::Range(..) => ExprPrecedence::Range,
+            ExprKind::Underscore => ExprPrecedence::Path,
             ExprKind::Path(..) => ExprPrecedence::Path,
             ExprKind::AddrOf(..) => ExprPrecedence::AddrOf,
             ExprKind::Break(..) => ExprPrecedence::Break,
@@ -1324,6 +1325,8 @@ pub enum ExprKind {
     Index(P<Expr>, P<Expr>),
     /// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`; and `..` in destructuring assingment).
     Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
+    /// An underscore, used in destructuring assignment to ignore a value.
+    Underscore,
 
     /// Variable reference, possibly containing `::` and/or type
     /// parameters (e.g., `foo::bar::<baz>`).
index 26097980e8be441c4a7b1c7273334d154cff05a9..ddae0ab03e40497dddbf5147e14492559138a9d6 100644 (file)
@@ -1232,6 +1232,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
             visit_opt(e1, |e1| vis.visit_expr(e1));
             visit_opt(e2, |e2| vis.visit_expr(e2));
         }
+        ExprKind::Underscore => {}
         ExprKind::Path(qself, path) => {
             vis.visit_qself(qself);
             vis.visit_path(path);
index 49b521afcdc782df7c80eee1abe38fdc663332b6..560064182e18df54246dcf638f11db15ccaf24ea 100644 (file)
@@ -806,6 +806,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
             walk_list!(visitor, visit_expr, start);
             walk_list!(visitor, visit_expr, end);
         }
+        ExprKind::Underscore => {}
         ExprKind::Path(ref maybe_qself, ref path) => {
             if let Some(ref qself) = *maybe_qself {
                 visitor.visit_ty(&qself.ty);
index 330776fc8c5980f8fdaee02cac5cfdb3a4c365e7..ecbe97bd45a0bedd5554cea6051ea788d277b4fe 100644 (file)
@@ -164,6 +164,16 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
                 ExprKind::Range(ref e1, ref e2, lims) => {
                     self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
                 }
+                ExprKind::Underscore => {
+                    self.sess
+                        .struct_span_err(
+                            e.span,
+                            "in expressions, `_` can only be used on the left-hand side of an assignment",
+                        )
+                        .span_label(e.span, "`_` not allowed here")
+                        .emit();
+                    hir::ExprKind::Err
+                }
                 ExprKind::Path(ref qself, ref path) => {
                     let qpath = self.lower_qpath(
                         e.id,
@@ -863,7 +873,10 @@ fn lower_expr_assign(
         // Return early in case of an ordinary assignment.
         fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
             match &lhs.kind {
-                ExprKind::Array(..) | ExprKind::Struct(..) | ExprKind::Tup(..) => false,
+                ExprKind::Array(..)
+                | ExprKind::Struct(..)
+                | ExprKind::Tup(..)
+                | ExprKind::Underscore => false,
                 // Check for tuple struct constructor.
                 ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
                 ExprKind::Paren(e) => {
@@ -943,6 +956,10 @@ fn destructure_assign(
         assignments: &mut Vec<hir::Stmt<'hir>>,
     ) -> &'hir hir::Pat<'hir> {
         match &lhs.kind {
+            // Underscore pattern.
+            ExprKind::Underscore => {
+                return self.pat_without_dbm(lhs.span, hir::PatKind::Wild);
+            }
             // Slice patterns.
             ExprKind::Array(elements) => {
                 let (pats, rest) =
index 2831675cb36712558a7fb6da536bf69696925e84..181783441f3ff6e1678d4a8004cc9aac8d190b5e 100644 (file)
@@ -630,7 +630,11 @@ macro_rules! gate_all {
     gate_all!(const_trait_impl, "const trait impls are experimental");
     gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
     gate_all!(inline_const, "inline-const is experimental");
-    gate_all!(destructuring_assignment, "destructuring assignments are unstable");
+    if sess.parse_sess.span_diagnostic.err_count() == 0 {
+        // Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
+        // involved, so we only emit errors where there are no other parsing errors.
+        gate_all!(destructuring_assignment, "destructuring assignments are unstable");
+    }
 
     // All uses of `gate_all!` below this point were added in #65742,
     // and subsequently disabled (with the non-early gating readded).
index a566200c3389678f511c95689dfc379ffedb4fc6..887b60f98f75f12bf3cdf609b14dad46f970fc54 100644 (file)
@@ -2068,6 +2068,7 @@ fn print_expr_outer_attr_style(&mut self, expr: &ast::Expr, is_inline: bool) {
                     self.print_expr_maybe_paren(e, fake_prec);
                 }
             }
+            ast::ExprKind::Underscore => self.s.word("_"),
             ast::ExprKind::Path(None, ref path) => self.print_path(path, true, 0),
             ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true),
             ast::ExprKind::Break(opt_label, ref opt_expr) => {
index 188bf227c4249ead979ceb1f9d8284985462c487..ffbf786491df2d6035020e4bbf0ee689c40eb503 100644 (file)
@@ -1089,6 +1089,9 @@ fn parse_bottom_expr(&mut self) -> PResult<'a, P<Expr>> {
             self.parse_yield_expr(attrs)
         } else if self.eat_keyword(kw::Let) {
             self.parse_let_expr(attrs)
+        } else if self.eat_keyword(kw::Underscore) {
+            self.sess.gated_spans.gate(sym::destructuring_assignment, self.prev_token.span);
+            Ok(self.mk_expr(self.prev_token.span, ExprKind::Underscore, attrs))
         } else if !self.unclosed_delims.is_empty() && self.check(&token::Semi) {
             // Don't complain about bare semicolons after unclosed braces
             // recovery in order to keep the error count down. Fixing the
index 74e9461803c52ee909296e10966d0d8b3699673e..1902ab94d4c0d1ec136f397bbc4f3239ab9f1576 100644 (file)
@@ -3,5 +3,6 @@
 
 fn main() {
     underscore!();
-    //~^ ERROR expected expression, found reserved identifier `_`
+    //~^ ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR destructuring assignments are unstable
 }
index f9101d8a583d35cb1834e825d96f34d46329e60c..b8658745060b014e5ed7ef03d7091a552d3f4689 100644 (file)
@@ -1,15 +1,31 @@
-error: expected expression, found reserved identifier `_`
+error[E0658]: destructuring assignments are unstable
   --> $DIR/underscore.rs:8:9
    |
 LL |         _
-   |         ^ expected expression
+   |         ^
    | 
   ::: $DIR/main.rs:5:5
    |
 LL |     underscore!();
    |     -------------- in this macro invocation
    |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
 
-error: aborting due to previous error
+error: in expressions, `_` can only be used on the left-hand side of an assignment
+  --> $DIR/underscore.rs:8:9
+   |
+LL |         _
+   |         ^ `_` not allowed here
+   | 
+  ::: $DIR/main.rs:5:5
+   |
+LL |     underscore!();
+   |     -------------- in this macro invocation
+   |
+   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 2 previous errors
 
+For more information about this error, try `rustc --explain E0658`.
index 393dfc16c0a1c131333f6526122e231d9b3186f9..0d45ff7da7249d9a698325dc1bbb96a621f7a38c 100644 (file)
@@ -14,4 +14,7 @@ fn main() {
     Struct { a: TupleStruct((a, b), c), b: [d] } =
         Struct { a: TupleStruct((0, 1), 2), b: [3] };
     assert_eq!((a, b, c, d), (0, 1, 2, 3));
+
+    // unnested underscore: just discard
+    _ = 1;
 }
index 3dd10aff19c72aaefb019b7d7d5a86fc309be765..76cdc1260fcded74a4530ee957ea89e9a1b8c10a 100644 (file)
@@ -9,6 +9,8 @@ fn main() {
   let mut c;
   [a, .., b, c] = [1, 2, 3, 4, 5];
   assert_eq!((a, b, c), (1, 4, 5));
+  [_, a, _] = [1, 2, 3];
+  assert_eq!((a, b), (2, 4));
   [..] = [1, 2, 3];
   [c, ..] = [5, 6, 6];
   assert_eq!(c, 5);
index f636ea3511c2672090e8e08db4a2a4b2f6d9e25a..90d93892f7f2249f75155ca0e2ae1bfdfdd4fdec 100644 (file)
@@ -4,4 +4,5 @@ fn main() {
   let (mut a, mut b);
   [a, .., b, ..] = [0, 1]; //~ ERROR `..` can only be used once per slice pattern
   [a, a, b] = [1, 2]; //~ ERROR pattern requires 3 elements but array has 2
+  [_] = [1, 2]; //~ ERROR pattern requires 1 element but array has 2
 }
index 728687deb8bbbd96af66338f9708a5ead40e22cd..cc412c72df51def9888fbbaee18cfee2b519cb7b 100644 (file)
@@ -12,6 +12,12 @@ error[E0527]: pattern requires 3 elements but array has 2
 LL |   [a, a, b] = [1, 2];
    |   ^^^^^^^^^ expected 2 elements
 
-error: aborting due to 2 previous errors
+error[E0527]: pattern requires 1 element but array has 2
+  --> $DIR/slice_destructure_fail.rs:7:3
+   |
+LL |   [_] = [1, 2];
+   |   ^^^ expected 2 elements
+
+error: aborting due to 3 previous errors
 
 For more information about this error, try `rustc --explain E0527`.
index b3a96ee1573460c0ef6b9a49322568fd33771f48..2bcbd9d0d742e5292126e544e0009bcebfae66a8 100644 (file)
@@ -12,8 +12,10 @@ fn main() {
     assert_eq!((a, b), (0, 1));
     Struct { a: b, b: a }  = Struct { a: 1, b: 2 };
     assert_eq!((a,b), (2, 1));
+    Struct { a: _, b } = Struct { a: 1, b: 2 };
+    assert_eq!((a, b), (2, 2));
     Struct { a, .. } = Struct { a: 1, b: 3 };
-    assert_eq!((a, b), (1, 1));
+    assert_eq!((a, b), (1, 2));
     Struct { .. } = Struct { a: 1, b: 4 };
-    assert_eq!((a, b), (1, 1));
+    assert_eq!((a, b), (1, 2));
 }
index c22695ed38849523faaec38300ab2cd322711f2d..4aa327b61f49709dd05f86fe8f400f90fe4b10a7 100644 (file)
@@ -9,6 +9,8 @@ fn main() {
     let mut c;
     let d = Struct { a: 0, b: 1 };
     Struct { a, b, c } = Struct { a: 0, b: 1 }; //~ ERROR does not have a field named `c`
+    Struct { a, _ } = Struct { a: 1, b: 2 }; //~ ERROR pattern does not mention field `b`
+    //~| ERROR expected identifier, found reserved identifier `_`
     Struct { a, ..d } = Struct { a: 1, b: 2 };
     //~^ ERROR functional record updates are not allowed in destructuring assignments
     Struct { a, .. }; //~ ERROR base expression required after `..`
index 4da4698804f1ad4ebea6cdfbbb083f0c28da2e92..81661a357e75079d46f32a717f1921139275ad48 100644 (file)
@@ -1,11 +1,19 @@
+error: expected identifier, found reserved identifier `_`
+  --> $DIR/struct_destructure_fail.rs:12:17
+   |
+LL |     Struct { a, _ } = Struct { a: 1, b: 2 };
+   |     ------      ^ expected identifier, found reserved identifier
+   |     |
+   |     while parsing this struct
+
 error: functional record updates are not allowed in destructuring assignments
-  --> $DIR/struct_destructure_fail.rs:12:19
+  --> $DIR/struct_destructure_fail.rs:14:19
    |
 LL |     Struct { a, ..d } = Struct { a: 1, b: 2 };
    |                   ^ help: consider removing the trailing pattern
 
 error: base expression required after `..`
-  --> $DIR/struct_destructure_fail.rs:14:19
+  --> $DIR/struct_destructure_fail.rs:16:19
    |
 LL |     Struct { a, .. };
    |                   ^ add a base expression here
@@ -16,6 +24,22 @@ error[E0026]: struct `Struct` does not have a field named `c`
 LL |     Struct { a, b, c } = Struct { a: 0, b: 1 };
    |                    ^ struct `Struct` does not have this field
 
-error: aborting due to 3 previous errors
+error[E0027]: pattern does not mention field `b`
+  --> $DIR/struct_destructure_fail.rs:12:5
+   |
+LL |     Struct { a, _ } = Struct { a: 1, b: 2 };
+   |     ^^^^^^^^^^^^^^^ missing field `b`
+   |
+help: include the missing field in the pattern
+   |
+LL |     Struct { a, b, _ } = Struct { a: 1, b: 2 };
+   |               ^^^
+help: if you don't care about this missing field, you can explicitly ignore it
+   |
+LL |     Struct { a, .., _ } = Struct { a: 1, b: 2 };
+   |               ^^^^
+
+error: aborting due to 5 previous errors
 
-For more information about this error, try `rustc --explain E0026`.
+Some errors have detailed explanations: E0026, E0027.
+For more information about an error, try `rustc --explain E0026`.
index 16aafc4693f3fba324a5fcd7c02ac136834cfaee..2096182d421cf5232478ef2caff1abd0282d189b 100644 (file)
@@ -16,6 +16,8 @@ fn main() {
     assert_eq!((a, b), (2, 2));
     (b, ..) = (5, 6, 7);
     assert_eq!(b, 5);
+    (a, _) = (8, 9);
+    assert_eq!(a, 8);
 
     // Test for a non-Copy type (String):
     let (mut c, mut d);
index b76f4968e6249541f9bb13ea6ede39fb66a967a1..5524e91dc401b6859a190c6f1f334059c2830290 100644 (file)
@@ -7,4 +7,5 @@ fn main() {
     (a, .., b, ..) = (0, 1); //~ ERROR `..` can only be used once per tuple pattern
     (a, a, b) = (1, 2); //~ ERROR mismatched types
     (C, ..) = (0,1); //~ ERROR invalid left-hand side of assignment
+    (_,) = (1, 2); //~ ERROR mismatched types
 }
index a60e1cb1eec628649404821d9bc6fa84fe2669b8..1146b88278d49f87f8825c5c4236788b93f2393f 100644 (file)
@@ -25,7 +25,18 @@ LL |     (C, ..) = (0,1);
    |      |
    |      cannot assign to this expression
 
-error: aborting due to 3 previous errors
+error[E0308]: mismatched types
+  --> $DIR/tuple_destructure_fail.rs:10:5
+   |
+LL |     (_,) = (1, 2);
+   |     ^^^^   ------ this expression has type `({integer}, {integer})`
+   |     |
+   |     expected a tuple with 2 elements, found one with 1 element
+   |
+   = note: expected type `({integer}, {integer})`
+             found tuple `(_,)`
+
+error: aborting due to 4 previous errors
 
 Some errors have detailed explanations: E0070, E0308.
 For more information about an error, try `rustc --explain E0070`.
index 106a9b16db45981167938c49cf75cedb3ba55b51..7b5c5ad2bae26b5156609dd44eb5b4da33ea7a35 100644 (file)
@@ -23,8 +23,10 @@ fn main() {
     assert_eq!((a, b), (0, 1));
     TupleStruct(a, .., b) = TupleStruct(1, 2);
     assert_eq!((a, b), (1, 2));
+    TupleStruct(_, a) = TupleStruct(2, 2);
+    assert_eq!((a, b), (2, 2));
     TupleStruct(..) = TupleStruct(3, 4);
-    assert_eq!((a, b), (1, 2));
+    assert_eq!((a, b), (2, 2));
     TupleStruct(5,6).assign(&mut a, &mut b);
     assert_eq!((a, b), (5, 6));
     Enum::SingleVariant(a, b) = Enum::SingleVariant(7, 8);
index 61ae42a51751f0b92d2be5504d83e5cf58e996bc..c39db06117767376a4c782df9786870f4c041c80 100644 (file)
@@ -29,8 +29,12 @@ fn main() {
 
     TupleStruct(a, a, b) = TupleStruct(1, 2);
     //~^ ERROR this pattern has 3 fields, but the corresponding tuple struct has 2 fields
+    TupleStruct(_) = TupleStruct(1, 2);
+    //~^ ERROR this pattern has 1 field, but the corresponding tuple struct has 2 fields
     Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2);
     //~^ ERROR this pattern has 3 fields, but the corresponding tuple variant has 2 fields
+    Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
+    //~^ ERROR this pattern has 1 field, but the corresponding tuple variant has 2 fields
 
     // Check if `test` is recognized as not a tuple struct but a function call:
     test() = TupleStruct(0, 0);
index 863eedecf7697ac96980966cbf358b1533f2509a..0e7174e5b19d6d4224ef4723f9765e833b8736cc 100644 (file)
@@ -23,17 +23,35 @@ LL | struct TupleStruct<S, T>(S, T);
 LL |     TupleStruct(a, a, b) = TupleStruct(1, 2);
    |     ^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
 
-error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
+error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
   --> $DIR/tuple_struct_destructure_fail.rs:32:5
    |
+LL | struct TupleStruct<S, T>(S, T);
+   | ------------------------------- tuple struct defined here
+...
+LL |     TupleStruct(_) = TupleStruct(1, 2);
+   |     ^^^^^^^^^^^^^^ expected 2 fields, found 1
+
+error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
+  --> $DIR/tuple_struct_destructure_fail.rs:34:5
+   |
 LL |     SingleVariant(S, T)
    |     ------------------- tuple variant defined here
 ...
 LL |     Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
 
+error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
+  --> $DIR/tuple_struct_destructure_fail.rs:36:5
+   |
+LL |     SingleVariant(S, T)
+   |     ------------------- tuple variant defined here
+...
+LL |     Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
+   |     ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
+
 error[E0070]: invalid left-hand side of assignment
-  --> $DIR/tuple_struct_destructure_fail.rs:36:12
+  --> $DIR/tuple_struct_destructure_fail.rs:40:12
    |
 LL |     test() = TupleStruct(0, 0);
    |     ------ ^
@@ -41,7 +59,7 @@ LL |     test() = TupleStruct(0, 0);
    |     cannot assign to this expression
 
 error[E0070]: invalid left-hand side of assignment
-  --> $DIR/tuple_struct_destructure_fail.rs:38:14
+  --> $DIR/tuple_struct_destructure_fail.rs:42:14
    |
 LL |     (test)() = TupleStruct(0, 0);
    |     -------- ^
@@ -49,14 +67,14 @@ LL |     (test)() = TupleStruct(0, 0);
    |     cannot assign to this expression
 
 error[E0070]: invalid left-hand side of assignment
-  --> $DIR/tuple_struct_destructure_fail.rs:40:38
+  --> $DIR/tuple_struct_destructure_fail.rs:44:38
    |
 LL |     <Alias::<isize> as Test>::test() = TupleStruct(0, 0);
    |     -------------------------------- ^
    |     |
    |     cannot assign to this expression
 
-error: aborting due to 7 previous errors
+error: aborting due to 9 previous errors
 
 Some errors have detailed explanations: E0023, E0070.
 For more information about an error, try `rustc --explain E0023`.
index b41f2f52a3d6f5ce4aa0ea1ffcd9a15c60f17702..4ed4f56702c3208df595eef2c12b541743852698 100644 (file)
@@ -4,5 +4,7 @@ struct S { x : u32 }
 
 #[cfg(FALSE)]
 fn foo() {
+    _; //~ ERROR destructuring assignments are unstable
+
     S { x: 5, .. }; //~ ERROR destructuring assignments are unstable
 }
index 442e36cd3065e6d10b1fe86e4a382745a6c87ffb..a5ed761a01c33f31df6fa9deae7e4f8654ebb1cc 100644 (file)
@@ -1,5 +1,14 @@
 error[E0658]: destructuring assignments are unstable
-  --> $DIR/underscore-range-expr-gating.rs:7:15
+  --> $DIR/underscore-range-expr-gating.rs:7:5
+   |
+LL |     _;
+   |     ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error[E0658]: destructuring assignments are unstable
+  --> $DIR/underscore-range-expr-gating.rs:9:15
    |
 LL |     S { x: 5, .. };
    |               ^^
@@ -7,6 +16,6 @@ LL |     S { x: 5, .. };
    = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
    = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
 
-error: aborting due to previous error
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0658`.
index a8ea3faefe876a12a5659691b5a542a58e9b5c4a..00638e04f5db9c38f4af93d80c474d2c9618664e 100644 (file)
@@ -8,12 +8,18 @@ fn baz(x: usize, y: usize) -> usize { x }
 
 fn main() {
     let _: usize = foo(_, _);
-    //~^ ERROR expected expression
-    //~| ERROR expected expression
+    //~^ ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR destructuring assignments are unstable
+    //~| ERROR destructuring assignments are unstable
     let _: S = S(_, _);
-    //~^ ERROR expected expression
-    //~| ERROR expected expression
+    //~^ ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR destructuring assignments are unstable
+    //~| ERROR destructuring assignments are unstable
     let _: usize = T::baz(_, _);
-    //~^ ERROR expected expression
-    //~| ERROR expected expression
+    //~^ ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR `_` can only be used on the left-hand side of an assignment
+    //~| ERROR destructuring assignments are unstable
+    //~| ERROR destructuring assignments are unstable
 }
index a6d1c4b859f2f73611763a074a60c07e72de991f..248fa6b9c9cb2e7fd0d16998ec5045dde7c70db7 100644 (file)
@@ -1,38 +1,93 @@
-error: expected expression, found reserved identifier `_`
+error[E0658]: destructuring assignments are unstable
   --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:10:24
    |
 LL |     let _: usize = foo(_, _);
-   |                        ^ expected expression
+   |                        ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error[E0658]: destructuring assignments are unstable
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:10:27
+   |
+LL |     let _: usize = foo(_, _);
+   |                           ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error[E0658]: destructuring assignments are unstable
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:15:18
+   |
+LL |     let _: S = S(_, _);
+   |                  ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error[E0658]: destructuring assignments are unstable
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:15:21
+   |
+LL |     let _: S = S(_, _);
+   |                     ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error[E0658]: destructuring assignments are unstable
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:20:27
+   |
+LL |     let _: usize = T::baz(_, _);
+   |                           ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error[E0658]: destructuring assignments are unstable
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:20:30
+   |
+LL |     let _: usize = T::baz(_, _);
+   |                              ^
+   |
+   = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
+   = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
+
+error: in expressions, `_` can only be used on the left-hand side of an assignment
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:10:24
+   |
+LL |     let _: usize = foo(_, _);
+   |                        ^ `_` not allowed here
 
-error: expected expression, found reserved identifier `_`
+error: in expressions, `_` can only be used on the left-hand side of an assignment
   --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:10:27
    |
 LL |     let _: usize = foo(_, _);
-   |                           ^ expected expression
+   |                           ^ `_` not allowed here
 
-error: expected expression, found reserved identifier `_`
-  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:13:18
+error: in expressions, `_` can only be used on the left-hand side of an assignment
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:15:18
    |
 LL |     let _: S = S(_, _);
-   |                  ^ expected expression
+   |                  ^ `_` not allowed here
 
-error: expected expression, found reserved identifier `_`
-  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:13:21
+error: in expressions, `_` can only be used on the left-hand side of an assignment
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:15:21
    |
 LL |     let _: S = S(_, _);
-   |                     ^ expected expression
+   |                     ^ `_` not allowed here
 
-error: expected expression, found reserved identifier `_`
-  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:16:27
+error: in expressions, `_` can only be used on the left-hand side of an assignment
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:20:27
    |
 LL |     let _: usize = T::baz(_, _);
-   |                           ^ expected expression
+   |                           ^ `_` not allowed here
 
-error: expected expression, found reserved identifier `_`
-  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:16:30
+error: in expressions, `_` can only be used on the left-hand side of an assignment
+  --> $DIR/fn-or-tuple-struct-with-underscore-args.rs:20:30
    |
 LL |     let _: usize = T::baz(_, _);
-   |                              ^ expected expression
+   |                              ^ `_` not allowed here
 
-error: aborting due to 6 previous errors
+error: aborting due to 12 previous errors
 
+For more information about this error, try `rustc --explain E0658`.
index 625120b880eb50f3f94fea5e21fc44894fd5bcc3..1fcd41e4dbfed7a2e27dc3f12b93040c3f698e62 100644 (file)
@@ -170,6 +170,7 @@ pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
             | ast::ExprKind::MacCall(..)
             | ast::ExprKind::MethodCall(..)
             | ast::ExprKind::Paren(..)
+            | ast::ExprKind::Underscore
             | ast::ExprKind::Path(..)
             | ast::ExprKind::Repeat(..)
             | ast::ExprKind::Ret(..)