1/*!
2Managing the scope stack. The scopes are tied to lexical scopes, so as
3we descend the THIR, we push a scope on the stack, build its
4contents, and then pop it off. Every scope is named by a
5`region::Scope`.
67### SEME Regions
89When pushing a new [Scope], we record the current point in the graph (a
10basic block); this marks the entry to the scope. We then generate more
11stuff in the control-flow graph. Whenever the scope is exited, either
12via a `break` or `return` or just by fallthrough, that marks an exit
13from the scope. Each lexical scope thus corresponds to a single-entry,
14multiple-exit (SEME) region in the control-flow graph.
1516For now, we record the `region::Scope` to each SEME region for later reference
17(see caveat in next paragraph). This is because destruction scopes are tied to
18them. This may change in the future so that MIR lowering determines its own
19destruction scopes.
2021### Not so SEME Regions
2223In the course of building matches, it sometimes happens that certain code
24(namely guards) gets executed multiple times. This means that the scope lexical
25scope may in fact correspond to multiple, disjoint SEME regions. So in fact our
26mapping is from one scope to a vector of SEME regions. Since the SEME regions
27are disjoint, the mapping is still one-to-one for the set of SEME regions that
28we're currently in.
2930Also in matches, the scopes assigned to arms are not always even SEME regions!
31Each arm has a single region with one entry for each pattern. We manually
32manipulate the scheduled drops in this scope to avoid dropping things multiple
33times.
3435### Drops
3637The primary purpose for scopes is to insert drops: while building
38the contents, we also accumulate places that need to be dropped upon
39exit from each scope. This is done by calling `schedule_drop`. Once a
40drop is scheduled, whenever we branch out we will insert drops of all
41those places onto the outgoing edge. Note that we don't know the full
42set of scheduled drops up front, and so whenever we exit from the
43scope we only drop the values scheduled thus far. For example, consider
44the scope S corresponding to this loop:
4546```
47# let cond = true;
48loop {
49 let x = ..;
50 if cond { break; }
51 let y = ..;
52}
53```
5455When processing the `let x`, we will add one drop to the scope for
56`x`. The break will then insert a drop for `x`. When we process `let
57y`, we will add another drop (in fact, to a subscope, but let's ignore
58that for now); any later drops would also drop `y`.
5960### Early exit
6162There are numerous "normal" ways to early exit a scope: `break`,
63`continue`, `return` (panics are handled separately). Whenever an
64early exit occurs, the method `break_scope` is called. It is given the
65current point in execution where the early exit occurs, as well as the
66scope you want to branch to (note that all early exits from to some
67other enclosing scope). `break_scope` will record the set of drops currently
68scheduled in a [DropTree]. Later, before `in_breakable_scope` exits, the drops
69will be added to the CFG.
7071Panics are handled in a similar fashion, except that the drops are added to the
72MIR once the rest of the function has finished being lowered. If a terminator
73can panic, call `diverge_from(block)` with the block containing the terminator
74`block`.
7576### Breakable scopes
7778In addition to the normal scope stack, we track a loop scope stack
79that contains only loops and breakable blocks. It tracks where a `break`,
80`continue` or `return` should go to.
8182*/
8384use std::mem;
8586use interpret::ErrorHandled;
87use rustc_data_structures::fx::FxHashMap;
88use rustc_hir::HirId;
89use rustc_index::{IndexSlice, IndexVec};
90use rustc_middle::middle::region;
91use rustc_middle::mir::{self, *};
92use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind};
93use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, ValTree};
94use rustc_middle::{bug, span_bug};
95use rustc_pattern_analysis::rustc::RustcPatCtxt;
96use rustc_session::lint::Level;
97use rustc_span::{DUMMY_SP, Span, Spanned};
98use tracing::{debug, instrument};
99100use super::matches::BuiltMatchTree;
101use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG};
102use crate::errors::{
103ConstContinueBadConst, ConstContinueNotMonomorphicConst, ConstContinueUnknownJumpTarget,
104};
105106#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Scopes<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["scopes", "breakable_scopes", "const_continuable_scopes",
"if_then_scope", "unwind_drops", "coroutine_drops"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.scopes, &self.breakable_scopes,
&self.const_continuable_scopes, &self.if_then_scope,
&self.unwind_drops, &&self.coroutine_drops];
::core::fmt::Formatter::debug_struct_fields_finish(f, "Scopes", names,
values)
}
}Debug)]
107pub(crate) struct Scopes<'tcx> {
108 scopes: Vec<Scope>,
109110/// The current set of breakable scopes. See module comment for more details.
111breakable_scopes: Vec<BreakableScope<'tcx>>,
112113 const_continuable_scopes: Vec<ConstContinuableScope<'tcx>>,
114115/// The scope of the innermost if-then currently being lowered.
116if_then_scope: Option<IfThenScope>,
117118/// Drops that need to be done on unwind paths. See the comment on
119 /// [DropTree] for more details.
120unwind_drops: DropTree,
121122/// Drops that need to be done on paths to the `CoroutineDrop` terminator.
123coroutine_drops: DropTree,
124}
125126#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Scope {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["source_scope", "region_scope", "drops", "moved_locals",
"cached_unwind_block", "cached_coroutine_drop_block"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.source_scope, &self.region_scope, &self.drops,
&self.moved_locals, &self.cached_unwind_block,
&&self.cached_coroutine_drop_block];
::core::fmt::Formatter::debug_struct_fields_finish(f, "Scope", names,
values)
}
}Debug)]
127struct Scope {
128/// The source scope this scope was created in.
129source_scope: SourceScope,
130131/// the region span of this scope within source code.
132region_scope: region::Scope,
133134/// set of places to drop when exiting this scope. This starts
135 /// out empty but grows as variables are declared during the
136 /// building process. This is a stack, so we always drop from the
137 /// end of the vector (top of the stack) first.
138drops: Vec<DropData>,
139140 moved_locals: Vec<Local>,
141142/// The drop index that will drop everything in and below this scope on an
143 /// unwind path.
144cached_unwind_block: Option<DropIdx>,
145146/// The drop index that will drop everything in and below this scope on a
147 /// coroutine drop path.
148cached_coroutine_drop_block: Option<DropIdx>,
149}
150151#[derive(#[automatically_derived]
impl ::core::clone::Clone for DropData {
#[inline]
fn clone(&self) -> DropData {
let _: ::core::clone::AssertParamIsClone<SourceInfo>;
let _: ::core::clone::AssertParamIsClone<Local>;
let _: ::core::clone::AssertParamIsClone<DropKind>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DropData { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for DropData {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "DropData",
"source_info", &self.source_info, "local", &self.local, "kind",
&&self.kind)
}
}Debug)]
152struct DropData {
153/// The `Span` where drop obligation was incurred (typically where place was
154 /// declared)
155source_info: SourceInfo,
156157/// local to drop
158local: Local,
159160/// Whether this is a value Drop or a StorageDead.
161kind: DropKind,
162}
163164#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
DropKind::Value => "Value",
DropKind::Storage => "Storage",
DropKind::ForLint => "ForLint",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for DropKind {
#[inline]
fn clone(&self) -> DropKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DropKind { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for DropKind {
#[inline]
fn eq(&self, other: &DropKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DropKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for DropKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash)]
165pub(crate) enum DropKind {
166 Value,
167 Storage,
168 ForLint,
169}
170171#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for BreakableScope<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f,
"BreakableScope", "region_scope", &self.region_scope,
"break_destination", &self.break_destination, "break_drops",
&self.break_drops, "continue_drops", &&self.continue_drops)
}
}Debug)]
172struct BreakableScope<'tcx> {
173/// Region scope of the loop
174region_scope: region::Scope,
175/// The destination of the loop/block expression itself (i.e., where to put
176 /// the result of a `break` or `return` expression)
177break_destination: Place<'tcx>,
178/// Drops that happen on the `break`/`return` path.
179break_drops: DropTree,
180/// Drops that happen on the `continue` path.
181continue_drops: Option<DropTree>,
182}
183184#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ConstContinuableScope<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f,
"ConstContinuableScope", "region_scope", &self.region_scope,
"state_place", &self.state_place, "arms", &self.arms,
"built_match_tree", &self.built_match_tree,
"const_continue_drops", &&self.const_continue_drops)
}
}Debug)]
185struct ConstContinuableScope<'tcx> {
186/// The scope for the `#[loop_match]` which its `#[const_continue]`s will jump to.
187region_scope: region::Scope,
188/// The place of the state of a `#[loop_match]`, which a `#[const_continue]` must update.
189state_place: Place<'tcx>,
190191 arms: Box<[ArmId]>,
192 built_match_tree: BuiltMatchTree<'tcx>,
193194/// Drops that happen on a `#[const_continue]`
195const_continue_drops: DropTree,
196}
197198#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IfThenScope {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "IfThenScope",
"region_scope", &self.region_scope, "else_drops",
&&self.else_drops)
}
}Debug)]
199struct IfThenScope {
200/// The if-then scope or arm scope
201region_scope: region::Scope,
202/// Drops that happen on the `else` path.
203else_drops: DropTree,
204}
205206/// The target of an expression that breaks out of a scope
207#[derive(#[automatically_derived]
impl ::core::clone::Clone for BreakableTarget {
#[inline]
fn clone(&self) -> BreakableTarget {
let _: ::core::clone::AssertParamIsClone<region::Scope>;
let _: ::core::clone::AssertParamIsClone<region::Scope>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for BreakableTarget { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for BreakableTarget {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
BreakableTarget::Continue(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Continue", &__self_0),
BreakableTarget::Break(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Break",
&__self_0),
BreakableTarget::Return =>
::core::fmt::Formatter::write_str(f, "Return"),
}
}
}Debug)]
208pub(crate) enum BreakableTarget {
209 Continue(region::Scope),
210 Break(region::Scope),
211 Return,
212}
213214impl ::std::fmt::Debug for DropIdx {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
fmt.write_fmt(format_args!("{0}", self.as_u32()))
}
}rustc_index::newtype_index! {
215#[orderable]
216struct DropIdx {}
217}218219const ROOT_NODE: DropIdx = DropIdx::ZERO;
220221/// A tree of drops that we have deferred lowering. It's used for:
222///
223/// * Drops on unwind paths
224/// * Drops on coroutine drop paths (when a suspended coroutine is dropped)
225/// * Drops on return and loop exit paths
226/// * Drops on the else path in an `if let` chain
227///
228/// Once no more nodes could be added to the tree, we lower it to MIR in one go
229/// in `build_mir`.
230#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropTree {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "DropTree",
"drop_nodes", &self.drop_nodes, "existing_drops_map",
&self.existing_drops_map, "entry_points", &&self.entry_points)
}
}Debug)]
231struct DropTree {
232/// Nodes in the drop tree, containing drop data and a link to the next node.
233drop_nodes: IndexVec<DropIdx, DropNode>,
234/// Map for finding the index of an existing node, given its contents.
235existing_drops_map: FxHashMap<DropNodeKey, DropIdx>,
236/// Edges into the `DropTree` that need to be added once it's lowered.
237entry_points: Vec<(DropIdx, BasicBlock)>,
238}
239240/// A single node in the drop tree.
241#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropNode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "DropNode",
"data", &self.data, "next", &&self.next)
}
}Debug)]
242struct DropNode {
243/// Info about the drop to be performed at this node in the drop tree.
244data: DropData,
245/// Index of the "next" drop to perform (in drop order, not declaration order).
246next: DropIdx,
247}
248249/// Subset of [`DropNode`] used for reverse lookup in a hash table.
250#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DropNodeKey {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "DropNodeKey",
"next", &self.next, "local", &&self.local)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for DropNodeKey {
#[inline]
fn eq(&self, other: &DropNodeKey) -> bool {
self.next == other.next && self.local == other.local
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DropNodeKey {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DropIdx>;
let _: ::core::cmp::AssertParamIsEq<Local>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for DropNodeKey {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.next, state);
::core::hash::Hash::hash(&self.local, state)
}
}Hash)]
251struct DropNodeKey {
252 next: DropIdx,
253 local: Local,
254}
255256impl Scope {
257/// Whether there's anything to do for the cleanup path, that is,
258 /// when unwinding through this scope. This includes destructors,
259 /// but not StorageDead statements, which don't get emitted at all
260 /// for unwinding, for several reasons:
261 /// * clang doesn't emit llvm.lifetime.end for C++ unwinding
262 /// * LLVM's memory dependency analysis can't handle it atm
263 /// * polluting the cleanup MIR with StorageDead creates
264 /// landing pads even though there's no actual destructors
265 /// * freeing up stack space has no effect during unwinding
266 /// Note that for coroutines we do emit StorageDeads, for the
267 /// use of optimizations in the MIR coroutine transform.
268fn needs_cleanup(&self) -> bool {
269self.drops.iter().any(|drop| match drop.kind {
270 DropKind::Value | DropKind::ForLint => true,
271 DropKind::Storage => false,
272 })
273 }
274275fn invalidate_cache(&mut self) {
276self.cached_unwind_block = None;
277self.cached_coroutine_drop_block = None;
278 }
279}
280281/// A trait that determined how [DropTree] creates its blocks and
282/// links to any entry nodes.
283trait DropTreeBuilder<'tcx> {
284/// Create a new block for the tree. This should call either
285 /// `cfg.start_new_block()` or `cfg.start_new_cleanup_block()`.
286fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock;
287288/// Links a block outside the drop tree, `from`, to the block `to` inside
289 /// the drop tree.
290fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock);
291}
292293impl DropTree {
294fn new() -> Self {
295// The root node of the tree doesn't represent a drop, but instead
296 // represents the block in the tree that should be jumped to once all
297 // of the required drops have been performed.
298let fake_source_info = SourceInfo::outermost(DUMMY_SP);
299let fake_data =
300DropData { source_info: fake_source_info, local: Local::MAX, kind: DropKind::Storage };
301let drop_nodes = IndexVec::from_raw(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[DropNode { data: fake_data, next: DropIdx::MAX }]))vec![DropNode { data: fake_data, next: DropIdx::MAX }]);
302Self { drop_nodes, entry_points: Vec::new(), existing_drops_map: FxHashMap::default() }
303 }
304305/// Adds a node to the drop tree, consisting of drop data and the index of
306 /// the "next" drop (in drop order), which could be the sentinel [`ROOT_NODE`].
307 ///
308 /// If there is already an equivalent node in the tree, nothing is added, and
309 /// that node's index is returned. Otherwise, the new node's index is returned.
310fn add_drop(&mut self, data: DropData, next: DropIdx) -> DropIdx {
311let drop_nodes = &mut self.drop_nodes;
312*self313 .existing_drops_map
314 .entry(DropNodeKey { next, local: data.local })
315// Create a new node, and also add its index to the map.
316.or_insert_with(|| drop_nodes.push(DropNode { data, next }))
317 }
318319/// Registers `from` as an entry point to this drop tree, at `to`.
320 ///
321 /// During [`Self::build_mir`], `from` will be linked to the corresponding
322 /// block within the drop tree.
323fn add_entry_point(&mut self, from: BasicBlock, to: DropIdx) {
324if true {
if !(to < self.drop_nodes.next_index()) {
::core::panicking::panic("assertion failed: to < self.drop_nodes.next_index()")
};
};debug_assert!(to < self.drop_nodes.next_index());
325self.entry_points.push((to, from));
326 }
327328/// Builds the MIR for a given drop tree.
329fn build_mir<'tcx, T: DropTreeBuilder<'tcx>>(
330&mut self,
331 cfg: &mut CFG<'tcx>,
332 root_node: Option<BasicBlock>,
333 ) -> IndexVec<DropIdx, Option<BasicBlock>> {
334{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:334",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(334u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("DropTree::build_mir(drops = {0:#?})",
self) as &dyn Value))])
});
} else { ; }
};debug!("DropTree::build_mir(drops = {:#?})", self);
335336let mut blocks = self.assign_blocks::<T>(cfg, root_node);
337self.link_blocks(cfg, &mut blocks);
338339blocks340 }
341342/// Assign blocks for all of the drops in the drop tree that need them.
343fn assign_blocks<'tcx, T: DropTreeBuilder<'tcx>>(
344&mut self,
345 cfg: &mut CFG<'tcx>,
346 root_node: Option<BasicBlock>,
347 ) -> IndexVec<DropIdx, Option<BasicBlock>> {
348// StorageDead statements can share blocks with each other and also with
349 // a Drop terminator. We iterate through the drops to find which drops
350 // need their own block.
351#[derive(#[automatically_derived]
impl ::core::clone::Clone for Block {
#[inline]
fn clone(&self) -> Block {
let _: ::core::clone::AssertParamIsClone<DropIdx>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Block { }Copy)]
352enum Block {
353// This drop is unreachable
354None,
355// This drop is only reachable through the `StorageDead` with the
356 // specified index.
357Shares(DropIdx),
358// This drop has more than one way of being reached, or it is
359 // branched to from outside the tree, or its predecessor is a
360 // `Value` drop.
361Own,
362 }
363364let mut blocks = IndexVec::from_elem(None, &self.drop_nodes);
365blocks[ROOT_NODE] = root_node;
366367let mut needs_block = IndexVec::from_elem(Block::None, &self.drop_nodes);
368if root_node.is_some() {
369// In some cases (such as drops for `continue`) the root node
370 // already has a block. In this case, make sure that we don't
371 // override it.
372needs_block[ROOT_NODE] = Block::Own;
373 }
374375// Sort so that we only need to check the last value.
376let entry_points = &mut self.entry_points;
377entry_points.sort();
378379for (drop_idx, drop_node) in self.drop_nodes.iter_enumerated().rev() {
380if entry_points.last().is_some_and(|entry_point| entry_point.0 == drop_idx) {
381let block = *blocks[drop_idx].get_or_insert_with(|| T::make_block(cfg));
382 needs_block[drop_idx] = Block::Own;
383while entry_points.last().is_some_and(|entry_point| entry_point.0 == drop_idx) {
384let entry_block = entry_points.pop().unwrap().1;
385 T::link_entry_point(cfg, entry_block, block);
386 }
387 }
388match needs_block[drop_idx] {
389 Block::None => continue,
390 Block::Own => {
391 blocks[drop_idx].get_or_insert_with(|| T::make_block(cfg));
392 }
393 Block::Shares(pred) => {
394 blocks[drop_idx] = blocks[pred];
395 }
396 }
397if let DropKind::Value = drop_node.data.kind {
398 needs_block[drop_node.next] = Block::Own;
399 } else if drop_idx != ROOT_NODE {
400match &mut needs_block[drop_node.next] {
401 pred @ Block::None => *pred = Block::Shares(drop_idx),
402 pred @ Block::Shares(_) => *pred = Block::Own,
403 Block::Own => (),
404 }
405 }
406 }
407408{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:408",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(408u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("assign_blocks: blocks = {0:#?}",
blocks) as &dyn Value))])
});
} else { ; }
};debug!("assign_blocks: blocks = {:#?}", blocks);
409if !entry_points.is_empty() {
::core::panicking::panic("assertion failed: entry_points.is_empty()")
};assert!(entry_points.is_empty());
410411blocks412 }
413414fn link_blocks<'tcx>(
415&self,
416 cfg: &mut CFG<'tcx>,
417 blocks: &IndexSlice<DropIdx, Option<BasicBlock>>,
418 ) {
419for (drop_idx, drop_node) in self.drop_nodes.iter_enumerated().rev() {
420let Some(block) = blocks[drop_idx] else { continue };
421match drop_node.data.kind {
422 DropKind::Value => {
423let terminator = TerminatorKind::Drop {
424 target: blocks[drop_node.next].unwrap(),
425// The caller will handle this if needed.
426unwind: UnwindAction::Terminate(UnwindTerminateReason::InCleanup),
427 place: drop_node.data.local.into(),
428 replace: false,
429 drop: None,
430 };
431 cfg.terminate(block, drop_node.data.source_info, terminator);
432 }
433 DropKind::ForLint => {
434let stmt = Statement::new(
435 drop_node.data.source_info,
436 StatementKind::BackwardIncompatibleDropHint {
437 place: Box::new(drop_node.data.local.into()),
438 reason: BackwardIncompatibleDropReason::Edition2024,
439 },
440 );
441 cfg.push(block, stmt);
442let target = blocks[drop_node.next].unwrap();
443if target != block {
444// Diagnostics don't use this `Span` but debuginfo
445 // might. Since we don't want breakpoints to be placed
446 // here, especially when this is on an unwind path, we
447 // use `DUMMY_SP`.
448let source_info =
449 SourceInfo { span: DUMMY_SP, ..drop_node.data.source_info };
450let terminator = TerminatorKind::Goto { target };
451 cfg.terminate(block, source_info, terminator);
452 }
453 }
454// Root nodes don't correspond to a drop.
455DropKind::Storage if drop_idx == ROOT_NODE => {}
456 DropKind::Storage => {
457let stmt = Statement::new(
458 drop_node.data.source_info,
459 StatementKind::StorageDead(drop_node.data.local),
460 );
461 cfg.push(block, stmt);
462let target = blocks[drop_node.next].unwrap();
463if target != block {
464// Diagnostics don't use this `Span` but debuginfo
465 // might. Since we don't want breakpoints to be placed
466 // here, especially when this is on an unwind path, we
467 // use `DUMMY_SP`.
468let source_info =
469 SourceInfo { span: DUMMY_SP, ..drop_node.data.source_info };
470let terminator = TerminatorKind::Goto { target };
471 cfg.terminate(block, source_info, terminator);
472 }
473 }
474 }
475 }
476 }
477}
478479impl<'tcx> Scopes<'tcx> {
480pub(crate) fn new() -> Self {
481Self {
482 scopes: Vec::new(),
483 breakable_scopes: Vec::new(),
484 const_continuable_scopes: Vec::new(),
485 if_then_scope: None,
486 unwind_drops: DropTree::new(),
487 coroutine_drops: DropTree::new(),
488 }
489 }
490491fn push_scope(&mut self, region_scope: (region::Scope, SourceInfo), vis_scope: SourceScope) {
492{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:492",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(492u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("push_scope({0:?})",
region_scope) as &dyn Value))])
});
} else { ; }
};debug!("push_scope({:?})", region_scope);
493self.scopes.push(Scope {
494 source_scope: vis_scope,
495 region_scope: region_scope.0,
496 drops: ::alloc::vec::Vec::new()vec![],
497 moved_locals: ::alloc::vec::Vec::new()vec![],
498 cached_unwind_block: None,
499 cached_coroutine_drop_block: None,
500 });
501 }
502503fn pop_scope(&mut self, region_scope: (region::Scope, SourceInfo)) -> Scope {
504let scope = self.scopes.pop().unwrap();
505match (&scope.region_scope, ®ion_scope.0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(scope.region_scope, region_scope.0);
506scope507 }
508509fn scope_index(&self, region_scope: region::Scope, span: Span) -> usize {
510self.scopes
511 .iter()
512 .rposition(|scope| scope.region_scope == region_scope)
513 .unwrap_or_else(|| ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("region_scope {0:?} does not enclose", region_scope))span_bug!(span, "region_scope {:?} does not enclose", region_scope))
514 }
515516/// Returns the topmost active scope, which is known to be alive until
517 /// the next scope expression.
518fn topmost(&self) -> region::Scope {
519self.scopes.last().expect("topmost_scope: no scopes present").region_scope
520 }
521}
522523/// Used by [`Builder::in_scope`] to create source scopes mapping from MIR back to HIR at points
524/// where lint levels change.
525#[derive(#[automatically_derived]
impl ::core::marker::Copy for LintLevel { }Copy, #[automatically_derived]
impl ::core::clone::Clone for LintLevel {
#[inline]
fn clone(&self) -> LintLevel {
let _: ::core::clone::AssertParamIsClone<HirId>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for LintLevel {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
LintLevel::Inherited =>
::core::fmt::Formatter::write_str(f, "Inherited"),
LintLevel::Explicit(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Explicit", &__self_0),
}
}
}Debug)]
526pub(crate) enum LintLevel {
527 Inherited,
528 Explicit(HirId),
529}
530531impl<'a, 'tcx> Builder<'a, 'tcx> {
532// Adding and removing scopes
533 // ==========================
534535/// Start a breakable scope, which tracks where `continue`, `break` and
536 /// `return` should branch to.
537pub(crate) fn in_breakable_scope<F>(
538&mut self,
539 loop_block: Option<BasicBlock>,
540 break_destination: Place<'tcx>,
541 span: Span,
542 f: F,
543 ) -> BlockAnd<()>
544where
545F: FnOnce(&mut Builder<'a, 'tcx>) -> Option<BlockAnd<()>>,
546 {
547let region_scope = self.scopes.topmost();
548let scope = BreakableScope {
549region_scope,
550break_destination,
551 break_drops: DropTree::new(),
552 continue_drops: loop_block.map(|_| DropTree::new()),
553 };
554self.scopes.breakable_scopes.push(scope);
555let normal_exit_block = f(self);
556let breakable_scope = self.scopes.breakable_scopes.pop().unwrap();
557if !(breakable_scope.region_scope == region_scope) {
::core::panicking::panic("assertion failed: breakable_scope.region_scope == region_scope")
};assert!(breakable_scope.region_scope == region_scope);
558let break_block =
559self.build_exit_tree(breakable_scope.break_drops, region_scope, span, None);
560if let Some(drops) = breakable_scope.continue_drops {
561self.build_exit_tree(drops, region_scope, span, loop_block);
562 }
563match (normal_exit_block, break_block) {
564 (Some(block), None) | (None, Some(block)) => block,
565 (None, None) => self.cfg.start_new_block().unit(),
566 (Some(normal_block), Some(exit_block)) => {
567let target = self.cfg.start_new_block();
568let source_info = self.source_info(span);
569self.cfg.terminate(
570normal_block.into_block(),
571source_info,
572 TerminatorKind::Goto { target },
573 );
574self.cfg.terminate(
575exit_block.into_block(),
576source_info,
577 TerminatorKind::Goto { target },
578 );
579target.unit()
580 }
581 }
582 }
583584/// Start a const-continuable scope, which tracks where `#[const_continue] break` should
585 /// branch to.
586pub(crate) fn in_const_continuable_scope<F>(
587&mut self,
588 arms: Box<[ArmId]>,
589 built_match_tree: BuiltMatchTree<'tcx>,
590 state_place: Place<'tcx>,
591 span: Span,
592 f: F,
593 ) -> BlockAnd<()>
594where
595F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<()>,
596 {
597let region_scope = self.scopes.topmost();
598let scope = ConstContinuableScope {
599region_scope,
600state_place,
601 const_continue_drops: DropTree::new(),
602arms,
603built_match_tree,
604 };
605self.scopes.const_continuable_scopes.push(scope);
606let normal_exit_block = f(self);
607let const_continue_scope = self.scopes.const_continuable_scopes.pop().unwrap();
608if !(const_continue_scope.region_scope == region_scope) {
::core::panicking::panic("assertion failed: const_continue_scope.region_scope == region_scope")
};assert!(const_continue_scope.region_scope == region_scope);
609610let break_block = self.build_exit_tree(
611const_continue_scope.const_continue_drops,
612region_scope,
613span,
614None,
615 );
616617match (normal_exit_block, break_block) {
618 (block, None) => block,
619 (normal_block, Some(exit_block)) => {
620let target = self.cfg.start_new_block();
621let source_info = self.source_info(span);
622self.cfg.terminate(
623normal_block.into_block(),
624source_info,
625 TerminatorKind::Goto { target },
626 );
627self.cfg.terminate(
628exit_block.into_block(),
629source_info,
630 TerminatorKind::Goto { target },
631 );
632target.unit()
633 }
634 }
635 }
636637/// Start an if-then scope which tracks drop for `if` expressions and `if`
638 /// guards.
639 ///
640 /// For an if-let chain:
641 ///
642 /// if let Some(x) = a && let Some(y) = b && let Some(z) = c { ... }
643 ///
644 /// There are three possible ways the condition can be false and we may have
645 /// to drop `x`, `x` and `y`, or neither depending on which binding fails.
646 /// To handle this correctly we use a `DropTree` in a similar way to a
647 /// `loop` expression and 'break' out on all of the 'else' paths.
648 ///
649 /// Notes:
650 /// - We don't need to keep a stack of scopes in the `Builder` because the
651 /// 'else' paths will only leave the innermost scope.
652 /// - This is also used for match guards.
653pub(crate) fn in_if_then_scope<F>(
654&mut self,
655 region_scope: region::Scope,
656 span: Span,
657 f: F,
658 ) -> (BasicBlock, BasicBlock)
659where
660F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<()>,
661 {
662let scope = IfThenScope { region_scope, else_drops: DropTree::new() };
663let previous_scope = mem::replace(&mut self.scopes.if_then_scope, Some(scope));
664665let then_block = f(self).into_block();
666667let if_then_scope = mem::replace(&mut self.scopes.if_then_scope, previous_scope).unwrap();
668if !(if_then_scope.region_scope == region_scope) {
::core::panicking::panic("assertion failed: if_then_scope.region_scope == region_scope")
};assert!(if_then_scope.region_scope == region_scope);
669670let else_block =
671self.build_exit_tree(if_then_scope.else_drops, region_scope, span, None).map_or_else(
672 || self.cfg.start_new_block(),
673 |else_block_and| else_block_and.into_block(),
674 );
675676 (then_block, else_block)
677 }
678679/// Convenience wrapper that pushes a scope and then executes `f`
680 /// to build its contents, popping the scope afterwards.
681#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("in_scope",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(681u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["region_scope",
"lint_level"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(®ion_scope)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&lint_level)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: BlockAnd<R> = loop {};
return __tracing_attr_fake_return;
}
{
let source_scope = self.source_scope;
if let LintLevel::Explicit(current_hir_id) = lint_level {
let parent_id =
self.source_scopes[source_scope].local_data.as_ref().unwrap_crate_local().lint_root;
self.maybe_new_source_scope(region_scope.1.span,
current_hir_id, parent_id);
}
self.push_scope(region_scope);
let mut block;
let rv = { let BlockAnd(b, v) = f(self); block = b; v };
block = self.pop_scope(region_scope, block).into_block();
self.source_scope = source_scope;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:702",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(702u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["block"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&block) as
&dyn Value))])
});
} else { ; }
};
block.and(rv)
}
}
}#[instrument(skip(self, f), level = "debug")]682pub(crate) fn in_scope<F, R>(
683&mut self,
684 region_scope: (region::Scope, SourceInfo),
685 lint_level: LintLevel,
686 f: F,
687 ) -> BlockAnd<R>
688where
689F: FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
690 {
691let source_scope = self.source_scope;
692if let LintLevel::Explicit(current_hir_id) = lint_level {
693let parent_id =
694self.source_scopes[source_scope].local_data.as_ref().unwrap_crate_local().lint_root;
695self.maybe_new_source_scope(region_scope.1.span, current_hir_id, parent_id);
696 }
697self.push_scope(region_scope);
698let mut block;
699let rv = unpack!(block = f(self));
700 block = self.pop_scope(region_scope, block).into_block();
701self.source_scope = source_scope;
702debug!(?block);
703 block.and(rv)
704 }
705706/// Convenience wrapper that executes `f` either within the current scope or a new scope.
707 /// Used for pattern matching, which introduces an additional scope for patterns with guards.
708pub(crate) fn opt_in_scope<R>(
709&mut self,
710 opt_region_scope: Option<(region::Scope, SourceInfo)>,
711 f: impl FnOnce(&mut Builder<'a, 'tcx>) -> BlockAnd<R>,
712 ) -> BlockAnd<R> {
713if let Some(region_scope) = opt_region_scope {
714self.in_scope(region_scope, LintLevel::Inherited, f)
715 } else {
716f(self)
717 }
718 }
719720/// Push a scope onto the stack. You can then build code in this
721 /// scope and call `pop_scope` afterwards. Note that these two
722 /// calls must be paired; using `in_scope` as a convenience
723 /// wrapper maybe preferable.
724pub(crate) fn push_scope(&mut self, region_scope: (region::Scope, SourceInfo)) {
725self.scopes.push_scope(region_scope, self.source_scope);
726 }
727728/// Pops a scope, which should have region scope `region_scope`,
729 /// adding any drops onto the end of `block` that are needed.
730 /// This must match 1-to-1 with `push_scope`.
731pub(crate) fn pop_scope(
732&mut self,
733 region_scope: (region::Scope, SourceInfo),
734mut block: BasicBlock,
735 ) -> BlockAnd<()> {
736{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:736",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(736u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("pop_scope({0:?}, {1:?})",
region_scope, block) as &dyn Value))])
});
} else { ; }
};debug!("pop_scope({:?}, {:?})", region_scope, block);
737738block = self.leave_top_scope(block);
739740self.scopes.pop_scope(region_scope);
741742block.unit()
743 }
744745/// Sets up the drops for breaking from `block` to `target`.
746pub(crate) fn break_scope(
747&mut self,
748mut block: BasicBlock,
749 value: Option<ExprId>,
750 target: BreakableTarget,
751 source_info: SourceInfo,
752 ) -> BlockAnd<()> {
753let span = source_info.span;
754755let get_scope_index = |scope: region::Scope| {
756// find the loop-scope by its `region::Scope`.
757self.scopes
758 .breakable_scopes
759 .iter()
760 .rposition(|breakable_scope| breakable_scope.region_scope == scope)
761 .unwrap_or_else(|| ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("no enclosing breakable scope found"))span_bug!(span, "no enclosing breakable scope found"))
762 };
763let (break_index, destination) = match target {
764 BreakableTarget::Return => {
765let scope = &self.scopes.breakable_scopes[0];
766if scope.break_destination != Place::return_place() {
767::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("`return` in item with no return scope"));span_bug!(span, "`return` in item with no return scope");
768 }
769 (0, Some(scope.break_destination))
770 }
771 BreakableTarget::Break(scope) => {
772let break_index = get_scope_index(scope);
773let scope = &self.scopes.breakable_scopes[break_index];
774 (break_index, Some(scope.break_destination))
775 }
776 BreakableTarget::Continue(scope) => {
777let break_index = get_scope_index(scope);
778 (break_index, None)
779 }
780 };
781782match (destination, value) {
783 (Some(destination), Some(value)) => {
784{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:784",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(784u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("stmt_expr Break val block_context.push(SubExpr)")
as &dyn Value))])
});
} else { ; }
};debug!("stmt_expr Break val block_context.push(SubExpr)");
785self.block_context.push(BlockFrame::SubExpr);
786block = self.expr_into_dest(destination, block, value).into_block();
787self.block_context.pop();
788 }
789 (Some(destination), None) => {
790self.cfg.push_assign_unit(block, source_info, destination, self.tcx)
791 }
792 (None, Some(_)) => {
793{
::core::panicking::panic_fmt(format_args!("`return`, `become` and `break` with value and must have a destination"));
}panic!("`return`, `become` and `break` with value and must have a destination")794 }
795 (None, None) => {
796if self.tcx.sess.instrument_coverage() {
797// Normally we wouldn't build any MIR in this case, but that makes it
798 // harder for coverage instrumentation to extract a relevant span for
799 // `continue` expressions. So here we inject a dummy statement with the
800 // desired span.
801self.cfg.push_coverage_span_marker(block, source_info);
802 }
803 }
804 }
805806let region_scope = self.scopes.breakable_scopes[break_index].region_scope;
807let scope_index = self.scopes.scope_index(region_scope, span);
808let drops = if destination.is_some() {
809&mut self.scopes.breakable_scopes[break_index].break_drops
810 } else {
811let Some(drops) = self.scopes.breakable_scopes[break_index].continue_drops.as_mut()
812else {
813self.tcx.dcx().span_delayed_bug(
814source_info.span,
815"unlabelled `continue` within labelled block",
816 );
817self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
818819return self.cfg.start_new_block().unit();
820 };
821drops822 };
823824let mut drop_idx = ROOT_NODE;
825for scope in &self.scopes.scopes[scope_index + 1..] {
826for drop in &scope.drops {
827 drop_idx = drops.add_drop(*drop, drop_idx);
828 }
829 }
830drops.add_entry_point(block, drop_idx);
831832// `build_drop_trees` doesn't have access to our source_info, so we
833 // create a dummy terminator now. `TerminatorKind::UnwindResume` is used
834 // because MIR type checking will panic if it hasn't been overwritten.
835 // (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
836self.cfg.terminate(block, source_info, TerminatorKind::UnwindResume);
837838self.cfg.start_new_block().unit()
839 }
840841/// Based on `FunctionCx::eval_unevaluated_mir_constant_to_valtree`.
842fn eval_unevaluated_mir_constant_to_valtree(
843&self,
844 constant: ConstOperand<'tcx>,
845 ) -> Result<(ty::ValTree<'tcx>, Ty<'tcx>), interpret::ErrorHandled> {
846if !!constant.const_.ty().has_param() {
::core::panicking::panic("assertion failed: !constant.const_.ty().has_param()")
};assert!(!constant.const_.ty().has_param());
847let (uv, ty) = match constant.const_ {
848 mir::Const::Unevaluated(uv, ty) => (uv.shrink(self.tcx), ty),
849 mir::Const::Ty(_, c) => match c.kind() {
850// A constant that came from a const generic but was then used as an argument to
851 // old-style simd_shuffle (passing as argument instead of as a generic param).
852ty::ConstKind::Value(cv) => return Ok((cv.valtree, cv.ty)),
853 other => ::rustc_middle::util::bug::span_bug_fmt(constant.span,
format_args!("{0:#?}", other))span_bug!(constant.span, "{other:#?}"),
854 },
855 mir::Const::Val(mir::ConstValue::Scalar(mir::interpret::Scalar::Int(val)), ty) => {
856return Ok((ValTree::from_scalar_int(self.tcx, val), ty));
857 }
858// We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
859 // a constant and write that value back into `Operand`s. This could happen, but is
860 // unlikely. Also: all users of `simd_shuffle` are on unstable and already need to take
861 // a lot of care around intrinsics. For an issue to happen here, it would require a
862 // macro expanding to a `simd_shuffle` call without wrapping the constant argument in a
863 // `const {}` block, but the user pass through arbitrary expressions.
864865 // FIXME(oli-obk): Replace the magic const generic argument of `simd_shuffle` with a
866 // real const generic, and get rid of this entire function.
867 other => ::rustc_middle::util::bug::span_bug_fmt(constant.span,
format_args!("{0:#?}", other))span_bug!(constant.span, "{other:#?}"),
868 };
869870match self.tcx.const_eval_resolve_for_typeck(self.typing_env(), uv, constant.span) {
871Ok(Ok(valtree)) => Ok((valtree, ty)),
872Ok(Err(ty)) => ::rustc_middle::util::bug::span_bug_fmt(constant.span,
format_args!("could not convert {0:?} to a valtree", ty))span_bug!(constant.span, "could not convert {ty:?} to a valtree"),
873Err(e) => Err(e),
874 }
875 }
876877/// Sets up the drops for jumping from `block` to `scope`.
878pub(crate) fn break_const_continuable_scope(
879&mut self,
880mut block: BasicBlock,
881 value: ExprId,
882 scope: region::Scope,
883 source_info: SourceInfo,
884 ) -> BlockAnd<()> {
885let span = source_info.span;
886887// A break can only break out of a scope, so the value should be a scope.
888let rustc_middle::thir::ExprKind::Scope { value, .. } = self.thir[value].kind else {
889::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("break value must be a scope"))span_bug!(span, "break value must be a scope")890 };
891892let expr = &self.thir[value];
893let constant = match &expr.kind {
894 ExprKind::Adt(AdtExpr { variant_index, fields, base, .. }) => {
895if !#[allow(non_exhaustive_omitted_patterns)] match base {
AdtExprBase::None => true,
_ => false,
} {
::core::panicking::panic("assertion failed: matches!(base, AdtExprBase::None)")
};assert!(matches!(base, AdtExprBase::None));
896if !fields.is_empty() {
::core::panicking::panic("assertion failed: fields.is_empty()")
};assert!(fields.is_empty());
897ConstOperand {
898 span: self.thir[value].span,
899 user_ty: None,
900 const_: Const::Ty(
901self.thir[value].ty,
902 ty::Const::new_value(
903self.tcx,
904ValTree::from_branches(
905self.tcx,
906 [ty::Const::new_value(
907self.tcx,
908ValTree::from_scalar_int(
909self.tcx,
910variant_index.as_u32().into(),
911 ),
912self.tcx.types.u32,
913 )],
914 ),
915self.thir[value].ty,
916 ),
917 ),
918 }
919 }
920921 ExprKind::Literal { .. }
922 | ExprKind::NonHirLiteral { .. }
923 | ExprKind::ZstLiteral { .. }
924 | ExprKind::NamedConst { .. } => self.as_constant(&self.thir[value]),
925926 other => {
927use crate::errors::ConstContinueNotMonomorphicConstReasonas Reason;
928929let span = expr.span;
930let reason = match other {
931 ExprKind::ConstParam { .. } => Reason::ConstantParameter { span },
932 ExprKind::ConstBlock { .. } => Reason::ConstBlock { span },
933_ => Reason::Other { span },
934 };
935936self.tcx
937 .dcx()
938 .emit_err(ConstContinueNotMonomorphicConst { span: expr.span, reason });
939return block.unit();
940 }
941 };
942943let break_index = self944 .scopes
945 .const_continuable_scopes
946 .iter()
947 .rposition(|const_continuable_scope| const_continuable_scope.region_scope == scope)
948 .unwrap_or_else(|| ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("no enclosing const-continuable scope found"))span_bug!(span, "no enclosing const-continuable scope found"));
949950let scope = &self.scopes.const_continuable_scopes[break_index];
951952let state_decl = &self.local_decls[scope.state_place.as_local().unwrap()];
953let state_ty = state_decl.ty;
954let (discriminant_ty, rvalue) = match state_ty.kind() {
955 ty::Adt(adt_def, _) if adt_def.is_enum() => {
956 (state_ty.discriminant_ty(self.tcx), Rvalue::Discriminant(scope.state_place))
957 }
958 ty::Uint(_) | ty::Int(_) | ty::Float(_) | ty::Bool | ty::Char => {
959 (state_ty, Rvalue::Use(Operand::Copy(scope.state_place), WithRetag::Yes))
960 }
961_ => ::rustc_middle::util::bug::span_bug_fmt(state_decl.source_info.span,
format_args!("unsupported #[loop_match] state"))span_bug!(state_decl.source_info.span, "unsupported #[loop_match] state"),
962 };
963964// The `PatCtxt` is normally used in pattern exhaustiveness checking, but reused
965 // here because it performs normalization and const evaluation.
966let dropless_arena = rustc_arena::DroplessArena::default();
967let typeck_results = self.tcx.typeck(self.def_id);
968let cx = RustcPatCtxt {
969 tcx: self.tcx,
970typeck_results,
971 module: self.tcx.parent_module(self.hir_id).to_def_id(),
972// FIXME(#132279): We're in a body, should handle opaques.
973typing_env: rustc_middle::ty::TypingEnv::non_body_analysis(self.tcx, self.def_id),
974 dropless_arena: &dropless_arena,
975 match_lint_level: self.hir_id,
976 whole_match_span: Some(rustc_span::Span::default()),
977 scrut_span: rustc_span::Span::default(),
978 refutable: true,
979 known_valid_scrutinee: true,
980 internal_state: Default::default(),
981 };
982983let valtree = match self.eval_unevaluated_mir_constant_to_valtree(constant) {
984Ok((valtree, ty)) => {
985// Defensively check that the type is monomorphic.
986if !!ty.has_param() {
::core::panicking::panic("assertion failed: !ty.has_param()")
};assert!(!ty.has_param());
987988valtree989 }
990Err(ErrorHandled::Reported(..)) => {
991return block.unit();
992 }
993Err(ErrorHandled::TooGeneric(_)) => {
994self.tcx.dcx().emit_fatal(ConstContinueBadConst { span: constant.span });
995 }
996 };
997998let Some(real_target) =
999self.static_pattern_match(&cx, valtree, &*scope.arms, &scope.built_match_tree)
1000else {
1001self.tcx.dcx().emit_fatal(ConstContinueUnknownJumpTarget { span })
1002 };
10031004self.block_context.push(BlockFrame::SubExpr);
1005let state_place = scope.state_place;
1006block = self.expr_into_dest(state_place, block, value).into_block();
1007self.block_context.pop();
10081009let discr = self.temp(discriminant_ty, source_info.span);
1010let scope_index = self1011 .scopes
1012 .scope_index(self.scopes.const_continuable_scopes[break_index].region_scope, span);
1013let scope = &mut self.scopes.const_continuable_scopes[break_index];
1014self.cfg.push_assign(block, source_info, discr, rvalue);
1015let drop_and_continue_block = self.cfg.start_new_block();
1016let imaginary_target = self.cfg.start_new_block();
1017self.cfg.terminate(
1018block,
1019source_info,
1020 TerminatorKind::FalseEdge { real_target: drop_and_continue_block, imaginary_target },
1021 );
10221023let drops = &mut scope.const_continue_drops;
10241025let drop_idx = self.scopes.scopes[scope_index + 1..]
1026 .iter()
1027 .flat_map(|scope| &scope.drops)
1028 .fold(ROOT_NODE, |drop_idx, &drop| drops.add_drop(drop, drop_idx));
10291030drops.add_entry_point(imaginary_target, drop_idx);
10311032self.cfg.terminate(imaginary_target, source_info, TerminatorKind::UnwindResume);
10331034let region_scope = scope.region_scope;
1035let scope_index = self.scopes.scope_index(region_scope, span);
1036let mut drops = DropTree::new();
10371038let drop_idx = self.scopes.scopes[scope_index + 1..]
1039 .iter()
1040 .flat_map(|scope| &scope.drops)
1041 .fold(ROOT_NODE, |drop_idx, &drop| drops.add_drop(drop, drop_idx));
10421043drops.add_entry_point(drop_and_continue_block, drop_idx);
10441045// `build_drop_trees` doesn't have access to our source_info, so we
1046 // create a dummy terminator now. `TerminatorKind::UnwindResume` is used
1047 // because MIR type checking will panic if it hasn't been overwritten.
1048 // (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
1049self.cfg.terminate(drop_and_continue_block, source_info, TerminatorKind::UnwindResume);
10501051self.build_exit_tree(drops, region_scope, span, Some(real_target));
10521053return self.cfg.start_new_block().unit();
1054 }
10551056/// Sets up the drops for breaking from `block` due to an `if` condition
1057 /// that turned out to be false.
1058 ///
1059 /// Must be called in the context of [`Builder::in_if_then_scope`], so that
1060 /// there is an if-then scope to tell us what the target scope is.
1061pub(crate) fn break_for_else(&mut self, block: BasicBlock, source_info: SourceInfo) {
1062let if_then_scope = self1063 .scopes
1064 .if_then_scope
1065 .as_ref()
1066 .unwrap_or_else(|| ::rustc_middle::util::bug::span_bug_fmt(source_info.span,
format_args!("no if-then scope found"))span_bug!(source_info.span, "no if-then scope found"));
10671068let target = if_then_scope.region_scope;
1069let scope_index = self.scopes.scope_index(target, source_info.span);
10701071// Upgrade `if_then_scope` to `&mut`.
1072let if_then_scope = self.scopes.if_then_scope.as_mut().expect("upgrading & to &mut");
10731074let mut drop_idx = ROOT_NODE;
1075let drops = &mut if_then_scope.else_drops;
1076for scope in &self.scopes.scopes[scope_index + 1..] {
1077for drop in &scope.drops {
1078 drop_idx = drops.add_drop(*drop, drop_idx);
1079 }
1080 }
1081drops.add_entry_point(block, drop_idx);
10821083// `build_drop_trees` doesn't have access to our source_info, so we
1084 // create a dummy terminator now. `TerminatorKind::UnwindResume` is used
1085 // because MIR type checking will panic if it hasn't been overwritten.
1086 // (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
1087self.cfg.terminate(block, source_info, TerminatorKind::UnwindResume);
1088 }
10891090/// Sets up the drops for explicit tail calls.
1091 ///
1092 /// Unlike other kinds of early exits, tail calls do not go through the drop tree.
1093 /// Instead, all scheduled drops are immediately added to the CFG.
1094pub(crate) fn break_for_tail_call(
1095&mut self,
1096mut block: BasicBlock,
1097 args: &[Spanned<Operand<'tcx>>],
1098 source_info: SourceInfo,
1099 ) -> BlockAnd<()> {
1100let arg_drops: Vec<_> = args1101 .iter()
1102 .rev()
1103 .filter_map(|arg| match &arg.node {
1104 Operand::Copy(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("copy op in tail call args"))bug!("copy op in tail call args"),
1105 Operand::Move(place) => {
1106let local =
1107place.as_local().unwrap_or_else(|| ::rustc_middle::util::bug::bug_fmt(format_args!("projection in tail call args"))bug!("projection in tail call args"));
11081109if !self.local_decls[local].ty.needs_drop(self.tcx, self.typing_env()) {
1110return None;
1111 }
11121113Some(DropData { source_info, local, kind: DropKind::Value })
1114 }
1115 Operand::Constant(_) | Operand::RuntimeChecks(_) => None,
1116 })
1117 .collect();
11181119let mut unwind_to = self.diverge_cleanup_target(
1120self.scopes.scopes.iter().rev().nth(1).unwrap().region_scope,
1121DUMMY_SP,
1122 );
1123let typing_env = self.typing_env();
1124let unwind_drops = &mut self.scopes.unwind_drops;
11251126// the innermost scope contains only the destructors for the tail call arguments
1127 // we only want to drop these in case of a panic, so we skip it
1128for scope in self.scopes.scopes[1..].iter().rev().skip(1) {
1129// FIXME(explicit_tail_calls) code duplication with `build_scope_drops`
1130for drop_data in scope.drops.iter().rev() {
1131let source_info = drop_data.source_info;
1132let local = drop_data.local;
11331134if !self.local_decls[local].ty.needs_drop(self.tcx, typing_env) {
1135continue;
1136 }
11371138match drop_data.kind {
1139 DropKind::Value => {
1140// `unwind_to` should drop the value that we're about to
1141 // schedule. If dropping this value panics, then we continue
1142 // with the *next* value on the unwind path.
1143if true {
match (&unwind_drops.drop_nodes[unwind_to].data.local, &drop_data.local) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(
1144 unwind_drops.drop_nodes[unwind_to].data.local,
1145 drop_data.local
1146 );
1147if true {
match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(
1148 unwind_drops.drop_nodes[unwind_to].data.kind,
1149 drop_data.kind
1150 );
1151 unwind_to = unwind_drops.drop_nodes[unwind_to].next;
11521153let mut unwind_entry_point = unwind_to;
11541155// the tail call arguments must be dropped if any of these drops panic
1156for drop in arg_drops.iter().copied() {
1157 unwind_entry_point = unwind_drops.add_drop(drop, unwind_entry_point);
1158 }
11591160 unwind_drops.add_entry_point(block, unwind_entry_point);
11611162let next = self.cfg.start_new_block();
1163self.cfg.terminate(
1164 block,
1165 source_info,
1166 TerminatorKind::Drop {
1167 place: local.into(),
1168 target: next,
1169 unwind: UnwindAction::Continue,
1170 replace: false,
1171 drop: None,
1172 },
1173 );
1174 block = next;
1175 }
1176 DropKind::ForLint => {
1177self.cfg.push(
1178 block,
1179 Statement::new(
1180 source_info,
1181 StatementKind::BackwardIncompatibleDropHint {
1182 place: Box::new(local.into()),
1183 reason: BackwardIncompatibleDropReason::Edition2024,
1184 },
1185 ),
1186 );
1187 }
1188 DropKind::Storage => {
1189// Only temps and vars need their storage dead.
1190if !(local.index() > self.arg_count) {
::core::panicking::panic("assertion failed: local.index() > self.arg_count")
};assert!(local.index() > self.arg_count);
1191self.cfg.push(
1192 block,
1193 Statement::new(source_info, StatementKind::StorageDead(local)),
1194 );
1195 }
1196 }
1197 }
1198 }
11991200block.unit()
1201 }
12021203fn is_async_drop_impl(
1204 tcx: TyCtxt<'tcx>,
1205 local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
1206 typing_env: ty::TypingEnv<'tcx>,
1207 local: Local,
1208 ) -> bool {
1209let ty = local_decls[local].ty;
1210if ty.is_async_drop(tcx, typing_env) || ty.is_coroutine() {
1211return true;
1212 }
1213ty.needs_async_drop(tcx, typing_env)
1214 }
1215fn is_async_drop(&self, local: Local) -> bool {
1216Self::is_async_drop_impl(self.tcx, &self.local_decls, self.typing_env(), local)
1217 }
12181219fn leave_top_scope(&mut self, block: BasicBlock) -> BasicBlock {
1220// If we are emitting a `drop` statement, we need to have the cached
1221 // diverge cleanup pads ready in case that drop panics.
1222let needs_cleanup = self.scopes.scopes.last().is_some_and(|scope| scope.needs_cleanup());
1223let is_coroutine = self.coroutine.is_some();
1224let unwind_to = if needs_cleanup { self.diverge_cleanup() } else { DropIdx::MAX };
12251226let scope = self.scopes.scopes.last().expect("leave_top_scope called with no scopes");
1227let has_async_drops = is_coroutine1228 && scope.drops.iter().any(|v| v.kind == DropKind::Value && self.is_async_drop(v.local));
1229let dropline_to = if has_async_drops { Some(self.diverge_dropline()) } else { None };
1230let scope = self.scopes.scopes.last().expect("leave_top_scope called with no scopes");
1231let typing_env = self.typing_env();
1232build_scope_drops(
1233&mut self.cfg,
1234&mut self.scopes.unwind_drops,
1235&mut self.scopes.coroutine_drops,
1236scope,
1237block,
1238unwind_to,
1239dropline_to,
1240is_coroutine && needs_cleanup,
1241self.arg_count,
1242 |v: Local| Self::is_async_drop_impl(self.tcx, &self.local_decls, typing_env, v),
1243 )
1244 .into_block()
1245 }
12461247/// Possibly creates a new source scope if `current_root` and `parent_root`
1248 /// are different, or if -Zmaximal-hir-to-mir-coverage is enabled.
1249pub(crate) fn maybe_new_source_scope(
1250&mut self,
1251 span: Span,
1252 current_id: HirId,
1253 parent_id: HirId,
1254 ) {
1255let (current_root, parent_root) =
1256if self.tcx.sess.opts.unstable_opts.maximal_hir_to_mir_coverage {
1257// Some consumers of rustc need to map MIR locations back to HIR nodes. Currently
1258 // the only part of rustc that tracks MIR -> HIR is the
1259 // `SourceScopeLocalData::lint_root` field that tracks lint levels for MIR
1260 // locations. Normally the number of source scopes is limited to the set of nodes
1261 // with lint annotations. The -Zmaximal-hir-to-mir-coverage flag changes this
1262 // behavior to maximize the number of source scopes, increasing the granularity of
1263 // the MIR->HIR mapping.
1264(current_id, parent_id)
1265 } else {
1266// Use `maybe_lint_level_root_bounded` to avoid adding Hir dependencies on our
1267 // parents. We estimate the true lint roots here to avoid creating a lot of source
1268 // scopes.
1269(
1270self.maybe_lint_level_root_bounded(current_id),
1271if parent_id == self.hir_id {
1272parent_id// this is very common
1273} else {
1274self.maybe_lint_level_root_bounded(parent_id)
1275 },
1276 )
1277 };
12781279if current_root != parent_root {
1280let lint_level = LintLevel::Explicit(current_root);
1281self.source_scope = self.new_source_scope(span, lint_level);
1282 }
1283 }
12841285/// Walks upwards from `orig_id` to find a node which might change lint levels with attributes.
1286 /// It stops at `self.hir_id` and just returns it if reached.
1287fn maybe_lint_level_root_bounded(&mut self, orig_id: HirId) -> HirId {
1288// This assertion lets us just store `ItemLocalId` in the cache, rather
1289 // than the full `HirId`.
1290match (&orig_id.owner, &self.hir_id.owner) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(orig_id.owner, self.hir_id.owner);
12911292let mut id = orig_id;
1293loop {
1294if id == self.hir_id {
1295// This is a moderately common case, mostly hit for previously unseen nodes.
1296break;
1297 }
12981299if self1300 .tcx
1301 .hir_attrs(id)
1302 .iter()
1303 .any(|attr| Level::from_opt_symbol(attr.name()).is_some())
1304 {
1305// This is a rare case. It's for a node path that doesn't reach the root due to an
1306 // intervening lint level attribute. This result doesn't get cached.
1307return id;
1308 }
13091310let next = self.tcx.parent_hir_id(id);
1311if next == id {
1312::rustc_middle::util::bug::bug_fmt(format_args!("lint traversal reached the root of the crate"));bug!("lint traversal reached the root of the crate");
1313 }
1314id = next;
13151316// This lookup is just an optimization; it can be removed without affecting
1317 // functionality. It might seem strange to see this at the end of this loop, but the
1318 // `orig_id` passed in to this function is almost always previously unseen, for which a
1319 // lookup will be a miss. So we only do lookups for nodes up the parent chain, where
1320 // cache lookups have a very high hit rate.
1321if self.lint_level_roots_cache.contains(id.local_id) {
1322break;
1323 }
1324 }
13251326// `orig_id` traced to `self_id`; record this fact. If `orig_id` is a leaf node it will
1327 // rarely (never?) subsequently be searched for, but it's hard to know if that is the case.
1328 // The performance wins from the cache all come from caching non-leaf nodes.
1329self.lint_level_roots_cache.insert(orig_id.local_id);
1330self.hir_id
1331 }
13321333/// Creates a new source scope, nested in the current one.
1334pub(crate) fn new_source_scope(&mut self, span: Span, lint_level: LintLevel) -> SourceScope {
1335let parent = self.source_scope;
1336{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:1336",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(1336u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("new_source_scope({0:?}, {1:?}) - parent({2:?})={3:?}",
span, lint_level, parent, self.source_scopes.get(parent)) as
&dyn Value))])
});
} else { ; }
};debug!(
1337"new_source_scope({:?}, {:?}) - parent({:?})={:?}",
1338 span,
1339 lint_level,
1340 parent,
1341self.source_scopes.get(parent)
1342 );
1343let scope_local_data = SourceScopeLocalData {
1344 lint_root: if let LintLevel::Explicit(lint_root) = lint_level {
1345lint_root1346 } else {
1347self.source_scopes[parent].local_data.as_ref().unwrap_crate_local().lint_root
1348 },
1349 };
1350self.source_scopes.push(SourceScopeData {
1351span,
1352 parent_scope: Some(parent),
1353 inlined: None,
1354 inlined_parent_scope: None,
1355 local_data: ClearCrossCrate::Set(scope_local_data),
1356 })
1357 }
13581359/// Given a span and the current source scope, make a SourceInfo.
1360pub(crate) fn source_info(&self, span: Span) -> SourceInfo {
1361SourceInfo { span, scope: self.source_scope }
1362 }
13631364// Finding scopes
1365 // ==============
13661367/// Returns the scope that we should use as the lifetime of an
1368 /// operand. Basically, an operand must live until it is consumed.
1369 /// This is similar to, but not quite the same as, the temporary
1370 /// scope (which can be larger or smaller).
1371 ///
1372 /// Consider:
1373 /// ```ignore (illustrative)
1374 /// let x = foo(bar(X, Y));
1375 /// ```
1376 /// We wish to pop the storage for X and Y after `bar()` is
1377 /// called, not after the whole `let` is completed.
1378 ///
1379 /// As another example, if the second argument diverges:
1380 /// ```ignore (illustrative)
1381 /// foo(Box::new(2), panic!())
1382 /// ```
1383 /// We would allocate the box but then free it on the unwinding
1384 /// path; we would also emit a free on the 'success' path from
1385 /// panic, but that will turn out to be removed as dead-code.
1386pub(crate) fn local_scope(&self) -> region::Scope {
1387self.scopes.topmost()
1388 }
13891390// Scheduling drops
1391 // ================
13921393pub(crate) fn schedule_drop_storage_and_value(
1394&mut self,
1395 span: Span,
1396 region_scope: region::Scope,
1397 local: Local,
1398 ) {
1399self.schedule_drop(span, region_scope, local, DropKind::Storage);
1400self.schedule_drop(span, region_scope, local, DropKind::Value);
1401 }
14021403/// Indicates that `place` should be dropped on exit from `region_scope`.
1404 ///
1405 /// When called with `DropKind::Storage`, `place` shouldn't be the return
1406 /// place, or a function parameter.
1407pub(crate) fn schedule_drop(
1408&mut self,
1409 span: Span,
1410 region_scope: region::Scope,
1411 local: Local,
1412 drop_kind: DropKind,
1413 ) {
1414let needs_drop = match drop_kind {
1415 DropKind::Value | DropKind::ForLint => {
1416if !self.local_decls[local].ty.needs_drop(self.tcx, self.typing_env()) {
1417return;
1418 }
1419true
1420}
1421 DropKind::Storage => {
1422if local.index() <= self.arg_count {
1423::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("`schedule_drop` called with body argument {0:?} but its storage does not require a drop",
local))span_bug!(
1424 span,
1425"`schedule_drop` called with body argument {:?} \
1426 but its storage does not require a drop",
1427 local,
1428 )1429 }
1430false
1431}
1432 };
14331434// When building drops, we try to cache chains of drops to reduce the
1435 // number of `DropTree::add_drop` calls. This, however, means that
1436 // whenever we add a drop into a scope which already had some entries
1437 // in the drop tree built (and thus, cached) for it, we must invalidate
1438 // all caches which might branch into the scope which had a drop just
1439 // added to it. This is necessary, because otherwise some other code
1440 // might use the cache to branch into already built chain of drops,
1441 // essentially ignoring the newly added drop.
1442 //
1443 // For example consider there’s two scopes with a drop in each. These
1444 // are built and thus the caches are filled:
1445 //
1446 // +--------------------------------------------------------+
1447 // | +---------------------------------+ |
1448 // | | +--------+ +-------------+ | +---------------+ |
1449 // | | | return | <-+ | drop(outer) | <-+ | drop(middle) | |
1450 // | | +--------+ +-------------+ | +---------------+ |
1451 // | +------------|outer_scope cache|--+ |
1452 // +------------------------------|middle_scope cache|------+
1453 //
1454 // Now, a new, innermost scope is added along with a new drop into
1455 // both innermost and outermost scopes:
1456 //
1457 // +------------------------------------------------------------+
1458 // | +----------------------------------+ |
1459 // | | +--------+ +-------------+ | +---------------+ | +-------------+
1460 // | | | return | <+ | drop(new) | <-+ | drop(middle) | <--+| drop(inner) |
1461 // | | +--------+ | | drop(outer) | | +---------------+ | +-------------+
1462 // | | +-+ +-------------+ | |
1463 // | +---|invalid outer_scope cache|----+ |
1464 // +----=----------------|invalid middle_scope cache|-----------+
1465 //
1466 // If, when adding `drop(new)` we do not invalidate the cached blocks for both
1467 // outer_scope and middle_scope, then, when building drops for the inner (rightmost)
1468 // scope, the old, cached blocks, without `drop(new)` will get used, producing the
1469 // wrong results.
1470 //
1471 // Note that this code iterates scopes from the innermost to the outermost,
1472 // invalidating caches of each scope visited. This way bare minimum of the
1473 // caches gets invalidated. i.e., if a new drop is added into the middle scope, the
1474 // cache of outer scope stays intact.
1475 //
1476 // Since we only cache drops for the unwind path and the coroutine drop
1477 // path, we only need to invalidate the cache for drops that happen on
1478 // the unwind or coroutine drop paths. This means that for
1479 // non-coroutines we don't need to invalidate caches for `DropKind::Storage`.
1480let invalidate_caches = needs_drop || self.coroutine.is_some();
1481for scope in self.scopes.scopes.iter_mut().rev() {
1482if invalidate_caches {
1483 scope.invalidate_cache();
1484 }
14851486if scope.region_scope == region_scope {
1487let region_scope_span = region_scope.span(self.tcx, self.region_scope_tree);
1488// Attribute scope exit drops to scope's closing brace.
1489let scope_end = self.tcx.sess.source_map().end_point(region_scope_span);
14901491 scope.drops.push(DropData {
1492 source_info: SourceInfo { span: scope_end, scope: scope.source_scope },
1493 local,
1494 kind: drop_kind,
1495 });
14961497return;
1498 }
1499 }
15001501::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("region scope {0:?} not in scope to drop {1:?}",
region_scope, local));span_bug!(span, "region scope {:?} not in scope to drop {:?}", region_scope, local);
1502 }
15031504/// Schedule emission of a backwards incompatible drop lint hint.
1505 /// Applicable only to temporary values for now.
1506#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("schedule_backwards_incompatible_drop",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(1506u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["span",
"region_scope", "local"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(®ion_scope)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&local)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
for scope in self.scopes.scopes.iter_mut().rev() {
scope.invalidate_cache();
if scope.region_scope == region_scope {
let region_scope_span =
region_scope.span(self.tcx, self.region_scope_tree);
let scope_end =
self.tcx.sess.source_map().end_point(region_scope_span);
scope.drops.push(DropData {
source_info: SourceInfo {
span: scope_end,
scope: scope.source_scope,
},
local,
kind: DropKind::ForLint,
});
return;
}
}
::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("region scope {0:?} not in scope to drop {1:?} for linting",
region_scope, local));
}
}
}#[instrument(level = "debug", skip(self))]1507pub(crate) fn schedule_backwards_incompatible_drop(
1508&mut self,
1509 span: Span,
1510 region_scope: region::Scope,
1511 local: Local,
1512 ) {
1513// Note that we are *not* gating BIDs here on whether they have significant destructor.
1514 // We need to know all of them so that we can capture potential borrow-checking errors.
1515for scope in self.scopes.scopes.iter_mut().rev() {
1516// Since we are inserting linting MIR statement, we have to invalidate the caches
1517scope.invalidate_cache();
1518if scope.region_scope == region_scope {
1519let region_scope_span = region_scope.span(self.tcx, self.region_scope_tree);
1520let scope_end = self.tcx.sess.source_map().end_point(region_scope_span);
15211522 scope.drops.push(DropData {
1523 source_info: SourceInfo { span: scope_end, scope: scope.source_scope },
1524 local,
1525 kind: DropKind::ForLint,
1526 });
15271528return;
1529 }
1530 }
1531span_bug!(
1532 span,
1533"region scope {:?} not in scope to drop {:?} for linting",
1534 region_scope,
1535 local
1536 );
1537 }
15381539/// Indicates that the "local operand" stored in `local` is
1540 /// *moved* at some point during execution (see `local_scope` for
1541 /// more information about what a "local operand" is -- in short,
1542 /// it's an intermediate operand created as part of preparing some
1543 /// MIR instruction). We use this information to suppress
1544 /// redundant drops on the non-unwind paths. This results in less
1545 /// MIR, but also avoids spurious borrow check errors
1546 /// (c.f. #64391).
1547 ///
1548 /// Example: when compiling the call to `foo` here:
1549 ///
1550 /// ```ignore (illustrative)
1551 /// foo(bar(), ...)
1552 /// ```
1553 ///
1554 /// we would evaluate `bar()` to an operand `_X`. We would also
1555 /// schedule `_X` to be dropped when the expression scope for
1556 /// `foo(bar())` is exited. This is relevant, for example, if the
1557 /// later arguments should unwind (it would ensure that `_X` gets
1558 /// dropped). However, if no unwind occurs, then `_X` will be
1559 /// unconditionally consumed by the `call`:
1560 ///
1561 /// ```ignore (illustrative)
1562 /// bb {
1563 /// ...
1564 /// _R = CALL(foo, _X, ...)
1565 /// }
1566 /// ```
1567 ///
1568 /// However, `_X` is still registered to be dropped, and so if we
1569 /// do nothing else, we would generate a `DROP(_X)` that occurs
1570 /// after the call. This will later be optimized out by the
1571 /// drop-elaboration code, but in the meantime it can lead to
1572 /// spurious borrow-check errors -- the problem, ironically, is
1573 /// not the `DROP(_X)` itself, but the (spurious) unwind pathways
1574 /// that it creates. See #64391 for an example.
1575pub(crate) fn record_operands_moved(&mut self, operands: &[Spanned<Operand<'tcx>>]) {
1576let local_scope = self.local_scope();
1577let scope = self.scopes.scopes.last_mut().unwrap();
15781579match (&scope.region_scope, &local_scope) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("local scope is not the topmost scope!")));
}
}
};assert_eq!(scope.region_scope, local_scope, "local scope is not the topmost scope!",);
15801581// look for moves of a local variable, like `MOVE(_X)`
1582let locals_moved = operands.iter().flat_map(|operand| match operand.node {
1583 Operand::Copy(_) | Operand::Constant(_) | Operand::RuntimeChecks(_) => None,
1584 Operand::Move(place) => place.as_local(),
1585 });
15861587for local in locals_moved {
1588// check if we have a Drop for this operand and -- if so
1589 // -- add it to the list of moved operands. Note that this
1590 // local might not have been an operand created for this
1591 // call, it could come from other places too.
1592if scope.drops.iter().any(|drop| drop.local == local && drop.kind == DropKind::Value) {
1593 scope.moved_locals.push(local);
1594 }
1595 }
1596 }
15971598// Other
1599 // =====
16001601/// Returns the [DropIdx] for the innermost drop if the function unwound at
1602 /// this point. The `DropIdx` will be created if it doesn't already exist.
1603fn diverge_cleanup(&mut self) -> DropIdx {
1604// It is okay to use dummy span because the getting scope index on the topmost scope
1605 // must always succeed.
1606self.diverge_cleanup_target(self.scopes.topmost(), DUMMY_SP)
1607 }
16081609/// This is similar to [diverge_cleanup](Self::diverge_cleanup) except its target is set to
1610 /// some ancestor scope instead of the current scope.
1611 /// It is possible to unwind to some ancestor scope if some drop panics as
1612 /// the program breaks out of a if-then scope.
1613fn diverge_cleanup_target(&mut self, target_scope: region::Scope, span: Span) -> DropIdx {
1614let target = self.scopes.scope_index(target_scope, span);
1615let (uncached_scope, mut cached_drop) = self.scopes.scopes[..=target]
1616 .iter()
1617 .enumerate()
1618 .rev()
1619 .find_map(|(scope_idx, scope)| {
1620scope.cached_unwind_block.map(|cached_block| (scope_idx + 1, cached_block))
1621 })
1622 .unwrap_or((0, ROOT_NODE));
16231624if uncached_scope > target {
1625return cached_drop;
1626 }
16271628let is_coroutine = self.coroutine.is_some();
1629for scope in &mut self.scopes.scopes[uncached_scope..=target] {
1630for drop in &scope.drops {
1631if is_coroutine || drop.kind == DropKind::Value {
1632 cached_drop = self.scopes.unwind_drops.add_drop(*drop, cached_drop);
1633 }
1634 }
1635 scope.cached_unwind_block = Some(cached_drop);
1636 }
16371638cached_drop1639 }
16401641/// Prepares to create a path that performs all required cleanup for a
1642 /// terminator that can unwind at the given basic block.
1643 ///
1644 /// This path terminates in Resume. The path isn't created until after all
1645 /// of the non-unwind paths in this item have been lowered.
1646pub(crate) fn diverge_from(&mut self, start: BasicBlock) {
1647if true {
if !#[allow(non_exhaustive_omitted_patterns)] match self.cfg.block_data(start).terminator().kind
{
TerminatorKind::Assert { .. } | TerminatorKind::Call { .. } |
TerminatorKind::Drop { .. } | TerminatorKind::FalseUnwind {
.. } | TerminatorKind::InlineAsm { .. } => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("diverge_from called on block with terminator that cannot unwind."));
}
};
};debug_assert!(
1648matches!(
1649self.cfg.block_data(start).terminator().kind,
1650 TerminatorKind::Assert { .. }
1651 | TerminatorKind::Call { .. }
1652 | TerminatorKind::Drop { .. }
1653 | TerminatorKind::FalseUnwind { .. }
1654 | TerminatorKind::InlineAsm { .. }
1655 ),
1656"diverge_from called on block with terminator that cannot unwind."
1657);
16581659let next_drop = self.diverge_cleanup();
1660self.scopes.unwind_drops.add_entry_point(start, next_drop);
1661 }
16621663/// Returns the [DropIdx] for the innermost drop for dropline (coroutine drop path).
1664 /// The `DropIdx` will be created if it doesn't already exist.
1665fn diverge_dropline(&mut self) -> DropIdx {
1666// It is okay to use dummy span because the getting scope index on the topmost scope
1667 // must always succeed.
1668self.diverge_dropline_target(self.scopes.topmost(), DUMMY_SP)
1669 }
16701671/// Similar to diverge_cleanup_target, but for dropline (coroutine drop path)
1672fn diverge_dropline_target(&mut self, target_scope: region::Scope, span: Span) -> DropIdx {
1673if true {
if !self.coroutine.is_some() {
{
::core::panicking::panic_fmt(format_args!("diverge_dropline_target is valid only for coroutine"));
}
};
};debug_assert!(
1674self.coroutine.is_some(),
1675"diverge_dropline_target is valid only for coroutine"
1676);
1677let target = self.scopes.scope_index(target_scope, span);
1678let (uncached_scope, mut cached_drop) = self.scopes.scopes[..=target]
1679 .iter()
1680 .enumerate()
1681 .rev()
1682 .find_map(|(scope_idx, scope)| {
1683scope.cached_coroutine_drop_block.map(|cached_block| (scope_idx + 1, cached_block))
1684 })
1685 .unwrap_or((0, ROOT_NODE));
16861687if uncached_scope > target {
1688return cached_drop;
1689 }
16901691for scope in &mut self.scopes.scopes[uncached_scope..=target] {
1692for drop in &scope.drops {
1693 cached_drop = self.scopes.coroutine_drops.add_drop(*drop, cached_drop);
1694 }
1695 scope.cached_coroutine_drop_block = Some(cached_drop);
1696 }
16971698cached_drop1699 }
17001701/// Sets up a path that performs all required cleanup for dropping a
1702 /// coroutine, starting from the given block that ends in
1703 /// [TerminatorKind::Yield].
1704 ///
1705 /// This path terminates in CoroutineDrop.
1706pub(crate) fn coroutine_drop_cleanup(&mut self, yield_block: BasicBlock) {
1707if true {
if !#[allow(non_exhaustive_omitted_patterns)] match self.cfg.block_data(yield_block).terminator().kind
{
TerminatorKind::Yield { .. } => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("coroutine_drop_cleanup called on block with non-yield terminator."));
}
};
};debug_assert!(
1708matches!(
1709self.cfg.block_data(yield_block).terminator().kind,
1710 TerminatorKind::Yield { .. }
1711 ),
1712"coroutine_drop_cleanup called on block with non-yield terminator."
1713);
1714let cached_drop = self.diverge_dropline();
1715self.scopes.coroutine_drops.add_entry_point(yield_block, cached_drop);
1716 }
17171718/// Utility function for *non*-scope code to build their own drops
1719 /// Force a drop at this point in the MIR by creating a new block.
1720pub(crate) fn build_drop_and_replace(
1721&mut self,
1722 block: BasicBlock,
1723 span: Span,
1724 place: Place<'tcx>,
1725 value: Rvalue<'tcx>,
1726 ) -> BlockAnd<()> {
1727let source_info = self.source_info(span);
17281729// create the new block for the assignment
1730let assign = self.cfg.start_new_block();
1731self.cfg.push_assign(assign, source_info, place, value.clone());
17321733// create the new block for the assignment in the case of unwinding
1734let assign_unwind = self.cfg.start_new_cleanup_block();
1735self.cfg.push_assign(assign_unwind, source_info, place, value.clone());
17361737self.cfg.terminate(
1738block,
1739source_info,
1740 TerminatorKind::Drop {
1741place,
1742 target: assign,
1743 unwind: UnwindAction::Cleanup(assign_unwind),
1744 replace: true,
1745 drop: None,
1746 },
1747 );
1748self.diverge_from(block);
17491750assign.unit()
1751 }
17521753/// Creates an `Assert` terminator and return the success block.
1754 /// If the boolean condition operand is not the expected value,
1755 /// a runtime panic will be caused with the given message.
1756pub(crate) fn assert(
1757&mut self,
1758 block: BasicBlock,
1759 cond: Operand<'tcx>,
1760 expected: bool,
1761 msg: AssertMessage<'tcx>,
1762 span: Span,
1763 ) -> BasicBlock {
1764let source_info = self.source_info(span);
1765let success_block = self.cfg.start_new_block();
17661767self.cfg.terminate(
1768block,
1769source_info,
1770 TerminatorKind::Assert {
1771cond,
1772expected,
1773 msg: Box::new(msg),
1774 target: success_block,
1775 unwind: UnwindAction::Continue,
1776 },
1777 );
1778self.diverge_from(block);
17791780success_block1781 }
17821783/// Unschedules any drops in the top two scopes.
1784 ///
1785 /// This is only needed for pattern-matches combining guards and or-patterns: or-patterns lead
1786 /// to guards being lowered multiple times before lowering the arm body, so we unschedle drops
1787 /// for guards' temporaries and bindings between lowering each instance of an match arm's guard.
1788pub(crate) fn clear_match_arm_and_guard_scopes(&mut self, region_scope: region::Scope) {
1789let [.., arm_scope, guard_scope] = &mut *self.scopes.scopes else {
1790::rustc_middle::util::bug::bug_fmt(format_args!("matches with guards should introduce separate scopes for the pattern and guard"));bug!("matches with guards should introduce separate scopes for the pattern and guard");
1791 };
17921793match (&arm_scope.region_scope, ®ion_scope) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(arm_scope.region_scope, region_scope);
1794match (&guard_scope.region_scope.data, ®ion::ScopeData::MatchGuard) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(guard_scope.region_scope.data, region::ScopeData::MatchGuard);
1795match (&guard_scope.region_scope.local_id, ®ion_scope.local_id) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(guard_scope.region_scope.local_id, region_scope.local_id);
17961797arm_scope.drops.clear();
1798arm_scope.invalidate_cache();
1799guard_scope.drops.clear();
1800guard_scope.invalidate_cache();
1801 }
1802}
18031804/// Builds drops for `pop_scope` and `leave_top_scope`.
1805///
1806/// # Parameters
1807///
1808/// * `unwind_drops`, the drop tree data structure storing what needs to be cleaned up if unwind occurs
1809/// * `scope`, describes the drops that will occur on exiting the scope in regular execution
1810/// * `block`, the block to branch to once drops are complete (assuming no unwind occurs)
1811/// * `unwind_to`, describes the drops that would occur at this point in the code if a
1812/// panic occurred (a subset of the drops in `scope`, since we sometimes elide StorageDead and other
1813/// instructions on unwinding)
1814/// * `dropline_to`, describes the drops that would occur at this point in the code if a
1815/// coroutine drop occurred.
1816/// * `storage_dead_on_unwind`, if true, then we should emit `StorageDead` even when unwinding
1817/// * `arg_count`, number of MIR local variables corresponding to fn arguments (used to assert that we don't drop those)
1818fn build_scope_drops<'tcx, F>(
1819 cfg: &mut CFG<'tcx>,
1820 unwind_drops: &mut DropTree,
1821 coroutine_drops: &mut DropTree,
1822 scope: &Scope,
1823 block: BasicBlock,
1824 unwind_to: DropIdx,
1825 dropline_to: Option<DropIdx>,
1826 storage_dead_on_unwind: bool,
1827 arg_count: usize,
1828 is_async_drop: F,
1829) -> BlockAnd<()>
1830where
1831F: Fn(Local) -> bool,
1832{
1833{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/scope.rs:1833",
"rustc_mir_build::builder::scope", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/scope.rs"),
::tracing_core::__macro_support::Option::Some(1833u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::scope"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("build_scope_drops({0:?} -> {1:?}), dropline_to={2:?}",
block, scope, dropline_to) as &dyn Value))])
});
} else { ; }
};debug!("build_scope_drops({:?} -> {:?}), dropline_to={:?}", block, scope, dropline_to);
18341835// Build up the drops in evaluation order. The end result will
1836 // look like:
1837 //
1838 // [SDs, drops[n]] --..> [SDs, drop[1]] -> [SDs, drop[0]] -> [[SDs]]
1839 // | | |
1840 // : | |
1841 // V V
1842 // [drop[n]] -...-> [drop[1]] ------> [drop[0]] ------> [last_unwind_to]
1843 //
1844 // The horizontal arrows represent the execution path when the drops return
1845 // successfully. The downwards arrows represent the execution path when the
1846 // drops panic (panicking while unwinding will abort, so there's no need for
1847 // another set of arrows).
1848 //
1849 // For coroutines, we unwind from a drop on a local to its StorageDead
1850 // statement. For other functions we don't worry about StorageDead. The
1851 // drops for the unwind path should have already been generated by
1852 // `diverge_cleanup_gen`.
18531854 // `unwind_to` indicates what needs to be dropped should unwinding occur.
1855 // This is a subset of what needs to be dropped when exiting the scope.
1856 // As we unwind the scope, we will also move `unwind_to` backwards to match,
1857 // so that we can use it should a destructor panic.
1858let mut unwind_to = unwind_to;
18591860// The block that we should jump to after drops complete. We start by building the final drop (`drops[n]`
1861 // in the diagram above) and then build the drops (e.g., `drop[1]`, `drop[0]`) that come before it.
1862 // block begins as the successor of `drops[n]` and then becomes `drops[n]` so that `drops[n-1]`
1863 // will branch to `drops[n]`.
1864let mut block = block;
18651866// `dropline_to` indicates what needs to be dropped should coroutine drop occur.
1867let mut dropline_to = dropline_to;
18681869for drop_data in scope.drops.iter().rev() {
1870let source_info = drop_data.source_info;
1871let local = drop_data.local;
18721873match drop_data.kind {
1874 DropKind::Value => {
1875// `unwind_to` should drop the value that we're about to
1876 // schedule. If dropping this value panics, then we continue
1877 // with the *next* value on the unwind path.
1878 //
1879 // We adjust this BEFORE we create the drop (e.g., `drops[n]`)
1880 // because `drops[n]` should unwind to `drops[n-1]`.
1881if true {
match (&unwind_drops.drop_nodes[unwind_to].data.local, &drop_data.local) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.local, drop_data.local);
1882if true {
match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.kind, drop_data.kind);
1883 unwind_to = unwind_drops.drop_nodes[unwind_to].next;
18841885if let Some(idx) = dropline_to {
1886if true {
match (&coroutine_drops.drop_nodes[idx].data.local, &drop_data.local) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.local, drop_data.local);
1887if true {
match (&coroutine_drops.drop_nodes[idx].data.kind, &drop_data.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.kind, drop_data.kind);
1888 dropline_to = Some(coroutine_drops.drop_nodes[idx].next);
1889 }
18901891// If the operand has been moved, and we are not on an unwind
1892 // path, then don't generate the drop. (We only take this into
1893 // account for non-unwind paths so as not to disturb the
1894 // caching mechanism.)
1895if scope.moved_locals.contains(&local) {
1896continue;
1897 }
18981899 unwind_drops.add_entry_point(block, unwind_to);
1900if let Some(to) = dropline_to
1901 && is_async_drop(local)
1902 {
1903 coroutine_drops.add_entry_point(block, to);
1904 }
19051906let next = cfg.start_new_block();
1907 cfg.terminate(
1908 block,
1909 source_info,
1910 TerminatorKind::Drop {
1911 place: local.into(),
1912 target: next,
1913 unwind: UnwindAction::Continue,
1914 replace: false,
1915 drop: None,
1916 },
1917 );
1918 block = next;
1919 }
1920 DropKind::ForLint => {
1921// As in the `DropKind::Storage` case below:
1922 // normally lint-related drops are not emitted for unwind,
1923 // so we can just leave `unwind_to` unmodified, but in some
1924 // cases we emit things ALSO on the unwind path, so we need to adjust
1925 // `unwind_to` in that case.
1926if storage_dead_on_unwind {
1927if true {
match (&unwind_drops.drop_nodes[unwind_to].data.local, &drop_data.local) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(
1928 unwind_drops.drop_nodes[unwind_to].data.local,
1929 drop_data.local
1930 );
1931if true {
match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.kind, drop_data.kind);
1932 unwind_to = unwind_drops.drop_nodes[unwind_to].next;
1933 }
19341935// If the operand has been moved, and we are not on an unwind
1936 // path, then don't generate the drop. (We only take this into
1937 // account for non-unwind paths so as not to disturb the
1938 // caching mechanism.)
1939if scope.moved_locals.contains(&local) {
1940continue;
1941 }
19421943 cfg.push(
1944 block,
1945 Statement::new(
1946 source_info,
1947 StatementKind::BackwardIncompatibleDropHint {
1948 place: Box::new(local.into()),
1949 reason: BackwardIncompatibleDropReason::Edition2024,
1950 },
1951 ),
1952 );
1953 }
1954 DropKind::Storage => {
1955// Ordinarily, storage-dead nodes are not emitted on unwind, so we don't
1956 // need to adjust `unwind_to` on this path. However, in some specific cases
1957 // we *do* emit storage-dead nodes on the unwind path, and in that case now that
1958 // the storage-dead has completed, we need to adjust the `unwind_to` pointer
1959 // so that any future drops we emit will not register storage-dead.
1960if storage_dead_on_unwind {
1961if true {
match (&unwind_drops.drop_nodes[unwind_to].data.local, &drop_data.local) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(
1962 unwind_drops.drop_nodes[unwind_to].data.local,
1963 drop_data.local
1964 );
1965if true {
match (&unwind_drops.drop_nodes[unwind_to].data.kind, &drop_data.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(unwind_drops.drop_nodes[unwind_to].data.kind, drop_data.kind);
1966 unwind_to = unwind_drops.drop_nodes[unwind_to].next;
1967 }
1968if let Some(idx) = dropline_to {
1969if true {
match (&coroutine_drops.drop_nodes[idx].data.local, &drop_data.local) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.local, drop_data.local);
1970if true {
match (&coroutine_drops.drop_nodes[idx].data.kind, &drop_data.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(coroutine_drops.drop_nodes[idx].data.kind, drop_data.kind);
1971 dropline_to = Some(coroutine_drops.drop_nodes[idx].next);
1972 }
1973// Only temps and vars need their storage dead.
1974if !(local.index() > arg_count) {
::core::panicking::panic("assertion failed: local.index() > arg_count")
};assert!(local.index() > arg_count);
1975 cfg.push(block, Statement::new(source_info, StatementKind::StorageDead(local)));
1976 }
1977 }
1978 }
1979block.unit()
1980}
19811982impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
1983/// Build a drop tree for a breakable scope.
1984 ///
1985 /// If `continue_block` is `Some`, then the tree is for `continue` inside a
1986 /// loop. Otherwise this is for `break` or `return`.
1987fn build_exit_tree(
1988&mut self,
1989mut drops: DropTree,
1990 else_scope: region::Scope,
1991 span: Span,
1992 continue_block: Option<BasicBlock>,
1993 ) -> Option<BlockAnd<()>> {
1994let blocks = drops.build_mir::<ExitScopes>(&mut self.cfg, continue_block);
1995let is_coroutine = self.coroutine.is_some();
19961997// Link the exit drop tree to unwind drop tree.
1998if drops.drop_nodes.iter().any(|drop_node| drop_node.data.kind == DropKind::Value) {
1999let unwind_target = self.diverge_cleanup_target(else_scope, span);
2000let mut unwind_indices = IndexVec::from_elem_n(unwind_target, 1);
2001for (drop_idx, drop_node) in drops.drop_nodes.iter_enumerated().skip(1) {
2002match drop_node.data.kind {
2003 DropKind::Storage | DropKind::ForLint => {
2004if is_coroutine {
2005let unwind_drop = self
2006.scopes
2007 .unwind_drops
2008 .add_drop(drop_node.data, unwind_indices[drop_node.next]);
2009 unwind_indices.push(unwind_drop);
2010 } else {
2011 unwind_indices.push(unwind_indices[drop_node.next]);
2012 }
2013 }
2014 DropKind::Value => {
2015let unwind_drop = self
2016.scopes
2017 .unwind_drops
2018 .add_drop(drop_node.data, unwind_indices[drop_node.next]);
2019self.scopes.unwind_drops.add_entry_point(
2020 blocks[drop_idx].unwrap(),
2021 unwind_indices[drop_node.next],
2022 );
2023 unwind_indices.push(unwind_drop);
2024 }
2025 }
2026 }
2027 }
2028// Link the exit drop tree to dropline drop tree (coroutine drop path) for async drops
2029if is_coroutine2030 && drops.drop_nodes.iter().any(|DropNode { data, next: _ }| {
2031data.kind == DropKind::Value && self.is_async_drop(data.local)
2032 })
2033 {
2034let dropline_target = self.diverge_dropline_target(else_scope, span);
2035let mut dropline_indices = IndexVec::from_elem_n(dropline_target, 1);
2036for (drop_idx, drop_data) in drops.drop_nodes.iter_enumerated().skip(1) {
2037let coroutine_drop = self
2038.scopes
2039 .coroutine_drops
2040 .add_drop(drop_data.data, dropline_indices[drop_data.next]);
2041match drop_data.data.kind {
2042 DropKind::Storage | DropKind::ForLint => {}
2043 DropKind::Value => {
2044if self.is_async_drop(drop_data.data.local) {
2045self.scopes.coroutine_drops.add_entry_point(
2046 blocks[drop_idx].unwrap(),
2047 dropline_indices[drop_data.next],
2048 );
2049 }
2050 }
2051 }
2052 dropline_indices.push(coroutine_drop);
2053 }
2054 }
2055blocks[ROOT_NODE].map(BasicBlock::unit)
2056 }
20572058/// Build the unwind and coroutine drop trees.
2059pub(crate) fn build_drop_trees(&mut self) {
2060if self.coroutine.is_some() {
2061self.build_coroutine_drop_trees();
2062 } else {
2063Self::build_unwind_tree(
2064&mut self.cfg,
2065&mut self.scopes.unwind_drops,
2066self.fn_span,
2067&mut None,
2068 );
2069 }
2070 }
20712072fn build_coroutine_drop_trees(&mut self) {
2073// Build the drop tree for dropping the coroutine while it's suspended.
2074let drops = &mut self.scopes.coroutine_drops;
2075let cfg = &mut self.cfg;
2076let fn_span = self.fn_span;
2077let blocks = drops.build_mir::<CoroutineDrop>(cfg, None);
2078if let Some(root_block) = blocks[ROOT_NODE] {
2079cfg.terminate(
2080root_block,
2081SourceInfo::outermost(fn_span),
2082 TerminatorKind::CoroutineDrop,
2083 );
2084 }
20852086// Build the drop tree for unwinding in the normal control flow paths.
2087let resume_block = &mut None;
2088let unwind_drops = &mut self.scopes.unwind_drops;
2089Self::build_unwind_tree(cfg, unwind_drops, fn_span, resume_block);
20902091// Build the drop tree for unwinding when dropping a suspended
2092 // coroutine.
2093 //
2094 // This is a different tree to the standard unwind paths here to
2095 // prevent drop elaboration from creating drop flags that would have
2096 // to be captured by the coroutine. I'm not sure how important this
2097 // optimization is, but it is here.
2098for (drop_idx, drop_node) in drops.drop_nodes.iter_enumerated() {
2099if let DropKind::Value = drop_node.data.kind
2100 && let Some(bb) = blocks[drop_idx]
2101 {
2102if true {
if !(drop_node.next < drops.drop_nodes.next_index()) {
::core::panicking::panic("assertion failed: drop_node.next < drops.drop_nodes.next_index()")
};
};debug_assert!(drop_node.next < drops.drop_nodes.next_index());
2103 drops.entry_points.push((drop_node.next, bb));
2104 }
2105 }
2106Self::build_unwind_tree(cfg, drops, fn_span, resume_block);
2107 }
21082109fn build_unwind_tree(
2110 cfg: &mut CFG<'tcx>,
2111 drops: &mut DropTree,
2112 fn_span: Span,
2113 resume_block: &mut Option<BasicBlock>,
2114 ) {
2115let blocks = drops.build_mir::<Unwind>(cfg, *resume_block);
2116if let (None, Some(resume)) = (*resume_block, blocks[ROOT_NODE]) {
2117cfg.terminate(resume, SourceInfo::outermost(fn_span), TerminatorKind::UnwindResume);
21182119*resume_block = blocks[ROOT_NODE];
2120 }
2121 }
2122}
21232124// DropTreeBuilder implementations.
21252126struct ExitScopes;
21272128impl<'tcx> DropTreeBuilder<'tcx> for ExitScopes {
2129fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock {
2130cfg.start_new_block()
2131 }
2132fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock) {
2133// There should be an existing terminator with real source info and a
2134 // dummy TerminatorKind. Replace it with a proper goto.
2135 // (The dummy is added by `break_scope` and `break_for_else`.)
2136let term = cfg.block_data_mut(from).terminator_mut();
2137if let TerminatorKind::UnwindResume = term.kind {
2138term.kind = TerminatorKind::Goto { target: to };
2139 } else {
2140::rustc_middle::util::bug::span_bug_fmt(term.source_info.span,
format_args!("unexpected dummy terminator kind: {0:?}", term.kind));span_bug!(term.source_info.span, "unexpected dummy terminator kind: {:?}", term.kind);
2141 }
2142 }
2143}
21442145struct CoroutineDrop;
21462147impl<'tcx> DropTreeBuilder<'tcx> for CoroutineDrop {
2148fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock {
2149cfg.start_new_block()
2150 }
2151fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock) {
2152let term = cfg.block_data_mut(from).terminator_mut();
2153if let TerminatorKind::Yield { ref mut drop, .. } = term.kind {
2154*drop = Some(to);
2155 } else if let TerminatorKind::Drop { ref mut drop, .. } = term.kind {
2156*drop = Some(to);
2157 } else {
2158::rustc_middle::util::bug::span_bug_fmt(term.source_info.span,
format_args!("cannot enter coroutine drop tree from {0:?}", term.kind))span_bug!(
2159 term.source_info.span,
2160"cannot enter coroutine drop tree from {:?}",
2161 term.kind
2162 )2163 }
2164 }
2165}
21662167struct Unwind;
21682169impl<'tcx> DropTreeBuilder<'tcx> for Unwind {
2170fn make_block(cfg: &mut CFG<'tcx>) -> BasicBlock {
2171cfg.start_new_cleanup_block()
2172 }
2173fn link_entry_point(cfg: &mut CFG<'tcx>, from: BasicBlock, to: BasicBlock) {
2174let term = &mut cfg.block_data_mut(from).terminator_mut();
2175match &mut term.kind {
2176 TerminatorKind::Drop { unwind, .. } => {
2177if let UnwindAction::Cleanup(unwind) = *unwind {
2178let source_info = term.source_info;
2179cfg.terminate(unwind, source_info, TerminatorKind::Goto { target: to });
2180 } else {
2181*unwind = UnwindAction::Cleanup(to);
2182 }
2183 }
2184 TerminatorKind::FalseUnwind { unwind, .. }
2185 | TerminatorKind::Call { unwind, .. }
2186 | TerminatorKind::Assert { unwind, .. }
2187 | TerminatorKind::InlineAsm { unwind, .. } => {
2188*unwind = UnwindAction::Cleanup(to);
2189 }
2190 TerminatorKind::Goto { .. }
2191 | TerminatorKind::SwitchInt { .. }
2192 | TerminatorKind::UnwindResume2193 | TerminatorKind::UnwindTerminate(_)
2194 | TerminatorKind::Return2195 | TerminatorKind::TailCall { .. }
2196 | TerminatorKind::Unreachable2197 | TerminatorKind::Yield { .. }
2198 | TerminatorKind::CoroutineDrop2199 | TerminatorKind::FalseEdge { .. } => {
2200::rustc_middle::util::bug::span_bug_fmt(term.source_info.span,
format_args!("cannot unwind from {0:?}", term.kind))span_bug!(term.source_info.span, "cannot unwind from {:?}", term.kind)2201 }
2202 }
2203 }
2204}