1 use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
4 use rustc_ast::ptr::P as AstP;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_data_structures::stack::ensure_sufficient_stack;
8 use rustc_data_structures::thin_vec::ThinVec;
9 use rustc_errors::struct_span_err;
11 use rustc_hir::def::Res;
12 use rustc_hir::definitions::DefPathData;
13 use rustc_session::parse::feature_err;
14 use rustc_span::hygiene::ExpnId;
15 use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
16 use rustc_span::symbol::{sym, Ident, Symbol};
17 use rustc_span::{hygiene::ForLoopLoc, DUMMY_SP};
18 use rustc_target::asm;
19 use std::collections::hash_map::Entry;
22 impl<'hir> LoweringContext<'_, 'hir> {
23 fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
24 self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
27 pub(super) fn lower_expr(&mut self, e: &Expr) -> &'hir hir::Expr<'hir> {
28 self.arena.alloc(self.lower_expr_mut(e))
31 pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
32 ensure_sufficient_stack(|| {
33 let kind = match e.kind {
34 ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)),
35 ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
36 ExprKind::ConstBlock(ref anon_const) => {
37 let anon_const = self.lower_anon_const(anon_const);
38 hir::ExprKind::ConstBlock(anon_const)
40 ExprKind::Repeat(ref expr, ref count) => {
41 let expr = self.lower_expr(expr);
42 let count = self.lower_anon_const(count);
43 hir::ExprKind::Repeat(expr, count)
45 ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
46 ExprKind::Call(ref f, ref args) => {
47 if let Some(legacy_args) = self.resolver.legacy_const_generic_args(f) {
48 self.lower_legacy_const_generics((**f).clone(), args.clone(), &legacy_args)
50 let f = self.lower_expr(f);
51 hir::ExprKind::Call(f, self.lower_exprs(args))
54 ExprKind::MethodCall(ref seg, ref args, span) => {
55 let hir_seg = self.arena.alloc(self.lower_path_segment(
60 ParenthesizedGenericArgs::Err,
61 ImplTraitContext::disallowed(),
64 let args = self.lower_exprs(args);
65 hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args, span)
67 ExprKind::Binary(binop, ref lhs, ref rhs) => {
68 let binop = self.lower_binop(binop);
69 let lhs = self.lower_expr(lhs);
70 let rhs = self.lower_expr(rhs);
71 hir::ExprKind::Binary(binop, lhs, rhs)
73 ExprKind::Unary(op, ref ohs) => {
74 let op = self.lower_unop(op);
75 let ohs = self.lower_expr(ohs);
76 hir::ExprKind::Unary(op, ohs)
78 ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.kind.clone())),
79 ExprKind::Cast(ref expr, ref ty) => {
80 let expr = self.lower_expr(expr);
81 let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
82 hir::ExprKind::Cast(expr, ty)
84 ExprKind::Type(ref expr, ref ty) => {
85 let expr = self.lower_expr(expr);
86 let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
87 hir::ExprKind::Type(expr, ty)
89 ExprKind::AddrOf(k, m, ref ohs) => {
90 let ohs = self.lower_expr(ohs);
91 hir::ExprKind::AddrOf(k, m, ohs)
93 ExprKind::Let(ref pat, ref scrutinee) => {
94 self.lower_expr_let(e.span, pat, scrutinee)
96 ExprKind::If(ref cond, ref then, ref else_opt) => match cond.kind {
97 ExprKind::Let(ref pat, ref scrutinee) => {
98 self.lower_expr_if_let(e.span, pat, scrutinee, then, else_opt.as_deref())
100 ExprKind::Paren(ref paren) => match paren.peel_parens().kind {
101 ExprKind::Let(ref pat, ref scrutinee) => {
102 // A user has written `if (let Some(x) = foo) {`, we want to avoid
103 // confusing them with mentions of nightly features.
104 // If this logic is changed, you will also likely need to touch
105 // `unused::UnusedParens::check_expr`.
106 self.if_let_expr_with_parens(cond, &paren.peel_parens());
107 self.lower_expr_if_let(
115 _ => self.lower_expr_if(cond, then, else_opt.as_deref()),
117 _ => self.lower_expr_if(cond, then, else_opt.as_deref()),
119 ExprKind::While(ref cond, ref body, opt_label) => self
120 .with_loop_scope(e.id, |this| {
121 this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
123 ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
125 this.lower_block(body, false),
127 hir::LoopSource::Loop,
131 ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
132 ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
133 self.lower_expr(expr),
134 self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))),
135 hir::MatchSource::Normal,
137 ExprKind::Async(capture_clause, closure_node_id, ref block) => self
143 hir::AsyncGeneratorKind::Block,
144 |this| this.with_new_scopes(|this| this.lower_block_expr(block)),
146 ExprKind::Await(ref expr) => self.lower_expr_await(e.span, expr),
155 if let Async::Yes { closure_id, .. } = asyncness {
156 self.lower_expr_async_closure(
164 self.lower_expr_closure(
173 ExprKind::Block(ref blk, opt_label) => {
174 hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label)
176 ExprKind::Assign(ref el, ref er, span) => {
177 self.lower_expr_assign(el, er, span, e.span)
179 ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
180 self.lower_binop(op),
184 ExprKind::Field(ref el, ident) => hir::ExprKind::Field(self.lower_expr(el), ident),
185 ExprKind::Index(ref el, ref er) => {
186 hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er))
188 ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
189 self.lower_expr_range_closed(e.span, e1, e2)
191 ExprKind::Range(ref e1, ref e2, lims) => {
192 self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
194 ExprKind::Underscore => {
198 "in expressions, `_` can only be used on the left-hand side of an assignment",
200 .span_label(e.span, "`_` not allowed here")
204 ExprKind::Path(ref qself, ref path) => {
205 let qpath = self.lower_qpath(
210 ImplTraitContext::disallowed(),
212 hir::ExprKind::Path(qpath)
214 ExprKind::Break(opt_label, ref opt_expr) => {
215 let opt_expr = opt_expr.as_ref().map(|x| self.lower_expr(x));
216 hir::ExprKind::Break(self.lower_jump_destination(e.id, opt_label), opt_expr)
218 ExprKind::Continue(opt_label) => {
219 hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
221 ExprKind::Ret(ref e) => {
222 let e = e.as_ref().map(|x| self.lower_expr(x));
223 hir::ExprKind::Ret(e)
225 ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(e.span, asm),
226 ExprKind::LlvmInlineAsm(ref asm) => self.lower_expr_llvm_asm(asm),
227 ExprKind::Struct(ref path, ref fields, ref rest) => {
228 let rest = match rest {
229 StructRest::Base(e) => Some(self.lower_expr(e)),
230 StructRest::Rest(sp) => {
232 .struct_span_err(*sp, "base expression required after `..`")
233 .span_label(*sp, "add a base expression here")
235 Some(&*self.arena.alloc(self.expr_err(*sp)))
237 StructRest::None => None,
239 hir::ExprKind::Struct(
240 self.arena.alloc(self.lower_qpath(
245 ImplTraitContext::disallowed(),
247 self.arena.alloc_from_iter(fields.iter().map(|x| self.lower_field(x))),
251 ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
252 ExprKind::Err => hir::ExprKind::Err,
253 ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
254 ExprKind::Paren(ref ex) => {
255 let mut ex = self.lower_expr_mut(ex);
256 // Include parens in span, but only if it is a super-span.
257 if e.span.contains(ex.span) {
260 // Merge attributes into the inner expression.
261 if !e.attrs.is_empty() {
262 let old_attrs = self.attrs.get(&ex.hir_id).map(|la| *la).unwrap_or(&[]);
265 &*self.arena.alloc_from_iter(
268 .map(|a| self.lower_attr(a))
269 .chain(old_attrs.iter().cloned()),
276 // Desugar `ExprForLoop`
277 // from: `[opt_ident]: for <pat> in <head> <body>`
278 ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
279 return self.lower_expr_for(e, pat, head, body, opt_label);
281 ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
284 let hir_id = self.lower_node_id(e.id);
285 self.lower_attrs(hir_id, &e.attrs);
286 hir::Expr { hir_id, kind, span: e.span }
290 fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
292 UnOp::Deref => hir::UnOp::Deref,
293 UnOp::Not => hir::UnOp::Not,
294 UnOp::Neg => hir::UnOp::Neg,
298 fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
301 BinOpKind::Add => hir::BinOpKind::Add,
302 BinOpKind::Sub => hir::BinOpKind::Sub,
303 BinOpKind::Mul => hir::BinOpKind::Mul,
304 BinOpKind::Div => hir::BinOpKind::Div,
305 BinOpKind::Rem => hir::BinOpKind::Rem,
306 BinOpKind::And => hir::BinOpKind::And,
307 BinOpKind::Or => hir::BinOpKind::Or,
308 BinOpKind::BitXor => hir::BinOpKind::BitXor,
309 BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
310 BinOpKind::BitOr => hir::BinOpKind::BitOr,
311 BinOpKind::Shl => hir::BinOpKind::Shl,
312 BinOpKind::Shr => hir::BinOpKind::Shr,
313 BinOpKind::Eq => hir::BinOpKind::Eq,
314 BinOpKind::Lt => hir::BinOpKind::Lt,
315 BinOpKind::Le => hir::BinOpKind::Le,
316 BinOpKind::Ne => hir::BinOpKind::Ne,
317 BinOpKind::Ge => hir::BinOpKind::Ge,
318 BinOpKind::Gt => hir::BinOpKind::Gt,
324 fn lower_legacy_const_generics(
327 args: Vec<AstP<Expr>>,
328 legacy_args_idx: &[usize],
329 ) -> hir::ExprKind<'hir> {
330 let path = match f.kind {
331 ExprKind::Path(None, ref mut path) => path,
335 // Split the arguments into const generics and normal arguments
336 let mut real_args = vec![];
337 let mut generic_args = vec![];
338 for (idx, arg) in args.into_iter().enumerate() {
339 if legacy_args_idx.contains(&idx) {
340 let parent_def_id = self.current_hir_id_owner.last().unwrap().0;
341 let node_id = self.resolver.next_node_id();
343 // Add a definition for the in-band const def.
344 self.resolver.create_def(
347 DefPathData::AnonConst,
352 let anon_const = AnonConst { id: node_id, value: arg };
353 generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
359 // Add generic args to the last element of the path.
360 let last_segment = path.segments.last_mut().unwrap();
361 assert!(last_segment.args.is_none());
362 last_segment.args = Some(AstP(GenericArgs::AngleBracketed(AngleBracketedArgs {
367 // Now lower everything as normal.
368 let f = self.lower_expr(&f);
369 hir::ExprKind::Call(f, self.lower_exprs(&real_args))
372 fn if_let_expr_with_parens(&mut self, cond: &Expr, paren: &Expr) {
373 let start = cond.span.until(paren.span);
374 let end = paren.span.shrink_to_hi().until(cond.span.shrink_to_hi());
378 "invalid parentheses around `let` expression in `if let`",
380 .multipart_suggestion(
381 "`if let` needs to be written without parentheses",
382 vec![(start, String::new()), (end, String::new())],
383 rustc_errors::Applicability::MachineApplicable,
386 // Ideally, we'd remove the feature gating of a `let` expression since we are already
387 // complaining about it here, but `feature_gate::check_crate` has already run by now:
388 // self.sess.parse_sess.gated_spans.ungate_last(sym::let_chains, paren.span);
391 /// Emit an error and lower `ast::ExprKind::Let(pat, scrutinee)` into:
393 /// match scrutinee { pats => true, _ => false }
395 fn lower_expr_let(&mut self, span: Span, pat: &Pat, scrutinee: &Expr) -> hir::ExprKind<'hir> {
396 // If we got here, the `let` expression is not allowed.
398 if self.sess.opts.unstable_features.is_nightly_build() {
400 .struct_span_err(span, "`let` expressions are not supported here")
402 "only supported directly without parentheses in conditions of `if`- and \
403 `while`-expressions, as well as in `let` chains within parentheses",
408 .struct_span_err(span, "expected expression, found statement (`let`)")
409 .note("variable declaration using `let` is a statement")
413 // For better recovery, we emit:
415 // match scrutinee { pat => true, _ => false }
417 // While this doesn't fully match the user's intent, it has key advantages:
418 // 1. We can avoid using `abort_if_errors`.
419 // 2. We can typeck both `pat` and `scrutinee`.
420 // 3. `pat` is allowed to be refutable.
421 // 4. The return type of the block is `bool` which seems like what the user wanted.
422 let scrutinee = self.lower_expr(scrutinee);
424 let pat = self.lower_pat(pat);
425 let expr = self.expr_bool(span, true);
429 let pat = self.pat_wild(span);
430 let expr = self.expr_bool(span, false);
433 hir::ExprKind::Match(
435 arena_vec![self; then_arm, else_arm],
436 hir::MatchSource::Normal,
444 else_opt: Option<&Expr>,
445 ) -> hir::ExprKind<'hir> {
446 macro_rules! make_if {
448 let cond = self.lower_expr(cond);
449 let then_expr = self.lower_block_expr(then);
450 hir::ExprKind::If(cond, self.arena.alloc(then_expr), $opt)
453 if let Some(rslt) = else_opt {
454 make_if!(Some(self.lower_expr(rslt)))
460 fn lower_expr_if_let(
466 else_opt: Option<&Expr>,
467 ) -> hir::ExprKind<'hir> {
468 // FIXME(#53667): handle lowering of && and parens.
470 // `_ => else_block` where `else_block` is `{}` if there's `None`:
471 let else_pat = self.pat_wild(span);
472 let (else_expr, contains_else_clause) = match else_opt {
473 None => (self.expr_block_empty(span.shrink_to_hi()), false),
474 Some(els) => (self.lower_expr(els), true),
476 let else_arm = self.arm(else_pat, else_expr);
478 // Handle then + scrutinee:
479 let scrutinee = self.lower_expr(scrutinee);
480 let then_pat = self.lower_pat(pat);
482 let then_expr = self.lower_block_expr(then);
483 let then_arm = self.arm(then_pat, self.arena.alloc(then_expr));
485 let desugar = hir::MatchSource::IfLetDesugar { contains_else_clause };
486 hir::ExprKind::Match(scrutinee, arena_vec![self; then_arm, else_arm], desugar)
489 fn lower_expr_while_in_loop_scope(
494 opt_label: Option<Label>,
495 ) -> hir::ExprKind<'hir> {
496 // FIXME(#53667): handle lowering of && and parens.
498 // Note that the block AND the condition are evaluated in the loop scope.
499 // This is done to allow `break` from inside the condition of the loop.
503 let else_pat = self.pat_wild(span);
504 let else_expr = self.expr_break(span, ThinVec::new());
505 self.arm(else_pat, else_expr)
508 // Handle then + scrutinee:
509 let (then_pat, scrutinee, desugar, source) = match cond.kind {
510 ExprKind::Let(ref pat, ref scrutinee) => {
513 // [opt_ident]: loop {
514 // match <sub_expr> {
519 let scrutinee = self.with_loop_condition_scope(|t| t.lower_expr(scrutinee));
520 let pat = self.lower_pat(pat);
521 (pat, scrutinee, hir::MatchSource::WhileLetDesugar, hir::LoopSource::WhileLet)
524 // We desugar: `'label: while $cond $body` into:
528 // match drop-temps { $cond } {
536 let cond = self.with_loop_condition_scope(|this| this.lower_expr(cond));
538 self.mark_span_with_reason(DesugaringKind::CondTemporary, cond.span, None);
539 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
540 // to preserve drop semantics since `while cond { ... }` does not
541 // let temporaries live outside of `cond`.
542 let cond = self.expr_drop_temps(span_block, cond, ThinVec::new());
544 let pat = self.pat_bool(span, true);
545 (pat, cond, hir::MatchSource::WhileDesugar, hir::LoopSource::While)
548 let then_expr = self.lower_block_expr(body);
549 let then_arm = self.arm(then_pat, self.arena.alloc(then_expr));
551 // `match <scrutinee> { ... }`
553 self.expr_match(span, scrutinee, arena_vec![self; then_arm, else_arm], desugar);
555 // `[opt_ident]: loop { ... }`
557 self.block_expr(self.arena.alloc(match_expr)),
560 span.with_hi(cond.span.hi()),
564 /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
565 /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_ok(()) }`
566 /// and save the block id to use it as a break target for desugaring of the `?` operator.
567 fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
568 self.with_catch_scope(body.id, |this| {
569 let mut block = this.lower_block_noalloc(body, true);
571 // Final expression of the block (if present) or `()` with span at the end of block
572 let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
574 this.mark_span_with_reason(
575 DesugaringKind::TryBlock,
577 this.allow_try_trait.clone(),
582 let try_span = this.mark_span_with_reason(
583 DesugaringKind::TryBlock,
584 this.sess.source_map().end_point(body.span),
585 this.allow_try_trait.clone(),
588 (try_span, this.expr_unit(try_span))
591 let ok_wrapped_span =
592 this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
594 // `::std::ops::Try::from_ok($tail_expr)`
595 block.expr = Some(this.wrap_in_try_constructor(
596 hir::LangItem::TryFromOk,
602 hir::ExprKind::Block(this.arena.alloc(block), None)
606 fn wrap_in_try_constructor(
608 lang_item: hir::LangItem,
610 expr: &'hir hir::Expr<'hir>,
612 ) -> &'hir hir::Expr<'hir> {
614 self.arena.alloc(self.expr_lang_item_path(method_span, lang_item, ThinVec::new()));
615 self.expr_call(overall_span, constructor, std::slice::from_ref(expr))
618 fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
619 let pat = self.lower_pat(&arm.pat);
620 let guard = arm.guard.as_ref().map(|cond| {
621 if let ExprKind::Let(ref pat, ref scrutinee) = cond.kind {
622 hir::Guard::IfLet(self.lower_pat(pat), self.lower_expr(scrutinee))
624 hir::Guard::If(self.lower_expr(cond))
627 let hir_id = self.next_id();
628 self.lower_attrs(hir_id, &arm.attrs);
629 hir::Arm { hir_id, pat, guard, body: self.lower_expr(&arm.body), span: arm.span }
632 /// Lower an `async` construct to a generator that is then wrapped so it implements `Future`.
637 /// std::future::from_generator(static move? |_task_context| -> <ret_ty> {
641 pub(super) fn make_async_expr(
643 capture_clause: CaptureBy,
644 closure_node_id: NodeId,
645 ret_ty: Option<AstP<Ty>>,
647 async_gen_kind: hir::AsyncGeneratorKind,
648 body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
649 ) -> hir::ExprKind<'hir> {
650 let output = match ret_ty {
651 Some(ty) => hir::FnRetTy::Return(self.lower_ty(&ty, ImplTraitContext::disallowed())),
652 None => hir::FnRetTy::DefaultReturn(span),
655 // Resume argument type. We let the compiler infer this to simplify the lowering. It is
656 // fully constrained by `future::from_generator`.
657 let input_ty = hir::Ty { hir_id: self.next_id(), kind: hir::TyKind::Infer, span };
659 // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`.
660 let decl = self.arena.alloc(hir::FnDecl {
661 inputs: arena_vec![self; input_ty],
664 implicit_self: hir::ImplicitSelfKind::None,
667 // Lower the argument pattern/ident. The ident is used again in the `.await` lowering.
668 let (pat, task_context_hid) = self.pat_ident_binding_mode(
670 Ident::with_dummy_span(sym::_task_context),
671 hir::BindingAnnotation::Mutable,
673 let param = hir::Param { hir_id: self.next_id(), pat, ty_span: span, span };
674 let params = arena_vec![self; param];
676 let body_id = self.lower_body(move |this| {
677 this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
679 let old_ctx = this.task_context;
680 this.task_context = Some(task_context_hid);
681 let res = body(this);
682 this.task_context = old_ctx;
686 // `static |_task_context| -> <ret_ty> { body }`:
687 let generator_kind = hir::ExprKind::Closure(
692 Some(hir::Movability::Static),
695 hir::Expr { hir_id: self.lower_node_id(closure_node_id), kind: generator_kind, span };
697 // `future::from_generator`:
699 self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone());
701 self.expr_lang_item_path(unstable_span, hir::LangItem::FromGenerator, ThinVec::new());
703 // `future::from_generator(generator)`:
704 hir::ExprKind::Call(self.arena.alloc(gen_future), arena_vec![self; generator])
707 /// Desugar `<expr>.await` into:
710 /// mut pinned => loop {
711 /// match unsafe { ::std::future::Future::poll(
712 /// <::std::pin::Pin>::new_unchecked(&mut pinned),
713 /// ::std::future::get_context(task_context),
715 /// ::std::task::Poll::Ready(result) => break result,
716 /// ::std::task::Poll::Pending => {}
718 /// task_context = yield ();
722 fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
723 match self.generator_kind {
724 Some(hir::GeneratorKind::Async(_)) => {}
725 Some(hir::GeneratorKind::Gen) | None => {
726 let mut err = struct_span_err!(
730 "`await` is only allowed inside `async` functions and blocks"
732 err.span_label(await_span, "only allowed inside `async` functions and blocks");
733 if let Some(item_sp) = self.current_item {
734 err.span_label(item_sp, "this is not `async`");
739 let span = self.mark_span_with_reason(DesugaringKind::Await, await_span, None);
740 let gen_future_span = self.mark_span_with_reason(
741 DesugaringKind::Await,
743 self.allow_gen_future.clone(),
745 let expr = self.lower_expr(expr);
747 let pinned_ident = Ident::with_dummy_span(sym::pinned);
748 let (pinned_pat, pinned_pat_hid) =
749 self.pat_ident_binding_mode(span, pinned_ident, hir::BindingAnnotation::Mutable);
751 let task_context_ident = Ident::with_dummy_span(sym::_task_context);
754 // ::std::future::Future::poll(
755 // ::std::pin::Pin::new_unchecked(&mut pinned),
756 // ::std::future::get_context(task_context),
760 let pinned = self.expr_ident(span, pinned_ident, pinned_pat_hid);
761 let ref_mut_pinned = self.expr_mut_addr_of(span, pinned);
762 let task_context = if let Some(task_context_hid) = self.task_context {
763 self.expr_ident_mut(span, task_context_ident, task_context_hid)
765 // Use of `await` outside of an async context, we cannot use `task_context` here.
768 let new_unchecked = self.expr_call_lang_item_fn_mut(
770 hir::LangItem::PinNewUnchecked,
771 arena_vec![self; ref_mut_pinned],
773 let get_context = self.expr_call_lang_item_fn_mut(
775 hir::LangItem::GetContext,
776 arena_vec![self; task_context],
778 let call = self.expr_call_lang_item_fn(
780 hir::LangItem::FuturePoll,
781 arena_vec![self; new_unchecked, get_context],
783 self.arena.alloc(self.expr_unsafe(call))
786 // `::std::task::Poll::Ready(result) => break result`
787 let loop_node_id = self.resolver.next_node_id();
788 let loop_hir_id = self.lower_node_id(loop_node_id);
790 let x_ident = Ident::with_dummy_span(sym::result);
791 let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
792 let x_expr = self.expr_ident(span, x_ident, x_pat_hid);
793 let ready_field = self.single_pat_field(span, x_pat);
794 let ready_pat = self.pat_lang_item_variant(span, hir::LangItem::PollReady, ready_field);
795 let break_x = self.with_loop_scope(loop_node_id, move |this| {
797 hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
798 this.arena.alloc(this.expr(await_span, expr_break, ThinVec::new()))
800 self.arm(ready_pat, break_x)
803 // `::std::task::Poll::Pending => {}`
805 let pending_pat = self.pat_lang_item_variant(span, hir::LangItem::PollPending, &[]);
806 let empty_block = self.expr_block_empty(span);
807 self.arm(pending_pat, empty_block)
810 let inner_match_stmt = {
811 let match_expr = self.expr_match(
814 arena_vec![self; ready_arm, pending_arm],
815 hir::MatchSource::AwaitDesugar,
817 self.stmt_expr(span, match_expr)
820 // task_context = yield ();
822 let unit = self.expr_unit(span);
823 let yield_expr = self.expr(
825 hir::ExprKind::Yield(unit, hir::YieldSource::Await { expr: Some(expr.hir_id) }),
828 let yield_expr = self.arena.alloc(yield_expr);
830 if let Some(task_context_hid) = self.task_context {
831 let lhs = self.expr_ident(span, task_context_ident, task_context_hid);
833 self.expr(span, hir::ExprKind::Assign(lhs, yield_expr, span), AttrVec::new());
834 self.stmt_expr(span, assign)
836 // Use of `await` outside of an async context. Return `yield_expr` so that we can
837 // proceed with type checking.
838 self.stmt(span, hir::StmtKind::Semi(yield_expr))
842 let loop_block = self.block_all(span, arena_vec![self; inner_match_stmt, yield_stmt], None);
845 let loop_expr = self.arena.alloc(hir::Expr {
847 kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop, span),
851 // mut pinned => loop { ... }
852 let pinned_arm = self.arm(pinned_pat, loop_expr);
855 // mut pinned => loop { .. }
857 hir::ExprKind::Match(expr, arena_vec![self; pinned_arm], hir::MatchSource::AwaitDesugar)
860 fn lower_expr_closure(
862 capture_clause: CaptureBy,
863 movability: Movability,
867 ) -> hir::ExprKind<'hir> {
868 let (body_id, generator_option) = self.with_new_scopes(move |this| {
869 let prev = this.current_item;
870 this.current_item = Some(fn_decl_span);
871 let mut generator_kind = None;
872 let body_id = this.lower_fn_body(decl, |this| {
873 let e = this.lower_expr_mut(body);
874 generator_kind = this.generator_kind;
877 let generator_option =
878 this.generator_movability_for_fn(&decl, fn_decl_span, generator_kind, movability);
879 this.current_item = prev;
880 (body_id, generator_option)
883 // Lower outside new scope to preserve `is_in_loop_condition`.
884 let fn_decl = self.lower_fn_decl(decl, None, false, None);
886 hir::ExprKind::Closure(capture_clause, fn_decl, body_id, fn_decl_span, generator_option)
889 fn generator_movability_for_fn(
893 generator_kind: Option<hir::GeneratorKind>,
894 movability: Movability,
895 ) -> Option<hir::Movability> {
896 match generator_kind {
897 Some(hir::GeneratorKind::Gen) => {
898 if decl.inputs.len() > 1 {
903 "too many parameters for a generator (expected 0 or 1 parameters)"
909 Some(hir::GeneratorKind::Async(_)) => {
910 panic!("non-`async` closure body turned `async` during lowering");
913 if movability == Movability::Static {
914 struct_span_err!(self.sess, fn_decl_span, E0697, "closures cannot be static")
922 fn lower_expr_async_closure(
924 capture_clause: CaptureBy,
929 ) -> hir::ExprKind<'hir> {
931 FnDecl { inputs: decl.inputs.clone(), output: FnRetTy::Default(fn_decl_span) };
933 let body_id = self.with_new_scopes(|this| {
934 // FIXME(cramertj): allow `async` non-`move` closures with arguments.
935 if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
940 "`async` non-`move` closures with parameters are not currently supported",
943 "consider using `let` statements to manually capture \
944 variables by reference before entering an `async move` closure",
949 // Transform `async |x: u8| -> X { ... }` into
950 // `|x: u8| future_from_generator(|| -> X { ... })`.
951 let body_id = this.lower_fn_body(&outer_decl, |this| {
953 if let FnRetTy::Ty(ty) = &decl.output { Some(ty.clone()) } else { None };
954 let async_body = this.make_async_expr(
959 hir::AsyncGeneratorKind::Closure,
960 |this| this.with_new_scopes(|this| this.lower_expr_mut(body)),
962 this.expr(fn_decl_span, async_body, ThinVec::new())
967 // We need to lower the declaration outside the new scope, because we
968 // have to conserve the state of being inside a loop condition for the
969 // closure argument types.
970 let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
972 hir::ExprKind::Closure(capture_clause, fn_decl, body_id, fn_decl_span, None)
975 /// Destructure the LHS of complex assignments.
976 /// For instance, lower `(a, b) = t` to `{ let (lhs1, lhs2) = t; a = lhs1; b = lhs2; }`.
977 fn lower_expr_assign(
983 ) -> hir::ExprKind<'hir> {
984 // Return early in case of an ordinary assignment.
985 fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
988 | ExprKind::Struct(..)
990 | ExprKind::Underscore => false,
991 // Check for tuple struct constructor.
992 ExprKind::Call(callee, ..) => lower_ctx.extract_tuple_struct_path(callee).is_none(),
993 ExprKind::Paren(e) => {
995 // We special-case `(..)` for consistency with patterns.
996 ExprKind::Range(None, None, RangeLimits::HalfOpen) => false,
997 _ => is_ordinary(lower_ctx, e),
1003 if is_ordinary(self, lhs) {
1004 return hir::ExprKind::Assign(self.lower_expr(lhs), self.lower_expr(rhs), eq_sign_span);
1006 if !self.sess.features_untracked().destructuring_assignment {
1008 &self.sess.parse_sess,
1009 sym::destructuring_assignment,
1011 "destructuring assignments are unstable",
1013 .span_label(lhs.span, "cannot assign to this expression")
1017 let mut assignments = vec![];
1019 // The LHS becomes a pattern: `(lhs1, lhs2)`.
1020 let pat = self.destructure_assign(lhs, eq_sign_span, &mut assignments);
1021 let rhs = self.lower_expr(rhs);
1023 // Introduce a `let` for destructuring: `let (lhs1, lhs2) = t`.
1024 let destructure_let = self.stmt_let_pat(
1029 hir::LocalSource::AssignDesugar(eq_sign_span),
1032 // `a = lhs1; b = lhs2;`.
1035 .alloc_from_iter(std::iter::once(destructure_let).chain(assignments.into_iter()));
1037 // Wrap everything in a block.
1038 hir::ExprKind::Block(&self.block_all(whole_span, stmts, None), None)
1041 /// If the given expression is a path to a tuple struct, returns that path.
1042 /// It is not a complete check, but just tries to reject most paths early
1043 /// if they are not tuple structs.
1044 /// Type checking will take care of the full validation later.
1045 fn extract_tuple_struct_path<'a>(&mut self, expr: &'a Expr) -> Option<&'a Path> {
1046 // For tuple struct destructuring, it must be a non-qualified path (like in patterns).
1047 if let ExprKind::Path(None, path) = &expr.kind {
1048 // Does the path resolves to something disallowed in a tuple struct/variant pattern?
1049 if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1050 if partial_res.unresolved_segments() == 0
1051 && !partial_res.base_res().expected_in_tuple_struct_pat()
1061 /// Convert the LHS of a destructuring assignment to a pattern.
1062 /// Each sub-assignment is recorded in `assignments`.
1063 fn destructure_assign(
1067 assignments: &mut Vec<hir::Stmt<'hir>>,
1068 ) -> &'hir hir::Pat<'hir> {
1070 // Underscore pattern.
1071 ExprKind::Underscore => {
1072 return self.pat_without_dbm(lhs.span, hir::PatKind::Wild);
1075 ExprKind::Array(elements) => {
1077 self.destructure_sequence(elements, "slice", eq_sign_span, assignments);
1078 let slice_pat = if let Some((i, span)) = rest {
1079 let (before, after) = pats.split_at(i);
1080 hir::PatKind::Slice(
1082 Some(self.pat_without_dbm(span, hir::PatKind::Wild)),
1086 hir::PatKind::Slice(pats, None, &[])
1088 return self.pat_without_dbm(lhs.span, slice_pat);
1091 ExprKind::Call(callee, args) => {
1092 if let Some(path) = self.extract_tuple_struct_path(callee) {
1093 let (pats, rest) = self.destructure_sequence(
1095 "tuple struct or variant",
1099 let qpath = self.lower_qpath(
1103 ParamMode::Optional,
1104 ImplTraitContext::disallowed(),
1106 // Destructure like a tuple struct.
1107 let tuple_struct_pat =
1108 hir::PatKind::TupleStruct(qpath, pats, rest.map(|r| r.0));
1109 return self.pat_without_dbm(lhs.span, tuple_struct_pat);
1113 ExprKind::Struct(path, fields, rest) => {
1114 let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| {
1115 let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
1117 hir_id: self.next_id(),
1120 is_shorthand: f.is_shorthand,
1124 let qpath = self.lower_qpath(
1128 ParamMode::Optional,
1129 ImplTraitContext::disallowed(),
1131 let fields_omitted = match rest {
1132 StructRest::Base(e) => {
1136 "functional record updates are not allowed in destructuring \
1141 "consider removing the trailing pattern",
1143 rustc_errors::Applicability::MachineApplicable,
1148 StructRest::Rest(_) => true,
1149 StructRest::None => false,
1151 let struct_pat = hir::PatKind::Struct(qpath, field_pats, fields_omitted);
1152 return self.pat_without_dbm(lhs.span, struct_pat);
1155 ExprKind::Tup(elements) => {
1157 self.destructure_sequence(elements, "tuple", eq_sign_span, assignments);
1158 let tuple_pat = hir::PatKind::Tuple(pats, rest.map(|r| r.0));
1159 return self.pat_without_dbm(lhs.span, tuple_pat);
1161 ExprKind::Paren(e) => {
1162 // We special-case `(..)` for consistency with patterns.
1163 if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1164 let tuple_pat = hir::PatKind::Tuple(&[], Some(0));
1165 return self.pat_without_dbm(lhs.span, tuple_pat);
1167 return self.destructure_assign(e, eq_sign_span, assignments);
1172 // Treat all other cases as normal lvalue.
1173 let ident = Ident::new(sym::lhs, lhs.span);
1174 let (pat, binding) = self.pat_ident(lhs.span, ident);
1175 let ident = self.expr_ident(lhs.span, ident, binding);
1176 let assign = hir::ExprKind::Assign(self.lower_expr(lhs), ident, eq_sign_span);
1177 let expr = self.expr(lhs.span, assign, ThinVec::new());
1178 assignments.push(self.stmt_expr(lhs.span, expr));
1182 /// Destructure a sequence of expressions occurring on the LHS of an assignment.
1183 /// Such a sequence occurs in a tuple (struct)/slice.
1184 /// Return a sequence of corresponding patterns, and the index and the span of `..` if it
1186 /// Each sub-assignment is recorded in `assignments`.
1187 fn destructure_sequence(
1189 elements: &[AstP<Expr>],
1192 assignments: &mut Vec<hir::Stmt<'hir>>,
1193 ) -> (&'hir [&'hir hir::Pat<'hir>], Option<(usize, Span)>) {
1194 let mut rest = None;
1196 self.arena.alloc_from_iter(elements.iter().enumerate().filter_map(|(i, e)| {
1197 // Check for `..` pattern.
1198 if let ExprKind::Range(None, None, RangeLimits::HalfOpen) = e.kind {
1199 if let Some((_, prev_span)) = rest {
1200 self.ban_extra_rest_pat(e.span, prev_span, ctx);
1202 rest = Some((i, e.span));
1206 Some(self.destructure_assign(e, eq_sign_span, assignments))
1212 /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
1213 fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> {
1214 let e1 = self.lower_expr_mut(e1);
1215 let e2 = self.lower_expr_mut(e2);
1216 let fn_path = hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, span);
1218 self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
1219 hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2])
1222 fn lower_expr_range(
1228 ) -> hir::ExprKind<'hir> {
1229 use rustc_ast::RangeLimits::*;
1231 let lang_item = match (e1, e2, lims) {
1232 (None, None, HalfOpen) => hir::LangItem::RangeFull,
1233 (Some(..), None, HalfOpen) => hir::LangItem::RangeFrom,
1234 (None, Some(..), HalfOpen) => hir::LangItem::RangeTo,
1235 (Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
1236 (None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
1237 (Some(..), Some(..), Closed) => unreachable!(),
1238 (_, None, Closed) => {
1239 self.diagnostic().span_fatal(span, "inclusive range with no end").raise()
1243 let fields = self.arena.alloc_from_iter(
1244 e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e))).map(|(s, e)| {
1245 let expr = self.lower_expr(&e);
1246 let ident = Ident::new(Symbol::intern(s), e.span);
1247 self.field(ident, expr, e.span)
1251 hir::ExprKind::Struct(self.arena.alloc(hir::QPath::LangItem(lang_item, span)), fields, None)
1254 fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
1255 let target_id = match destination {
1257 if let Some(loop_id) = self.resolver.get_label_res(id) {
1258 Ok(self.lower_node_id(loop_id))
1260 Err(hir::LoopIdError::UnresolvedLabel)
1267 .map(|id| Ok(self.lower_node_id(id)))
1268 .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
1270 hir::Destination { label: destination.map(|(_, label)| label), target_id }
1273 fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
1274 if self.is_in_loop_condition && opt_label.is_none() {
1277 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition),
1280 self.lower_loop_destination(opt_label.map(|label| (id, label)))
1284 fn with_catch_scope<T>(&mut self, catch_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1285 let len = self.catch_scopes.len();
1286 self.catch_scopes.push(catch_id);
1288 let result = f(self);
1291 self.catch_scopes.len(),
1292 "catch scopes should be added and removed in stack order"
1295 self.catch_scopes.pop().unwrap();
1300 fn with_loop_scope<T>(&mut self, loop_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1301 // We're no longer in the base loop's condition; we're in another loop.
1302 let was_in_loop_condition = self.is_in_loop_condition;
1303 self.is_in_loop_condition = false;
1305 let len = self.loop_scopes.len();
1306 self.loop_scopes.push(loop_id);
1308 let result = f(self);
1311 self.loop_scopes.len(),
1312 "loop scopes should be added and removed in stack order"
1315 self.loop_scopes.pop().unwrap();
1317 self.is_in_loop_condition = was_in_loop_condition;
1322 fn with_loop_condition_scope<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
1323 let was_in_loop_condition = self.is_in_loop_condition;
1324 self.is_in_loop_condition = true;
1326 let result = f(self);
1328 self.is_in_loop_condition = was_in_loop_condition;
1333 fn lower_expr_asm(&mut self, sp: Span, asm: &InlineAsm) -> hir::ExprKind<'hir> {
1334 if self.sess.asm_arch.is_none() {
1335 struct_span_err!(self.sess, sp, E0472, "asm! is unsupported on this target").emit();
1337 if asm.options.contains(InlineAsmOptions::ATT_SYNTAX)
1340 Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64)
1344 .struct_span_err(sp, "the `att_syntax` option is only supported on x86")
1348 // Lower operands to HIR, filter_map skips any operands with invalid
1349 // register classes.
1350 let sess = self.sess;
1351 let operands: Vec<_> = asm
1354 .filter_map(|(op, op_sp)| {
1355 let lower_reg = |reg| {
1357 InlineAsmRegOrRegClass::Reg(s) => asm::InlineAsmRegOrRegClass::Reg(
1358 asm::InlineAsmReg::parse(
1360 |feature| sess.target_features.contains(&Symbol::intern(feature)),
1365 let msg = format!("invalid register `{}`: {}", s.as_str(), e);
1366 sess.struct_span_err(*op_sp, &msg).emit();
1370 InlineAsmRegOrRegClass::RegClass(s) => {
1371 asm::InlineAsmRegOrRegClass::RegClass(
1372 asm::InlineAsmRegClass::parse(sess.asm_arch?, s)
1375 "invalid register class `{}`: {}",
1379 sess.struct_span_err(*op_sp, &msg).emit();
1387 // lower_reg is executed last because we need to lower all
1388 // sub-expressions even if we throw them away later.
1389 let op = match *op {
1390 InlineAsmOperand::In { reg, ref expr } => hir::InlineAsmOperand::In {
1391 expr: self.lower_expr_mut(expr),
1392 reg: lower_reg(reg)?,
1394 InlineAsmOperand::Out { reg, late, ref expr } => hir::InlineAsmOperand::Out {
1396 expr: expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
1397 reg: lower_reg(reg)?,
1399 InlineAsmOperand::InOut { reg, late, ref expr } => {
1400 hir::InlineAsmOperand::InOut {
1402 expr: self.lower_expr_mut(expr),
1403 reg: lower_reg(reg)?,
1406 InlineAsmOperand::SplitInOut { reg, late, ref in_expr, ref out_expr } => {
1407 hir::InlineAsmOperand::SplitInOut {
1409 in_expr: self.lower_expr_mut(in_expr),
1410 out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
1411 reg: lower_reg(reg)?,
1414 InlineAsmOperand::Const { ref expr } => {
1415 hir::InlineAsmOperand::Const { expr: self.lower_expr_mut(expr) }
1417 InlineAsmOperand::Sym { ref expr } => {
1418 hir::InlineAsmOperand::Sym { expr: self.lower_expr_mut(expr) }
1425 // Stop if there were any errors when lowering the register classes
1426 if operands.len() != asm.operands.len() || sess.asm_arch.is_none() {
1427 return hir::ExprKind::Err;
1430 // Validate template modifiers against the register classes for the operands
1431 let asm_arch = sess.asm_arch.unwrap();
1432 for p in &asm.template {
1433 if let InlineAsmTemplatePiece::Placeholder {
1435 modifier: Some(modifier),
1436 span: placeholder_span,
1439 let op_sp = asm.operands[operand_idx].1;
1440 match &operands[operand_idx].0 {
1441 hir::InlineAsmOperand::In { reg, .. }
1442 | hir::InlineAsmOperand::Out { reg, .. }
1443 | hir::InlineAsmOperand::InOut { reg, .. }
1444 | hir::InlineAsmOperand::SplitInOut { reg, .. } => {
1445 let class = reg.reg_class();
1446 let valid_modifiers = class.valid_modifiers(asm_arch);
1447 if !valid_modifiers.contains(&modifier) {
1448 let mut err = sess.struct_span_err(
1450 "invalid asm template modifier for this register class",
1452 err.span_label(placeholder_span, "template modifier");
1453 err.span_label(op_sp, "argument");
1454 if !valid_modifiers.is_empty() {
1455 let mut mods = format!("`{}`", valid_modifiers[0]);
1456 for m in &valid_modifiers[1..] {
1457 let _ = write!(mods, ", `{}`", m);
1460 "the `{}` register class supports \
1461 the following template modifiers: {}",
1467 "the `{}` register class does not support template modifiers",
1474 hir::InlineAsmOperand::Const { .. } => {
1475 let mut err = sess.struct_span_err(
1477 "asm template modifiers are not allowed for `const` arguments",
1479 err.span_label(placeholder_span, "template modifier");
1480 err.span_label(op_sp, "argument");
1483 hir::InlineAsmOperand::Sym { .. } => {
1484 let mut err = sess.struct_span_err(
1486 "asm template modifiers are not allowed for `sym` arguments",
1488 err.span_label(placeholder_span, "template modifier");
1489 err.span_label(op_sp, "argument");
1496 let mut used_input_regs = FxHashMap::default();
1497 let mut used_output_regs = FxHashMap::default();
1498 let mut required_features: Vec<&str> = vec![];
1499 for (idx, &(ref op, op_sp)) in operands.iter().enumerate() {
1500 if let Some(reg) = op.reg() {
1501 // Make sure we don't accidentally carry features from the
1502 // previous iteration.
1503 required_features.clear();
1505 // Validate register classes against currently enabled target
1506 // features. We check that at least one type is available for
1507 // the current target.
1508 let reg_class = reg.reg_class();
1509 for &(_, feature) in reg_class.supported_types(asm_arch) {
1510 if let Some(feature) = feature {
1511 if self.sess.target_features.contains(&Symbol::intern(feature)) {
1512 required_features.clear();
1515 required_features.push(feature);
1518 required_features.clear();
1522 // We are sorting primitive strs here and can use unstable sort here
1523 required_features.sort_unstable();
1524 required_features.dedup();
1525 match &required_features[..] {
1529 "register class `{}` requires the `{}` target feature",
1533 sess.struct_span_err(op_sp, &msg).emit();
1537 "register class `{}` requires at least one target feature: {}",
1541 sess.struct_span_err(op_sp, &msg).emit();
1545 // Check for conflicts between explicit register operands.
1546 if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
1547 let (input, output) = match op {
1548 hir::InlineAsmOperand::In { .. } => (true, false),
1549 // Late output do not conflict with inputs, but normal outputs do
1550 hir::InlineAsmOperand::Out { late, .. } => (!late, true),
1551 hir::InlineAsmOperand::InOut { .. }
1552 | hir::InlineAsmOperand::SplitInOut { .. } => (true, true),
1553 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::Sym { .. } => {
1558 // Flag to output the error only once per operand
1559 let mut skip = false;
1560 reg.overlapping_regs(|r| {
1561 let mut check = |used_regs: &mut FxHashMap<asm::InlineAsmReg, usize>,
1563 match used_regs.entry(r) {
1564 Entry::Occupied(o) => {
1570 let idx2 = *o.get();
1571 let &(ref op2, op_sp2) = &operands[idx2];
1572 let reg2 = match op2.reg() {
1573 Some(asm::InlineAsmRegOrRegClass::Reg(r)) => r,
1574 _ => unreachable!(),
1578 "register `{}` conflicts with register `{}`",
1582 let mut err = sess.struct_span_err(op_sp, &msg);
1583 err.span_label(op_sp, &format!("register `{}`", reg.name()));
1584 err.span_label(op_sp2, &format!("register `{}`", reg2.name()));
1588 hir::InlineAsmOperand::In { .. },
1589 hir::InlineAsmOperand::Out { late, .. },
1592 hir::InlineAsmOperand::Out { late, .. },
1593 hir::InlineAsmOperand::In { .. },
1596 let out_op_sp = if input { op_sp2 } else { op_sp };
1597 let msg = "use `lateout` instead of \
1598 `out` to avoid conflict";
1599 err.span_help(out_op_sp, msg);
1606 Entry::Vacant(v) => {
1612 check(&mut used_input_regs, true);
1615 check(&mut used_output_regs, false);
1622 let operands = self.arena.alloc_from_iter(operands);
1623 let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
1624 let line_spans = self.arena.alloc_slice(&asm.line_spans[..]);
1625 let hir_asm = hir::InlineAsm { template, operands, options: asm.options, line_spans };
1626 hir::ExprKind::InlineAsm(self.arena.alloc(hir_asm))
1629 fn lower_expr_llvm_asm(&mut self, asm: &LlvmInlineAsm) -> hir::ExprKind<'hir> {
1630 let inner = hir::LlvmInlineAsmInner {
1631 inputs: asm.inputs.iter().map(|&(c, _)| c).collect(),
1635 .map(|out| hir::LlvmInlineAsmOutput {
1636 constraint: out.constraint,
1638 is_indirect: out.is_indirect,
1639 span: out.expr.span,
1643 asm_str_style: asm.asm_str_style,
1644 clobbers: asm.clobbers.clone(),
1645 volatile: asm.volatile,
1646 alignstack: asm.alignstack,
1647 dialect: asm.dialect,
1649 let hir_asm = hir::LlvmInlineAsm {
1651 inputs_exprs: self.arena.alloc_from_iter(
1652 asm.inputs.iter().map(|&(_, ref input)| self.lower_expr_mut(input)),
1656 .alloc_from_iter(asm.outputs.iter().map(|out| self.lower_expr_mut(&out.expr))),
1658 hir::ExprKind::LlvmInlineAsm(self.arena.alloc(hir_asm))
1661 fn lower_field(&mut self, f: &Field) -> hir::Field<'hir> {
1663 hir_id: self.next_id(),
1665 expr: self.lower_expr(&f.expr),
1667 is_shorthand: f.is_shorthand,
1671 fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
1672 match self.generator_kind {
1673 Some(hir::GeneratorKind::Gen) => {}
1674 Some(hir::GeneratorKind::Async(_)) => {
1679 "`async` generators are not yet supported"
1683 None => self.generator_kind = Some(hir::GeneratorKind::Gen),
1687 opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| self.expr_unit(span));
1689 hir::ExprKind::Yield(expr, hir::YieldSource::Yield)
1692 /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1695 /// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
1697 /// [opt_ident]: loop {
1699 /// match ::std::iter::Iterator::next(&mut iter) {
1700 /// ::std::option::Option::Some(val) => __next = val,
1701 /// ::std::option::Option::None => break
1703 /// let <pat> = __next;
1704 /// StmtKind::Expr(<body>);
1717 opt_label: Option<Label>,
1718 ) -> hir::Expr<'hir> {
1719 let orig_head_span = head.span;
1721 let mut head = self.lower_expr_mut(head);
1722 let desugared_span = self.mark_span_with_reason(
1723 DesugaringKind::ForLoop(ForLoopLoc::Head),
1727 head.span = desugared_span;
1729 let iter = Ident::with_dummy_span(sym::iter);
1731 let next_ident = Ident::with_dummy_span(sym::__next);
1732 let (next_pat, next_pat_hid) = self.pat_ident_binding_mode(
1735 hir::BindingAnnotation::Mutable,
1738 // `::std::option::Option::Some(val) => __next = val`
1740 let val_ident = Ident::with_dummy_span(sym::val);
1741 let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
1742 let val_expr = self.expr_ident(pat.span, val_ident, val_pat_hid);
1743 let next_expr = self.expr_ident(pat.span, next_ident, next_pat_hid);
1744 let assign = self.arena.alloc(self.expr(
1746 hir::ExprKind::Assign(next_expr, val_expr, pat.span),
1749 let some_pat = self.pat_some(pat.span, val_pat);
1750 self.arm(some_pat, assign)
1753 // `::std::option::Option::None => break`
1756 self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
1757 let pat = self.pat_none(e.span);
1758 self.arm(pat, break_expr)
1762 let (iter_pat, iter_pat_nid) =
1763 self.pat_ident_binding_mode(desugared_span, iter, hir::BindingAnnotation::Mutable);
1765 // `match ::std::iter::Iterator::next(&mut iter) { ... }`
1767 let iter = self.expr_ident(desugared_span, iter, iter_pat_nid);
1768 let ref_mut_iter = self.expr_mut_addr_of(desugared_span, iter);
1769 let next_expr = self.expr_call_lang_item_fn(
1771 hir::LangItem::IteratorNext,
1772 arena_vec![self; ref_mut_iter],
1774 let arms = arena_vec![self; pat_arm, break_arm];
1776 self.expr_match(desugared_span, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1778 let match_stmt = self.stmt_expr(desugared_span, match_expr);
1780 let next_expr = self.expr_ident(desugared_span, next_ident, next_pat_hid);
1783 let next_let = self.stmt_let_pat(
1788 hir::LocalSource::ForLoopDesugar,
1791 // `let <pat> = __next`
1792 let pat = self.lower_pat(pat);
1793 let pat_let = self.stmt_let_pat(
1798 hir::LocalSource::ForLoopDesugar,
1801 let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1802 let body_expr = self.expr_block(body_block, ThinVec::new());
1803 let body_stmt = self.stmt_expr(body.span, body_expr);
1805 let loop_block = self.block_all(
1807 arena_vec![self; next_let, match_stmt, pat_let, body_stmt],
1811 // `[opt_ident]: loop { ... }`
1812 let kind = hir::ExprKind::Loop(
1815 hir::LoopSource::ForLoop,
1816 e.span.with_hi(orig_head_span.hi()),
1819 self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: e.span });
1821 // `mut iter => { ... }`
1822 let iter_arm = self.arm(iter_pat, loop_expr);
1824 let into_iter_span = self.mark_span_with_reason(
1825 DesugaringKind::ForLoop(ForLoopLoc::IntoIter),
1830 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1831 let into_iter_expr = {
1832 self.expr_call_lang_item_fn(
1834 hir::LangItem::IntoIterIntoIter,
1835 arena_vec![self; head],
1839 let match_expr = self.arena.alloc(self.expr_match(
1842 arena_vec![self; iter_arm],
1843 hir::MatchSource::ForLoopDesugar,
1846 let attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect();
1848 // This is effectively `{ let _result = ...; _result }`.
1849 // The construct was introduced in #21984 and is necessary to make sure that
1850 // temporaries in the `head` expression are dropped and do not leak to the
1851 // surrounding scope of the `match` since the `match` is not a terminating scope.
1853 // Also, add the attributes to the outer returned expr node.
1854 self.expr_drop_temps_mut(desugared_span, match_expr, attrs.into())
1857 /// Desugar `ExprKind::Try` from: `<expr>?` into:
1859 /// match Try::into_result(<expr>) {
1860 /// Ok(val) => #[allow(unreachable_code)] val,
1861 /// Err(err) => #[allow(unreachable_code)]
1862 /// // If there is an enclosing `try {...}`:
1863 /// break 'catch_target Try::from_error(From::from(err)),
1865 /// return Try::from_error(From::from(err)),
1868 fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
1869 let unstable_span = self.mark_span_with_reason(
1870 DesugaringKind::QuestionMark,
1872 self.allow_try_trait.clone(),
1874 let try_span = self.sess.source_map().end_point(span);
1875 let try_span = self.mark_span_with_reason(
1876 DesugaringKind::QuestionMark,
1878 self.allow_try_trait.clone(),
1881 // `Try::into_result(<expr>)`
1884 let sub_expr = self.lower_expr_mut(sub_expr);
1886 self.expr_call_lang_item_fn(
1888 hir::LangItem::TryIntoResult,
1889 arena_vec![self; sub_expr],
1893 // `#[allow(unreachable_code)]`
1895 // `allow(unreachable_code)`
1897 let allow_ident = Ident::new(sym::allow, span);
1898 let uc_ident = Ident::new(sym::unreachable_code, span);
1899 let uc_nested = attr::mk_nested_word_item(uc_ident);
1900 attr::mk_list_item(allow_ident, vec![uc_nested])
1902 attr::mk_attr_outer(allow)
1904 let attrs = vec![attr];
1906 // `Ok(val) => #[allow(unreachable_code)] val,`
1908 let val_ident = Ident::with_dummy_span(sym::val);
1909 let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1910 let val_expr = self.arena.alloc(self.expr_ident_with_attrs(
1914 ThinVec::from(attrs.clone()),
1916 let ok_pat = self.pat_ok(span, val_pat);
1917 self.arm(ok_pat, val_expr)
1920 // `Err(err) => #[allow(unreachable_code)]
1921 // return Try::from_error(From::from(err)),`
1923 let err_ident = Ident::with_dummy_span(sym::err);
1924 let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
1926 let err_expr = self.expr_ident_mut(try_span, err_ident, err_local_nid);
1927 self.expr_call_lang_item_fn(
1929 hir::LangItem::FromFrom,
1930 arena_vec![self; err_expr],
1933 let from_err_expr = self.wrap_in_try_constructor(
1934 hir::LangItem::TryFromError,
1939 let thin_attrs = ThinVec::from(attrs);
1940 let catch_scope = self.catch_scopes.last().copied();
1941 let ret_expr = if let Some(catch_node) = catch_scope {
1942 let target_id = Ok(self.lower_node_id(catch_node));
1943 self.arena.alloc(self.expr(
1945 hir::ExprKind::Break(
1946 hir::Destination { label: None, target_id },
1947 Some(from_err_expr),
1952 self.arena.alloc(self.expr(
1954 hir::ExprKind::Ret(Some(from_err_expr)),
1959 let err_pat = self.pat_err(try_span, err_local);
1960 self.arm(err_pat, ret_expr)
1963 hir::ExprKind::Match(
1965 arena_vec![self; err_arm, ok_arm],
1966 hir::MatchSource::TryDesugar,
1970 // =========================================================================
1971 // Helper methods for building HIR.
1972 // =========================================================================
1974 /// Constructs a `true` or `false` literal expression.
1975 pub(super) fn expr_bool(&mut self, span: Span, val: bool) -> &'hir hir::Expr<'hir> {
1976 let lit = Spanned { span, node: LitKind::Bool(val) };
1977 self.arena.alloc(self.expr(span, hir::ExprKind::Lit(lit), ThinVec::new()))
1980 /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
1982 /// In terms of drop order, it has the same effect as wrapping `expr` in
1983 /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
1985 /// The drop order can be important in e.g. `if expr { .. }`.
1986 pub(super) fn expr_drop_temps(
1989 expr: &'hir hir::Expr<'hir>,
1991 ) -> &'hir hir::Expr<'hir> {
1992 self.arena.alloc(self.expr_drop_temps_mut(span, expr, attrs))
1995 pub(super) fn expr_drop_temps_mut(
1998 expr: &'hir hir::Expr<'hir>,
2000 ) -> hir::Expr<'hir> {
2001 self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
2007 arg: &'hir hir::Expr<'hir>,
2008 arms: &'hir [hir::Arm<'hir>],
2009 source: hir::MatchSource,
2010 ) -> hir::Expr<'hir> {
2011 self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
2014 fn expr_break(&mut self, span: Span, attrs: AttrVec) -> &'hir hir::Expr<'hir> {
2015 let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
2016 self.arena.alloc(self.expr(span, expr_break, attrs))
2019 fn expr_mut_addr_of(&mut self, span: Span, e: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
2022 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e),
2027 fn expr_unit(&mut self, sp: Span) -> &'hir hir::Expr<'hir> {
2028 self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[]), ThinVec::new()))
2034 e: &'hir hir::Expr<'hir>,
2035 args: &'hir [hir::Expr<'hir>],
2036 ) -> hir::Expr<'hir> {
2037 self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
2043 e: &'hir hir::Expr<'hir>,
2044 args: &'hir [hir::Expr<'hir>],
2045 ) -> &'hir hir::Expr<'hir> {
2046 self.arena.alloc(self.expr_call_mut(span, e, args))
2049 fn expr_call_lang_item_fn_mut(
2052 lang_item: hir::LangItem,
2053 args: &'hir [hir::Expr<'hir>],
2054 ) -> hir::Expr<'hir> {
2055 let path = self.arena.alloc(self.expr_lang_item_path(span, lang_item, ThinVec::new()));
2056 self.expr_call_mut(span, path, args)
2059 fn expr_call_lang_item_fn(
2062 lang_item: hir::LangItem,
2063 args: &'hir [hir::Expr<'hir>],
2064 ) -> &'hir hir::Expr<'hir> {
2065 self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args))
2068 fn expr_lang_item_path(
2071 lang_item: hir::LangItem,
2073 ) -> hir::Expr<'hir> {
2074 self.expr(span, hir::ExprKind::Path(hir::QPath::LangItem(lang_item, span)), attrs)
2077 pub(super) fn expr_ident(
2081 binding: hir::HirId,
2082 ) -> &'hir hir::Expr<'hir> {
2083 self.arena.alloc(self.expr_ident_mut(sp, ident, binding))
2086 pub(super) fn expr_ident_mut(
2090 binding: hir::HirId,
2091 ) -> hir::Expr<'hir> {
2092 self.expr_ident_with_attrs(sp, ident, binding, ThinVec::new())
2095 fn expr_ident_with_attrs(
2099 binding: hir::HirId,
2101 ) -> hir::Expr<'hir> {
2102 let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
2104 self.arena.alloc(hir::Path {
2106 res: Res::Local(binding),
2107 segments: arena_vec![self; hir::PathSegment::from_ident(ident)],
2111 self.expr(span, expr_path, attrs)
2114 fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> {
2115 let hir_id = self.next_id();
2116 let span = expr.span;
2119 hir::ExprKind::Block(
2120 self.arena.alloc(hir::Block {
2124 rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
2126 targeted_by_break: false,
2134 fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
2135 let blk = self.block_all(span, &[], None);
2136 let expr = self.expr_block(blk, ThinVec::new());
2137 self.arena.alloc(expr)
2140 pub(super) fn expr_block(
2142 b: &'hir hir::Block<'hir>,
2144 ) -> hir::Expr<'hir> {
2145 self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
2151 kind: hir::ExprKind<'hir>,
2153 ) -> hir::Expr<'hir> {
2154 let hir_id = self.next_id();
2155 self.lower_attrs(hir_id, &attrs);
2156 hir::Expr { hir_id, kind, span }
2159 fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> {
2160 hir::Field { hir_id: self.next_id(), ident, span, expr, is_shorthand: false }
2163 fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> {
2164 hir::Arm { hir_id: self.next_id(), pat, guard: None, span: expr.span, body: expr }