]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/thir/cx/block.rs
Auto merge of #86433 - paolobarbolini:string-overlapping, r=m-ou-se
[rust.git] / compiler / rustc_mir_build / src / thir / cx / block.rs
1 use crate::thir::cx::Cx;
2
3 use rustc_hir as hir;
4 use rustc_middle::middle::region;
5 use rustc_middle::thir::*;
6 use rustc_middle::ty;
7
8 use rustc_index::vec::Idx;
9
10 impl<'tcx> Cx<'tcx> {
11     crate fn mirror_block(&mut self, block: &'tcx hir::Block<'tcx>) -> Block {
12         // We have to eagerly lower the "spine" of the statements
13         // in order to get the lexical scoping correctly.
14         let stmts = self.mirror_stmts(block.hir_id.local_id, block.stmts);
15         let opt_destruction_scope =
16             self.region_scope_tree.opt_destruction_scope(block.hir_id.local_id);
17         Block {
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             },
23             opt_destruction_scope,
24             span: block.span,
25             stmts,
26             expr: block.expr.map(|expr| self.mirror_expr(expr)),
27             safety_mode: match block.rules {
28                 hir::BlockCheckMode::DefaultBlock => BlockSafety::Safe,
29                 hir::BlockCheckMode::UnsafeBlock(..) => BlockSafety::ExplicitUnsafe(block.hir_id),
30             },
31         }
32     }
33
34     fn mirror_stmts(
35         &mut self,
36         block_id: hir::ItemLocalId,
37         stmts: &'tcx [hir::Stmt<'tcx>],
38     ) -> Box<[StmtId]> {
39         stmts
40             .iter()
41             .enumerate()
42             .filter_map(|(index, stmt)| {
43                 let hir_id = stmt.hir_id;
44                 let opt_dxn_ext = self.region_scope_tree.opt_destruction_scope(hir_id.local_id);
45                 match stmt.kind {
46                     hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => {
47                         let stmt = Stmt {
48                             kind: StmtKind::Expr {
49                                 scope: region::Scope {
50                                     id: hir_id.local_id,
51                                     data: region::ScopeData::Node,
52                                 },
53                                 expr: self.mirror_expr(expr),
54                             },
55                             opt_destruction_scope: opt_dxn_ext,
56                         };
57                         Some(self.thir.stmts.push(stmt))
58                     }
59                     hir::StmtKind::Item(..) => {
60                         // ignore for purposes of the MIR
61                         None
62                     }
63                     hir::StmtKind::Local(ref local) => {
64                         let remainder_scope = region::Scope {
65                             id: block_id,
66                             data: region::ScopeData::Remainder(region::FirstStatementIndex::new(
67                                 index,
68                             )),
69                         };
70
71                         let mut pattern = self.pattern_from_hir(local.pat);
72
73                         if let Some(ty) = &local.ty {
74                             if let Some(&user_ty) =
75                                 self.typeck_results.user_provided_types().get(ty.hir_id)
76                             {
77                                 debug!("mirror_stmts: user_ty={:?}", user_ty);
78                                 pattern = Pat {
79                                     ty: pattern.ty,
80                                     span: pattern.span,
81                                     kind: Box::new(PatKind::AscribeUserType {
82                                         ascription: Ascription {
83                                             user_ty: PatTyProj::from_user_type(user_ty),
84                                             user_ty_span: ty.span,
85                                             variance: ty::Variance::Covariant,
86                                         },
87                                         subpattern: pattern,
88                                     }),
89                                 };
90                             }
91                         }
92
93                         let stmt = Stmt {
94                             kind: StmtKind::Let {
95                                 remainder_scope,
96                                 init_scope: region::Scope {
97                                     id: hir_id.local_id,
98                                     data: region::ScopeData::Node,
99                                 },
100                                 pattern,
101                                 initializer: local.init.map(|init| self.mirror_expr(init)),
102                                 lint_level: LintLevel::Explicit(local.hir_id),
103                             },
104                             opt_destruction_scope: opt_dxn_ext,
105                         };
106                         Some(self.thir.stmts.push(stmt))
107                     }
108                 }
109             })
110             .collect()
111     }
112 }