]> git.proxmox.com Git - rustc.git/blame_incremental - compiler/rustc_mir_build/src/build/block.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_mir_build / src / build / block.rs
... / ...
CommitLineData
1use crate::build::matches::ArmHasGuard;
2use crate::build::ForGuard::OutsideGuard;
3use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
4use rustc_middle::mir::*;
5use rustc_middle::thir::*;
6use rustc_session::lint::builtin::UNSAFE_OP_IN_UNSAFE_FN;
7use rustc_session::lint::Level;
8use rustc_span::Span;
9
10impl<'a, 'tcx> Builder<'a, 'tcx> {
11 crate fn ast_block(
12 &mut self,
13 destination: Place<'tcx>,
14 block: BasicBlock,
15 ast_block: &Block,
16 source_info: SourceInfo,
17 ) -> BlockAnd<()> {
18 let Block {
19 region_scope,
20 opt_destruction_scope,
21 span,
22 ref stmts,
23 expr,
24 targeted_by_break,
25 safety_mode,
26 } = *ast_block;
27 let expr = expr.map(|expr| &self.thir[expr]);
28 self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
29 this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
30 if targeted_by_break {
31 this.in_breakable_scope(None, destination, span, |this| {
32 Some(this.ast_block_stmts(
33 destination,
34 block,
35 span,
36 &stmts,
37 expr,
38 safety_mode,
39 ))
40 })
41 } else {
42 this.ast_block_stmts(destination, block, span, &stmts, expr, safety_mode)
43 }
44 })
45 })
46 }
47
48 fn ast_block_stmts(
49 &mut self,
50 destination: Place<'tcx>,
51 mut block: BasicBlock,
52 span: Span,
53 stmts: &[StmtId],
54 expr: Option<&Expr<'tcx>>,
55 safety_mode: BlockSafety,
56 ) -> BlockAnd<()> {
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.
75 let mut let_scope_stack = Vec::with_capacity(8);
76 let outer_source_scope = this.source_scope;
77 let outer_in_scope_unsafe = this.in_scope_unsafe;
78 this.update_source_scope_for_safety_mode(span, safety_mode);
79
80 let source_info = this.source_info(span);
81 for stmt in stmts {
82 let Stmt { ref kind, opt_destruction_scope } = this.thir[*stmt];
83 match kind {
84 StmtKind::Expr { scope, expr } => {
85 this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
86 unpack!(
87 block = this.in_opt_scope(
88 opt_destruction_scope.map(|de| (de, source_info)),
89 |this| {
90 let si = (*scope, source_info);
91 this.in_scope(si, LintLevel::Inherited, |this| {
92 this.stmt_expr(block, &this.thir[*expr], Some(*scope))
93 })
94 }
95 )
96 );
97 }
98 StmtKind::Let {
99 remainder_scope,
100 init_scope,
101 ref pattern,
102 initializer,
103 lint_level,
104 } => {
105 let ignores_expr_result = matches!(*pattern.kind, PatKind::Wild);
106 this.block_context.push(BlockFrame::Statement { ignores_expr_result });
107
108 // Enter the remainder scope, i.e., the bindings' destruction scope.
109 this.push_scope((*remainder_scope, source_info));
110 let_scope_stack.push(remainder_scope);
111
112 // Declare the bindings, which may create a source scope.
113 let remainder_span = remainder_scope.span(this.tcx, this.region_scope_tree);
114
115 let visibility_scope =
116 Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
117
118 // Evaluate the initializer, if present.
119 if let Some(init) = initializer {
120 let init = &this.thir[*init];
121 let initializer_span = init.span;
122
123 unpack!(
124 block = this.in_opt_scope(
125 opt_destruction_scope.map(|de| (de, source_info)),
126 |this| {
127 let scope = (*init_scope, source_info);
128 this.in_scope(scope, *lint_level, |this| {
129 this.declare_bindings(
130 visibility_scope,
131 remainder_span,
132 pattern,
133 ArmHasGuard(false),
134 Some((None, initializer_span)),
135 );
136 this.expr_into_pattern(block, pattern.clone(), init)
137 })
138 }
139 )
140 );
141 } else {
142 let scope = (*init_scope, source_info);
143 unpack!(this.in_scope(scope, *lint_level, |this| {
144 this.declare_bindings(
145 visibility_scope,
146 remainder_span,
147 pattern,
148 ArmHasGuard(false),
149 None,
150 );
151 block.unit()
152 }));
153
154 debug!("ast_block_stmts: pattern={:?}", pattern);
155 this.visit_primary_bindings(
156 pattern,
157 UserTypeProjections::none(),
158 &mut |this, _, _, _, node, span, _, _| {
159 this.storage_live_binding(block, node, span, OutsideGuard, true);
160 this.schedule_drop_for_binding(node, span, OutsideGuard);
161 },
162 )
163 }
164
165 // Enter the visibility scope, after evaluating the initializer.
166 if let Some(source_scope) = visibility_scope {
167 this.source_scope = source_scope;
168 }
169 }
170 }
171
172 let popped = this.block_context.pop();
173 assert!(popped.map_or(false, |bf| bf.is_statement()));
174 }
175
176 // Then, the block may have an optional trailing expression which is a “return” value
177 // of the block, which is stored into `destination`.
178 let tcx = this.tcx;
179 let destination_ty = destination.ty(&this.local_decls, tcx).ty;
180 if let Some(expr) = expr {
181 let tail_result_is_ignored =
182 destination_ty.is_unit() || this.block_context.currently_ignores_tail_results();
183 this.block_context
184 .push(BlockFrame::TailExpr { tail_result_is_ignored, span: expr.span });
185
186 unpack!(block = this.expr_into_dest(destination, block, expr));
187 let popped = this.block_context.pop();
188
189 assert!(popped.map_or(false, |bf| bf.is_tail_expr()));
190 } else {
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.
195 if destination_ty.is_unit() {
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.)
198 this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
199 }
200 }
201 // Finally, we pop all the let scopes before exiting out from the scope of block
202 // itself.
203 for scope in let_scope_stack.into_iter().rev() {
204 unpack!(block = this.pop_scope((*scope, source_info), block));
205 }
206 // Restore the original source scope.
207 this.source_scope = outer_source_scope;
208 this.in_scope_unsafe = outer_in_scope_unsafe;
209 block.unit()
210 }
211
212 /// If we are changing the safety mode, create a new source scope
213 fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) {
214 debug!("update_source_scope_for({:?}, {:?})", span, safety_mode);
215 let new_unsafety = match safety_mode {
216 BlockSafety::Safe => None,
217 BlockSafety::BuiltinUnsafe => Some(Safety::BuiltinUnsafe),
218 BlockSafety::ExplicitUnsafe(hir_id) => {
219 match self.in_scope_unsafe {
220 Safety::Safe => {}
221 // no longer treat `unsafe fn`s as `unsafe` contexts (see RFC #2585)
222 Safety::FnUnsafe
223 if self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0
224 != Level::Allow => {}
225 _ => return,
226 }
227 self.in_scope_unsafe = Safety::ExplicitUnsafe(hir_id);
228 Some(Safety::ExplicitUnsafe(hir_id))
229 }
230 };
231
232 if let Some(unsafety) = new_unsafety {
233 self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(unsafety));
234 }
235 }
236}