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