]> git.proxmox.com Git - rustc.git/blob - src/librustc/hir/lowering/expr.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc / hir / lowering / expr.rs
1 use super::{LoweringContext, ParamMode, ParenthesizedGenericArgs, ImplTraitContext};
2 use crate::hir::{self, HirVec};
3 use crate::hir::def::Res;
4 use crate::hir::ptr::P;
5
6 use rustc_data_structures::thin_vec::ThinVec;
7
8 use syntax::attr;
9 use syntax::ptr::P as AstP;
10 use syntax::ast::*;
11 use syntax::source_map::{respan, DesugaringKind, Span, Spanned};
12 use syntax::symbol::{sym, Symbol};
13
14 use rustc_error_codes::*;
15
16 impl LoweringContext<'_> {
17 fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> HirVec<hir::Expr> {
18 exprs.iter().map(|x| self.lower_expr(x)).collect()
19 }
20
21 pub(super) fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
22 let kind = match e.kind {
23 ExprKind::Box(ref inner) => hir::ExprKind::Box(P(self.lower_expr(inner))),
24 ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
25 ExprKind::Repeat(ref expr, ref count) => {
26 let expr = P(self.lower_expr(expr));
27 let count = self.lower_anon_const(count);
28 hir::ExprKind::Repeat(expr, count)
29 }
30 ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
31 ExprKind::Call(ref f, ref args) => {
32 let f = P(self.lower_expr(f));
33 hir::ExprKind::Call(f, self.lower_exprs(args))
34 }
35 ExprKind::MethodCall(ref seg, ref args) => {
36 let hir_seg = P(self.lower_path_segment(
37 e.span,
38 seg,
39 ParamMode::Optional,
40 0,
41 ParenthesizedGenericArgs::Err,
42 ImplTraitContext::disallowed(),
43 None,
44 ));
45 let args = self.lower_exprs(args);
46 hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args)
47 }
48 ExprKind::Binary(binop, ref lhs, ref rhs) => {
49 let binop = self.lower_binop(binop);
50 let lhs = P(self.lower_expr(lhs));
51 let rhs = P(self.lower_expr(rhs));
52 hir::ExprKind::Binary(binop, lhs, rhs)
53 }
54 ExprKind::Unary(op, ref ohs) => {
55 let op = self.lower_unop(op);
56 let ohs = P(self.lower_expr(ohs));
57 hir::ExprKind::Unary(op, ohs)
58 }
59 ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.kind.clone())),
60 ExprKind::Cast(ref expr, ref ty) => {
61 let expr = P(self.lower_expr(expr));
62 hir::ExprKind::Cast(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
63 }
64 ExprKind::Type(ref expr, ref ty) => {
65 let expr = P(self.lower_expr(expr));
66 hir::ExprKind::Type(expr, self.lower_ty(ty, ImplTraitContext::disallowed()))
67 }
68 ExprKind::AddrOf(k, m, ref ohs) => {
69 let ohs = P(self.lower_expr(ohs));
70 hir::ExprKind::AddrOf(k, m, ohs)
71 }
72 ExprKind::Let(ref pat, ref scrutinee) => self.lower_expr_let(e.span, pat, scrutinee),
73 ExprKind::If(ref cond, ref then, ref else_opt) => {
74 self.lower_expr_if(e.span, cond, then, else_opt.as_deref())
75 }
76 ExprKind::While(ref cond, ref body, opt_label) => self.with_loop_scope(e.id, |this| {
77 this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
78 }),
79 ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
80 hir::ExprKind::Loop(
81 this.lower_block(body, false),
82 this.lower_label(opt_label),
83 hir::LoopSource::Loop,
84 )
85 }),
86 ExprKind::TryBlock(ref body) => self.lower_expr_try_block(body),
87 ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
88 P(self.lower_expr(expr)),
89 arms.iter().map(|x| self.lower_arm(x)).collect(),
90 hir::MatchSource::Normal,
91 ),
92 ExprKind::Async(capture_clause, closure_node_id, ref block) => {
93 self.make_async_expr(
94 capture_clause,
95 closure_node_id,
96 None,
97 block.span,
98 hir::AsyncGeneratorKind::Block,
99 |this| this.with_new_scopes(|this| this.lower_block_expr(block)),
100 )
101 }
102 ExprKind::Await(ref expr) => self.lower_expr_await(e.span, expr),
103 ExprKind::Closure(
104 capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span
105 ) => if let IsAsync::Async { closure_id, .. } = asyncness {
106 self.lower_expr_async_closure(capture_clause, closure_id, decl, body, fn_decl_span)
107 } else {
108 self.lower_expr_closure(capture_clause, movability, decl, body, fn_decl_span)
109 }
110 ExprKind::Block(ref blk, opt_label) => {
111 hir::ExprKind::Block(self.lower_block(blk,
112 opt_label.is_some()),
113 self.lower_label(opt_label))
114 }
115 ExprKind::Assign(ref el, ref er) => {
116 hir::ExprKind::Assign(P(self.lower_expr(el)), P(self.lower_expr(er)))
117 }
118 ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
119 self.lower_binop(op),
120 P(self.lower_expr(el)),
121 P(self.lower_expr(er)),
122 ),
123 ExprKind::Field(ref el, ident) => hir::ExprKind::Field(P(self.lower_expr(el)), ident),
124 ExprKind::Index(ref el, ref er) => {
125 hir::ExprKind::Index(P(self.lower_expr(el)), P(self.lower_expr(er)))
126 }
127 ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
128 self.lower_expr_range_closed(e.span, e1, e2)
129 }
130 ExprKind::Range(ref e1, ref e2, lims) => {
131 self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
132 }
133 ExprKind::Path(ref qself, ref path) => {
134 let qpath = self.lower_qpath(
135 e.id,
136 qself,
137 path,
138 ParamMode::Optional,
139 ImplTraitContext::disallowed(),
140 );
141 hir::ExprKind::Path(qpath)
142 }
143 ExprKind::Break(opt_label, ref opt_expr) => {
144 hir::ExprKind::Break(
145 self.lower_jump_destination(e.id, opt_label),
146 opt_expr.as_ref().map(|x| P(self.lower_expr(x))),
147 )
148 }
149 ExprKind::Continue(opt_label) => {
150 hir::ExprKind::Continue(self.lower_jump_destination(e.id, opt_label))
151 }
152 ExprKind::Ret(ref e) => hir::ExprKind::Ret(e.as_ref().map(|x| P(self.lower_expr(x)))),
153 ExprKind::InlineAsm(ref asm) => self.lower_expr_asm(asm),
154 ExprKind::Struct(ref path, ref fields, ref maybe_expr) => hir::ExprKind::Struct(
155 P(self.lower_qpath(
156 e.id,
157 &None,
158 path,
159 ParamMode::Optional,
160 ImplTraitContext::disallowed(),
161 )),
162 fields.iter().map(|x| self.lower_field(x)).collect(),
163 maybe_expr.as_ref().map(|x| P(self.lower_expr(x))),
164 ),
165 ExprKind::Paren(ref ex) => {
166 let mut ex = self.lower_expr(ex);
167 // Include parens in span, but only if it is a super-span.
168 if e.span.contains(ex.span) {
169 ex.span = e.span;
170 }
171 // Merge attributes into the inner expression.
172 let mut attrs = e.attrs.clone();
173 attrs.extend::<Vec<_>>(ex.attrs.into());
174 ex.attrs = attrs;
175 return ex;
176 }
177
178 ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
179
180 ExprKind::Err => hir::ExprKind::Err,
181
182 // Desugar `ExprForLoop`
183 // from: `[opt_ident]: for <pat> in <head> <body>`
184 ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
185 return self.lower_expr_for(e, pat, head, body, opt_label);
186 }
187 ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
188 ExprKind::Mac(_) => panic!("Shouldn't exist here"),
189 };
190
191 hir::Expr {
192 hir_id: self.lower_node_id(e.id),
193 kind,
194 span: e.span,
195 attrs: e.attrs.clone(),
196 }
197 }
198
199 fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
200 match u {
201 UnOp::Deref => hir::UnDeref,
202 UnOp::Not => hir::UnNot,
203 UnOp::Neg => hir::UnNeg,
204 }
205 }
206
207 fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
208 Spanned {
209 node: match b.node {
210 BinOpKind::Add => hir::BinOpKind::Add,
211 BinOpKind::Sub => hir::BinOpKind::Sub,
212 BinOpKind::Mul => hir::BinOpKind::Mul,
213 BinOpKind::Div => hir::BinOpKind::Div,
214 BinOpKind::Rem => hir::BinOpKind::Rem,
215 BinOpKind::And => hir::BinOpKind::And,
216 BinOpKind::Or => hir::BinOpKind::Or,
217 BinOpKind::BitXor => hir::BinOpKind::BitXor,
218 BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
219 BinOpKind::BitOr => hir::BinOpKind::BitOr,
220 BinOpKind::Shl => hir::BinOpKind::Shl,
221 BinOpKind::Shr => hir::BinOpKind::Shr,
222 BinOpKind::Eq => hir::BinOpKind::Eq,
223 BinOpKind::Lt => hir::BinOpKind::Lt,
224 BinOpKind::Le => hir::BinOpKind::Le,
225 BinOpKind::Ne => hir::BinOpKind::Ne,
226 BinOpKind::Ge => hir::BinOpKind::Ge,
227 BinOpKind::Gt => hir::BinOpKind::Gt,
228 },
229 span: b.span,
230 }
231 }
232
233 /// Emit an error and lower `ast::ExprKind::Let(pat, scrutinee)` into:
234 /// ```rust
235 /// match scrutinee { pats => true, _ => false }
236 /// ```
237 fn lower_expr_let(&mut self, span: Span, pat: &Pat, scrutinee: &Expr) -> hir::ExprKind {
238 // If we got here, the `let` expression is not allowed.
239
240 if self.sess.opts.unstable_features.is_nightly_build() {
241 self.sess
242 .struct_span_err(span, "`let` expressions are not supported here")
243 .note("only supported directly in conditions of `if`- and `while`-expressions")
244 .note("as well as when nested within `&&` and parenthesis in those conditions")
245 .emit();
246 }
247 else {
248 self.sess
249 .struct_span_err(span, "expected expression, found statement (`let`)")
250 .note("variable declaration using `let` is a statement")
251 .emit();
252 }
253
254 // For better recovery, we emit:
255 // ```
256 // match scrutinee { pat => true, _ => false }
257 // ```
258 // While this doesn't fully match the user's intent, it has key advantages:
259 // 1. We can avoid using `abort_if_errors`.
260 // 2. We can typeck both `pat` and `scrutinee`.
261 // 3. `pat` is allowed to be refutable.
262 // 4. The return type of the block is `bool` which seems like what the user wanted.
263 let scrutinee = self.lower_expr(scrutinee);
264 let then_arm = {
265 let pat = self.lower_pat(pat);
266 let expr = self.expr_bool(span, true);
267 self.arm(pat, P(expr))
268 };
269 let else_arm = {
270 let pat = self.pat_wild(span);
271 let expr = self.expr_bool(span, false);
272 self.arm(pat, P(expr))
273 };
274 hir::ExprKind::Match(
275 P(scrutinee),
276 vec![then_arm, else_arm].into(),
277 hir::MatchSource::Normal,
278 )
279 }
280
281 fn lower_expr_if(
282 &mut self,
283 span: Span,
284 cond: &Expr,
285 then: &Block,
286 else_opt: Option<&Expr>,
287 ) -> hir::ExprKind {
288 // FIXME(#53667): handle lowering of && and parens.
289
290 // `_ => else_block` where `else_block` is `{}` if there's `None`:
291 let else_pat = self.pat_wild(span);
292 let (else_expr, contains_else_clause) = match else_opt {
293 None => (self.expr_block_empty(span), false),
294 Some(els) => (self.lower_expr(els), true),
295 };
296 let else_arm = self.arm(else_pat, P(else_expr));
297
298 // Handle then + scrutinee:
299 let then_expr = self.lower_block_expr(then);
300 let (then_pat, scrutinee, desugar) = match cond.kind {
301 // `<pat> => <then>`:
302 ExprKind::Let(ref pat, ref scrutinee) => {
303 let scrutinee = self.lower_expr(scrutinee);
304 let pat = self.lower_pat(pat);
305 (pat, scrutinee, hir::MatchSource::IfLetDesugar { contains_else_clause })
306 }
307 // `true => <then>`:
308 _ => {
309 // Lower condition:
310 let cond = self.lower_expr(cond);
311 let span_block = self.mark_span_with_reason(
312 DesugaringKind::CondTemporary,
313 cond.span,
314 None
315 );
316 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
317 // to preserve drop semantics since `if cond { ... }` does not
318 // let temporaries live outside of `cond`.
319 let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
320 let pat = self.pat_bool(span, true);
321 (pat, cond, hir::MatchSource::IfDesugar { contains_else_clause })
322 }
323 };
324 let then_arm = self.arm(then_pat, P(then_expr));
325
326 hir::ExprKind::Match(P(scrutinee), vec![then_arm, else_arm].into(), desugar)
327 }
328
329 fn lower_expr_while_in_loop_scope(
330 &mut self,
331 span: Span,
332 cond: &Expr,
333 body: &Block,
334 opt_label: Option<Label>
335 ) -> hir::ExprKind {
336 // FIXME(#53667): handle lowering of && and parens.
337
338 // Note that the block AND the condition are evaluated in the loop scope.
339 // This is done to allow `break` from inside the condition of the loop.
340
341 // `_ => break`:
342 let else_arm = {
343 let else_pat = self.pat_wild(span);
344 let else_expr = self.expr_break(span, ThinVec::new());
345 self.arm(else_pat, else_expr)
346 };
347
348 // Handle then + scrutinee:
349 let then_expr = self.lower_block_expr(body);
350 let (then_pat, scrutinee, desugar, source) = match cond.kind {
351 ExprKind::Let(ref pat, ref scrutinee) => {
352 // to:
353 //
354 // [opt_ident]: loop {
355 // match <sub_expr> {
356 // <pat> => <body>,
357 // _ => break
358 // }
359 // }
360 let scrutinee = self.with_loop_condition_scope(|t| t.lower_expr(scrutinee));
361 let pat = self.lower_pat(pat);
362 (pat, scrutinee, hir::MatchSource::WhileLetDesugar, hir::LoopSource::WhileLet)
363 }
364 _ => {
365 // We desugar: `'label: while $cond $body` into:
366 //
367 // ```
368 // 'label: loop {
369 // match drop-temps { $cond } {
370 // true => $body,
371 // _ => break,
372 // }
373 // }
374 // ```
375
376 // Lower condition:
377 let cond = self.with_loop_condition_scope(|this| this.lower_expr(cond));
378 let span_block = self.mark_span_with_reason(
379 DesugaringKind::CondTemporary,
380 cond.span,
381 None,
382 );
383 // Wrap in a construct equivalent to `{ let _t = $cond; _t }`
384 // to preserve drop semantics since `while cond { ... }` does not
385 // let temporaries live outside of `cond`.
386 let cond = self.expr_drop_temps(span_block, P(cond), ThinVec::new());
387 // `true => <then>`:
388 let pat = self.pat_bool(span, true);
389 (pat, cond, hir::MatchSource::WhileDesugar, hir::LoopSource::While)
390 }
391 };
392 let then_arm = self.arm(then_pat, P(then_expr));
393
394 // `match <scrutinee> { ... }`
395 let match_expr = self.expr_match(
396 scrutinee.span,
397 P(scrutinee),
398 hir_vec![then_arm, else_arm],
399 desugar,
400 );
401
402 // `[opt_ident]: loop { ... }`
403 hir::ExprKind::Loop(
404 P(self.block_expr(P(match_expr))),
405 self.lower_label(opt_label),
406 source
407 )
408 }
409
410 /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
411 /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_ok(()) }`
412 /// and save the block id to use it as a break target for desugaring of the `?` operator.
413 fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind {
414 self.with_catch_scope(body.id, |this| {
415 let mut block = this.lower_block(body, true).into_inner();
416
417 let try_span = this.mark_span_with_reason(
418 DesugaringKind::TryBlock,
419 body.span,
420 this.allow_try_trait.clone(),
421 );
422
423 // Final expression of the block (if present) or `()` with span at the end of block
424 let tail_expr = block.expr.take().map_or_else(
425 || this.expr_unit(this.sess.source_map().end_point(try_span)),
426 |x: P<hir::Expr>| x.into_inner(),
427 );
428
429 let ok_wrapped_span = this.mark_span_with_reason(
430 DesugaringKind::TryBlock,
431 tail_expr.span,
432 None
433 );
434
435 // `::std::ops::Try::from_ok($tail_expr)`
436 block.expr = Some(this.wrap_in_try_constructor(
437 sym::from_ok, try_span, tail_expr, ok_wrapped_span));
438
439 hir::ExprKind::Block(P(block), None)
440 })
441 }
442
443 fn wrap_in_try_constructor(
444 &mut self,
445 method: Symbol,
446 method_span: Span,
447 expr: hir::Expr,
448 overall_span: Span,
449 ) -> P<hir::Expr> {
450 let path = &[sym::ops, sym::Try, method];
451 let constructor = P(self.expr_std_path(method_span, path, None, ThinVec::new()));
452 P(self.expr_call(overall_span, constructor, hir_vec![expr]))
453 }
454
455 fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
456 hir::Arm {
457 hir_id: self.next_id(),
458 attrs: self.lower_attrs(&arm.attrs),
459 pat: self.lower_pat(&arm.pat),
460 guard: match arm.guard {
461 Some(ref x) => Some(hir::Guard::If(P(self.lower_expr(x)))),
462 _ => None,
463 },
464 body: P(self.lower_expr(&arm.body)),
465 span: arm.span,
466 }
467 }
468
469 pub(super) fn make_async_expr(
470 &mut self,
471 capture_clause: CaptureBy,
472 closure_node_id: NodeId,
473 ret_ty: Option<AstP<Ty>>,
474 span: Span,
475 async_gen_kind: hir::AsyncGeneratorKind,
476 body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
477 ) -> hir::ExprKind {
478 let output = match ret_ty {
479 Some(ty) => FunctionRetTy::Ty(ty),
480 None => FunctionRetTy::Default(span),
481 };
482 let ast_decl = FnDecl {
483 inputs: vec![],
484 output,
485 };
486 let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None);
487 let body_id = self.lower_fn_body(&ast_decl, |this| {
488 this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind));
489 body(this)
490 });
491
492 // `static || -> <ret_ty> { body }`:
493 let generator_kind = hir::ExprKind::Closure(
494 capture_clause,
495 decl,
496 body_id,
497 span,
498 Some(hir::Movability::Static)
499 );
500 let generator = hir::Expr {
501 hir_id: self.lower_node_id(closure_node_id),
502 kind: generator_kind,
503 span,
504 attrs: ThinVec::new(),
505 };
506
507 // `future::from_generator`:
508 let unstable_span = self.mark_span_with_reason(
509 DesugaringKind::Async,
510 span,
511 self.allow_gen_future.clone(),
512 );
513 let gen_future = self.expr_std_path(
514 unstable_span,
515 &[sym::future, sym::from_generator],
516 None,
517 ThinVec::new()
518 );
519
520 // `future::from_generator(generator)`:
521 hir::ExprKind::Call(P(gen_future), hir_vec![generator])
522 }
523
524 /// Desugar `<expr>.await` into:
525 /// ```rust
526 /// match <expr> {
527 /// mut pinned => loop {
528 /// match ::std::future::poll_with_tls_context(unsafe {
529 /// <::std::pin::Pin>::new_unchecked(&mut pinned)
530 /// }) {
531 /// ::std::task::Poll::Ready(result) => break result,
532 /// ::std::task::Poll::Pending => {}
533 /// }
534 /// yield ();
535 /// }
536 /// }
537 /// ```
538 fn lower_expr_await(&mut self, await_span: Span, expr: &Expr) -> hir::ExprKind {
539 match self.generator_kind {
540 Some(hir::GeneratorKind::Async(_)) => {},
541 Some(hir::GeneratorKind::Gen) |
542 None => {
543 let mut err = struct_span_err!(
544 self.sess,
545 await_span,
546 E0728,
547 "`await` is only allowed inside `async` functions and blocks"
548 );
549 err.span_label(await_span, "only allowed inside `async` functions and blocks");
550 if let Some(item_sp) = self.current_item {
551 err.span_label(item_sp, "this is not `async`");
552 }
553 err.emit();
554 }
555 }
556 let span = self.mark_span_with_reason(
557 DesugaringKind::Await,
558 await_span,
559 None,
560 );
561 let gen_future_span = self.mark_span_with_reason(
562 DesugaringKind::Await,
563 await_span,
564 self.allow_gen_future.clone(),
565 );
566
567 let pinned_ident = Ident::with_dummy_span(sym::pinned);
568 let (pinned_pat, pinned_pat_hid) = self.pat_ident_binding_mode(
569 span,
570 pinned_ident,
571 hir::BindingAnnotation::Mutable,
572 );
573
574 // ::std::future::poll_with_tls_context(unsafe {
575 // ::std::pin::Pin::new_unchecked(&mut pinned)
576 // })`
577 let poll_expr = {
578 let pinned = P(self.expr_ident(span, pinned_ident, pinned_pat_hid));
579 let ref_mut_pinned = self.expr_mut_addr_of(span, pinned);
580 let pin_ty_id = self.next_id();
581 let new_unchecked_expr_kind = self.expr_call_std_assoc_fn(
582 pin_ty_id,
583 span,
584 &[sym::pin, sym::Pin],
585 "new_unchecked",
586 hir_vec![ref_mut_pinned],
587 );
588 let new_unchecked = P(self.expr(span, new_unchecked_expr_kind, ThinVec::new()));
589 let unsafe_expr = self.expr_unsafe(new_unchecked);
590 P(self.expr_call_std_path(
591 gen_future_span,
592 &[sym::future, sym::poll_with_tls_context],
593 hir_vec![unsafe_expr],
594 ))
595 };
596
597 // `::std::task::Poll::Ready(result) => break result`
598 let loop_node_id = self.resolver.next_node_id();
599 let loop_hir_id = self.lower_node_id(loop_node_id);
600 let ready_arm = {
601 let x_ident = Ident::with_dummy_span(sym::result);
602 let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident);
603 let x_expr = P(self.expr_ident(span, x_ident, x_pat_hid));
604 let ready_pat = self.pat_std_enum(
605 span,
606 &[sym::task, sym::Poll, sym::Ready],
607 hir_vec![x_pat],
608 );
609 let break_x = self.with_loop_scope(loop_node_id, |this| {
610 let expr_break = hir::ExprKind::Break(
611 this.lower_loop_destination(None),
612 Some(x_expr),
613 );
614 P(this.expr(await_span, expr_break, ThinVec::new()))
615 });
616 self.arm(ready_pat, break_x)
617 };
618
619 // `::std::task::Poll::Pending => {}`
620 let pending_arm = {
621 let pending_pat = self.pat_std_enum(
622 span,
623 &[sym::task, sym::Poll, sym::Pending],
624 hir_vec![],
625 );
626 let empty_block = P(self.expr_block_empty(span));
627 self.arm(pending_pat, empty_block)
628 };
629
630 let inner_match_stmt = {
631 let match_expr = self.expr_match(
632 span,
633 poll_expr,
634 hir_vec![ready_arm, pending_arm],
635 hir::MatchSource::AwaitDesugar,
636 );
637 self.stmt_expr(span, match_expr)
638 };
639
640 let yield_stmt = {
641 let unit = self.expr_unit(span);
642 let yield_expr = self.expr(
643 span,
644 hir::ExprKind::Yield(P(unit), hir::YieldSource::Await),
645 ThinVec::new(),
646 );
647 self.stmt_expr(span, yield_expr)
648 };
649
650 let loop_block = P(self.block_all(
651 span,
652 hir_vec![inner_match_stmt, yield_stmt],
653 None,
654 ));
655
656 // loop { .. }
657 let loop_expr = P(hir::Expr {
658 hir_id: loop_hir_id,
659 kind: hir::ExprKind::Loop(
660 loop_block,
661 None,
662 hir::LoopSource::Loop,
663 ),
664 span,
665 attrs: ThinVec::new(),
666 });
667
668 // mut pinned => loop { ... }
669 let pinned_arm = self.arm(pinned_pat, loop_expr);
670
671 // match <expr> {
672 // mut pinned => loop { .. }
673 // }
674 let expr = P(self.lower_expr(expr));
675 hir::ExprKind::Match(expr, hir_vec![pinned_arm], hir::MatchSource::AwaitDesugar)
676 }
677
678 fn lower_expr_closure(
679 &mut self,
680 capture_clause: CaptureBy,
681 movability: Movability,
682 decl: &FnDecl,
683 body: &Expr,
684 fn_decl_span: Span,
685 ) -> hir::ExprKind {
686 // Lower outside new scope to preserve `is_in_loop_condition`.
687 let fn_decl = self.lower_fn_decl(decl, None, false, None);
688
689 self.with_new_scopes(|this| {
690 let prev = this.current_item;
691 this.current_item = Some(fn_decl_span);
692 let mut generator_kind = None;
693 let body_id = this.lower_fn_body(decl, |this| {
694 let e = this.lower_expr(body);
695 generator_kind = this.generator_kind;
696 e
697 });
698 let generator_option = this.generator_movability_for_fn(
699 &decl,
700 fn_decl_span,
701 generator_kind,
702 movability,
703 );
704 this.current_item = prev;
705 hir::ExprKind::Closure(
706 capture_clause,
707 fn_decl,
708 body_id,
709 fn_decl_span,
710 generator_option,
711 )
712 })
713 }
714
715 fn generator_movability_for_fn(
716 &mut self,
717 decl: &FnDecl,
718 fn_decl_span: Span,
719 generator_kind: Option<hir::GeneratorKind>,
720 movability: Movability,
721 ) -> Option<hir::Movability> {
722 match generator_kind {
723 Some(hir::GeneratorKind::Gen) => {
724 if !decl.inputs.is_empty() {
725 span_err!(
726 self.sess,
727 fn_decl_span,
728 E0628,
729 "generators cannot have explicit parameters"
730 );
731 }
732 Some(movability)
733 },
734 Some(hir::GeneratorKind::Async(_)) => {
735 bug!("non-`async` closure body turned `async` during lowering");
736 },
737 None => {
738 if movability == Movability::Static {
739 span_err!(
740 self.sess,
741 fn_decl_span,
742 E0697,
743 "closures cannot be static"
744 );
745 }
746 None
747 },
748 }
749 }
750
751 fn lower_expr_async_closure(
752 &mut self,
753 capture_clause: CaptureBy,
754 closure_id: NodeId,
755 decl: &FnDecl,
756 body: &Expr,
757 fn_decl_span: Span,
758 ) -> hir::ExprKind {
759 let outer_decl = FnDecl {
760 inputs: decl.inputs.clone(),
761 output: FunctionRetTy::Default(fn_decl_span),
762 };
763 // We need to lower the declaration outside the new scope, because we
764 // have to conserve the state of being inside a loop condition for the
765 // closure argument types.
766 let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
767
768 self.with_new_scopes(|this| {
769 // FIXME(cramertj): allow `async` non-`move` closures with arguments.
770 if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
771 struct_span_err!(
772 this.sess,
773 fn_decl_span,
774 E0708,
775 "`async` non-`move` closures with parameters are not currently supported",
776 )
777 .help(
778 "consider using `let` statements to manually capture \
779 variables by reference before entering an `async move` closure"
780 )
781 .emit();
782 }
783
784 // Transform `async |x: u8| -> X { ... }` into
785 // `|x: u8| future_from_generator(|| -> X { ... })`.
786 let body_id = this.lower_fn_body(&outer_decl, |this| {
787 let async_ret_ty = if let FunctionRetTy::Ty(ty) = &decl.output {
788 Some(ty.clone())
789 } else {
790 None
791 };
792 let async_body = this.make_async_expr(
793 capture_clause,
794 closure_id,
795 async_ret_ty,
796 body.span,
797 hir::AsyncGeneratorKind::Closure,
798 |this| this.with_new_scopes(|this| this.lower_expr(body)),
799 );
800 this.expr(fn_decl_span, async_body, ThinVec::new())
801 });
802 hir::ExprKind::Closure(
803 capture_clause,
804 fn_decl,
805 body_id,
806 fn_decl_span,
807 None,
808 )
809 })
810 }
811
812 /// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
813 fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind {
814 let id = self.next_id();
815 let e1 = self.lower_expr(e1);
816 let e2 = self.lower_expr(e2);
817 self.expr_call_std_assoc_fn(
818 id,
819 span,
820 &[sym::ops, sym::RangeInclusive],
821 "new",
822 hir_vec![e1, e2],
823 )
824 }
825
826 fn lower_expr_range(
827 &mut self,
828 span: Span,
829 e1: Option<&Expr>,
830 e2: Option<&Expr>,
831 lims: RangeLimits,
832 ) -> hir::ExprKind {
833 use syntax::ast::RangeLimits::*;
834
835 let path = match (e1, e2, lims) {
836 (None, None, HalfOpen) => sym::RangeFull,
837 (Some(..), None, HalfOpen) => sym::RangeFrom,
838 (None, Some(..), HalfOpen) => sym::RangeTo,
839 (Some(..), Some(..), HalfOpen) => sym::Range,
840 (None, Some(..), Closed) => sym::RangeToInclusive,
841 (Some(..), Some(..), Closed) => unreachable!(),
842 (_, None, Closed) => self.diagnostic()
843 .span_fatal(span, "inclusive range with no end")
844 .raise(),
845 };
846
847 let fields = e1.iter()
848 .map(|e| ("start", e))
849 .chain(e2.iter().map(|e| ("end", e)))
850 .map(|(s, e)| {
851 let expr = P(self.lower_expr(&e));
852 let ident = Ident::new(Symbol::intern(s), e.span);
853 self.field(ident, expr, e.span)
854 })
855 .collect::<P<[hir::Field]>>();
856
857 let is_unit = fields.is_empty();
858 let struct_path = [sym::ops, path];
859 let struct_path = self.std_path(span, &struct_path, None, is_unit);
860 let struct_path = hir::QPath::Resolved(None, P(struct_path));
861
862 if is_unit {
863 hir::ExprKind::Path(struct_path)
864 } else {
865 hir::ExprKind::Struct(P(struct_path), fields, None)
866 }
867 }
868
869 fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
870 label.map(|label| hir::Label {
871 ident: label.ident,
872 })
873 }
874
875 fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
876 let target_id = match destination {
877 Some((id, _)) => {
878 if let Some(loop_id) = self.resolver.get_label_res(id) {
879 Ok(self.lower_node_id(loop_id))
880 } else {
881 Err(hir::LoopIdError::UnresolvedLabel)
882 }
883 }
884 None => {
885 self.loop_scopes
886 .last()
887 .cloned()
888 .map(|id| Ok(self.lower_node_id(id)))
889 .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
890 .into()
891 }
892 };
893 hir::Destination {
894 label: self.lower_label(destination.map(|(_, label)| label)),
895 target_id,
896 }
897 }
898
899 fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination {
900 if self.is_in_loop_condition && opt_label.is_none() {
901 hir::Destination {
902 label: None,
903 target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
904 }
905 } else {
906 self.lower_loop_destination(opt_label.map(|label| (id, label)))
907 }
908 }
909
910 fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
911 where
912 F: FnOnce(&mut LoweringContext<'_>) -> T,
913 {
914 let len = self.catch_scopes.len();
915 self.catch_scopes.push(catch_id);
916
917 let result = f(self);
918 assert_eq!(
919 len + 1,
920 self.catch_scopes.len(),
921 "catch scopes should be added and removed in stack order"
922 );
923
924 self.catch_scopes.pop().unwrap();
925
926 result
927 }
928
929 fn with_loop_scope<T, F>(&mut self, loop_id: NodeId, f: F) -> T
930 where
931 F: FnOnce(&mut LoweringContext<'_>) -> T,
932 {
933 // We're no longer in the base loop's condition; we're in another loop.
934 let was_in_loop_condition = self.is_in_loop_condition;
935 self.is_in_loop_condition = false;
936
937 let len = self.loop_scopes.len();
938 self.loop_scopes.push(loop_id);
939
940 let result = f(self);
941 assert_eq!(
942 len + 1,
943 self.loop_scopes.len(),
944 "loop scopes should be added and removed in stack order"
945 );
946
947 self.loop_scopes.pop().unwrap();
948
949 self.is_in_loop_condition = was_in_loop_condition;
950
951 result
952 }
953
954 fn with_loop_condition_scope<T, F>(&mut self, f: F) -> T
955 where
956 F: FnOnce(&mut LoweringContext<'_>) -> T,
957 {
958 let was_in_loop_condition = self.is_in_loop_condition;
959 self.is_in_loop_condition = true;
960
961 let result = f(self);
962
963 self.is_in_loop_condition = was_in_loop_condition;
964
965 result
966 }
967
968 fn lower_expr_asm(&mut self, asm: &InlineAsm) -> hir::ExprKind {
969 let inner = hir::InlineAsmInner {
970 inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
971 outputs: asm.outputs
972 .iter()
973 .map(|out| hir::InlineAsmOutput {
974 constraint: out.constraint.clone(),
975 is_rw: out.is_rw,
976 is_indirect: out.is_indirect,
977 span: out.expr.span,
978 })
979 .collect(),
980 asm: asm.asm.clone(),
981 asm_str_style: asm.asm_str_style,
982 clobbers: asm.clobbers.clone().into(),
983 volatile: asm.volatile,
984 alignstack: asm.alignstack,
985 dialect: asm.dialect,
986 };
987 let hir_asm = hir::InlineAsm {
988 inner,
989 inputs_exprs: asm.inputs
990 .iter()
991 .map(|&(_, ref input)| self.lower_expr(input))
992 .collect(),
993 outputs_exprs: asm.outputs
994 .iter()
995 .map(|out| self.lower_expr(&out.expr))
996 .collect(),
997 };
998 hir::ExprKind::InlineAsm(P(hir_asm))
999 }
1000
1001 fn lower_field(&mut self, f: &Field) -> hir::Field {
1002 hir::Field {
1003 hir_id: self.next_id(),
1004 ident: f.ident,
1005 expr: P(self.lower_expr(&f.expr)),
1006 span: f.span,
1007 is_shorthand: f.is_shorthand,
1008 }
1009 }
1010
1011 fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind {
1012 match self.generator_kind {
1013 Some(hir::GeneratorKind::Gen) => {},
1014 Some(hir::GeneratorKind::Async(_)) => {
1015 span_err!(
1016 self.sess,
1017 span,
1018 E0727,
1019 "`async` generators are not yet supported",
1020 );
1021 return hir::ExprKind::Err;
1022 },
1023 None => self.generator_kind = Some(hir::GeneratorKind::Gen),
1024 }
1025
1026 let expr = opt_expr
1027 .as_ref()
1028 .map(|x| self.lower_expr(x))
1029 .unwrap_or_else(|| self.expr_unit(span));
1030
1031 hir::ExprKind::Yield(P(expr), hir::YieldSource::Yield)
1032 }
1033
1034 /// Desugar `ExprForLoop` from: `[opt_ident]: for <pat> in <head> <body>` into:
1035 /// ```rust
1036 /// {
1037 /// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
1038 /// mut iter => {
1039 /// [opt_ident]: loop {
1040 /// let mut __next;
1041 /// match ::std::iter::Iterator::next(&mut iter) {
1042 /// ::std::option::Option::Some(val) => __next = val,
1043 /// ::std::option::Option::None => break
1044 /// };
1045 /// let <pat> = __next;
1046 /// StmtKind::Expr(<body>);
1047 /// }
1048 /// }
1049 /// };
1050 /// result
1051 /// }
1052 /// ```
1053 fn lower_expr_for(
1054 &mut self,
1055 e: &Expr,
1056 pat: &Pat,
1057 head: &Expr,
1058 body: &Block,
1059 opt_label: Option<Label>,
1060 ) -> hir::Expr {
1061 // expand <head>
1062 let mut head = self.lower_expr(head);
1063 let desugared_span = self.mark_span_with_reason(
1064 DesugaringKind::ForLoop,
1065 head.span,
1066 None,
1067 );
1068 head.span = desugared_span;
1069
1070 let iter = Ident::with_dummy_span(sym::iter);
1071
1072 let next_ident = Ident::with_dummy_span(sym::__next);
1073 let (next_pat, next_pat_hid) = self.pat_ident_binding_mode(
1074 desugared_span,
1075 next_ident,
1076 hir::BindingAnnotation::Mutable,
1077 );
1078
1079 // `::std::option::Option::Some(val) => __next = val`
1080 let pat_arm = {
1081 let val_ident = Ident::with_dummy_span(sym::val);
1082 let (val_pat, val_pat_hid) = self.pat_ident(pat.span, val_ident);
1083 let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat_hid));
1084 let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat_hid));
1085 let assign = P(self.expr(
1086 pat.span,
1087 hir::ExprKind::Assign(next_expr, val_expr),
1088 ThinVec::new(),
1089 ));
1090 let some_pat = self.pat_some(pat.span, val_pat);
1091 self.arm(some_pat, assign)
1092 };
1093
1094 // `::std::option::Option::None => break`
1095 let break_arm = {
1096 let break_expr =
1097 self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
1098 let pat = self.pat_none(e.span);
1099 self.arm(pat, break_expr)
1100 };
1101
1102 // `mut iter`
1103 let (iter_pat, iter_pat_nid) = self.pat_ident_binding_mode(
1104 desugared_span,
1105 iter,
1106 hir::BindingAnnotation::Mutable
1107 );
1108
1109 // `match ::std::iter::Iterator::next(&mut iter) { ... }`
1110 let match_expr = {
1111 let iter = P(self.expr_ident(desugared_span, iter, iter_pat_nid));
1112 let ref_mut_iter = self.expr_mut_addr_of(desugared_span, iter);
1113 let next_path = &[sym::iter, sym::Iterator, sym::next];
1114 let next_expr = P(self.expr_call_std_path(
1115 desugared_span,
1116 next_path,
1117 hir_vec![ref_mut_iter],
1118 ));
1119 let arms = hir_vec![pat_arm, break_arm];
1120
1121 self.expr_match(desugared_span, next_expr, arms, hir::MatchSource::ForLoopDesugar)
1122 };
1123 let match_stmt = self.stmt_expr(desugared_span, match_expr);
1124
1125 let next_expr = P(self.expr_ident(desugared_span, next_ident, next_pat_hid));
1126
1127 // `let mut __next`
1128 let next_let = self.stmt_let_pat(
1129 ThinVec::new(),
1130 desugared_span,
1131 None,
1132 next_pat,
1133 hir::LocalSource::ForLoopDesugar,
1134 );
1135
1136 // `let <pat> = __next`
1137 let pat = self.lower_pat(pat);
1138 let pat_let = self.stmt_let_pat(
1139 ThinVec::new(),
1140 desugared_span,
1141 Some(next_expr),
1142 pat,
1143 hir::LocalSource::ForLoopDesugar,
1144 );
1145
1146 let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1147 let body_expr = self.expr_block(body_block, ThinVec::new());
1148 let body_stmt = self.stmt_expr(body.span, body_expr);
1149
1150 let loop_block = P(self.block_all(
1151 e.span,
1152 hir_vec![next_let, match_stmt, pat_let, body_stmt],
1153 None,
1154 ));
1155
1156 // `[opt_ident]: loop { ... }`
1157 let kind = hir::ExprKind::Loop(
1158 loop_block,
1159 self.lower_label(opt_label),
1160 hir::LoopSource::ForLoop,
1161 );
1162 let loop_expr = P(hir::Expr {
1163 hir_id: self.lower_node_id(e.id),
1164 kind,
1165 span: e.span,
1166 attrs: ThinVec::new(),
1167 });
1168
1169 // `mut iter => { ... }`
1170 let iter_arm = self.arm(iter_pat, loop_expr);
1171
1172 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1173 let into_iter_expr = {
1174 let into_iter_path =
1175 &[sym::iter, sym::IntoIterator, sym::into_iter];
1176 P(self.expr_call_std_path(
1177 desugared_span,
1178 into_iter_path,
1179 hir_vec![head],
1180 ))
1181 };
1182
1183 let match_expr = P(self.expr_match(
1184 desugared_span,
1185 into_iter_expr,
1186 hir_vec![iter_arm],
1187 hir::MatchSource::ForLoopDesugar,
1188 ));
1189
1190 // This is effectively `{ let _result = ...; _result }`.
1191 // The construct was introduced in #21984 and is necessary to make sure that
1192 // temporaries in the `head` expression are dropped and do not leak to the
1193 // surrounding scope of the `match` since the `match` is not a terminating scope.
1194 //
1195 // Also, add the attributes to the outer returned expr node.
1196 self.expr_drop_temps(desugared_span, match_expr, e.attrs.clone())
1197 }
1198
1199 /// Desugar `ExprKind::Try` from: `<expr>?` into:
1200 /// ```rust
1201 /// match Try::into_result(<expr>) {
1202 /// Ok(val) => #[allow(unreachable_code)] val,
1203 /// Err(err) => #[allow(unreachable_code)]
1204 /// // If there is an enclosing `try {...}`:
1205 /// break 'catch_target Try::from_error(From::from(err)),
1206 /// // Otherwise:
1207 /// return Try::from_error(From::from(err)),
1208 /// }
1209 /// ```
1210 fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind {
1211 let unstable_span = self.mark_span_with_reason(
1212 DesugaringKind::QuestionMark,
1213 span,
1214 self.allow_try_trait.clone(),
1215 );
1216 let try_span = self.sess.source_map().end_point(span);
1217 let try_span = self.mark_span_with_reason(
1218 DesugaringKind::QuestionMark,
1219 try_span,
1220 self.allow_try_trait.clone(),
1221 );
1222
1223 // `Try::into_result(<expr>)`
1224 let scrutinee = {
1225 // expand <expr>
1226 let sub_expr = self.lower_expr(sub_expr);
1227
1228 let path = &[sym::ops, sym::Try, sym::into_result];
1229 P(self.expr_call_std_path(unstable_span, path, hir_vec![sub_expr]))
1230 };
1231
1232 // `#[allow(unreachable_code)]`
1233 let attr = {
1234 // `allow(unreachable_code)`
1235 let allow = {
1236 let allow_ident = Ident::new(sym::allow, span);
1237 let uc_ident = Ident::new(sym::unreachable_code, span);
1238 let uc_nested = attr::mk_nested_word_item(uc_ident);
1239 attr::mk_list_item(allow_ident, vec![uc_nested])
1240 };
1241 attr::mk_attr_outer(allow)
1242 };
1243 let attrs = vec![attr];
1244
1245 // `Ok(val) => #[allow(unreachable_code)] val,`
1246 let ok_arm = {
1247 let val_ident = Ident::with_dummy_span(sym::val);
1248 let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
1249 let val_expr = P(self.expr_ident_with_attrs(
1250 span,
1251 val_ident,
1252 val_pat_nid,
1253 ThinVec::from(attrs.clone()),
1254 ));
1255 let ok_pat = self.pat_ok(span, val_pat);
1256 self.arm(ok_pat, val_expr)
1257 };
1258
1259 // `Err(err) => #[allow(unreachable_code)]
1260 // return Try::from_error(From::from(err)),`
1261 let err_arm = {
1262 let err_ident = Ident::with_dummy_span(sym::err);
1263 let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
1264 let from_expr = {
1265 let from_path = &[sym::convert, sym::From, sym::from];
1266 let err_expr = self.expr_ident(try_span, err_ident, err_local_nid);
1267 self.expr_call_std_path(try_span, from_path, hir_vec![err_expr])
1268 };
1269 let from_err_expr =
1270 self.wrap_in_try_constructor(sym::from_error, unstable_span, from_expr, try_span);
1271 let thin_attrs = ThinVec::from(attrs);
1272 let catch_scope = self.catch_scopes.last().map(|x| *x);
1273 let ret_expr = if let Some(catch_node) = catch_scope {
1274 let target_id = Ok(self.lower_node_id(catch_node));
1275 P(self.expr(
1276 try_span,
1277 hir::ExprKind::Break(
1278 hir::Destination {
1279 label: None,
1280 target_id,
1281 },
1282 Some(from_err_expr),
1283 ),
1284 thin_attrs,
1285 ))
1286 } else {
1287 P(self.expr(try_span, hir::ExprKind::Ret(Some(from_err_expr)), thin_attrs))
1288 };
1289
1290 let err_pat = self.pat_err(try_span, err_local);
1291 self.arm(err_pat, ret_expr)
1292 };
1293
1294 hir::ExprKind::Match(
1295 scrutinee,
1296 hir_vec![err_arm, ok_arm],
1297 hir::MatchSource::TryDesugar,
1298 )
1299 }
1300
1301 // =========================================================================
1302 // Helper methods for building HIR.
1303 // =========================================================================
1304
1305 /// Constructs a `true` or `false` literal expression.
1306 pub(super) fn expr_bool(&mut self, span: Span, val: bool) -> hir::Expr {
1307 let lit = Spanned { span, node: LitKind::Bool(val) };
1308 self.expr(span, hir::ExprKind::Lit(lit), ThinVec::new())
1309 }
1310
1311 /// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
1312 ///
1313 /// In terms of drop order, it has the same effect as wrapping `expr` in
1314 /// `{ let _t = $expr; _t }` but should provide better compile-time performance.
1315 ///
1316 /// The drop order can be important in e.g. `if expr { .. }`.
1317 pub(super) fn expr_drop_temps(
1318 &mut self,
1319 span: Span,
1320 expr: P<hir::Expr>,
1321 attrs: ThinVec<Attribute>
1322 ) -> hir::Expr {
1323 self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
1324 }
1325
1326 fn expr_match(
1327 &mut self,
1328 span: Span,
1329 arg: P<hir::Expr>,
1330 arms: hir::HirVec<hir::Arm>,
1331 source: hir::MatchSource,
1332 ) -> hir::Expr {
1333 self.expr(span, hir::ExprKind::Match(arg, arms, source), ThinVec::new())
1334 }
1335
1336 fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
1337 let expr_break = hir::ExprKind::Break(self.lower_loop_destination(None), None);
1338 P(self.expr(span, expr_break, attrs))
1339 }
1340
1341 fn expr_mut_addr_of(&mut self, span: Span, e: P<hir::Expr>) -> hir::Expr {
1342 self.expr(
1343 span,
1344 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mutable, e),
1345 ThinVec::new(),
1346 )
1347 }
1348
1349 fn expr_unit(&mut self, sp: Span) -> hir::Expr {
1350 self.expr_tuple(sp, hir_vec![])
1351 }
1352
1353 fn expr_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> hir::Expr {
1354 self.expr(sp, hir::ExprKind::Tup(exprs), ThinVec::new())
1355 }
1356
1357 fn expr_call(
1358 &mut self,
1359 span: Span,
1360 e: P<hir::Expr>,
1361 args: hir::HirVec<hir::Expr>,
1362 ) -> hir::Expr {
1363 self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
1364 }
1365
1366 // Note: associated functions must use `expr_call_std_path`.
1367 fn expr_call_std_path(
1368 &mut self,
1369 span: Span,
1370 path_components: &[Symbol],
1371 args: hir::HirVec<hir::Expr>,
1372 ) -> hir::Expr {
1373 let path = P(self.expr_std_path(span, path_components, None, ThinVec::new()));
1374 self.expr_call(span, path, args)
1375 }
1376
1377 // Create an expression calling an associated function of an std type.
1378 //
1379 // Associated functions cannot be resolved through the normal `std_path` function,
1380 // as they are resolved differently and so cannot use `expr_call_std_path`.
1381 //
1382 // This function accepts the path component (`ty_path_components`) separately from
1383 // the name of the associated function (`assoc_fn_name`) in order to facilitate
1384 // separate resolution of the type and creation of a path referring to its associated
1385 // function.
1386 fn expr_call_std_assoc_fn(
1387 &mut self,
1388 ty_path_id: hir::HirId,
1389 span: Span,
1390 ty_path_components: &[Symbol],
1391 assoc_fn_name: &str,
1392 args: hir::HirVec<hir::Expr>,
1393 ) -> hir::ExprKind {
1394 let ty_path = P(self.std_path(span, ty_path_components, None, false));
1395 let ty = P(self.ty_path(ty_path_id, span, hir::QPath::Resolved(None, ty_path)));
1396 let fn_seg = P(hir::PathSegment::from_ident(Ident::from_str(assoc_fn_name)));
1397 let fn_path = hir::QPath::TypeRelative(ty, fn_seg);
1398 let fn_expr = P(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new()));
1399 hir::ExprKind::Call(fn_expr, args)
1400 }
1401
1402 fn expr_std_path(
1403 &mut self,
1404 span: Span,
1405 components: &[Symbol],
1406 params: Option<P<hir::GenericArgs>>,
1407 attrs: ThinVec<Attribute>,
1408 ) -> hir::Expr {
1409 let path = self.std_path(span, components, params, true);
1410 self.expr(
1411 span,
1412 hir::ExprKind::Path(hir::QPath::Resolved(None, P(path))),
1413 attrs,
1414 )
1415 }
1416
1417 pub(super) fn expr_ident(&mut self, sp: Span, ident: Ident, binding: hir::HirId) -> hir::Expr {
1418 self.expr_ident_with_attrs(sp, ident, binding, ThinVec::new())
1419 }
1420
1421 fn expr_ident_with_attrs(
1422 &mut self,
1423 span: Span,
1424 ident: Ident,
1425 binding: hir::HirId,
1426 attrs: ThinVec<Attribute>,
1427 ) -> hir::Expr {
1428 let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
1429 None,
1430 P(hir::Path {
1431 span,
1432 res: Res::Local(binding),
1433 segments: hir_vec![hir::PathSegment::from_ident(ident)],
1434 }),
1435 ));
1436
1437 self.expr(span, expr_path, attrs)
1438 }
1439
1440 fn expr_unsafe(&mut self, expr: P<hir::Expr>) -> hir::Expr {
1441 let hir_id = self.next_id();
1442 let span = expr.span;
1443 self.expr(
1444 span,
1445 hir::ExprKind::Block(P(hir::Block {
1446 stmts: hir_vec![],
1447 expr: Some(expr),
1448 hir_id,
1449 rules: hir::UnsafeBlock(hir::CompilerGenerated),
1450 span,
1451 targeted_by_break: false,
1452 }), None),
1453 ThinVec::new(),
1454 )
1455 }
1456
1457 fn expr_block_empty(&mut self, span: Span) -> hir::Expr {
1458 let blk = self.block_all(span, hir_vec![], None);
1459 self.expr_block(P(blk), ThinVec::new())
1460 }
1461
1462 pub(super) fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
1463 self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1464 }
1465
1466 pub(super) fn expr(
1467 &mut self,
1468 span: Span,
1469 kind: hir::ExprKind,
1470 attrs: ThinVec<Attribute>
1471 ) -> hir::Expr {
1472 hir::Expr { hir_id: self.next_id(), kind, span, attrs }
1473 }
1474
1475 fn field(&mut self, ident: Ident, expr: P<hir::Expr>, span: Span) -> hir::Field {
1476 hir::Field {
1477 hir_id: self.next_id(),
1478 ident,
1479 span,
1480 expr,
1481 is_shorthand: false,
1482 }
1483 }
1484
1485 fn arm(&mut self, pat: P<hir::Pat>, expr: P<hir::Expr>) -> hir::Arm {
1486 hir::Arm {
1487 hir_id: self.next_id(),
1488 attrs: hir_vec![],
1489 pat,
1490 guard: None,
1491 span: expr.span,
1492 body: expr,
1493 }
1494 }
1495 }