@@ -164,6 +164,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
164164 ExprKind :: Range ( ref e1, ref e2, lims) => {
165165 self . lower_expr_range ( e. span , e1. as_deref ( ) , e2. as_deref ( ) , lims)
166166 }
167+ ExprKind :: Underscore => {
168+ self . sess
169+ . struct_span_err (
170+ e. span ,
171+ "in expressions, `_` can only be used on the left-hand side of an assignment" ,
172+ )
173+ . span_label ( e. span , "`_` not allowed here" )
174+ . emit ( ) ;
175+ hir:: ExprKind :: Err
176+ }
167177 ExprKind :: Path ( ref qself, ref path) => {
168178 let qpath = self . lower_qpath (
169179 e. id ,
@@ -863,7 +873,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
863873 // Return early in case of an ordinary assignment.
864874 fn is_ordinary ( lower_ctx : & mut LoweringContext < ' _ , ' _ > , lhs : & Expr ) -> bool {
865875 match & lhs. kind {
866- ExprKind :: Array ( ..) | ExprKind :: Struct ( ..) | ExprKind :: Tup ( ..) => false ,
876+ ExprKind :: Array ( ..)
877+ | ExprKind :: Struct ( ..)
878+ | ExprKind :: Tup ( ..)
879+ | ExprKind :: Underscore => false ,
867880 // Check for tuple struct constructor.
868881 ExprKind :: Call ( callee, ..) => lower_ctx. extract_tuple_struct_path ( callee) . is_none ( ) ,
869882 ExprKind :: Paren ( e) => {
@@ -943,6 +956,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
943956 assignments : & mut Vec < hir:: Stmt < ' hir > > ,
944957 ) -> & ' hir hir:: Pat < ' hir > {
945958 match & lhs. kind {
959+ // Underscore pattern.
960+ ExprKind :: Underscore => {
961+ return self . pat_without_dbm ( lhs. span , hir:: PatKind :: Wild ) ;
962+ }
946963 // Slice patterns.
947964 ExprKind :: Array ( elements) => {
948965 let ( pats, rest) =
0 commit comments