2019-02-08 06:28:15 +09:00
|
|
|
use crate::build::matches::ArmHasGuard;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::build::ForGuard::OutsideGuard;
|
|
|
|
|
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
|
2020-07-21 09:09:27 +00:00
|
|
|
use crate::thir::*;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-04-16 00:00:22 +02:00
|
|
|
use rustc_middle::mir::*;
|
2020-05-13 23:43:21 +02:00
|
|
|
use rustc_session::lint::builtin::UNSAFE_OP_IN_UNSAFE_FN;
|
|
|
|
|
use rustc_session::lint::Level;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2019-06-01 13:38:36 +02:00
|
|
|
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn ast_block(
|
2019-12-22 17:42:04 -05:00
|
|
|
&mut self,
|
2020-03-31 14:08:48 -03:00
|
|
|
destination: Place<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
block: BasicBlock,
|
2019-11-30 15:08:22 +01:00
|
|
|
ast_block: &'tcx hir::Block<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
source_info: SourceInfo,
|
|
|
|
|
) -> BlockAnd<()> {
|
2017-09-19 16:20:02 +03:00
|
|
|
let Block {
|
|
|
|
|
region_scope,
|
|
|
|
|
opt_destruction_scope,
|
|
|
|
|
span,
|
|
|
|
|
stmts,
|
|
|
|
|
expr,
|
|
|
|
|
targeted_by_break,
|
2019-12-22 17:42:04 -05:00
|
|
|
safety_mode,
|
|
|
|
|
} = self.hir.mirror(ast_block);
|
|
|
|
|
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
|
2019-03-30 21:49:52 +00:00
|
|
|
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
|
2017-05-23 13:18:20 +02:00
|
|
|
if targeted_by_break {
|
2020-06-04 11:34:42 -04:00
|
|
|
// This is a `break`-able block
|
|
|
|
|
let exit_block = this.cfg.start_new_block();
|
|
|
|
|
let block_exit =
|
|
|
|
|
this.in_breakable_scope(None, exit_block, destination, |this| {
|
|
|
|
|
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
|
|
|
|
|
});
|
|
|
|
|
this.cfg.goto(unpack!(block_exit), source_info, exit_block);
|
|
|
|
|
exit_block.unit()
|
2017-05-23 13:18:20 +02:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
|
2017-05-23 13:18:20 +02:00
|
|
|
}
|
|
|
|
|
})
|
2017-03-22 11:40:29 -04:00
|
|
|
})
|
|
|
|
|
}
|
2016-05-31 20:27:36 +03:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn ast_block_stmts(
|
|
|
|
|
&mut self,
|
2020-03-31 14:08:48 -03:00
|
|
|
destination: Place<'tcx>,
|
2019-12-22 17:42:04 -05:00
|
|
|
mut block: BasicBlock,
|
|
|
|
|
span: Span,
|
|
|
|
|
stmts: Vec<StmtRef<'tcx>>,
|
|
|
|
|
expr: Option<ExprRef<'tcx>>,
|
|
|
|
|
safety_mode: BlockSafety,
|
|
|
|
|
) -> BlockAnd<()> {
|
2017-03-22 11:40:29 -04:00
|
|
|
let this = self;
|
|
|
|
|
|
|
|
|
|
// This convoluted structure is to avoid using recursion as we walk down a list
|
|
|
|
|
// of statements. Basically, the structure we get back is something like:
|
|
|
|
|
//
|
|
|
|
|
// let x = <init> in {
|
|
|
|
|
// expr1;
|
|
|
|
|
// let y = <init> in {
|
|
|
|
|
// expr2;
|
|
|
|
|
// expr3;
|
|
|
|
|
// ...
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// The let bindings are valid till the end of block so all we have to do is to pop all
|
|
|
|
|
// the let-scopes at the end.
|
|
|
|
|
//
|
|
|
|
|
// First we build all the statements in the block.
|
2017-08-31 21:37:38 +03:00
|
|
|
let mut let_scope_stack = Vec::with_capacity(8);
|
2018-05-28 14:16:09 +03:00
|
|
|
let outer_source_scope = this.source_scope;
|
2017-09-19 16:20:02 +03:00
|
|
|
let outer_push_unsafe_count = this.push_unsafe_count;
|
|
|
|
|
let outer_unpushed_unsafe = this.unpushed_unsafe;
|
2018-05-28 14:16:09 +03:00
|
|
|
this.update_source_scope_for_safety_mode(span, safety_mode);
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2017-05-26 18:21:03 +02:00
|
|
|
let source_info = this.source_info(span);
|
2017-03-22 11:40:29 -04:00
|
|
|
for stmt in stmts {
|
2019-06-15 10:08:41 +01:00
|
|
|
let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt);
|
2017-03-22 11:40:29 -04:00
|
|
|
match kind {
|
|
|
|
|
StmtKind::Expr { scope, expr } => {
|
2018-09-22 00:51:48 +02:00
|
|
|
this.block_context.push(BlockFrame::Statement { ignores_expr_result: true });
|
2019-12-22 17:42:04 -05:00
|
|
|
unpack!(
|
|
|
|
|
block = this.in_opt_scope(
|
|
|
|
|
opt_destruction_scope.map(|de| (de, source_info)),
|
|
|
|
|
|this| {
|
|
|
|
|
let si = (scope, source_info);
|
|
|
|
|
this.in_scope(si, LintLevel::Inherited, |this| {
|
|
|
|
|
let expr = this.hir.mirror(expr);
|
|
|
|
|
this.stmt_expr(block, expr, Some(scope))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
2017-03-22 11:40:29 -04:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
StmtKind::Let { remainder_scope, init_scope, pattern, initializer, lint_level } => {
|
|
|
|
|
let ignores_expr_result =
|
|
|
|
|
if let PatKind::Wild = *pattern.kind { true } else { false };
|
2018-09-22 00:51:48 +02:00
|
|
|
this.block_context.push(BlockFrame::Statement { ignores_expr_result });
|
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Enter the remainder scope, i.e., the bindings' destruction scope.
|
2017-07-31 16:00:12 +03:00
|
|
|
this.push_scope((remainder_scope, source_info));
|
2017-08-31 21:37:38 +03:00
|
|
|
let_scope_stack.push(remainder_scope);
|
2016-05-31 20:27:36 +03:00
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
// Declare the bindings, which may create a source scope.
|
2019-12-22 17:42:04 -05:00
|
|
|
let remainder_span =
|
|
|
|
|
remainder_scope.span(this.hir.tcx(), &this.hir.region_scope_tree);
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2019-04-02 22:29:28 +01:00
|
|
|
let visibility_scope =
|
|
|
|
|
Some(this.new_source_scope(remainder_span, LintLevel::Inherited, None));
|
2016-05-31 20:27:36 +03:00
|
|
|
|
2017-03-22 11:40:29 -04:00
|
|
|
// Evaluate the initializer, if present.
|
|
|
|
|
if let Some(init) = initializer {
|
2018-06-27 22:06:54 +01:00
|
|
|
let initializer_span = init.span();
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
unpack!(
|
|
|
|
|
block = this.in_opt_scope(
|
|
|
|
|
opt_destruction_scope.map(|de| (de, source_info)),
|
|
|
|
|
|this| {
|
|
|
|
|
let scope = (init_scope, source_info);
|
|
|
|
|
this.in_scope(scope, lint_level, |this| {
|
|
|
|
|
this.declare_bindings(
|
|
|
|
|
visibility_scope,
|
|
|
|
|
remainder_span,
|
|
|
|
|
&pattern,
|
|
|
|
|
ArmHasGuard(false),
|
|
|
|
|
Some((None, initializer_span)),
|
|
|
|
|
);
|
|
|
|
|
this.expr_into_pattern(block, pattern, init)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
2017-03-22 11:40:29 -04:00
|
|
|
} else {
|
2019-04-02 22:29:28 +01:00
|
|
|
let scope = (init_scope, source_info);
|
|
|
|
|
unpack!(this.in_scope(scope, lint_level, |this| {
|
|
|
|
|
this.declare_bindings(
|
|
|
|
|
visibility_scope,
|
|
|
|
|
remainder_span,
|
|
|
|
|
&pattern,
|
|
|
|
|
ArmHasGuard(false),
|
|
|
|
|
None,
|
|
|
|
|
);
|
|
|
|
|
block.unit()
|
|
|
|
|
}));
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2018-11-25 15:14:39 +01:00
|
|
|
debug!("ast_block_stmts: pattern={:?}", pattern);
|
2020-04-19 18:17:31 +01:00
|
|
|
this.visit_primary_bindings(
|
2018-10-22 14:23:44 +02:00
|
|
|
&pattern,
|
2018-12-19 16:47:06 +01:00
|
|
|
UserTypeProjections::none(),
|
2018-10-22 14:23:44 +02:00
|
|
|
&mut |this, _, _, _, node, span, _, _| {
|
2020-01-25 00:10:40 +00:00
|
|
|
this.storage_live_binding(block, node, span, OutsideGuard, true);
|
2018-10-22 14:23:44 +02:00
|
|
|
this.schedule_drop_for_binding(node, span, OutsideGuard);
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
|
)
|
2017-03-22 11:40:29 -04:00
|
|
|
}
|
|
|
|
|
|
2019-04-02 22:29:28 +01:00
|
|
|
// Enter the visibility scope, after evaluating the initializer.
|
|
|
|
|
if let Some(source_scope) = visibility_scope {
|
2018-05-28 14:16:09 +03:00
|
|
|
this.source_scope = source_scope;
|
2016-02-24 17:14:03 +02:00
|
|
|
}
|
2015-12-30 20:00:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-22 00:51:48 +02:00
|
|
|
|
|
|
|
|
let popped = this.block_context.pop();
|
2019-12-22 17:42:04 -05:00
|
|
|
assert!(popped.map_or(false, |bf| bf.is_statement()));
|
2017-03-22 11:40:29 -04:00
|
|
|
}
|
2018-09-22 00:51:48 +02:00
|
|
|
|
2017-03-22 11:40:29 -04:00
|
|
|
// Then, the block may have an optional trailing expression which is a “return” value
|
2018-09-22 00:51:48 +02:00
|
|
|
// of the block, which is stored into `destination`.
|
|
|
|
|
let tcx = this.hir.tcx();
|
2019-03-28 19:08:31 -07:00
|
|
|
let destination_ty = destination.ty(&this.local_decls, tcx).ty;
|
2017-03-22 11:40:29 -04:00
|
|
|
if let Some(expr) = expr {
|
2019-12-22 17:42:04 -05:00
|
|
|
let tail_result_is_ignored =
|
|
|
|
|
destination_ty.is_unit() || this.block_context.currently_ignores_tail_results();
|
2020-04-16 12:43:40 -07:00
|
|
|
let span = match expr {
|
2020-07-21 09:09:27 +00:00
|
|
|
ExprRef::Thir(expr) => expr.span,
|
2020-04-16 12:43:40 -07:00
|
|
|
ExprRef::Mirror(ref expr) => expr.span,
|
|
|
|
|
};
|
|
|
|
|
this.block_context.push(BlockFrame::TailExpr { tail_result_is_ignored, span });
|
2018-09-22 00:51:48 +02:00
|
|
|
|
2019-10-09 20:06:15 +01:00
|
|
|
unpack!(block = this.into(destination, block, expr));
|
2018-09-22 00:51:48 +02:00
|
|
|
let popped = this.block_context.pop();
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
assert!(popped.map_or(false, |bf| bf.is_tail_expr()));
|
2017-03-22 11:40:29 -04:00
|
|
|
} else {
|
2018-01-26 15:39:19 +00:00
|
|
|
// If a block has no trailing expression, then it is given an implicit return type.
|
|
|
|
|
// This return type is usually `()`, unless the block is diverging, in which case the
|
|
|
|
|
// return type is `!`. For the unit type, we need to actually return the unit, but in
|
|
|
|
|
// the case of `!`, no return value is required, as the block will never return.
|
2018-09-22 00:51:48 +02:00
|
|
|
if destination_ty.is_unit() {
|
2018-01-25 18:03:48 +00:00
|
|
|
// We only want to assign an implicit `()` as the return value of the block if the
|
|
|
|
|
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
|
2020-04-09 12:24:53 +02:00
|
|
|
this.cfg.push_assign_unit(block, source_info, destination, this.hir.tcx());
|
2018-01-25 18:03:48 +00:00
|
|
|
}
|
2017-03-22 11:40:29 -04:00
|
|
|
}
|
|
|
|
|
// Finally, we pop all the let scopes before exiting out from the scope of block
|
|
|
|
|
// itself.
|
2017-08-31 21:37:38 +03:00
|
|
|
for scope in let_scope_stack.into_iter().rev() {
|
|
|
|
|
unpack!(block = this.pop_scope((scope, source_info), block));
|
2017-03-22 11:40:29 -04:00
|
|
|
}
|
2018-05-28 14:16:09 +03:00
|
|
|
// Restore the original source scope.
|
|
|
|
|
this.source_scope = outer_source_scope;
|
2017-09-19 16:20:02 +03:00
|
|
|
this.push_unsafe_count = outer_push_unsafe_count;
|
|
|
|
|
this.unpushed_unsafe = outer_unpushed_unsafe;
|
2017-03-22 11:40:29 -04:00
|
|
|
block.unit()
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2017-09-19 16:20:02 +03:00
|
|
|
|
2018-05-28 14:16:09 +03:00
|
|
|
/// If we are changing the safety mode, create a new source scope
|
2019-12-22 17:42:04 -05:00
|
|
|
fn update_source_scope_for_safety_mode(&mut self, span: Span, safety_mode: BlockSafety) {
|
2018-05-28 14:16:09 +03:00
|
|
|
debug!("update_source_scope_for({:?}, {:?})", span, safety_mode);
|
2017-09-19 16:20:02 +03:00
|
|
|
let new_unsafety = match safety_mode {
|
|
|
|
|
BlockSafety::Safe => None,
|
2019-02-22 15:48:14 +01:00
|
|
|
BlockSafety::ExplicitUnsafe(hir_id) => {
|
2017-09-19 16:20:02 +03:00
|
|
|
assert_eq!(self.push_unsafe_count, 0);
|
|
|
|
|
match self.unpushed_unsafe {
|
|
|
|
|
Safety::Safe => {}
|
2020-05-03 23:11:34 +02:00
|
|
|
// no longer treat `unsafe fn`s as `unsafe` contexts (see RFC #2585)
|
2020-05-13 23:43:21 +02:00
|
|
|
Safety::FnUnsafe
|
|
|
|
|
if self.hir.tcx().lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, hir_id).0
|
|
|
|
|
!= Level::Allow => {}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => return,
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
2019-02-22 15:48:14 +01:00
|
|
|
self.unpushed_unsafe = Safety::ExplicitUnsafe(hir_id);
|
|
|
|
|
Some(Safety::ExplicitUnsafe(hir_id))
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
BlockSafety::PushUnsafe => {
|
|
|
|
|
self.push_unsafe_count += 1;
|
|
|
|
|
Some(Safety::BuiltinUnsafe)
|
|
|
|
|
}
|
|
|
|
|
BlockSafety::PopUnsafe => {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.push_unsafe_count = self
|
|
|
|
|
.push_unsafe_count
|
|
|
|
|
.checked_sub(1)
|
|
|
|
|
.unwrap_or_else(|| span_bug!(span, "unsafe count underflow"));
|
2020-07-21 09:09:27 +00:00
|
|
|
if self.push_unsafe_count == 0 { Some(self.unpushed_unsafe) } else { None }
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(unsafety) = new_unsafety {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.source_scope = self.new_source_scope(span, LintLevel::Inherited, Some(unsafety));
|
2017-09-19 16:20:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|