1 use super::{LoweringContext, ParamMode, ParenthesizedGenericArgs, ImplTraitContext};
2 use crate::hir::{self, HirVec};
3 use crate::hir::ptr::P;
5 use rustc_data_structures::thin_vec::ThinVec;
8 use syntax::ptr::P as AstP;
10 use syntax::source_map::{respan, DesugaringKind, Span};
11 use syntax::symbol::{sym, Symbol};
13 impl LoweringContext<'_> {
14 fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> HirVec<hir::Expr> {
15 exprs.iter().map(|x| self.lower_expr(x)).collect()
18 pub(super) fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
19 let kind = match e.node {
20 ExprKind::Box(ref inner) => hir::ExprKind::Box(P(self.lower_expr(inner))),
21 ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
22 ExprKind::Repeat(ref expr, ref count) => {
23 let expr = P(self.lower_expr(expr));
24 let count = self.lower_anon_const(count);
25 hir::ExprKind::Repeat(expr, count)
27 ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
28 ExprKind::Call(ref f, ref args) => {
29 let f = P(self.lower_expr(f));
30 hir::ExprKind::Call(f, self.lower_exprs(args))
32 ExprKind::MethodCall(ref seg, ref args) => {
33 let hir_seg = P(self.lower_path_segment(
38 ParenthesizedGenericArgs::Err,
39 ImplTraitContext::disallowed(),
42 let args = self.lower_exprs(args);
43 hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
45 ExprKind::Binary(binop, ref lhs, ref rhs) => {
46 let binop = self.lower_binop(binop);
47 let lhs = P(self.lower_expr(lhs));
48 let rhs = P(self.lower_expr(rhs));
49 hir::ExprKind::Binary(binop, lhs, rhs)
51 ExprKind::Unary(op, ref ohs) => {
52 let op = self.lower_unop(op);
53 let ohs = P(self.lower_expr(ohs));
54 hir::ExprKind::Unary(op, ohs)
56 ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.node.clone())),
57 ExprKind::Cast(ref expr, ref ty) => {
58 let expr = P(self.lower_expr(expr));
59 hir::ExprKind::Cast(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
61 ExprKind::Type(ref expr, ref ty) => {
62 let expr = P(self.lower_expr(expr));
63 hir::ExprKind::Type(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
65 ExprKind::AddrOf(m, ref ohs) => {
66 let m = self.lower_mutability(m);
67 let ohs = P(self.lower_expr(ohs));
68 hir::ExprKind::AddrOf(m, ohs)
70 ExprKind::Let(ref pats, ref scrutinee) => {
71 // If we got here, the `let` expression is not allowed.
73 .struct_span_err(e.span, "`let` expressions are not supported here")
74 .note("only supported directly in conditions of `if`- and `while`-expressions")
75 .note("as well as when nested within `&&` and parenthesis in those conditions")
78 // For better recovery, we emit:
80 // match scrutinee { pats => true, _ => false }
82 // While this doesn't fully match the user's intent, it has key advantages:
83 // 1. We can avoid using `abort_if_errors`.
84 // 2. We can typeck both `pats` and `scrutinee`.
85 // 3. `pats` is allowed to be refutable.
86 // 4. The return type of the block is `bool` which seems like what the user wanted.
87 let scrutinee = self.lower_expr(scrutinee);
89 let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
90 let expr = self.expr_bool(e.span, true);
91 self.arm(pats, P(expr))
94 let pats = hir_vec![self.pat_wild(e.span)];
95 let expr = self.expr_bool(e.span, false);
96 self.arm(pats, P(expr))
100 vec![then_arm, else_arm].into(),
101 hir::MatchSource::Normal,
104 // FIXME(#53667): handle lowering of && and parens.
105 ExprKind::If(ref cond, ref then, ref else_opt) => {
106 // `_ => else_block` where `else_block` is `{}` if there's `None`:
107 let else_pat = self.pat_wild(e.span);
108 let (else_expr, contains_else_clause) = match else_opt {
109 None => (self.expr_block_empty(e.span), false),
110 Some(els) => (self.lower_expr(els), true),
112 let else_arm = self.arm(hir_vec![else_pat], P(else_expr));
114 // Handle then + scrutinee:
115 let then_blk = self.lower_block(then, false);
116 let then_expr = self.expr_block(then_blk, ThinVec::new());
117 let (then_pats, scrutinee, desugar) = match cond.node {
118 // `<pat> => <then>`:
119 ExprKind::Let(ref pats, ref scrutinee) => {
120 let scrutinee = self.lower_expr(scrutinee);
121 let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
122 let desugar = hir::MatchSource::IfLetDesugar { contains_else_clause };
123 (pats, scrutinee, desugar)
128 let cond = self.lower_expr(cond);
129 let span_block = self.mark_span_with_reason(
130 DesugaringKind::CondTemporary, cond.span, None
132 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
133 // to preserve drop semantics since `if cond { ... }` does not
134 // let temporaries live outside of `cond`.
135 let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
137 let desugar = hir::MatchSource::IfDesugar { contains_else_clause };
138 let pats = hir_vec![self.pat_bool(e.span, true)];
139 (pats, cond, desugar)
142 let then_arm = self.arm(then_pats, P(then_expr));
144 hir::ExprKind::Match(P(scrutinee), vec![then_arm, else_arm].into(), desugar)
146 // FIXME(#53667): handle lowering of && and parens.
147 ExprKind::While(ref cond, ref body, opt_label) => self.with_loop_scope(e.id, |this| {
148 // Note that the block AND the condition are evaluated in the loop scope.
149 // This is done to allow `break` from inside the condition of the loop.
153 let else_pat = this.pat_wild(e.span);
154 let else_expr = this.expr_break(e.span, ThinVec::new());
155 this.arm(hir_vec![else_pat], else_expr)
158 // Handle then + scrutinee:
159 let then_blk = this.lower_block(body, false);
160 let then_expr = this.expr_block(then_blk, ThinVec::new());
161 let (then_pats, scrutinee, desugar, source) = match cond.node {
162 ExprKind::Let(ref pats, ref scrutinee) => {
165 // [opt_ident]: loop {
166 // match <sub_expr> {
171 let scrutinee = this.with_loop_condition_scope(|t| t.lower_expr(scrutinee));
172 let pats = pats.iter().map(|pat| this.lower_pat(pat)).collect();
173 let desugar = hir::MatchSource::WhileLetDesugar;
174 (pats, scrutinee, desugar, hir::LoopSource::WhileLet)
177 // We desugar: `'label: while $cond $body` into:
181 // match DropTemps($cond) {
189 let cond = this.with_loop_condition_scope(|this| this.lower_expr(cond));
190 let span_block = this.mark_span_with_reason(
191 DesugaringKind::CondTemporary, cond.span, None
193 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
194 // to preserve drop semantics since `while cond { ... }` does not
195 // let temporaries live outside of `cond`.
196 let cond = this.expr_drop_temps(span_block, P(cond), ThinVec::new());
198 let desugar = hir::MatchSource::WhileDesugar;
200 let pats = hir_vec![this.pat_bool(e.span, true)];
201 (pats, cond, desugar, hir::LoopSource::While)
204 let then_arm = this.arm(then_pats, P(then_expr));
206 // `match <scrutinee> { ... }`
207 let match_expr = this.expr_match(
210 hir_vec![then_arm, else_arm],
214 // `[opt_ident]: loop { ... }`
216 P(this.block_expr(P(match_expr))),
217 this.lower_label(opt_label),
221 ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
223 this.lower_block(body, false),
224 this.lower_label(opt_label),
225 hir::LoopSource::Loop,
228 ExprKind::TryBlock(ref body) => {
229 self.with_catch_scope(body.id, |this| {
230 let unstable_span = this.mark_span_with_reason(
231 DesugaringKind::TryBlock,
233 this.allow_try_trait.clone(),
235 let mut block = this.lower_block(body, true).into_inner();
236 let tail = block.expr.take().map_or_else(
238 let span = this.sess.source_map().end_point(unstable_span);
241 node: hir::ExprKind::Tup(hir_vec![]),
242 attrs: ThinVec::new(),
243 hir_id: this.next_id(),
246 |x: P<hir::Expr>| x.into_inner(),
248 block.expr = Some(this.wrap_in_try_constructor(
249 sym::from_ok, tail, unstable_span));
250 hir::ExprKind::Block(P(block), None)
253 ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
254 P(self.lower_expr(expr)),
255 arms.iter().map(|x| self.lower_arm(x)).collect(),
256 hir::MatchSource::Normal,
258 ExprKind::Async(capture_clause, closure_node_id, ref block) => {
259 self.make_async_expr(capture_clause, closure_node_id, None, block.span, |this| {
260 this.with_new_scopes(|this| {
261 let block = this.lower_block(block, false);
262 this.expr_block(block, ThinVec::new())
266 ExprKind::Await(ref expr) => self.lower_await(e.span, expr),
268 capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span
270 if let IsAsync::Async { closure_id, .. } = asyncness {
271 self.lower_expr_async_closure(
279 // Lower outside new scope to preserve `is_in_loop_condition`.
280 let fn_decl = self.lower_fn_decl(decl, None, false, None);
282 self.with_new_scopes(|this| {
283 this.current_item = Some(fn_decl_span);
284 let mut generator_kind = None;
285 let body_id = this.lower_fn_body(decl, |this| {
286 let e = this.lower_expr(body);
287 generator_kind = this.generator_kind;
290 let generator_option = this.generator_movability_for_fn(
296 hir::ExprKind::Closure(
297 this.lower_capture_clause(capture_clause),
306 ExprKind::Block(ref blk, opt_label) => {
307 hir::ExprKind::Block(self.lower_block(blk,
308 opt_label.is_some()),
309 self.lower_label(opt_label))
311 ExprKind::Assign(ref el, ref er) => {
312 hir::ExprKind::Assign(P(self.lower_expr(el)), P(self.lower_expr(er)))
314 ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
315 self.lower_binop(op),
316 P(self.lower_expr(el)),
317 P(self.lower_expr(er)),
319 ExprKind::Field(ref el, ident) => hir::ExprKind::Field(P(self.lower_expr(el)), ident),
320 ExprKind::Index(ref el, ref er) => {
321 hir::ExprKind::Index(P(self.lower_expr(el)), P(self.lower_expr(er)))
323 ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
324 self.lower_expr_range_closed(e.span, e1, e2)
326 ExprKind::Range(ref e1, ref e2, lims) => {
327 self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
329 ExprKind::Path(ref qself, ref path) => {
330 let qpath = self.lower_qpath(
335 ImplTraitContext::disallowed(),
337 hir::ExprKind::Path(qpath)
339 ExprKind::Break(opt_label, ref opt_expr) => {
340 let destination = if self.is_in_loop_condition && opt_label.is_none() {
343 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
346 self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
348 hir::ExprKind::Break(
350 opt_expr.as_ref().map(|x| P(self.lower_expr(x))),
353 ExprKind::Continue(opt_label) => {
354 hir::ExprKind::Continue(if self.is_in_loop_condition && opt_label.is_none() {
357 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
360 self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
363 ExprKind::Ret(ref e) => hir::ExprKind::Ret(e.as_ref().map(|x| P(self.lower_expr(x)))),
364 ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(asm),
365 ExprKind::Struct(ref path, ref fields, ref maybe_expr) => hir::ExprKind::Struct(
371 ImplTraitContext::disallowed(),
373 fields.iter().map(|x| self.lower_field(x)).collect(),
374 maybe_expr.as_ref().map(|x| P(self.lower_expr(x))),
376 ExprKind::Paren(ref ex) => {
377 let mut ex = self.lower_expr(ex);
378 // Include parens in span, but only if it is a super-span.
379 if e.span.contains(ex.span) {
382 // Merge attributes into the inner expression.
383 let mut attrs = e.attrs.clone();
384 attrs.extend::<Vec<_>>(ex.attrs.into());
389 ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
391 ExprKind::Err => hir::ExprKind::Err,
393 // Desugar `ExprForLoop`
394 // from: `[opt_ident]: for <pat> in <head> <body>`
395 ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
396 return self.lower_expr_for(e, pat, head, body, opt_label);
398 ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
399 ExprKind::Mac(_) => panic!("Shouldn't exist here"),
403 hir_id: self.lower_node_id(e.id),
406 attrs: e.attrs.clone(),
410 fn lower_expr_async_closure(
412 capture_clause: CaptureBy,
418 let outer_decl = FnDecl {
419 inputs: decl.inputs.clone(),
420 output: FunctionRetTy::Default(fn_decl_span),
423 // We need to lower the declaration outside the new scope, because we
424 // have to conserve the state of being inside a loop condition for the
425 // closure argument types.
426 let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
428 self.with_new_scopes(|this| {
429 // FIXME(cramertj): allow `async` non-`move` closures with arguments.
430 if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
435 "`async` non-`move` closures with arguments are not currently supported",
438 "consider using `let` statements to manually capture \
439 variables by reference before entering an `async move` closure"
444 // Transform `async |x: u8| -> X { ... }` into
445 // `|x: u8| future_from_generator(|| -> X { ... })`.
446 let body_id = this.lower_fn_body(&outer_decl, |this| {
447 let async_ret_ty = if let FunctionRetTy::Ty(ty) = &decl.output {
452 let async_body = this.make_async_expr(
453 capture_clause, closure_id, async_ret_ty, body.span,
455 this.with_new_scopes(|this| this.lower_expr(body))
458 this.expr(fn_decl_span, async_body, ThinVec::new())
460 hir::ExprKind::Closure(
461 this.lower_capture_clause(capture_clause),
470 /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
471 fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind {
472 let id = self.next_id();
473 let e1 = self.lower_expr(e1);
474 let e2 = self.lower_expr(e2);
475 self.expr_call_std_assoc_fn(
478 &[sym::ops, sym::RangeInclusive],
491 use syntax::ast::RangeLimits::*;
493 let path = match (e1, e2, lims) {
494 (None, None, HalfOpen) => sym::RangeFull,
495 (Some(..), None, HalfOpen) => sym::RangeFrom,
496 (None, Some(..), HalfOpen) => sym::RangeTo,
497 (Some(..), Some(..), HalfOpen) => sym::Range,
498 (None, Some(..), Closed) => sym::RangeToInclusive,
499 (Some(..), Some(..), Closed) => unreachable!(),
500 (_, None, Closed) => self.diagnostic()
501 .span_fatal(span, "inclusive range with no end")
505 let fields = e1.iter()
506 .map(|e| ("start", e))
507 .chain(e2.iter().map(|e| ("end", e)))
509 let expr = P(self.lower_expr(&e));
510 let ident = Ident::new(Symbol::intern(s), e.span);
511 self.field(ident, expr, e.span)
513 .collect::<P<[hir::Field]>>();
515 let is_unit = fields.is_empty();
516 let struct_path = [sym::ops, path];
517 let struct_path = self.std_path(span, &struct_path, None, is_unit);
518 let struct_path = hir::QPath::Resolved(None, P(struct_path));
521 hir::ExprKind::Path(struct_path)
523 hir::ExprKind::Struct(P(struct_path), fields, None)
527 fn lower_expr_asm(&mut self, asm: &InlineAsm) -> hir::ExprKind {
528 let hir_asm = hir::InlineAsm {
529 inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
532 .map(|out| hir::InlineAsmOutput {
533 constraint: out.constraint.clone(),
535 is_indirect: out.is_indirect,
539 asm: asm.asm.clone(),
540 asm_str_style: asm.asm_str_style,
541 clobbers: asm.clobbers.clone().into(),
542 volatile: asm.volatile,
543 alignstack: asm.alignstack,
544 dialect: asm.dialect,
548 let outputs = asm.outputs
550 .map(|out| self.lower_expr(&out.expr))
553 let inputs = asm.inputs
555 .map(|&(_, ref input)| self.lower_expr(input))
558 hir::ExprKind::InlineAsm(P(hir_asm), outputs, inputs)
561 fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind {
562 match self.generator_kind {
563 Some(hir::GeneratorKind::Gen) => {},
564 Some(hir::GeneratorKind::Async) => {
569 "`async` generators are not yet supported",
571 self.sess.abort_if_errors();
573 None => self.generator_kind = Some(hir::GeneratorKind::Gen),
578 .map(|x| self.lower_expr(x))
579 .unwrap_or_else(|| self.expr_unit(span));
581 hir::ExprKind::Yield(P(expr), hir::YieldSource::Yield)
584 /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
587 /// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
589 /// [opt_ident]: loop {
591 /// match ::std::iter::Iterator::next(&mut iter) {
592 /// ::std::option::Option::Some(val) => __next = val,
593 /// ::std::option::Option::None => break
595 /// let <pat> = __next;
596 /// StmtKind::Expr(<body>);
609 opt_label: Option<Label>,
612 let mut head = self.lower_expr(head);
613 let head_sp = head.span;
614 let desugared_span = self.mark_span_with_reason(
615 DesugaringKind::ForLoop,
619 head.span = desugared_span;
621 let iter = Ident::with_empty_ctxt(sym::iter);
623 let next_ident = Ident::with_empty_ctxt(sym::__next);
624 let (next_pat, next_pat_hid) = self.pat_ident_binding_mode(
627 hir::BindingAnnotation::Mutable,
630 // `::std::option::Option::Some(val) => __next = val`
632 let val_ident = Ident::with_empty_ctxt(sym::val);
633 let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
634 let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat_hid));
635 let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat_hid));
636 let assign = P(self.expr(
638 hir::ExprKind::Assign(next_expr, val_expr),
641 let some_pat = self.pat_some(pat.span, val_pat);
642 self.arm(hir_vec![some_pat], assign)
645 // `::std::option::Option::None => break`
648 self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
649 let pat = self.pat_none(e.span);
650 self.arm(hir_vec![pat], break_expr)
654 let (iter_pat, iter_pat_nid) = self.pat_ident_binding_mode(
657 hir::BindingAnnotation::Mutable
660 // `match ::std::iter::Iterator::next(&mut iter) { ... }`
662 let iter = P(self.expr_ident(head_sp, iter, iter_pat_nid));
663 let ref_mut_iter = self.expr_mut_addr_of(head_sp, iter);
664 let next_path = &[sym::iter, sym::Iterator, sym::next];
665 let next_expr = P(self.expr_call_std_path(
668 hir_vec![ref_mut_iter],
670 let arms = hir_vec![pat_arm, break_arm];
672 self.expr_match(head_sp, next_expr, arms, hir::MatchSource::ForLoopDesugar)
674 let match_stmt = self.stmt_expr(head_sp, match_expr);
676 let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat_hid));
679 let next_let = self.stmt_let_pat(
684 hir::LocalSource::ForLoopDesugar,
687 // `let <pat> = __next`
688 let pat = self.lower_pat(pat);
689 let pat_let = self.stmt_let_pat(
694 hir::LocalSource::ForLoopDesugar,
697 let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
698 let body_expr = self.expr_block(body_block, ThinVec::new());
699 let body_stmt = self.stmt_expr(body.span, body_expr);
701 let loop_block = P(self.block_all(
703 hir_vec![next_let, match_stmt, pat_let, body_stmt],
707 // `[opt_ident]: loop { ... }`
708 let loop_expr = hir::ExprKind::Loop(
710 self.lower_label(opt_label),
711 hir::LoopSource::ForLoop,
713 let loop_expr = P(hir::Expr {
714 hir_id: self.lower_node_id(e.id),
717 attrs: ThinVec::new(),
720 // `mut iter => { ... }`
721 let iter_arm = self.arm(hir_vec![iter_pat], loop_expr);
723 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
724 let into_iter_expr = {
726 &[sym::iter, sym::IntoIterator, sym::into_iter];
727 P(self.expr_call_std_path(
734 let match_expr = P(self.expr_match(
738 hir::MatchSource::ForLoopDesugar,
741 // This is effectively `{ let _result = ...; _result }`.
742 // The construct was introduced in #21984 and is necessary to make sure that
743 // temporaries in the `head` expression are dropped and do not leak to the
744 // surrounding scope of the `match` since the `match` is not a terminating scope.
746 // Also, add the attributes to the outer returned expr node.
747 self.expr_drop_temps(head_sp, match_expr, e.attrs.clone())
750 /// Desugar `ExprKind::Try` from: `<expr>?` into:
752 /// match Try::into_result(<expr>) {
753 /// Ok(val) => #[allow(unreachable_code)] val,
754 /// Err(err) => #[allow(unreachable_code)]
755 /// // If there is an enclosing `try {...}`:
756 /// break 'catch_target Try::from_error(From::from(err)),
758 /// return Try::from_error(From::from(err)),
761 fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind {
762 let unstable_span = self.mark_span_with_reason(
763 DesugaringKind::QuestionMark,
765 self.allow_try_trait.clone(),
767 let try_span = self.sess.source_map().end_point(span);
768 let try_span = self.mark_span_with_reason(
769 DesugaringKind::QuestionMark,
771 self.allow_try_trait.clone(),
774 // `Try::into_result(<expr>)`
777 let sub_expr = self.lower_expr(sub_expr);
779 let path = &[sym::ops, sym::Try, sym::into_result];
780 P(self.expr_call_std_path(unstable_span, path, hir_vec![sub_expr]))
783 // `#[allow(unreachable_code)]`
785 // `allow(unreachable_code)`
787 let allow_ident = Ident::new(sym::allow, span);
788 let uc_ident = Ident::new(sym::unreachable_code, span);
789 let uc_nested = attr::mk_nested_word_item(uc_ident);
790 attr::mk_list_item(allow_ident, vec![uc_nested])
792 attr::mk_attr_outer(allow)
794 let attrs = vec![attr];
796 // `Ok(val) => #[allow(unreachable_code)] val,`
798 let val_ident = Ident::with_empty_ctxt(sym::val);
799 let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
800 let val_expr = P(self.expr_ident_with_attrs(
804 ThinVec::from(attrs.clone()),
806 let ok_pat = self.pat_ok(span, val_pat);
808 self.arm(hir_vec![ok_pat], val_expr)
811 // `Err(err) => #[allow(unreachable_code)]
812 // return Try::from_error(From::from(err)),`
814 let err_ident = Ident::with_empty_ctxt(sym::err);
815 let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
817 let from_path = &[sym::convert, sym::From, sym::from];
818 let err_expr = self.expr_ident(try_span, err_ident, err_local_nid);
819 self.expr_call_std_path(try_span, from_path, hir_vec![err_expr])
822 self.wrap_in_try_constructor(sym::from_error, from_expr, unstable_span);
823 let thin_attrs = ThinVec::from(attrs);
824 let catch_scope = self.catch_scopes.last().map(|x| *x);
825 let ret_expr = if let Some(catch_node) = catch_scope {
826 let target_id = Ok(self.lower_node_id(catch_node));
829 hir::ExprKind::Break(
839 P(self.expr(try_span, hir::ExprKind::Ret(Some(from_err_expr)), thin_attrs))
842 let err_pat = self.pat_err(try_span, err_local);
843 self.arm(hir_vec![err_pat], ret_expr)
846 hir::ExprKind::Match(
848 hir_vec![err_arm, ok_arm],
849 hir::MatchSource::TryDesugar,