rustc_mir_build/thir/cx/
block.rs

1use rustc_hir as hir;
2use rustc_index::Idx;
3use rustc_middle::middle::region;
4use rustc_middle::thir::*;
5use tracing::debug;
6
7use crate::thir::cx::ThirBuildCx;
8
9impl<'tcx> ThirBuildCx<'tcx> {
10    pub(crate) fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> BlockId {
11        // We have to eagerly lower the "spine" of the statements
12        // in order to get the lexical scoping correctly.
13        let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
14        let block = Block {
15            targeted_by_break: block.targeted_by_break,
16            region_scope: region::Scope {
17                local_id: block.hir_id.local_id,
18                data: region::ScopeData::Node,
19            },
20            span: block.span,
21            stmts,
22            expr: block.expr.map(|expr| self.mirror_expr(expr)),
23            safety_mode: match block.rules {
24                hir::BlockCheckMode::DefaultBlock => BlockSafety::Safe,
25                hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated) => {
26                    BlockSafety::BuiltinUnsafe
27                }
28                hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) => {
29                    BlockSafety::ExplicitUnsafe(block.hir_id)
30                }
31            },
32        };
33
34        self.thir.blocks.push(block)
35    }
36
37    fn mirror_stmts(
38        &mut self,
39        block_id: hir::ItemLocalId,
40        stmts: &'tcx [hir::Stmt<'tcx>],
41    ) -> Box<[StmtId]> {
42        stmts
43            .iter()
44            .enumerate()
45            .filter_map(|(index, stmt)| {
46                let hir_id = stmt.hir_id;
47                match stmt.kind {
48                    hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => {
49                        let stmt = Stmt {
50                            kind: StmtKind::Expr {
51                                scope: region::Scope {
52                                    local_id: hir_id.local_id,
53                                    data: region::ScopeData::Node,
54                                },
55                                expr: self.mirror_expr(expr),
56                            },
57                        };
58                        Some(self.thir.stmts.push(stmt))
59                    }
60                    hir::StmtKind::Item(..) => {
61                        // ignore for purposes of the MIR
62                        None
63                    }
64                    hir::StmtKind::Let(local) => {
65                        let remainder_scope = region::Scope {
66                            local_id: block_id,
67                            data: region::ScopeData::Remainder(region::FirstStatementIndex::new(
68                                index,
69                            )),
70                        };
71
72                        let else_block = local.els.map(|els| self.mirror_block(els));
73
74                        let pattern = self.pattern_from_hir_with_annotation(local.pat, local.ty);
75                        debug!(?pattern);
76
77                        let span = match local.init {
78                            Some(init) => local.span.with_hi(init.span.hi()),
79                            None => local.span,
80                        };
81                        let stmt = Stmt {
82                            kind: StmtKind::Let {
83                                remainder_scope,
84                                init_scope: region::Scope {
85                                    local_id: hir_id.local_id,
86                                    data: region::ScopeData::Node,
87                                },
88                                pattern,
89                                initializer: local.init.map(|init| self.mirror_expr(init)),
90                                else_block,
91                                lint_level: LintLevel::Explicit(local.hir_id),
92                                span,
93                            },
94                        };
95                        Some(self.thir.stmts.push(stmt))
96                    }
97                }
98            })
99            .collect()
100    }
101}