]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | use crate::build::matches::ArmHasGuard; |
dfeec247 XL |
2 | use crate::build::ForGuard::OutsideGuard; |
3 | use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder}; | |
ba9703b0 | 4 | use rustc_middle::mir::*; |
17df50a5 | 5 | use rustc_middle::thir::*; |
f9f354fc XL |
6 | use rustc_session::lint::builtin::UNSAFE_OP_IN_UNSAFE_FN; |
7 | use rustc_session::lint::Level; | |
dfeec247 | 8 | use rustc_span::Span; |
e9174d1e | 9 | |
dc9dc135 | 10 | impl<'a, 'tcx> Builder<'a, 'tcx> { |
dfeec247 XL |
11 | crate fn ast_block( |
12 | &mut self, | |
ba9703b0 | 13 | destination: Place<'tcx>, |
dfeec247 | 14 | block: BasicBlock, |
17df50a5 | 15 | ast_block: &Block, |
dfeec247 XL |
16 | source_info: SourceInfo, |
17 | ) -> BlockAnd<()> { | |
ea8adc8c XL |
18 | let Block { |
19 | region_scope, | |
20 | opt_destruction_scope, | |
21 | span, | |
17df50a5 | 22 | ref stmts, |
ea8adc8c XL |
23 | expr, |
24 | targeted_by_break, | |
dfeec247 | 25 | safety_mode, |
6a06907d | 26 | } = *ast_block; |
17df50a5 | 27 | let expr = expr.map(|expr| &self.thir[expr]); |
dfeec247 | 28 | self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| { |
dc9dc135 | 29 | this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| { |
041b39d2 | 30 | if targeted_by_break { |
29967ef6 XL |
31 | this.in_breakable_scope(None, destination, span, |this| { |
32 | Some(this.ast_block_stmts( | |
33 | destination, | |
34 | block, | |
35 | span, | |
17df50a5 | 36 | &stmts, |
29967ef6 XL |
37 | expr, |
38 | safety_mode, | |
39 | )) | |
40 | }) | |
041b39d2 | 41 | } else { |
17df50a5 | 42 | this.ast_block_stmts(destination, block, span, &stmts, expr, safety_mode) |
041b39d2 XL |
43 | } |
44 | }) | |
cc61c64b XL |
45 | }) |
46 | } | |
3157f602 | 47 | |
dfeec247 XL |
48 | fn ast_block_stmts( |
49 | &mut self, | |
ba9703b0 | 50 | destination: Place<'tcx>, |
dfeec247 XL |
51 | mut block: BasicBlock, |
52 | span: Span, | |
17df50a5 XL |
53 | stmts: &[StmtId], |
54 | expr: Option<&Expr<'tcx>>, | |
dfeec247 XL |
55 | safety_mode: BlockSafety, |
56 | ) -> BlockAnd<()> { | |
cc61c64b XL |
57 | let this = self; |
58 | ||
59 | // This convoluted structure is to avoid using recursion as we walk down a list | |
60 | // of statements. Basically, the structure we get back is something like: | |
61 | // | |
62 | // let x = <init> in { | |
63 | // expr1; | |
64 | // let y = <init> in { | |
65 | // expr2; | |
66 | // expr3; | |
67 | // ... | |
68 | // } | |
69 | // } | |
70 | // | |
71 | // The let bindings are valid till the end of block so all we have to do is to pop all | |
72 | // the let-scopes at the end. | |
73 | // | |
74 | // First we build all the statements in the block. | |
ea8adc8c | 75 | let mut let_scope_stack = Vec::with_capacity(8); |
94b46f34 | 76 | let outer_source_scope = this.source_scope; |
136023e0 | 77 | let outer_in_scope_unsafe = this.in_scope_unsafe; |
94b46f34 | 78 | this.update_source_scope_for_safety_mode(span, safety_mode); |
ea8adc8c | 79 | |
041b39d2 | 80 | let source_info = this.source_info(span); |
17df50a5 XL |
81 | for stmt in stmts { |
82 | let Stmt { ref kind, opt_destruction_scope } = this.thir[*stmt]; | |
cc61c64b | 83 | match kind { |
17df50a5 | 84 | StmtKind::Expr { scope, expr } => { |
0bf4aa26 | 85 | this.block_context.push(BlockFrame::Statement { ignores_expr_result: true }); |
dfeec247 XL |
86 | unpack!( |
87 | block = this.in_opt_scope( | |
88 | opt_destruction_scope.map(|de| (de, source_info)), | |
89 | |this| { | |
17df50a5 | 90 | let si = (*scope, source_info); |
dfeec247 | 91 | this.in_scope(si, LintLevel::Inherited, |this| { |
17df50a5 | 92 | this.stmt_expr(block, &this.thir[*expr], Some(*scope)) |
dfeec247 XL |
93 | }) |
94 | } | |
95 | ) | |
96 | ); | |
cc61c64b | 97 | } |
17df50a5 XL |
98 | StmtKind::Let { |
99 | remainder_scope, | |
100 | init_scope, | |
101 | ref pattern, | |
102 | initializer, | |
103 | lint_level, | |
104 | } => { | |
1b1a35ee | 105 | let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild); |
0bf4aa26 XL |
106 | this.block_context.push(BlockFrame::Statement { ignores_expr_result }); |
107 | ||
0731742a | 108 | // Enter the remainder scope, i.e., the bindings' destruction scope. |
6a06907d | 109 | this.push_scope((*remainder_scope, source_info)); |
ea8adc8c | 110 | let_scope_stack.push(remainder_scope); |
3157f602 | 111 | |
94b46f34 | 112 | // Declare the bindings, which may create a source scope. |
6a06907d | 113 | let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree); |
8faf50e0 | 114 | |
dc9dc135 XL |
115 | let visibility_scope = |
116 | Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None)); | |
3157f602 | 117 | |
cc61c64b XL |
118 | // Evaluate the initializer, if present. |
119 | if let Some(init) = initializer { | |
17df50a5 | 120 | let init = &this.thir[*init]; |
6a06907d | 121 | let initializer_span = init.span; |
8faf50e0 | 122 | |
dfeec247 XL |
123 | unpack!( |
124 | block = this.in_opt_scope( | |
125 | opt_destruction_scope.map(|de| (de, source_info)), | |
126 | |this| { | |
6a06907d XL |
127 | let scope = (*init_scope, source_info); |
128 | this.in_scope(scope, *lint_level, |this| { | |
dfeec247 XL |
129 | this.declare_bindings( |
130 | visibility_scope, | |
131 | remainder_span, | |
6a06907d | 132 | pattern, |
dfeec247 XL |
133 | ArmHasGuard(false), |
134 | Some((None, initializer_span)), | |
135 | ); | |
6a06907d | 136 | this.expr_into_pattern(block, pattern.clone(), init) |
dfeec247 XL |
137 | }) |
138 | } | |
139 | ) | |
140 | ); | |
cc61c64b | 141 | } else { |
6a06907d XL |
142 | let scope = (*init_scope, source_info); |
143 | unpack!(this.in_scope(scope, *lint_level, |this| { | |
dc9dc135 XL |
144 | this.declare_bindings( |
145 | visibility_scope, | |
146 | remainder_span, | |
6a06907d | 147 | pattern, |
dc9dc135 XL |
148 | ArmHasGuard(false), |
149 | None, | |
150 | ); | |
151 | block.unit() | |
152 | })); | |
8faf50e0 | 153 | |
0731742a | 154 | debug!("ast_block_stmts: pattern={:?}", pattern); |
f9f354fc | 155 | this.visit_primary_bindings( |
17df50a5 | 156 | pattern, |
0731742a | 157 | UserTypeProjections::none(), |
0bf4aa26 | 158 | &mut |this, _, _, _, node, span, _, _| { |
74b04a01 | 159 | this.storage_live_binding(block, node, span, OutsideGuard, true); |
0bf4aa26 | 160 | this.schedule_drop_for_binding(node, span, OutsideGuard); |
dfeec247 XL |
161 | }, |
162 | ) | |
cc61c64b XL |
163 | } |
164 | ||
dc9dc135 XL |
165 | // Enter the visibility scope, after evaluating the initializer. |
166 | if let Some(source_scope) = visibility_scope { | |
94b46f34 | 167 | this.source_scope = source_scope; |
54a0048b | 168 | } |
9cc50fc6 SL |
169 | } |
170 | } | |
0bf4aa26 XL |
171 | |
172 | let popped = this.block_context.pop(); | |
dfeec247 | 173 | assert!(popped.map_or(false, |bf| bf.is_statement())); |
cc61c64b | 174 | } |
0bf4aa26 | 175 | |
cc61c64b | 176 | // Then, the block may have an optional trailing expression which is a “return” value |
0bf4aa26 | 177 | // of the block, which is stored into `destination`. |
6a06907d | 178 | let tcx = this.tcx; |
532ac7d7 | 179 | let destination_ty = destination.ty(&this.local_decls, tcx).ty; |
cc61c64b | 180 | if let Some(expr) = expr { |
dfeec247 XL |
181 | let tail_result_is_ignored = |
182 | destination_ty.is_unit() || this.block_context.currently_ignores_tail_results(); | |
6a06907d XL |
183 | this.block_context |
184 | .push(BlockFrame::TailExpr { tail_result_is_ignored, span: expr.span }); | |
0bf4aa26 | 185 | |
6a06907d | 186 | unpack!(block = this.expr_into_dest(destination, block, expr)); |
0bf4aa26 XL |
187 | let popped = this.block_context.pop(); |
188 | ||
dfeec247 | 189 | assert!(popped.map_or(false, |bf| bf.is_tail_expr())); |
cc61c64b | 190 | } else { |
2c00a5a8 XL |
191 | // If a block has no trailing expression, then it is given an implicit return type. |
192 | // This return type is usually `()`, unless the block is diverging, in which case the | |
193 | // return type is `!`. For the unit type, we need to actually return the unit, but in | |
194 | // the case of `!`, no return value is required, as the block will never return. | |
0bf4aa26 | 195 | if destination_ty.is_unit() { |
2c00a5a8 XL |
196 | // We only want to assign an implicit `()` as the return value of the block if the |
197 | // block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.) | |
6a06907d | 198 | this.cfg.push_assign_unit(block, source_info, destination, this.tcx); |
2c00a5a8 | 199 | } |
cc61c64b XL |
200 | } |
201 | // Finally, we pop all the let scopes before exiting out from the scope of block | |
202 | // itself. | |
ea8adc8c | 203 | for scope in let_scope_stack.into_iter().rev() { |
6a06907d | 204 | unpack!(block = this.pop_scope((*scope, source_info), block)); |
cc61c64b | 205 | } |
94b46f34 XL |
206 | // Restore the original source scope. |
207 | this.source_scope = outer_source_scope; | |
136023e0 | 208 | this.in_scope_unsafe = outer_in_scope_unsafe; |
cc61c64b | 209 | block.unit() |
e9174d1e | 210 | } |
ea8adc8c | 211 | |
94b46f34 | 212 | /// If we are changing the safety mode, create a new source scope |
dfeec247 | 213 | fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) { |
94b46f34 | 214 | debug!("update_source_scope_for({:?}, {:?})", span, safety_mode); |
ea8adc8c XL |
215 | let new_unsafety = match safety_mode { |
216 | BlockSafety::Safe => None, | |
136023e0 | 217 | BlockSafety::BuiltinUnsafe => Some(Safety::BuiltinUnsafe), |
532ac7d7 | 218 | BlockSafety::ExplicitUnsafe(hir_id) => { |
136023e0 | 219 | match self.in_scope_unsafe { |
ea8adc8c | 220 | Safety::Safe => {} |
f9f354fc XL |
221 | // no longer treat `unsafe fn`s as `unsafe` contexts (see RFC #2585) |
222 | Safety::FnUnsafe | |
6a06907d | 223 | if self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0 |
f9f354fc | 224 | != Level::Allow => {} |
dfeec247 | 225 | _ => return, |
ea8adc8c | 226 | } |
136023e0 | 227 | self.in_scope_unsafe = Safety::ExplicitUnsafe(hir_id); |
532ac7d7 | 228 | Some(Safety::ExplicitUnsafe(hir_id)) |
ea8adc8c | 229 | } |
ea8adc8c XL |
230 | }; |
231 | ||
232 | if let Some(unsafety) = new_unsafety { | |
dfeec247 | 233 | self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(unsafety)); |
ea8adc8c XL |
234 | } |
235 | } | |
e9174d1e | 236 | } |