1 use crate::thir::cx::Cx;
4 use rustc_middle::middle::region;
5 use rustc_middle::thir::*;
8 use rustc_index::vec::Idx;
9 use rustc_middle::ty::CanonicalUserTypeAnnotation;
12 pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId {
13 // We have to eagerly lower the "spine" of the statements
14 // in order to get the lexical scoping correctly.
15 let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
16 let opt_destruction_scope =
17 self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
19 targeted_by_break: block.targeted_by_break,
20 region_scope: region::Scope {
21 id: block.hir_id.local_id,
22 data: region::ScopeData::Node,
24 opt_destruction_scope,
27 expr: block.expr.map(|expr| self.mirror_expr(expr)),
28 safety_mode: match block.rules {
29 hir::BlockCheckMode::DefaultBlock => BlockSafety::Safe,
30 hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated) => {
31 BlockSafety::BuiltinUnsafe
33 hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) => {
34 BlockSafety::ExplicitUnsafe(block.hir_id)
39 self.thir.blocks.push(block)
44 block_id: hir::ItemLocalId,
45 stmts: &'tcx [hir::Stmt<'tcx>],
50 .filter_map(|(index, stmt)| {
51 let hir_id = stmt.hir_id;
52 let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id);
54 hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => {
56 kind: StmtKind::Expr {
57 scope: region::Scope {
59 data: region::ScopeData::Node,
61 expr: self.mirror_expr(expr),
63 opt_destruction_scope: opt_dxn_ext,
65 Some(self.thir.stmts.push(stmt))
67 hir::StmtKind::Item(..) => {
68 // ignore for purposes of the MIR
71 hir::StmtKind::Local(ref local) => {
72 let remainder_scope = region::Scope {
74 data: region::ScopeData::Remainder(region::FirstStatementIndex::new(
79 let else_block = local.els.map(|els| self.mirror_block(els));
81 let mut pattern = self.pattern_from_hir(local.pat);
84 if let Some(ty) = &local.ty {
85 if let Some(&user_ty) =
86 self.typeck_results.user_provided_types().get(ty.hir_id)
88 debug!("mirror_stmts: user_ty={:?}", user_ty);
89 let annotation = CanonicalUserTypeAnnotation {
90 user_ty: Box::new(user_ty),
92 inferred_ty: self.typeck_results.node_type(ty.hir_id),
94 pattern = Box::new(Pat {
97 kind: PatKind::AscribeUserType {
98 ascription: Ascription {
100 variance: ty::Variance::Covariant,
109 kind: StmtKind::Let {
111 init_scope: region::Scope {
113 data: region::ScopeData::Node,
116 initializer: local.init.map(|init| self.mirror_expr(init)),
118 lint_level: LintLevel::Explicit(local.hir_id),
120 opt_destruction_scope: opt_dxn_ext,
122 Some(self.thir.stmts.push(stmt))