|
| 1 | +use std::sync::Arc; |
| 2 | + |
1 | 3 | use rustc_ast::ptr::P; |
2 | 4 | use rustc_ast::*; |
3 | 5 | use rustc_data_structures::stack::ensure_sufficient_stack; |
4 | 6 | use rustc_hir as hir; |
5 | 7 | use rustc_hir::def::Res; |
6 | | -use rustc_span::source_map::Spanned; |
| 8 | +use rustc_middle::span_bug; |
| 9 | +use rustc_span::source_map::{Spanned, respan}; |
7 | 10 | use rustc_span::{Ident,Span}; |
8 | 11 |
|
9 | 12 | usesuper::errors::{ |
@@ -35,8 +38,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { |
35 | 38 | lower_sub, |
36 | 39 | ); |
37 | 40 | } |
38 | | -PatKind::Lit(e) => { |
39 | | -break hir::PatKind::Lit(self.lower_expr_within_pat(e,false)); |
| 41 | +PatKind::Expr(e) => { |
| 42 | +break hir::PatKind::Expr(self.lower_expr_within_pat(e,false)); |
40 | 43 | } |
41 | 44 | PatKind::TupleStruct(qself, path, pats) => { |
42 | 45 | let qpath = self.lower_qpath( |
@@ -367,24 +370,54 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { |
367 | 370 | // } |
368 | 371 | // m!(S); |
369 | 372 | // ``` |
370 | | -fnlower_expr_within_pat(&mutself,expr:&Expr,allow_paths:bool) -> &'hir hir::Expr<'hir>{ |
371 | | -match&expr.kind{ |
372 | | -ExprKind::Lit(..) |
373 | | - | ExprKind::ConstBlock(..) |
374 | | - | ExprKind::IncludedBytes(..) |
375 | | - | ExprKind::Err(_) |
376 | | - | ExprKind::Dummy => {} |
377 | | -ExprKind::Path(..)if allow_paths => {} |
378 | | -ExprKind::Unary(UnOp::Neg, inner)ifmatches!(inner.kind,ExprKind::Lit(_)) => {} |
| 373 | +fnlower_expr_within_pat( |
| 374 | +&mutself, |
| 375 | +expr:&Expr, |
| 376 | +allow_paths:bool, |
| 377 | +) -> &'hir hir::PatExpr<'hir>{ |
| 378 | +let err = |guar| hir::PatExprKind::Lit{ |
| 379 | +lit:self.arena.alloc(respan(self.lower_span(expr.span),LitKind::Err(guar))), |
| 380 | +negated:false, |
| 381 | +}; |
| 382 | +let kind = match&expr.kind{ |
| 383 | +ExprKind::Lit(lit) => { |
| 384 | + hir::PatExprKind::Lit{lit:self.lower_lit(lit, expr.span),negated:false} |
| 385 | +} |
| 386 | +ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)), |
| 387 | +ExprKind::IncludedBytes(bytes) => hir::PatExprKind::Lit{ |
| 388 | +lit:self.arena.alloc(respan( |
| 389 | +self.lower_span(expr.span), |
| 390 | +LitKind::ByteStr(Arc::clone(bytes),StrStyle::Cooked), |
| 391 | +)), |
| 392 | +negated:false, |
| 393 | +}, |
| 394 | +ExprKind::Err(guar) => err(*guar), |
| 395 | +ExprKind::Dummy => span_bug!(expr.span,"lowered ExprKind::Dummy"), |
| 396 | +ExprKind::Path(qself, path)if allow_paths => hir::PatExprKind::Path(self.lower_qpath( |
| 397 | + expr.id, |
| 398 | + qself, |
| 399 | + path, |
| 400 | +ParamMode::Optional, |
| 401 | +AllowReturnTypeNotation::No, |
| 402 | +ImplTraitContext::Disallowed(ImplTraitPosition::Path), |
| 403 | +None, |
| 404 | +)), |
| 405 | +ExprKind::Unary(UnOp::Neg, inner)ifletExprKind::Lit(lit) = &inner.kind => { |
| 406 | + hir::PatExprKind::Lit{lit:self.lower_lit(lit, expr.span),negated:true} |
| 407 | +} |
379 | 408 | _ => { |
380 | 409 | let pattern_from_macro = expr.is_approximately_pattern(); |
381 | 410 | let guar = self.dcx().emit_err(ArbitraryExpressionInPattern{ |
382 | 411 | span: expr.span, |
383 | 412 | pattern_from_macro_note: pattern_from_macro, |
384 | 413 | }); |
385 | | -returnself.arena.alloc(self.expr_err(expr.span,guar)); |
| 414 | +err(guar) |
386 | 415 | } |
387 | | -} |
388 | | -self.lower_expr(expr) |
| 416 | +}; |
| 417 | +self.arena.alloc(hir::PatExpr{ |
| 418 | +hir_id:self.lower_node_id(expr.id), |
| 419 | +span: expr.span, |
| 420 | + kind, |
| 421 | +}) |
389 | 422 | } |
390 | 423 | } |
0 commit comments