]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/thir/cx/block.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / compiler / rustc_mir_build / src / thir / cx / block.rs
CommitLineData
3dfed10e
XL
1use crate::thir::cx::Cx;
2use crate::thir::{self, *};
60c5eb7d 3
dfeec247 4use rustc_hir as hir;
ba9703b0
XL
5use rustc_middle::middle::region;
6use rustc_middle::ty;
ea8adc8c 7
e74abb32 8use rustc_index::vec::Idx;
e9174d1e 9
6a06907d
XL
10impl<'thir, 'tcx> Cx<'thir, 'tcx> {
11 crate fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> Block<'thir, 'tcx> {
94b46f34 12 // We have to eagerly lower the "spine" of the statements
e9174d1e 13 // in order to get the lexical scoping correctly.
6a06907d 14 let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
ea8adc8c 15 let opt_destruction_scope =
6a06907d 16 self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
e9174d1e 17 Block {
6a06907d
XL
18 targeted_by_break: block.targeted_by_break,
19 region_scope: region::Scope {
20 id: block.hir_id.local_id,
21 data: region::ScopeData::Node,
22 },
ea8adc8c 23 opt_destruction_scope,
6a06907d 24 span: block.span,
3b2f2976 25 stmts,
6a06907d
XL
26 expr: block.expr.map(|expr| self.mirror_expr(expr)),
27 safety_mode: match block.rules {
dfeec247 28 hir::BlockCheckMode::DefaultBlock => BlockSafety::Safe,
6a06907d 29 hir::BlockCheckMode::UnsafeBlock(..) => BlockSafety::ExplicitUnsafe(block.hir_id),
dfeec247
XL
30 hir::BlockCheckMode::PushUnsafeBlock(..) => BlockSafety::PushUnsafe,
31 hir::BlockCheckMode::PopUnsafeBlock(..) => BlockSafety::PopUnsafe,
ea8adc8c 32 },
e9174d1e
SL
33 }
34 }
e9174d1e 35
6a06907d
XL
36 fn mirror_stmts(
37 &mut self,
38 block_id: hir::ItemLocalId,
39 stmts: &'tcx [hir::Stmt<'tcx>],
40 ) -> &'thir [Stmt<'thir, 'tcx>] {
41 self.arena.alloc_from_iter(stmts.iter().enumerate().filter_map(|(index, stmt)| {
42 let hir_id = stmt.hir_id;
43 let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id);
44 match stmt.kind {
45 hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => Some(Stmt {
54a0048b 46 kind: StmtKind::Expr {
dfeec247 47 scope: region::Scope { id: hir_id.local_id, data: region::ScopeData::Node },
6a06907d 48 expr: self.mirror_expr(expr),
476ff2be 49 },
ea8adc8c 50 opt_destruction_scope: opt_dxn_ext,
6a06907d
XL
51 }),
52 hir::StmtKind::Item(..) => {
53 // ignore for purposes of the MIR
54 None
55 }
56 hir::StmtKind::Local(ref local) => {
57 let remainder_scope = region::Scope {
58 id: block_id,
59 data: region::ScopeData::Remainder(region::FirstStatementIndex::new(index)),
60 };
b7449926 61
6a06907d 62 let mut pattern = self.pattern_from_hir(local.pat);
54a0048b 63
6a06907d
XL
64 if let Some(ty) = &local.ty {
65 if let Some(&user_ty) =
66 self.typeck_results.user_provided_types().get(ty.hir_id)
67 {
68 debug!("mirror_stmts: user_ty={:?}", user_ty);
69 pattern = Pat {
70 ty: pattern.ty,
71 span: pattern.span,
72 kind: Box::new(PatKind::AscribeUserType {
73 ascription: thir::pattern::Ascription {
74 user_ty: PatTyProj::from_user_type(user_ty),
75 user_ty_span: ty.span,
76 variance: ty::Variance::Covariant,
77 },
78 subpattern: pattern,
79 }),
80 };
81 }
476ff2be 82 }
9fa01778 83
6a06907d
XL
84 Some(Stmt {
85 kind: StmtKind::Let {
86 remainder_scope,
87 init_scope: region::Scope {
88 id: hir_id.local_id,
89 data: region::ScopeData::Node,
90 },
91 pattern,
92 initializer: local.init.map(|init| self.mirror_expr(init)),
93 lint_level: LintLevel::Explicit(local.hir_id),
9fa01778 94 },
6a06907d
XL
95 opt_destruction_scope: opt_dxn_ext,
96 })
97 }
e9174d1e 98 }
6a06907d 99 }))
e9174d1e 100 }
e9174d1e 101}