1//! Code related to match expressions. These are sufficiently complex to
2//! warrant their own module and submodules. :) This main module includes the
3//! high-level algorithm, the submodules contain the details.
4//!
5//! This also includes code for pattern bindings in `let` statements and
6//! function parameters.
78use std::borrow::Borrow;
9use std::sync::Arc;
10use std::{debug_assert_matches, mem};
1112use itertools::{Itertools, Position};
13use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
14use rustc_data_structures::fx::FxIndexMap;
15use rustc_data_structures::stack::ensure_sufficient_stack;
16use rustc_hir::{BindingMode, ByRef, LangItem, LetStmt, LocalSource, Node};
17use rustc_middle::middle::region::{self, TempLifetime};
18use rustc_middle::mir::*;
19use rustc_middle::thir::{self, *};
20use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, ValTree, ValTreeKind};
21use rustc_middle::{bug, span_bug};
22use rustc_pattern_analysis::constructor::RangeEnd;
23use rustc_pattern_analysis::rustc::{DeconstructedPat, RustcPatCtxt};
24use rustc_span::{BytePos, Pos, Span, Symbol, sym};
25use tracing::{debug, instrument};
2627use crate::builder::ForGuard::{self, OutsideGuard, RefWithinGuard};
28use crate::builder::expr::as_place::PlaceBuilder;
29use crate::builder::matches::buckets::PartitionedCandidates;
30use crate::builder::matches::user_ty::ProjectedUserTypesNode;
31use crate::builder::scope::{DropKind, LintLevel};
32use crate::builder::{
33BlockAnd, BlockAndExtension, Builder, GuardFrame, GuardFrameLocal, LocalsForNode,
34};
3536// helper functions, broken out by category:
37mod buckets;
38mod match_pair;
39mod test;
40mod user_ty;
41mod util;
4243/// Arguments to [`Builder::then_else_break_inner`] that are usually forwarded
44/// to recursive invocations.
45#[derive(#[automatically_derived]
impl ::core::clone::Clone for ThenElseArgs {
#[inline]
fn clone(&self) -> ThenElseArgs {
let _: ::core::clone::AssertParamIsClone<Option<region::Scope>>;
let _: ::core::clone::AssertParamIsClone<SourceInfo>;
let _: ::core::clone::AssertParamIsClone<DeclareLetBindings>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ThenElseArgs { }Copy)]
46struct ThenElseArgs {
47/// Used as the temp scope for lowering `expr`. If absent (for match guards),
48 /// `self.local_scope()` is used.
49temp_scope_override: Option<region::Scope>,
50 variable_source_info: SourceInfo,
51/// Determines how bindings should be handled when lowering `let` expressions.
52 ///
53 /// Forwarded to [`Builder::lower_let_expr`] when lowering [`ExprKind::Let`].
54declare_let_bindings: DeclareLetBindings,
55}
5657/// Should lowering a `let` expression also declare its bindings?
58///
59/// Used by [`Builder::lower_let_expr`] when lowering [`ExprKind::Let`].
60#[derive(#[automatically_derived]
impl ::core::clone::Clone for DeclareLetBindings {
#[inline]
fn clone(&self) -> DeclareLetBindings { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DeclareLetBindings { }Copy)]
61pub(crate) enum DeclareLetBindings {
62/// Yes, declare `let` bindings as normal for `if` conditions.
63Yes,
64/// No, don't declare `let` bindings, because the caller declares them
65 /// separately due to special requirements.
66 ///
67 /// Used for match guards and let-else.
68No,
69/// Let expressions are not permitted in this context, so it is a bug to
70 /// try to lower one (e.g inside lazy-boolean-or or boolean-not).
71LetNotPermitted,
72}
7374/// Used by [`Builder::storage_live_binding`] and [`Builder::bind_matched_candidate_for_arm_body`]
75/// to decide whether to schedule drops.
76#[derive(#[automatically_derived]
impl ::core::clone::Clone for ScheduleDrops {
#[inline]
fn clone(&self) -> ScheduleDrops { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for ScheduleDrops { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for ScheduleDrops {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
ScheduleDrops::Yes => "Yes",
ScheduleDrops::No => "No",
})
}
}Debug)]
77pub(crate) enum ScheduleDrops {
78/// Yes, the relevant functions should also schedule drops as appropriate.
79Yes,
80/// No, don't schedule drops. The caller has taken responsibility for any
81 /// appropriate drops.
82No,
83}
8485impl<'a, 'tcx> Builder<'a, 'tcx> {
86/// Lowers a condition in a way that ensures that variables bound in any let
87 /// expressions are definitely initialized in the if body.
88 ///
89 /// If `declare_let_bindings` is false then variables created in `let`
90 /// expressions will not be declared. This is for if let guards on arms with
91 /// an or pattern, where the guard is lowered multiple times.
92pub(crate) fn then_else_break(
93&mut self,
94 block: BasicBlock,
95 expr_id: ExprId,
96 temp_scope_override: Option<region::Scope>,
97 variable_source_info: SourceInfo,
98 declare_let_bindings: DeclareLetBindings,
99 ) -> BlockAnd<()> {
100self.then_else_break_inner(
101block,
102expr_id,
103ThenElseArgs { temp_scope_override, variable_source_info, declare_let_bindings },
104 )
105 }
106107fn then_else_break_inner(
108&mut self,
109 block: BasicBlock, // Block that the condition and branch will be lowered into
110expr_id: ExprId, // Condition expression to lower
111args: ThenElseArgs,
112 ) -> BlockAnd<()> {
113let this = self; // See "LET_THIS_SELF".
114let expr = &this.thir[expr_id];
115let expr_span = expr.span;
116117match expr.kind {
118 ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
119let lhs_then_block = this.then_else_break_inner(block, lhs, args).into_block();
120let rhs_then_block =
121this.then_else_break_inner(lhs_then_block, rhs, args).into_block();
122rhs_then_block.unit()
123 }
124 ExprKind::LogicalOp { op: LogicalOp::Or, lhs, rhs } => {
125let local_scope = this.local_scope();
126let (lhs_success_block, failure_block) =
127this.in_if_then_scope(local_scope, expr_span, |this| {
128this.then_else_break_inner(
129block,
130lhs,
131ThenElseArgs {
132 declare_let_bindings: DeclareLetBindings::LetNotPermitted,
133 ..args134 },
135 )
136 });
137let rhs_success_block = this138 .then_else_break_inner(
139failure_block,
140rhs,
141ThenElseArgs {
142 declare_let_bindings: DeclareLetBindings::LetNotPermitted,
143 ..args144 },
145 )
146 .into_block();
147148// Make the LHS and RHS success arms converge to a common block.
149 // (We can't just make LHS goto RHS, because `rhs_success_block`
150 // might contain statements that we don't want on the LHS path.)
151let success_block = this.cfg.start_new_block();
152this.cfg.goto(lhs_success_block, args.variable_source_info, success_block);
153this.cfg.goto(rhs_success_block, args.variable_source_info, success_block);
154success_block.unit()
155 }
156 ExprKind::Unary { op: UnOp::Not, arg } => {
157// Improve branch coverage instrumentation by noting conditions
158 // nested within one or more `!` expressions.
159 // (Skipped if branch coverage is not enabled.)
160if let Some(coverage_info) = this.coverage_info.as_mut() {
161coverage_info.visit_unary_not(this.thir, expr_id);
162 }
163164let local_scope = this.local_scope();
165let (success_block, failure_block) =
166this.in_if_then_scope(local_scope, expr_span, |this| {
167// Help out coverage instrumentation by injecting a dummy statement with
168 // the original condition's span (including `!`). This fixes #115468.
169if this.tcx.sess.instrument_coverage() {
170this.cfg.push_coverage_span_marker(block, this.source_info(expr_span));
171 }
172this.then_else_break_inner(
173block,
174arg,
175ThenElseArgs {
176 declare_let_bindings: DeclareLetBindings::LetNotPermitted,
177 ..args178 },
179 )
180 });
181this.break_for_else(success_block, args.variable_source_info);
182failure_block.unit()
183 }
184 ExprKind::Scope { region_scope, hir_id, value } => {
185let region_scope = (region_scope, this.source_info(expr_span));
186this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
187this.then_else_break_inner(block, value, args)
188 })
189 }
190 ExprKind::Use { source } => this.then_else_break_inner(block, source, args),
191 ExprKind::Let { expr, ref pat } => this.lower_let_expr(
192block,
193expr,
194pat,
195Some(args.variable_source_info.scope),
196args.variable_source_info.span,
197args.declare_let_bindings,
198 ),
199_ => {
200let mut block = block;
201let temp_scope = args.temp_scope_override.unwrap_or_else(|| this.local_scope());
202let mutability = Mutability::Mut;
203204let place = {
let BlockAnd(b, v) =
this.as_temp(block,
TempLifetime {
temp_lifetime: Some(temp_scope),
backwards_incompatible: None,
}, expr_id, mutability);
block = b;
v
}unpack!(
205 block = this.as_temp(
206 block,
207 TempLifetime {
208 temp_lifetime: Some(temp_scope),
209 backwards_incompatible: None
210},
211 expr_id,
212 mutability
213 )
214 );
215216let operand = Operand::Move(Place::from(place));
217218let then_block = this.cfg.start_new_block();
219let else_block = this.cfg.start_new_block();
220let term = TerminatorKind::if_(operand, then_block, else_block);
221222// Record branch coverage info for this condition.
223 // (Does nothing if branch coverage is not enabled.)
224this.visit_coverage_branch_condition(expr_id, then_block, else_block);
225226let source_info = this.source_info(expr_span);
227this.cfg.terminate(block, source_info, term);
228this.break_for_else(else_block, source_info);
229230then_block.unit()
231 }
232 }
233 }
234235/// Generates MIR for a `match` expression.
236 ///
237 /// The MIR that we generate for a match looks like this.
238 ///
239 /// ```text
240 /// [ 0. Pre-match ]
241 /// |
242 /// [ 1. Evaluate Scrutinee (expression being matched on) ]
243 /// [ (PlaceMention of scrutinee) ]
244 /// |
245 /// [ 2. Decision tree -- check discriminants ] <--------+
246 /// | |
247 /// | (once a specific arm is chosen) |
248 /// | |
249 /// [pre_binding_block] [otherwise_block]
250 /// | |
251 /// [ 3. Create "guard bindings" for arm ] |
252 /// [ (create fake borrows) ] |
253 /// | |
254 /// [ 4. Execute guard code ] |
255 /// [ (read fake borrows) ] --(guard is false)-----------+
256 /// |
257 /// | (guard results in true)
258 /// |
259 /// [ 5. Create real bindings and execute arm ]
260 /// |
261 /// [ Exit match ]
262 /// ```
263 ///
264 /// All of the different arms have been stacked on top of each other to
265 /// simplify the diagram. For an arm with no guard the blocks marked 3 and
266 /// 4 and the fake borrows are omitted.
267 ///
268 /// We generate MIR in the following steps:
269 ///
270 /// 1. Evaluate the scrutinee and add the PlaceMention of it ([Builder::lower_scrutinee]).
271 /// 2. Create the decision tree ([Builder::lower_match_tree]).
272 /// 3. Determine the fake borrows that are needed from the places that were
273 /// matched against and create the required temporaries for them
274 /// ([util::collect_fake_borrows]).
275 /// 4. Create everything else: the guards and the arms ([Builder::lower_match_arms]).
276 ///
277 /// ## False edges
278 ///
279 /// We don't want to have the exact structure of the decision tree be visible through borrow
280 /// checking. Specifically we want borrowck to think that:
281 /// - at any point, any or none of the patterns and guards seen so far may have been tested;
282 /// - after the match, any of the patterns may have matched.
283 ///
284 /// For example, all of these would fail to error if borrowck could see the real CFG (examples
285 /// taken from `tests/ui/nll/match-cfg-fake-edges.rs`):
286 /// ```ignore (too many errors, this is already in the test suite)
287 /// let x = String::new();
288 /// let _ = match true {
289 /// _ => {},
290 /// _ => drop(x),
291 /// };
292 /// // Borrowck must not know the second arm is never run.
293 /// drop(x); //~ ERROR use of moved value
294 ///
295 /// let x;
296 /// # let y = true;
297 /// match y {
298 /// _ if { x = 2; true } => {},
299 /// // Borrowck must not know the guard is always run.
300 /// _ => drop(x), //~ ERROR used binding `x` is possibly-uninitialized
301 /// };
302 ///
303 /// let x = String::new();
304 /// # let y = true;
305 /// match y {
306 /// false if { drop(x); true } => {},
307 /// // Borrowck must not know the guard is not run in the `true` case.
308 /// true => drop(x), //~ ERROR use of moved value: `x`
309 /// false => {},
310 /// };
311 ///
312 /// # let mut y = (true, true);
313 /// let r = &mut y.1;
314 /// match y {
315 /// //~^ ERROR cannot use `y.1` because it was mutably borrowed
316 /// (false, true) => {}
317 /// // Borrowck must not know we don't test `y.1` when `y.0` is `true`.
318 /// (true, _) => drop(r),
319 /// (false, _) => {}
320 /// };
321 /// ```
322 ///
323 /// We add false edges to act as if we were naively matching each arm in order. What we need is
324 /// a (fake) path from each candidate to the next, specifically from candidate C's pre-binding
325 /// block to next candidate D's pre-binding block. For maximum precision (needed for deref
326 /// patterns), we choose the earliest node on D's success path that doesn't also lead to C (to
327 /// avoid loops).
328 ///
329 /// This turns out to be easy to compute: that block is the `start_block` of the first call to
330 /// `match_candidates` where D is the first candidate in the list.
331 ///
332 /// For example:
333 /// ```rust
334 /// # let (x, y) = (true, true);
335 /// match (x, y) {
336 /// (true, true) => 1,
337 /// (false, true) => 2,
338 /// (true, false) => 3,
339 /// _ => 4,
340 /// }
341 /// # ;
342 /// ```
343 /// In this example, the pre-binding block of arm 1 has a false edge to the block for result
344 /// `false` of the first test on `x`. The other arms have false edges to the pre-binding blocks
345 /// of the next arm.
346 ///
347 /// On top of this, we also add a false edge from the otherwise_block of each guard to the
348 /// aforementioned start block of the next candidate, to ensure borrock doesn't rely on which
349 /// guards may have run.
350#[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("match_expr",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(350u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["destination",
"block", "scrutinee_id", "span", "scrutinee_span"],
::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(&destination)
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(&block)
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(&scrutinee_id)
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(&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(&scrutinee_span)
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<()> = loop {};
return __tracing_attr_fake_return;
}
{
let scrutinee_place =
{
let BlockAnd(b, v) =
self.lower_scrutinee(block, scrutinee_id, scrutinee_span);
block = b;
v
};
let match_start_span = span.shrink_to_lo().to(scrutinee_span);
let patterns =
arms.iter().map(|&arm|
{
let arm = &self.thir[arm];
let has_match_guard =
if arm.guard.is_some() {
HasMatchGuard::Yes
} else { HasMatchGuard::No };
(&*arm.pattern, has_match_guard)
}).collect();
let built_tree =
self.lower_match_tree(block, scrutinee_span, &scrutinee_place,
match_start_span, patterns, false);
self.lower_match_arms(destination, scrutinee_place,
scrutinee_span, arms, built_tree, self.source_info(span))
}
}
}#[instrument(level = "debug", skip(self, arms))]351pub(crate) fn match_expr(
352&mut self,
353 destination: Place<'tcx>,
354mut block: BasicBlock,
355 scrutinee_id: ExprId,
356 arms: &[ArmId],
357 span: Span,
358 scrutinee_span: Span,
359 ) -> BlockAnd<()> {
360let scrutinee_place =
361unpack!(block = self.lower_scrutinee(block, scrutinee_id, scrutinee_span));
362363let match_start_span = span.shrink_to_lo().to(scrutinee_span);
364let patterns = arms
365 .iter()
366 .map(|&arm| {
367let arm = &self.thir[arm];
368let has_match_guard =
369if arm.guard.is_some() { HasMatchGuard::Yes } else { HasMatchGuard::No };
370 (&*arm.pattern, has_match_guard)
371 })
372 .collect();
373let built_tree = self.lower_match_tree(
374 block,
375 scrutinee_span,
376&scrutinee_place,
377 match_start_span,
378 patterns,
379false,
380 );
381382self.lower_match_arms(
383 destination,
384 scrutinee_place,
385 scrutinee_span,
386 arms,
387 built_tree,
388self.source_info(span),
389 )
390 }
391392/// Evaluate the scrutinee and add the PlaceMention for it.
393pub(crate) fn lower_scrutinee(
394&mut self,
395mut block: BasicBlock,
396 scrutinee_id: ExprId,
397 scrutinee_span: Span,
398 ) -> BlockAnd<PlaceBuilder<'tcx>> {
399let scrutinee_place_builder = {
let BlockAnd(b, v) = self.as_place_builder(block, scrutinee_id);
block = b;
v
}unpack!(block = self.as_place_builder(block, scrutinee_id));
400if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
401let source_info = self.source_info(scrutinee_span);
402self.cfg.push_place_mention(block, source_info, scrutinee_place);
403 }
404405block.and(scrutinee_place_builder)
406 }
407408/// Lower the bindings, guards and arm bodies of a `match` expression.
409 ///
410 /// The decision tree should have already been created
411 /// (by [Builder::lower_match_tree]).
412 ///
413 /// `outer_source_info` is the SourceInfo for the whole match.
414pub(crate) fn lower_match_arms(
415&mut self,
416 destination: Place<'tcx>,
417 scrutinee_place_builder: PlaceBuilder<'tcx>,
418 scrutinee_span: Span,
419 arms: &[ArmId],
420 built_match_tree: BuiltMatchTree<'tcx>,
421 outer_source_info: SourceInfo,
422 ) -> BlockAnd<()> {
423let arm_end_blocks: Vec<BasicBlock> = arms424 .iter()
425 .map(|&arm| &self.thir[arm])
426 .zip(built_match_tree.branches)
427 .map(|(arm, branch)| {
428{
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/matches/mod.rs:428",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(428u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("lowering arm {0:?}\ncorresponding branch = {1:?}",
arm, branch) as &dyn Value))])
});
} else { ; }
};debug!("lowering arm {:?}\ncorresponding branch = {:?}", arm, branch);
429430let arm_source_info = self.source_info(arm.span);
431let arm_scope = (arm.scope, arm_source_info);
432let match_scope = self.local_scope();
433let guard_scope = arm434 .guard
435 .map(|_| region::Scope { data: region::ScopeData::MatchGuard, ..arm.scope });
436self.in_scope(arm_scope, LintLevel::Explicit(arm.hir_id), |this| {
437this.opt_in_scope(guard_scope.map(|scope| (scope, arm_source_info)), |this| {
438// `if let` guard temps needing deduplicating will be in the guard scope.
439let old_dedup_scope =
440 mem::replace(&mut this.fixed_temps_scope, guard_scope);
441442// `try_to_place` may fail if it is unable to resolve the given
443 // `PlaceBuilder` inside a closure. In this case, we don't want to include
444 // a scrutinee place. `scrutinee_place_builder` will fail to be resolved
445 // if the only match arm is a wildcard (`_`).
446 // Example:
447 // ```
448 // let foo = (0, 1);
449 // let c = || {
450 // match foo { _ => () };
451 // };
452 // ```
453let scrutinee_place = scrutinee_place_builder.try_to_place(this);
454let opt_scrutinee_place =
455scrutinee_place.as_ref().map(|place| (Some(place), scrutinee_span));
456let scope = this.declare_bindings(
457None,
458arm.span,
459&arm.pattern,
460arm.guard,
461opt_scrutinee_place,
462 );
463464let arm_block = this.bind_pattern(
465outer_source_info,
466branch,
467&built_match_tree.fake_borrow_temps,
468scrutinee_span,
469Some((arm, match_scope)),
470 );
471472this.fixed_temps_scope = old_dedup_scope;
473474if let Some(source_scope) = scope {
475this.source_scope = source_scope;
476 }
477478this.expr_into_dest(destination, arm_block, arm.body)
479 })
480 })
481 .into_block()
482 })
483 .collect();
484485// all the arm blocks will rejoin here
486let end_block = self.cfg.start_new_block();
487488let end_brace = self.source_info(
489outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)),
490 );
491for arm_block in arm_end_blocks {
492let block = &self.cfg.basic_blocks[arm_block];
493let last_location = block.statements.last().map(|s| s.source_info);
494495self.cfg.goto(arm_block, last_location.unwrap_or(end_brace), end_block);
496 }
497498self.source_scope = outer_source_info.scope;
499500end_block.unit()
501 }
502503/// For a top-level `match` arm or a `let` binding, binds the variables and
504 /// ascribes types, and also checks the match arm guard (if present).
505 ///
506 /// `arm_scope` should be `Some` if and only if this is called for a
507 /// `match` arm.
508 ///
509 /// In the presence of or-patterns, a match arm might have multiple
510 /// sub-branches representing different ways to match, with each sub-branch
511 /// requiring its own bindings and its own copy of the guard. This method
512 /// handles those sub-branches individually, and then has them jump together
513 /// to a common block.
514 ///
515 /// Returns a single block that the match arm can be lowered into.
516 /// (For `let` bindings, this is the code that can use the bindings.)
517fn bind_pattern(
518&mut self,
519 outer_source_info: SourceInfo,
520 branch: MatchTreeBranch<'tcx>,
521 fake_borrow_temps: &[(Place<'tcx>, Local, FakeBorrowKind)],
522 scrutinee_span: Span,
523 arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
524 ) -> BasicBlock {
525if branch.sub_branches.len() == 1 {
526let [sub_branch] = branch.sub_branches.try_into().unwrap();
527// Avoid generating another `BasicBlock` when we only have one sub branch.
528self.bind_and_guard_matched_candidate(
529sub_branch,
530fake_borrow_temps,
531scrutinee_span,
532arm_match_scope,
533 ScheduleDrops::Yes,
534 )
535 } else {
536// It's helpful to avoid scheduling drops multiple times to save
537 // drop elaboration from having to clean up the extra drops.
538 //
539 // If we are in a `let` then we only schedule drops for the first
540 // candidate.
541 //
542 // If we're in a `match` arm then we could have a case like so:
543 //
544 // Ok(x) | Err(x) if return => { /* ... */ }
545 //
546 // In this case we don't want a drop of `x` scheduled when we
547 // return: it isn't bound by move until right before enter the arm.
548 // To handle this we instead unschedule it's drop after each time
549 // we lower the guard.
550 // As a result, we end up with the drop order of the last sub-branch we lower. To use
551 // the drop order for the first sub-branch, we lower sub-branches in reverse (#142163).
552let target_block = self.cfg.start_new_block();
553for (pos, sub_branch) in branch.sub_branches.into_iter().rev().with_position() {
554if true {
if !(pos != Position::Only) {
::core::panicking::panic("assertion failed: pos != Position::Only")
};
};debug_assert!(pos != Position::Only);
555let schedule_drops =
556if pos == Position::Last { ScheduleDrops::Yes } else { ScheduleDrops::No };
557let binding_end = self.bind_and_guard_matched_candidate(
558 sub_branch,
559 fake_borrow_temps,
560 scrutinee_span,
561 arm_match_scope,
562 schedule_drops,
563 );
564self.cfg.goto(binding_end, outer_source_info, target_block);
565 }
566567target_block568 }
569 }
570571pub(super) fn expr_into_pattern(
572&mut self,
573mut block: BasicBlock,
574 irrefutable_pat: &Pat<'tcx>,
575 initializer_id: ExprId,
576 ) -> BlockAnd<()> {
577match irrefutable_pat.kind {
578// Optimize `let x = ...` and `let x: T = ...` to write directly into `x`,
579 // and then require that `T == typeof(x)` if present.
580PatKind::Binding { mode: BindingMode(ByRef::No, _), var, subpattern: None, .. } => {
581let place = self.storage_live_binding(
582block,
583var,
584irrefutable_pat.span,
585false,
586OutsideGuard,
587 ScheduleDrops::Yes,
588 );
589block = self.expr_into_dest(place, block, initializer_id).into_block();
590591// Inject a fake read, see comments on `FakeReadCause::ForLet`.
592let source_info = self.source_info(irrefutable_pat.span);
593self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLet(None), place);
594595let ascriptions: &[_] =
596try { irrefutable_pat.extra.as_deref()?.ascriptions.as_slice() }
597 .unwrap_or_default();
598for thir::Ascription { annotation, variance: _ } in ascriptions {
599let ty_source_info = self.source_info(annotation.span);
600601let base = self.canonical_user_type_annotations.push(annotation.clone());
602let stmt = Statement::new(
603 ty_source_info,
604 StatementKind::AscribeUserType(
605 Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
606// We always use invariant as the variance here. This is because the
607 // variance field from the ascription refers to the variance to use
608 // when applying the type to the value being matched, but this
609 // ascription applies rather to the type of the binding. e.g., in this
610 // example:
611 //
612 // ```
613 // let x: T = <expr>
614 // ```
615 //
616 // We are creating an ascription that defines the type of `x` to be
617 // exactly `T` (i.e., with invariance). The variance field, in
618 // contrast, is intended to be used to relate `T` to the type of
619 // `<expr>`.
620ty::Invariant,
621 ),
622 );
623self.cfg.push(block, stmt);
624 }
625626self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
627block.unit()
628 }
629630_ => {
631let initializer = &self.thir[initializer_id];
632let place_builder =
633{
let BlockAnd(b, v) =
self.lower_scrutinee(block, initializer_id, initializer.span);
block = b;
v
}unpack!(block = self.lower_scrutinee(block, initializer_id, initializer.span));
634self.place_into_pattern(block, irrefutable_pat, place_builder, true)
635 }
636 }
637 }
638639pub(crate) fn place_into_pattern(
640&mut self,
641 block: BasicBlock,
642 irrefutable_pat: &Pat<'tcx>,
643 initializer: PlaceBuilder<'tcx>,
644 set_match_place: bool,
645 ) -> BlockAnd<()> {
646let built_tree = self.lower_match_tree(
647block,
648irrefutable_pat.span,
649&initializer,
650irrefutable_pat.span,
651::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(irrefutable_pat, HasMatchGuard::No)]))vec![(irrefutable_pat, HasMatchGuard::No)],
652false,
653 );
654let [branch] = built_tree.branches.try_into().unwrap();
655656// For matches and function arguments, the place that is being matched
657 // can be set when creating the variables. But the place for
658 // let PATTERN = ... might not even exist until we do the assignment.
659 // so we set it here instead.
660if set_match_place {
661// `try_to_place` may fail if it is unable to resolve the given `PlaceBuilder` inside a
662 // closure. In this case, we don't want to include a scrutinee place.
663 // `scrutinee_place_builder` will fail for destructured assignments. This is because a
664 // closure only captures the precise places that it will read and as a result a closure
665 // may not capture the entire tuple/struct and rather have individual places that will
666 // be read in the final MIR.
667 // Example:
668 // ```
669 // let foo = (0, 1);
670 // let c = || {
671 // let (v1, v2) = foo;
672 // };
673 // ```
674if let Some(place) = initializer.try_to_place(self) {
675// Because or-alternatives bind the same variables, we only explore the first one.
676let first_sub_branch = branch.sub_branches.first().unwrap();
677for binding in &first_sub_branch.bindings {
678let local = self.var_local_id(binding.var_id, OutsideGuard);
679if let LocalInfo::User(BindingForm::Var(VarBindingForm {
680 opt_match_place: Some((ref mut match_place, _)),
681 ..
682 })) = **self.local_decls[local].local_info.as_mut().unwrap_crate_local()
683 {
684*match_place = Some(place);
685 } else {
686::rustc_middle::util::bug::bug_fmt(format_args!("Let binding to non-user variable."))bug!("Let binding to non-user variable.")687 };
688 }
689 }
690 }
691692self.bind_pattern(
693self.source_info(irrefutable_pat.span),
694branch,
695&[],
696irrefutable_pat.span,
697None,
698 )
699 .unit()
700 }
701702/// Declares the bindings of the given patterns and returns the visibility
703 /// scope for the bindings in these patterns, if such a scope had to be
704 /// created. NOTE: Declaring the bindings should always be done in their
705 /// drop scope.
706#[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("declare_bindings",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(706u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["visibility_scope",
"scope_span", "pattern", "guard", "opt_match_place"],
::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(&visibility_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(&scope_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(&pattern)
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(&guard)
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(&opt_match_place)
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: Option<SourceScope> = loop {};
return __tracing_attr_fake_return;
}
{
self.visit_primary_bindings_special(pattern,
&ProjectedUserTypesNode::None,
&mut |this, name, mode, var, span, ty, user_tys|
{
let saved_scope = this.source_scope;
this.set_correct_source_scope_for_arg(var.0, saved_scope,
span);
let vis_scope =
*visibility_scope.get_or_insert_with(||
this.new_source_scope(scope_span, LintLevel::Inherited));
let source_info =
SourceInfo { span, scope: this.source_scope };
let user_tys = user_tys.build_user_type_projections();
this.declare_binding(source_info, vis_scope, name, mode,
var, ty, user_tys, ArmHasGuard(guard.is_some()),
opt_match_place.map(|(x, y)| (x.cloned(), y)),
pattern.span);
this.source_scope = saved_scope;
});
if let Some(guard_expr) = guard {
self.declare_guard_bindings(guard_expr, scope_span,
visibility_scope);
}
visibility_scope
}
}
}#[instrument(skip(self), level = "debug")]707pub(crate) fn declare_bindings(
708&mut self,
709mut visibility_scope: Option<SourceScope>,
710 scope_span: Span,
711 pattern: &Pat<'tcx>,
712 guard: Option<ExprId>,
713 opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
714 ) -> Option<SourceScope> {
715self.visit_primary_bindings_special(
716 pattern,
717&ProjectedUserTypesNode::None,
718&mut |this, name, mode, var, span, ty, user_tys| {
719let saved_scope = this.source_scope;
720 this.set_correct_source_scope_for_arg(var.0, saved_scope, span);
721let vis_scope = *visibility_scope
722 .get_or_insert_with(|| this.new_source_scope(scope_span, LintLevel::Inherited));
723let source_info = SourceInfo { span, scope: this.source_scope };
724let user_tys = user_tys.build_user_type_projections();
725726 this.declare_binding(
727 source_info,
728 vis_scope,
729 name,
730 mode,
731 var,
732 ty,
733 user_tys,
734 ArmHasGuard(guard.is_some()),
735 opt_match_place.map(|(x, y)| (x.cloned(), y)),
736 pattern.span,
737 );
738 this.source_scope = saved_scope;
739 },
740 );
741if let Some(guard_expr) = guard {
742self.declare_guard_bindings(guard_expr, scope_span, visibility_scope);
743 }
744 visibility_scope
745 }
746747/// Declare bindings in a guard. This has to be done when declaring bindings
748 /// for an arm to ensure that or patterns only have one version of each
749 /// variable.
750pub(crate) fn declare_guard_bindings(
751&mut self,
752 guard_expr: ExprId,
753 scope_span: Span,
754 visibility_scope: Option<SourceScope>,
755 ) {
756match self.thir.exprs[guard_expr].kind {
757 ExprKind::Let { expr: _, pat: ref guard_pat } => {
758// FIXME: pass a proper `opt_match_place`
759self.declare_bindings(visibility_scope, scope_span, guard_pat, None, None);
760 }
761 ExprKind::Scope { value, .. } => {
762self.declare_guard_bindings(value, scope_span, visibility_scope);
763 }
764 ExprKind::Use { source } => {
765self.declare_guard_bindings(source, scope_span, visibility_scope);
766 }
767 ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
768self.declare_guard_bindings(lhs, scope_span, visibility_scope);
769self.declare_guard_bindings(rhs, scope_span, visibility_scope);
770 }
771_ => {}
772 }
773 }
774775/// Emits a [`StatementKind::StorageLive`] for the given var, and also
776 /// schedules a drop if requested (and possible).
777pub(crate) fn storage_live_binding(
778&mut self,
779 block: BasicBlock,
780 var: LocalVarId,
781 span: Span,
782 is_shorthand: bool,
783 for_guard: ForGuard,
784 schedule_drop: ScheduleDrops,
785 ) -> Place<'tcx> {
786let local_id = self.var_local_id(var, for_guard);
787let source_info = self.source_info(span);
788self.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(local_id)));
789// Although there is almost always scope for given variable in corner cases
790 // like #92893 we might get variable with no scope.
791if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id)
792 && #[allow(non_exhaustive_omitted_patterns)] match schedule_drop {
ScheduleDrops::Yes => true,
_ => false,
}matches!(schedule_drop, ScheduleDrops::Yes)793 {
794self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
795 }
796let local_info = self.local_decls[local_id].local_info.as_mut().unwrap_crate_local();
797if let LocalInfo::User(BindingForm::Var(var_info)) = &mut **local_info {
798var_info.introductions.push(VarBindingIntroduction { span, is_shorthand });
799 }
800Place::from(local_id)
801 }
802803pub(crate) fn schedule_drop_for_binding(
804&mut self,
805 var: LocalVarId,
806 span: Span,
807 for_guard: ForGuard,
808 ) {
809let local_id = self.var_local_id(var, for_guard);
810if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) {
811self.schedule_drop(span, region_scope, local_id, DropKind::Value);
812 }
813 }
814815/// Visits all of the "primary" bindings in a pattern, i.e. the leftmost
816 /// occurrence of each variable bound by the pattern.
817 /// See [`PatKind::Binding::is_primary`] for more context.
818 ///
819 /// This variant provides only the limited subset of binding data needed
820 /// by its callers, and should be a "pure" visit without side-effects.
821pub(super) fn visit_primary_bindings(
822&mut self,
823 pattern: &Pat<'tcx>,
824 f: &mut impl FnMut(&mut Self, LocalVarId, Span),
825 ) {
826pattern.walk_always(|pat| {
827if let PatKind::Binding { var, is_primary: true, .. } = pat.kind {
828f(self, var, pat.span);
829 }
830 })
831 }
832833/// Visits all of the "primary" bindings in a pattern, while preparing
834 /// additional user-type-annotation data needed by `declare_bindings`.
835 ///
836 /// This also has the side-effect of pushing all user type annotations
837 /// onto `canonical_user_type_annotations`, so that they end up in MIR
838 /// even if they aren't associated with any bindings.
839#[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("visit_primary_bindings_special",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(839u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["pattern",
"user_tys"],
::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(&pattern)
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(&user_tys)
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;
}
{
let user_tys =
match pattern.extra.as_deref() {
Some(PatExtra { ascriptions, .. }) if
!ascriptions.is_empty() => {
let base_user_tys =
ascriptions.iter().map(|thir::Ascription {
annotation, variance: _ }|
{
self.canonical_user_type_annotations.push(annotation.clone())
}).collect();
&user_tys.push_user_types(base_user_tys)
}
_ => user_tys,
};
let visit_subpat =
|this: &mut Self, subpat, user_tys: &_, f: &mut _|
{
this.visit_primary_bindings_special(subpat, user_tys, f)
};
match pattern.kind {
PatKind::Binding {
name, mode, var, ty, ref subpattern, is_primary, .. } => {
if is_primary {
f(self, name, mode, var, pattern.span, ty, user_tys);
}
if let Some(subpattern) = subpattern.as_ref() {
visit_subpat(self, subpattern, user_tys, f);
}
}
PatKind::Array { ref prefix, ref slice, ref suffix } |
PatKind::Slice { ref prefix, ref slice, ref suffix } => {
let from = u64::try_from(prefix.len()).unwrap();
let to = u64::try_from(suffix.len()).unwrap();
for subpattern in prefix.iter() {
visit_subpat(self, subpattern, &user_tys.index(), f);
}
if let Some(subpattern) = slice {
visit_subpat(self, subpattern, &user_tys.subslice(from, to),
f);
}
for subpattern in suffix.iter() {
visit_subpat(self, subpattern, &user_tys.index(), f);
}
}
PatKind::Constant { .. } | PatKind::Range { .. } |
PatKind::Missing | PatKind::Wild | PatKind::Never |
PatKind::Error(_) => {}
PatKind::Deref { pin: Pinnedness::Pinned, ref subpattern } =>
{
visit_subpat(self, subpattern,
&user_tys.leaf(FieldIdx::ZERO).deref(), f);
}
PatKind::Deref { pin: Pinnedness::Not, ref subpattern } => {
visit_subpat(self, subpattern, &user_tys.deref(), f);
}
PatKind::DerefPattern { ref subpattern, .. } => {
visit_subpat(self, subpattern,
&ProjectedUserTypesNode::None, f);
}
PatKind::Leaf { ref subpatterns } => {
for subpattern in subpatterns {
let subpattern_user_tys = user_tys.leaf(subpattern.field);
{
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/matches/mod.rs:926",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(926u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("visit_primary_bindings: subpattern_user_tys={0:?}",
subpattern_user_tys) as &dyn Value))])
});
} else { ; }
};
visit_subpat(self, &subpattern.pattern,
&subpattern_user_tys, f);
}
}
PatKind::Variant {
adt_def, args: _, variant_index, ref subpatterns } => {
for subpattern in subpatterns {
let subpattern_user_tys =
user_tys.variant(adt_def, variant_index, subpattern.field);
visit_subpat(self, &subpattern.pattern,
&subpattern_user_tys, f);
}
}
PatKind::Or { ref pats } => {
for subpattern in pats.iter() {
visit_subpat(self, subpattern, user_tys, f);
}
}
}
}
}
}#[instrument(level = "debug", skip(self, f))]840fn visit_primary_bindings_special(
841&mut self,
842 pattern: &Pat<'tcx>,
843 user_tys: &ProjectedUserTypesNode<'_>,
844 f: &mut impl FnMut(
845&mut Self,
846Symbol,
847BindingMode,
848LocalVarId,
849Span,
850Ty<'tcx>,
851&ProjectedUserTypesNode<'_>,
852 ),
853 ) {
854// Ascriptions correspond to user-written types like `let A::<'a>(_): A<'static> = ...;`.
855 //
856 // Caution: Pushing user types here is load-bearing even for
857 // patterns containing no bindings, to ensure that the type ends
858 // up represented in MIR _somewhere_.
859let user_tys = match pattern.extra.as_deref() {
860Some(PatExtra { ascriptions, .. }) if !ascriptions.is_empty() => {
861let base_user_tys = ascriptions
862 .iter()
863 .map(|thir::Ascription { annotation, variance: _ }| {
864// Note that the variance doesn't apply here, as we are tracking the effect
865 // of user types on any bindings contained with subpattern.
866self.canonical_user_type_annotations.push(annotation.clone())
867 })
868 .collect();
869&user_tys.push_user_types(base_user_tys)
870 }
871_ => user_tys,
872 };
873874// Avoid having to write the full method name at each recursive call.
875let visit_subpat = |this: &mut Self, subpat, user_tys: &_, f: &mut _| {
876 this.visit_primary_bindings_special(subpat, user_tys, f)
877 };
878879match pattern.kind {
880 PatKind::Binding { name, mode, var, ty, ref subpattern, is_primary, .. } => {
881if is_primary {
882 f(self, name, mode, var, pattern.span, ty, user_tys);
883 }
884if let Some(subpattern) = subpattern.as_ref() {
885 visit_subpat(self, subpattern, user_tys, f);
886 }
887 }
888889 PatKind::Array { ref prefix, ref slice, ref suffix }
890 | PatKind::Slice { ref prefix, ref slice, ref suffix } => {
891let from = u64::try_from(prefix.len()).unwrap();
892let to = u64::try_from(suffix.len()).unwrap();
893for subpattern in prefix.iter() {
894 visit_subpat(self, subpattern, &user_tys.index(), f);
895 }
896if let Some(subpattern) = slice {
897 visit_subpat(self, subpattern, &user_tys.subslice(from, to), f);
898 }
899for subpattern in suffix.iter() {
900 visit_subpat(self, subpattern, &user_tys.index(), f);
901 }
902 }
903904 PatKind::Constant { .. }
905 | PatKind::Range { .. }
906 | PatKind::Missing
907 | PatKind::Wild
908 | PatKind::Never
909 | PatKind::Error(_) => {}
910911 PatKind::Deref { pin: Pinnedness::Pinned, ref subpattern } => {
912// Project into the `Pin(_)` struct, then deref the inner `&` or `&mut`.
913visit_subpat(self, subpattern, &user_tys.leaf(FieldIdx::ZERO).deref(), f);
914 }
915 PatKind::Deref { pin: Pinnedness::Not, ref subpattern } => {
916 visit_subpat(self, subpattern, &user_tys.deref(), f);
917 }
918919 PatKind::DerefPattern { ref subpattern, .. } => {
920 visit_subpat(self, subpattern, &ProjectedUserTypesNode::None, f);
921 }
922923 PatKind::Leaf { ref subpatterns } => {
924for subpattern in subpatterns {
925let subpattern_user_tys = user_tys.leaf(subpattern.field);
926debug!("visit_primary_bindings: subpattern_user_tys={subpattern_user_tys:?}");
927 visit_subpat(self, &subpattern.pattern, &subpattern_user_tys, f);
928 }
929 }
930931 PatKind::Variant { adt_def, args: _, variant_index, ref subpatterns } => {
932for subpattern in subpatterns {
933let subpattern_user_tys =
934 user_tys.variant(adt_def, variant_index, subpattern.field);
935 visit_subpat(self, &subpattern.pattern, &subpattern_user_tys, f);
936 }
937 }
938 PatKind::Or { ref pats } => {
939// In cases where we recover from errors the primary bindings
940 // may not all be in the leftmost subpattern. For example in
941 // `let (x | y) = ...`, the primary binding of `y` occurs in
942 // the right subpattern
943for subpattern in pats.iter() {
944 visit_subpat(self, subpattern, user_tys, f);
945 }
946 }
947 }
948 }
949}
950951/// Data extracted from a pattern that doesn't affect which branch is taken. Collected during
952/// pattern simplification and not mutated later.
953#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for PatternExtraData<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f,
"PatternExtraData", "span", &self.span, "bindings",
&self.bindings, "ascriptions", &self.ascriptions, "is_never",
&&self.is_never)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for PatternExtraData<'tcx> {
#[inline]
fn clone(&self) -> PatternExtraData<'tcx> {
PatternExtraData {
span: ::core::clone::Clone::clone(&self.span),
bindings: ::core::clone::Clone::clone(&self.bindings),
ascriptions: ::core::clone::Clone::clone(&self.ascriptions),
is_never: ::core::clone::Clone::clone(&self.is_never),
}
}
}Clone)]
954struct PatternExtraData<'tcx> {
955/// [`Span`] of the original pattern.
956span: Span,
957958/// Bindings that must be established.
959bindings: Vec<SubpatternBindings<'tcx>>,
960961/// Types that must be asserted.
962ascriptions: Vec<Ascription<'tcx>>,
963964/// Whether this corresponds to a never pattern.
965is_never: bool,
966}
967968impl<'tcx> PatternExtraData<'tcx> {
969fn is_empty(&self) -> bool {
970self.bindings.is_empty() && self.ascriptions.is_empty()
971 }
972}
973974#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for SubpatternBindings<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
SubpatternBindings::One(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "One",
&__self_0),
SubpatternBindings::FromOrPattern =>
::core::fmt::Formatter::write_str(f, "FromOrPattern"),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for SubpatternBindings<'tcx> {
#[inline]
fn clone(&self) -> SubpatternBindings<'tcx> {
match self {
SubpatternBindings::One(__self_0) =>
SubpatternBindings::One(::core::clone::Clone::clone(__self_0)),
SubpatternBindings::FromOrPattern =>
SubpatternBindings::FromOrPattern,
}
}
}Clone)]
975enum SubpatternBindings<'tcx> {
976/// A single binding.
977One(Binding<'tcx>),
978/// Holds the place for an or-pattern's bindings. This ensures their drops are scheduled in the
979 /// order the primary bindings appear. See rust-lang/rust#142163 for more information.
980FromOrPattern,
981}
982983/// A pattern in a form suitable for lowering the match tree, with all irrefutable
984/// patterns simplified away.
985///
986/// Here, "flat" indicates that irrefutable nodes in the pattern tree have been
987/// recursively replaced with their refutable subpatterns. They are not
988/// necessarily flat in an absolute sense.
989///
990/// Will typically be incorporated into a [`Candidate`].
991#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for FlatPat<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "FlatPat",
"match_pairs", &self.match_pairs, "extra_data", &&self.extra_data)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for FlatPat<'tcx> {
#[inline]
fn clone(&self) -> FlatPat<'tcx> {
FlatPat {
match_pairs: ::core::clone::Clone::clone(&self.match_pairs),
extra_data: ::core::clone::Clone::clone(&self.extra_data),
}
}
}Clone)]
992struct FlatPat<'tcx> {
993/// To match the pattern, all of these must be satisfied...
994match_pairs: Vec<MatchPairTree<'tcx>>,
995996 extra_data: PatternExtraData<'tcx>,
997}
998999impl<'tcx> FlatPat<'tcx> {
1000/// Creates a `FlatPat` containing a simplified [`MatchPairTree`] list/forest
1001 /// for the given pattern.
1002fn new(place: PlaceBuilder<'tcx>, pattern: &Pat<'tcx>, cx: &mut Builder<'_, 'tcx>) -> Self {
1003// Recursively build a tree of match pairs for the given pattern.
1004let mut match_pairs = ::alloc::vec::Vec::new()vec![];
1005let mut extra_data = PatternExtraData {
1006 span: pattern.span,
1007 bindings: Vec::new(),
1008 ascriptions: Vec::new(),
1009 is_never: pattern.is_never_pattern(),
1010 };
1011MatchPairTree::for_pattern(place, pattern, cx, &mut match_pairs, &mut extra_data);
10121013Self { match_pairs, extra_data }
1014 }
1015}
10161017/// Candidates are a generalization of (a) top-level match arms, and
1018/// (b) sub-branches of or-patterns, allowing the match-lowering process to handle
1019/// them both in a mostly-uniform way. For example, the list of candidates passed
1020/// to [`Builder::match_candidates`] will often contain a mixture of top-level
1021/// candidates and or-pattern subcandidates.
1022///
1023/// At the start of match lowering, there is one candidate for each match arm.
1024/// During match lowering, arms with or-patterns will be expanded into a tree
1025/// of candidates, where each "leaf" candidate represents one of the ways for
1026/// the arm pattern to successfully match.
1027#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Candidate<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["match_pairs", "subcandidates", "has_guard", "extra_data",
"or_span", "pre_binding_block", "otherwise_block",
"false_edge_start_block"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.match_pairs, &self.subcandidates, &self.has_guard,
&self.extra_data, &self.or_span, &self.pre_binding_block,
&self.otherwise_block, &&self.false_edge_start_block];
::core::fmt::Formatter::debug_struct_fields_finish(f, "Candidate",
names, values)
}
}Debug)]
1028struct Candidate<'tcx> {
1029/// For the candidate to match, all of these must be satisfied...
1030 ///
1031 /// ---
1032 /// Initially contains a list of match pairs created by [`FlatPat`], but is
1033 /// subsequently mutated (in a queue-like way) while lowering the match tree.
1034 /// When this list becomes empty, the candidate is fully matched and becomes
1035 /// a leaf (see [`Builder::select_matched_candidate`]).
1036 ///
1037 /// Key mutations include:
1038 ///
1039 /// - When a match pair is fully satisfied by a test, it is removed from the
1040 /// list, and its subpairs are added instead (see [`Builder::choose_bucket_for_candidate`]).
1041 /// - During or-pattern expansion, any leading or-pattern is removed, and is
1042 /// converted into subcandidates (see [`Builder::expand_and_match_or_candidates`]).
1043 /// - After a candidate's subcandidates have been lowered, a copy of any remaining
1044 /// or-patterns is added to each leaf subcandidate
1045 /// (see [`Builder::test_remaining_match_pairs_after_or`]).
1046 ///
1047 /// Invariants:
1048 /// - All or-patterns ([`TestableCase::Or`]) have been sorted to the end.
1049match_pairs: Vec<MatchPairTree<'tcx>>,
10501051/// ...and if this is non-empty, one of these subcandidates also has to match...
1052 ///
1053 /// ---
1054 /// Initially a candidate has no subcandidates; they are added (and then immediately
1055 /// lowered) during or-pattern expansion. Their main function is to serve as _output_
1056 /// of match tree lowering, allowing later steps to see the leaf candidates that
1057 /// represent a match of the entire match arm.
1058 ///
1059 /// A candidate no subcandidates is either incomplete (if it has match pairs left),
1060 /// or is a leaf in the match tree. A candidate with one or more subcandidates is
1061 /// an internal node in the match tree.
1062 ///
1063 /// Invariant: at the end of match tree lowering, this must not contain an
1064 /// `is_never` candidate, because that would break binding consistency.
1065 /// - See [`Builder::remove_never_subcandidates`].
1066subcandidates: Vec<Candidate<'tcx>>,
10671068/// ...and if there is a guard it must be evaluated; if it's `false` then branch to `otherwise_block`.
1069 ///
1070 /// ---
1071 /// For subcandidates, this is copied from the parent candidate, so it indicates
1072 /// whether the enclosing match arm has a guard.
1073has_guard: bool,
10741075/// Holds extra pattern data that was prepared by [`FlatPat`], including bindings and
1076 /// ascriptions that must be established if this candidate succeeds.
1077extra_data: PatternExtraData<'tcx>,
10781079/// When setting `self.subcandidates`, we store here the span of the or-pattern they came from.
1080 ///
1081 /// ---
1082 /// Invariant: it is `None` iff `subcandidates.is_empty()`.
1083 /// - FIXME: We sometimes don't unset this when clearing `subcandidates`.
1084or_span: Option<Span>,
10851086/// The block before the `bindings` have been established.
1087 ///
1088 /// After the match tree has been lowered, [`Builder::lower_match_arms`]
1089 /// will use this as the start point for lowering bindings and guards, and
1090 /// then jump to a shared block containing the arm body.
1091pre_binding_block: Option<BasicBlock>,
10921093/// The block to branch to if the guard or a nested candidate fails to match.
1094otherwise_block: Option<BasicBlock>,
10951096/// The earliest block that has only candidates >= this one as descendents. Used for false
1097 /// edges, see the doc for [`Builder::match_expr`].
1098false_edge_start_block: Option<BasicBlock>,
1099}
11001101impl<'tcx> Candidate<'tcx> {
1102fn new(
1103 place: PlaceBuilder<'tcx>,
1104 pattern: &Pat<'tcx>,
1105 has_guard: HasMatchGuard,
1106 cx: &mut Builder<'_, 'tcx>,
1107 ) -> Self {
1108// Use `FlatPat` to build simplified match pairs, then immediately
1109 // incorporate them into a new candidate.
1110Self::from_flat_pat(
1111FlatPat::new(place, pattern, cx),
1112#[allow(non_exhaustive_omitted_patterns)] match has_guard {
HasMatchGuard::Yes => true,
_ => false,
}matches!(has_guard, HasMatchGuard::Yes),
1113 )
1114 }
11151116/// Incorporates an already-simplified [`FlatPat`] into a new candidate.
1117fn from_flat_pat(flat_pat: FlatPat<'tcx>, has_guard: bool) -> Self {
1118let mut this = Candidate {
1119 match_pairs: flat_pat.match_pairs,
1120 extra_data: flat_pat.extra_data,
1121has_guard,
1122 subcandidates: Vec::new(),
1123 or_span: None,
1124 otherwise_block: None,
1125 pre_binding_block: None,
1126 false_edge_start_block: None,
1127 };
1128this.sort_match_pairs();
1129this1130 }
11311132/// Restores the invariant that or-patterns must be sorted to the end.
1133fn sort_match_pairs(&mut self) {
1134self.match_pairs.sort_by_key(|pair| #[allow(non_exhaustive_omitted_patterns)] match pair.testable_case {
TestableCase::Or { .. } => true,
_ => false,
}matches!(pair.testable_case, TestableCase::Or { .. }));
1135 }
11361137/// Returns whether the first match pair of this candidate is an or-pattern.
1138fn starts_with_or_pattern(&self) -> bool {
1139#[allow(non_exhaustive_omitted_patterns)] match &*self.match_pairs {
[MatchPairTree { testable_case: TestableCase::Or { .. }, .. }, ..] =>
true,
_ => false,
}matches!(
1140&*self.match_pairs,
1141 [MatchPairTree { testable_case: TestableCase::Or { .. }, .. }, ..]
1142 )1143 }
11441145/// Visit the leaf candidates (those with no subcandidates) contained in
1146 /// this candidate.
1147fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1148traverse_candidate(
1149self,
1150&mut (),
1151&mut move |c, _| visit_leaf(c),
1152move |c, _| c.subcandidates.iter_mut(),
1153 |_| {},
1154 );
1155 }
11561157/// Visit the leaf candidates in reverse order.
1158fn visit_leaves_rev<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1159traverse_candidate(
1160self,
1161&mut (),
1162&mut move |c, _| visit_leaf(c),
1163move |c, _| c.subcandidates.iter_mut().rev(),
1164 |_| {},
1165 );
1166 }
1167}
11681169/// A depth-first traversal of the `Candidate` and all of its recursive
1170/// subcandidates.
1171///
1172/// This signature is very generic, to support traversing candidate trees by
1173/// reference or by value, and to allow a mutable "context" to be shared by the
1174/// traversal callbacks. Most traversals can use the simpler
1175/// [`Candidate::visit_leaves`] wrapper instead.
1176fn traverse_candidate<'tcx, C, T, I>(
1177 candidate: C,
1178 context: &mut T,
1179// Called when visiting a "leaf" candidate (with no subcandidates).
1180visit_leaf: &mut impl FnMut(C, &mut T),
1181// Called when visiting a "node" candidate (with one or more subcandidates).
1182 // Returns an iterator over the candidate's children (by value or reference).
1183 // Can perform setup before visiting the node's children.
1184get_children: impl Copy + Fn(C, &mut T) -> I,
1185// Called after visiting a "node" candidate's children.
1186complete_children: impl Copy + Fn(&mut T),
1187) where
1188C: Borrow<Candidate<'tcx>>, // Typically `Candidate` or `&mut Candidate`
1189I: Iterator<Item = C>,
1190{
1191if candidate.borrow().subcandidates.is_empty() {
1192visit_leaf(candidate, context)
1193 } else {
1194for child in get_children(candidate, context) {
1195 traverse_candidate(child, context, visit_leaf, get_children, complete_children);
1196 }
1197complete_children(context)
1198 }
1199}
12001201#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for Binding<'tcx> {
#[inline]
fn clone(&self) -> Binding<'tcx> {
let _: ::core::clone::AssertParamIsClone<Span>;
let _: ::core::clone::AssertParamIsClone<Place<'tcx>>;
let _: ::core::clone::AssertParamIsClone<LocalVarId>;
let _: ::core::clone::AssertParamIsClone<BindingMode>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for Binding<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Binding<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f, "Binding",
"span", &self.span, "source", &self.source, "var_id",
&self.var_id, "binding_mode", &self.binding_mode, "is_shorthand",
&&self.is_shorthand)
}
}Debug)]
1202struct Binding<'tcx> {
1203 span: Span,
1204 source: Place<'tcx>,
1205 var_id: LocalVarId,
1206 binding_mode: BindingMode,
1207 is_shorthand: bool,
1208}
12091210/// Indicates that the type of `source` must be a subtype of the
1211/// user-given type `user_ty`; this is basically a no-op but can
1212/// influence region inference.
1213#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for Ascription<'tcx> {
#[inline]
fn clone(&self) -> Ascription<'tcx> {
Ascription {
source: ::core::clone::Clone::clone(&self.source),
annotation: ::core::clone::Clone::clone(&self.annotation),
variance: ::core::clone::Clone::clone(&self.variance),
}
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Ascription<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "Ascription",
"source", &self.source, "annotation", &self.annotation,
"variance", &&self.variance)
}
}Debug)]
1214struct Ascription<'tcx> {
1215 source: Place<'tcx>,
1216 annotation: CanonicalUserTypeAnnotation<'tcx>,
1217 variance: ty::Variance,
1218}
12191220/// Partial summary of a [`thir::Pat`], indicating what sort of test should be
1221/// performed to match/reject the pattern, and what the desired test outcome is.
1222/// This avoids having to perform a full match on [`thir::PatKind`] in some places,
1223/// and helps [`TestKind::Switch`] and [`TestKind::SwitchInt`] know what target
1224/// values to use.
1225///
1226/// Created by [`MatchPairTree::for_pattern`], and then inspected primarily by:
1227/// - [`Builder::pick_test_for_match_pair`] (to choose a test)
1228/// - [`Builder::choose_bucket_for_candidate`] (to see how the test interacts with a match pair)
1229///
1230/// Note that or-patterns are not tested directly like the other variants.
1231/// Instead they participate in or-pattern expansion, where they are transformed into
1232/// subcandidates. See [`Builder::expand_and_match_or_candidates`].
1233#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TestableCase<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TestableCase::Variant { adt_def: __self_0, variant_index: __self_1
} =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Variant", "adt_def", __self_0, "variant_index", &__self_1),
TestableCase::Constant { value: __self_0, kind: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Constant", "value", __self_0, "kind", &__self_1),
TestableCase::Range(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Range",
&__self_0),
TestableCase::Slice { len: __self_0, op: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "Slice",
"len", __self_0, "op", &__self_1),
TestableCase::Deref { temp: __self_0, mutability: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "Deref",
"temp", __self_0, "mutability", &__self_1),
TestableCase::Never =>
::core::fmt::Formatter::write_str(f, "Never"),
TestableCase::Or { pats: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f, "Or",
"pats", &__self_0),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TestableCase<'tcx> {
#[inline]
fn clone(&self) -> TestableCase<'tcx> {
match self {
TestableCase::Variant { adt_def: __self_0, variant_index: __self_1
} =>
TestableCase::Variant {
adt_def: ::core::clone::Clone::clone(__self_0),
variant_index: ::core::clone::Clone::clone(__self_1),
},
TestableCase::Constant { value: __self_0, kind: __self_1 } =>
TestableCase::Constant {
value: ::core::clone::Clone::clone(__self_0),
kind: ::core::clone::Clone::clone(__self_1),
},
TestableCase::Range(__self_0) =>
TestableCase::Range(::core::clone::Clone::clone(__self_0)),
TestableCase::Slice { len: __self_0, op: __self_1 } =>
TestableCase::Slice {
len: ::core::clone::Clone::clone(__self_0),
op: ::core::clone::Clone::clone(__self_1),
},
TestableCase::Deref { temp: __self_0, mutability: __self_1 } =>
TestableCase::Deref {
temp: ::core::clone::Clone::clone(__self_0),
mutability: ::core::clone::Clone::clone(__self_1),
},
TestableCase::Never => TestableCase::Never,
TestableCase::Or { pats: __self_0 } =>
TestableCase::Or {
pats: ::core::clone::Clone::clone(__self_0),
},
}
}
}Clone)]
1234enum TestableCase<'tcx> {
1235 Variant { adt_def: ty::AdtDef<'tcx>, variant_index: VariantIdx },
1236 Constant { value: ty::Value<'tcx>, kind: PatConstKind },
1237 Range(Arc<PatRange<'tcx>>),
1238 Slice { len: u64, op: SliceLenOp },
1239 Deref { temp: Place<'tcx>, mutability: Mutability },
1240 Never,
1241 Or { pats: Box<[FlatPat<'tcx>]> },
1242}
12431244impl<'tcx> TestableCase<'tcx> {
1245fn as_range(&self) -> Option<&PatRange<'tcx>> {
1246if let Self::Range(v) = self { Some(v.as_ref()) } else { None }
1247 }
1248}
12491250/// Sub-classification of [`TestableCase::Constant`], which helps to avoid
1251/// some redundant ad-hoc checks when preparing and lowering tests.
1252#[derive(#[automatically_derived]
impl ::core::fmt::Debug for PatConstKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
PatConstKind::Bool => "Bool",
PatConstKind::IntOrChar => "IntOrChar",
PatConstKind::Float => "Float",
PatConstKind::String => "String",
PatConstKind::Other => "Other",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for PatConstKind {
#[inline]
fn clone(&self) -> PatConstKind {
match self {
PatConstKind::Bool => PatConstKind::Bool,
PatConstKind::IntOrChar => PatConstKind::IntOrChar,
PatConstKind::Float => PatConstKind::Float,
PatConstKind::String => PatConstKind::String,
PatConstKind::Other => PatConstKind::Other,
}
}
}Clone)]
1253enum PatConstKind {
1254/// The primitive `bool` type, which is like an integer but simpler,
1255 /// having only two values.
1256Bool,
1257/// Primitive unsigned/signed integer types, plus `char`.
1258 /// These types interact nicely with `SwitchInt`.
1259IntOrChar,
1260/// Floating-point primitives, e.g. `f32`, `f64`.
1261 /// These types don't support `SwitchInt` and require an equality test,
1262 /// but can also interact with range pattern tests.
1263Float,
1264/// Constant string values, tested via string equality.
1265String,
1266/// Any other constant-pattern is usually tested via some kind of equality
1267 /// check. Types that might be encountered here include:
1268 /// - raw pointers derived from integer values
1269 /// - pattern types, e.g. `pattern_type!(u32 is 1..)`
1270Other,
1271}
12721273/// Node in a tree of "match pairs", where each pair consists of a place to be
1274/// tested, and a test to perform on that place.
1275///
1276/// Each node also has a list of subpairs (possibly empty) that must also match,
1277/// and a reference to the THIR pattern it represents.
1278#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for MatchPairTree<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f, "MatchPairTree",
"place", &self.place, "testable_case", &self.testable_case,
"subpairs", &self.subpairs, "pattern_ty", &self.pattern_ty,
"pattern_span", &&self.pattern_span)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for MatchPairTree<'tcx> {
#[inline]
fn clone(&self) -> MatchPairTree<'tcx> {
MatchPairTree {
place: ::core::clone::Clone::clone(&self.place),
testable_case: ::core::clone::Clone::clone(&self.testable_case),
subpairs: ::core::clone::Clone::clone(&self.subpairs),
pattern_ty: ::core::clone::Clone::clone(&self.pattern_ty),
pattern_span: ::core::clone::Clone::clone(&self.pattern_span),
}
}
}Clone)]
1279pub(crate) struct MatchPairTree<'tcx> {
1280/// This place...
1281 ///
1282 /// ---
1283 /// This can be `None` if it referred to a non-captured place in a closure.
1284 ///
1285 /// Invariant: Can only be `None` when `testable_case` is `Or`.
1286 /// Therefore this must be `Some(_)` after or-pattern expansion.
1287place: Option<Place<'tcx>>,
12881289/// ... must pass this test...
1290testable_case: TestableCase<'tcx>,
12911292/// ... and these subpairs must match.
1293 ///
1294 /// ---
1295 /// Subpairs typically represent tests that can only be performed after their
1296 /// parent has succeeded. For example, the pattern `Some(3)` might have an
1297 /// outer match pair that tests for the variant `Some`, and then a subpair
1298 /// that tests its field for the value `3`.
1299subpairs: Vec<Self>,
13001301/// Type field of the pattern this node was created from.
1302pattern_ty: Ty<'tcx>,
1303/// Span field of the pattern this node was created from.
1304pattern_span: Span,
1305}
13061307/// A runtime test to perform to determine which candidates match a scrutinee place.
1308///
1309/// The kind of test to perform is indicated by [`TestKind`].
1310#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Test<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Test", "span",
&self.span, "kind", &&self.kind)
}
}Debug)]
1311pub(crate) struct Test<'tcx> {
1312 span: Span,
1313 kind: TestKind<'tcx>,
1314}
13151316/// The kind of runtime test to perform to determine which candidates match a
1317/// scrutinee place. This is the main component of [`Test`].
1318///
1319/// Some of these variants don't contain the constant value(s) being tested
1320/// against, because those values are stored in the corresponding bucketed
1321/// candidates instead.
1322#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for TestKind<'tcx> {
#[inline]
fn clone(&self) -> TestKind<'tcx> {
match self {
TestKind::Switch { adt_def: __self_0 } =>
TestKind::Switch {
adt_def: ::core::clone::Clone::clone(__self_0),
},
TestKind::SwitchInt => TestKind::SwitchInt,
TestKind::If => TestKind::If,
TestKind::StringEq { value: __self_0 } =>
TestKind::StringEq {
value: ::core::clone::Clone::clone(__self_0),
},
TestKind::ScalarEq { value: __self_0 } =>
TestKind::ScalarEq {
value: ::core::clone::Clone::clone(__self_0),
},
TestKind::Range(__self_0) =>
TestKind::Range(::core::clone::Clone::clone(__self_0)),
TestKind::SliceLen { len: __self_0, op: __self_1 } =>
TestKind::SliceLen {
len: ::core::clone::Clone::clone(__self_0),
op: ::core::clone::Clone::clone(__self_1),
},
TestKind::Deref { temp: __self_0, mutability: __self_1 } =>
TestKind::Deref {
temp: ::core::clone::Clone::clone(__self_0),
mutability: ::core::clone::Clone::clone(__self_1),
},
TestKind::Never => TestKind::Never,
}
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TestKind<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TestKind::Switch { adt_def: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Switch", "adt_def", &__self_0),
TestKind::SwitchInt =>
::core::fmt::Formatter::write_str(f, "SwitchInt"),
TestKind::If => ::core::fmt::Formatter::write_str(f, "If"),
TestKind::StringEq { value: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"StringEq", "value", &__self_0),
TestKind::ScalarEq { value: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"ScalarEq", "value", &__self_0),
TestKind::Range(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Range",
&__self_0),
TestKind::SliceLen { len: __self_0, op: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"SliceLen", "len", __self_0, "op", &__self_1),
TestKind::Deref { temp: __self_0, mutability: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "Deref",
"temp", __self_0, "mutability", &__self_1),
TestKind::Never => ::core::fmt::Formatter::write_str(f, "Never"),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for TestKind<'tcx> {
#[inline]
fn eq(&self, other: &TestKind<'tcx>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(TestKind::Switch { adt_def: __self_0 }, TestKind::Switch {
adt_def: __arg1_0 }) => __self_0 == __arg1_0,
(TestKind::StringEq { value: __self_0 }, TestKind::StringEq {
value: __arg1_0 }) => __self_0 == __arg1_0,
(TestKind::ScalarEq { value: __self_0 }, TestKind::ScalarEq {
value: __arg1_0 }) => __self_0 == __arg1_0,
(TestKind::Range(__self_0), TestKind::Range(__arg1_0)) =>
__self_0 == __arg1_0,
(TestKind::SliceLen { len: __self_0, op: __self_1 },
TestKind::SliceLen { len: __arg1_0, op: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(TestKind::Deref { temp: __self_0, mutability: __self_1 },
TestKind::Deref { temp: __arg1_0, mutability: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq)]
1323enum TestKind<'tcx> {
1324/// Test what enum variant a value is.
1325 ///
1326 /// The subset of expected variants is not stored here; instead they are
1327 /// extracted from the [`TestableCase`]s of the candidates participating in the
1328 /// test.
1329Switch {
1330/// The enum type being tested.
1331adt_def: ty::AdtDef<'tcx>,
1332 },
13331334/// Test what value an integer or `char` has.
1335 ///
1336 /// The test's target values are not stored here; instead they are extracted
1337 /// from the [`TestableCase`]s of the candidates participating in the test.
1338SwitchInt,
13391340/// Test whether a `bool` is `true` or `false`.
1341If,
13421343/// Tests the place against a string constant using string equality.
1344StringEq {
1345/// Constant string value to test against.
1346 /// Note that this value has type `str` (not `&str`).
1347value: ty::Value<'tcx>,
1348 },
13491350/// Tests the place against a constant using scalar equality.
1351ScalarEq { value: ty::Value<'tcx> },
13521353/// Test whether the value falls within an inclusive or exclusive range.
1354Range(Arc<PatRange<'tcx>>),
13551356/// Test that the length of the slice is `== len` or `>= len`.
1357SliceLen { len: u64, op: SliceLenOp },
13581359/// Call `Deref::deref[_mut]` on the value.
1360Deref {
1361/// Temporary to store the result of `deref()`/`deref_mut()`.
1362temp: Place<'tcx>,
1363 mutability: Mutability,
1364 },
13651366/// Assert unreachability of never patterns.
1367Never,
1368}
13691370/// Indicates the kind of slice-length constraint imposed by a slice pattern,
1371/// or its corresponding test.
1372#[derive(#[automatically_derived]
impl ::core::fmt::Debug for SliceLenOp {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
SliceLenOp::Equal => "Equal",
SliceLenOp::GreaterOrEqual => "GreaterOrEqual",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for SliceLenOp {
#[inline]
fn clone(&self) -> SliceLenOp { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for SliceLenOp { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for SliceLenOp {
#[inline]
fn eq(&self, other: &SliceLenOp) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
1373enum SliceLenOp {
1374/// The slice pattern can only match a slice with exactly `len` elements.
1375Equal,
1376/// The slice pattern can match a slice with `len` or more elements
1377 /// (i.e. it contains a `..` subpattern in the middle).
1378GreaterOrEqual,
1379}
13801381/// The branch to be taken after a test.
1382#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TestBranch<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TestBranch::Success =>
::core::fmt::Formatter::write_str(f, "Success"),
TestBranch::Constant(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Constant", &__self_0),
TestBranch::Variant(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Variant", &__self_0),
TestBranch::Failure =>
::core::fmt::Formatter::write_str(f, "Failure"),
}
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TestBranch<'tcx> {
#[inline]
fn clone(&self) -> TestBranch<'tcx> {
let _: ::core::clone::AssertParamIsClone<ty::Value<'tcx>>;
let _: ::core::clone::AssertParamIsClone<VariantIdx>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for TestBranch<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for TestBranch<'tcx> {
#[inline]
fn eq(&self, other: &TestBranch<'tcx>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(TestBranch::Constant(__self_0),
TestBranch::Constant(__arg1_0)) => __self_0 == __arg1_0,
(TestBranch::Variant(__self_0), TestBranch::Variant(__arg1_0))
=> __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for TestBranch<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<ty::Value<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<VariantIdx>;
}
}Eq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for TestBranch<'tcx> {
#[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);
match self {
TestBranch::Constant(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
TestBranch::Variant(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
1383enum TestBranch<'tcx> {
1384/// Success branch, used for tests with two possible outcomes.
1385Success,
1386/// Branch corresponding to this constant. Must be a scalar.
1387Constant(ty::Value<'tcx>),
1388/// Branch corresponding to this variant.
1389Variant(VariantIdx),
1390/// Failure branch for tests with two possible outcomes, and "otherwise" branch for other tests.
1391Failure,
1392}
13931394impl<'tcx> TestBranch<'tcx> {
1395fn as_constant(&self) -> Option<ty::Value<'tcx>> {
1396if let Self::Constant(v) = self { Some(*v) } else { None }
1397 }
1398}
13991400/// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether
1401/// a match arm has a guard expression attached to it.
1402#[derive(#[automatically_derived]
impl ::core::marker::Copy for ArmHasGuard { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ArmHasGuard {
#[inline]
fn clone(&self) -> ArmHasGuard {
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ArmHasGuard {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "ArmHasGuard",
&&self.0)
}
}Debug)]
1403pub(crate) struct ArmHasGuard(pub(crate) bool);
14041405///////////////////////////////////////////////////////////////////////////
1406// Main matching algorithm
14071408/// A sub-branch in the output of match lowering. Match lowering has generated MIR code that will
1409/// branch to `success_block` when the matched value matches the corresponding pattern. If there is
1410/// a guard, its failure must continue to `otherwise_block`, which will resume testing patterns.
1411#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for MatchTreeSubBranch<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["span", "success_block", "otherwise_block", "bindings",
"ascriptions", "is_never"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.span, &self.success_block, &self.otherwise_block,
&self.bindings, &self.ascriptions, &&self.is_never];
::core::fmt::Formatter::debug_struct_fields_finish(f,
"MatchTreeSubBranch", names, values)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for MatchTreeSubBranch<'tcx> {
#[inline]
fn clone(&self) -> MatchTreeSubBranch<'tcx> {
MatchTreeSubBranch {
span: ::core::clone::Clone::clone(&self.span),
success_block: ::core::clone::Clone::clone(&self.success_block),
otherwise_block: ::core::clone::Clone::clone(&self.otherwise_block),
bindings: ::core::clone::Clone::clone(&self.bindings),
ascriptions: ::core::clone::Clone::clone(&self.ascriptions),
is_never: ::core::clone::Clone::clone(&self.is_never),
}
}
}Clone)]
1412struct MatchTreeSubBranch<'tcx> {
1413 span: Span,
1414/// The block that is branched to if the corresponding subpattern matches.
1415success_block: BasicBlock,
1416/// The block to branch to if this arm had a guard and the guard fails.
1417otherwise_block: BasicBlock,
1418/// The bindings to set up in this sub-branch.
1419bindings: Vec<Binding<'tcx>>,
1420/// The ascriptions to set up in this sub-branch.
1421ascriptions: Vec<Ascription<'tcx>>,
1422/// Whether the sub-branch corresponds to a never pattern.
1423is_never: bool,
1424}
14251426/// A branch in the output of match lowering.
1427#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for MatchTreeBranch<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"MatchTreeBranch", "sub_branches", &&self.sub_branches)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for MatchTreeBranch<'tcx> {
#[inline]
fn clone(&self) -> MatchTreeBranch<'tcx> {
MatchTreeBranch {
sub_branches: ::core::clone::Clone::clone(&self.sub_branches),
}
}
}Clone)]
1428struct MatchTreeBranch<'tcx> {
1429 sub_branches: Vec<MatchTreeSubBranch<'tcx>>,
1430}
14311432/// The result of generating MIR for a pattern-matching expression. Each input branch/arm/pattern
1433/// gives rise to an output `MatchTreeBranch`. If one of the patterns matches, we branch to the
1434/// corresponding `success_block`. If none of the patterns matches, we branch to `otherwise_block`.
1435///
1436/// Each branch is made of one of more sub-branches, corresponding to or-patterns. E.g.
1437/// ```ignore(illustrative)
1438/// match foo {
1439/// (x, false) | (false, x) => {}
1440/// (true, true) => {}
1441/// }
1442/// ```
1443/// Here the first arm gives the first `MatchTreeBranch`, which has two sub-branches, one for each
1444/// alternative of the or-pattern. They are kept separate because each needs to bind `x` to a
1445/// different place.
1446#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for BuiltMatchTree<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f,
"BuiltMatchTree", "branches", &self.branches, "otherwise_block",
&self.otherwise_block, "fake_borrow_temps",
&&self.fake_borrow_temps)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for BuiltMatchTree<'tcx> {
#[inline]
fn clone(&self) -> BuiltMatchTree<'tcx> {
BuiltMatchTree {
branches: ::core::clone::Clone::clone(&self.branches),
otherwise_block: ::core::clone::Clone::clone(&self.otherwise_block),
fake_borrow_temps: ::core::clone::Clone::clone(&self.fake_borrow_temps),
}
}
}Clone)]
1447pub(crate) struct BuiltMatchTree<'tcx> {
1448 branches: Vec<MatchTreeBranch<'tcx>>,
1449 otherwise_block: BasicBlock,
1450/// If any of the branches had a guard, we collect here the places and locals to fakely borrow
1451 /// to ensure match guards can't modify the values as we match them. For more details, see
1452 /// [`util::collect_fake_borrows`].
1453fake_borrow_temps: Vec<(Place<'tcx>, Local, FakeBorrowKind)>,
1454}
14551456impl<'tcx> MatchTreeSubBranch<'tcx> {
1457fn from_sub_candidate(
1458 candidate: Candidate<'tcx>,
1459 parent_data: &Vec<PatternExtraData<'tcx>>,
1460 ) -> Self {
1461if true {
if !candidate.match_pairs.is_empty() {
::core::panicking::panic("assertion failed: candidate.match_pairs.is_empty()")
};
};debug_assert!(candidate.match_pairs.is_empty());
1462MatchTreeSubBranch {
1463 span: candidate.extra_data.span,
1464 success_block: candidate.pre_binding_block.unwrap(),
1465 otherwise_block: candidate.otherwise_block.unwrap(),
1466 bindings: sub_branch_bindings(parent_data, &candidate.extra_data.bindings),
1467 ascriptions: parent_data1468 .iter()
1469 .flat_map(|d| &d.ascriptions)
1470 .cloned()
1471 .chain(candidate.extra_data.ascriptions)
1472 .collect(),
1473 is_never: candidate.extra_data.is_never,
1474 }
1475 }
1476}
14771478impl<'tcx> MatchTreeBranch<'tcx> {
1479fn from_candidate(candidate: Candidate<'tcx>) -> Self {
1480let mut sub_branches = Vec::new();
1481traverse_candidate(
1482candidate,
1483&mut Vec::new(),
1484&mut |candidate: Candidate<'_>, parent_data: &mut Vec<PatternExtraData<'_>>| {
1485sub_branches.push(MatchTreeSubBranch::from_sub_candidate(candidate, parent_data));
1486 },
1487 |inner_candidate, parent_data| {
1488parent_data.push(inner_candidate.extra_data);
1489inner_candidate.subcandidates.into_iter()
1490 },
1491 |parent_data| {
1492parent_data.pop();
1493 },
1494 );
1495MatchTreeBranch { sub_branches }
1496 }
1497}
14981499/// Collects the bindings for a [`MatchTreeSubBranch`], preserving the order they appear in the
1500/// pattern, as though the or-alternatives chosen in this sub-branch were inlined.
1501fn sub_branch_bindings<'tcx>(
1502 parents: &[PatternExtraData<'tcx>],
1503 leaf_bindings: &[SubpatternBindings<'tcx>],
1504) -> Vec<Binding<'tcx>> {
1505// In the common case, all bindings will be in leaves. Allocate to fit the leaf's bindings.
1506let mut all_bindings = Vec::with_capacity(leaf_bindings.len());
1507let mut remainder = parents1508 .iter()
1509 .map(|parent| parent.bindings.as_slice())
1510 .chain([leaf_bindings])
1511// Skip over unsimplified or-patterns without bindings.
1512.filter(|bindings| !bindings.is_empty());
1513if let Some(candidate_bindings) = remainder.next() {
1514push_sub_branch_bindings(&mut all_bindings, candidate_bindings, &mut remainder);
1515 }
1516// Make sure we've included all bindings. For ill-formed patterns like `(x, _ | y)`, we may not
1517 // have collected all bindings yet, since we only check the first alternative when determining
1518 // whether to inline subcandidates' bindings.
1519 // FIXME(@dianne): prevent ill-formed patterns from getting here
1520while let Some(candidate_bindings) = remainder.next() {
1521 ty::tls::with(|tcx| {
1522 tcx.dcx().delayed_bug("mismatched or-pattern bindings but no error emitted")
1523 });
1524// To recover, we collect the rest in an arbitrary order.
1525push_sub_branch_bindings(&mut all_bindings, candidate_bindings, &mut remainder);
1526 }
1527all_bindings1528}
15291530/// Helper for [`sub_branch_bindings`]. Collects bindings from `candidate_bindings` into
1531/// `flattened`. Bindings in or-patterns are collected recursively from `remainder`.
1532fn push_sub_branch_bindings<'c, 'tcx: 'c>(
1533 flattened: &mut Vec<Binding<'tcx>>,
1534 candidate_bindings: &'c [SubpatternBindings<'tcx>],
1535 remainder: &mut impl Iterator<Item = &'c [SubpatternBindings<'tcx>]>,
1536) {
1537for subpat_bindings in candidate_bindings {
1538match subpat_bindings {
1539 SubpatternBindings::One(binding) => flattened.push(*binding),
1540 SubpatternBindings::FromOrPattern => {
1541// Inline bindings from an or-pattern. By construction, this always
1542 // corresponds to a subcandidate and its closest descendants (i.e. those
1543 // from nested or-patterns, but not adjacent or-patterns). To handle
1544 // adjacent or-patterns, e.g. `(x | x, y | y)`, we update the `remainder` to
1545 // point to the first descendant candidate from outside this or-pattern.
1546if let Some(subcandidate_bindings) = remainder.next() {
1547 push_sub_branch_bindings(flattened, subcandidate_bindings, remainder);
1548 } else {
1549// For ill-formed patterns like `x | _`, we may not have any subcandidates left
1550 // to inline bindings from.
1551 // FIXME(@dianne): prevent ill-formed patterns from getting here
1552ty::tls::with(|tcx| {
1553 tcx.dcx().delayed_bug("mismatched or-pattern bindings but no error emitted")
1554 });
1555 };
1556 }
1557 }
1558 }
1559}
15601561#[derive(#[automatically_derived]
impl ::core::fmt::Debug for HasMatchGuard {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
HasMatchGuard::Yes => "Yes",
HasMatchGuard::No => "No",
})
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for HasMatchGuard {
#[inline]
fn clone(&self) -> HasMatchGuard { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for HasMatchGuard { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for HasMatchGuard {
#[inline]
fn eq(&self, other: &HasMatchGuard) -> 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 HasMatchGuard {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
1562pub(crate) enum HasMatchGuard {
1563 Yes,
1564 No,
1565}
15661567impl<'a, 'tcx> Builder<'a, 'tcx> {
1568/// The entrypoint of the matching algorithm. Create the decision tree for the match expression,
1569 /// starting from `block`.
1570 ///
1571 /// `patterns` is a list of patterns, one for each arm. The associated boolean indicates whether
1572 /// the arm has a guard.
1573 ///
1574 /// `refutable` indicates whether the candidate list is refutable (for `if let` and `let else`)
1575 /// or not (for `let` and `match`). In the refutable case we return the block to which we branch
1576 /// on failure.
1577pub(crate) fn lower_match_tree(
1578&mut self,
1579 block: BasicBlock,
1580 scrutinee_span: Span,
1581 scrutinee_place_builder: &PlaceBuilder<'tcx>,
1582 match_start_span: Span,
1583 patterns: Vec<(&Pat<'tcx>, HasMatchGuard)>,
1584 refutable: bool,
1585 ) -> BuiltMatchTree<'tcx> {
1586// Assemble the initial list of candidates. These top-level candidates are 1:1 with the
1587 // input patterns, but other parts of match lowering also introduce subcandidates (for
1588 // sub-or-patterns). So inside the algorithm, the candidates list may not correspond to
1589 // match arms directly.
1590let mut candidates: Vec<Candidate<'_>> = patterns1591 .into_iter()
1592 .map(|(pat, has_guard)| {
1593Candidate::new(scrutinee_place_builder.clone(), pat, has_guard, self)
1594 })
1595 .collect();
15961597let fake_borrow_temps = util::collect_fake_borrows(
1598self,
1599&candidates,
1600scrutinee_span,
1601scrutinee_place_builder.base(),
1602 );
16031604// This will generate code to test scrutinee_place and branch to the appropriate arm block.
1605 // If none of the arms match, we branch to `otherwise_block`. When lowering a `match`
1606 // expression, exhaustiveness checking ensures that this block is unreachable.
1607let mut candidate_refs = candidates.iter_mut().collect::<Vec<_>>();
1608let otherwise_block =
1609self.match_candidates(match_start_span, scrutinee_span, block, &mut candidate_refs);
16101611// Set up false edges so that the borrow-checker cannot make use of the specific CFG we
1612 // generated. We falsely branch from each candidate to the one below it to make it as if we
1613 // were testing match branches one by one in order. In the refutable case we also want a
1614 // false edge to the final failure block.
1615let mut next_candidate_start_block = if refutable { Some(otherwise_block) } else { None };
1616for candidate in candidates.iter_mut().rev() {
1617let has_guard = candidate.has_guard;
1618 candidate.visit_leaves_rev(|leaf_candidate| {
1619if let Some(next_candidate_start_block) = next_candidate_start_block {
1620let source_info = self.source_info(leaf_candidate.extra_data.span);
1621// Falsely branch to `next_candidate_start_block` before reaching pre_binding.
1622let old_pre_binding = leaf_candidate.pre_binding_block.unwrap();
1623let new_pre_binding = self.cfg.start_new_block();
1624self.false_edges(
1625 old_pre_binding,
1626 new_pre_binding,
1627 next_candidate_start_block,
1628 source_info,
1629 );
1630 leaf_candidate.pre_binding_block = Some(new_pre_binding);
1631if has_guard {
1632// Falsely branch to `next_candidate_start_block` also if the guard fails.
1633let new_otherwise = self.cfg.start_new_block();
1634let old_otherwise = leaf_candidate.otherwise_block.unwrap();
1635self.false_edges(
1636 new_otherwise,
1637 old_otherwise,
1638 next_candidate_start_block,
1639 source_info,
1640 );
1641 leaf_candidate.otherwise_block = Some(new_otherwise);
1642 }
1643 }
1644if !leaf_candidate.false_edge_start_block.is_some() {
::core::panicking::panic("assertion failed: leaf_candidate.false_edge_start_block.is_some()")
};assert!(leaf_candidate.false_edge_start_block.is_some());
1645 next_candidate_start_block = leaf_candidate.false_edge_start_block;
1646 });
1647 }
16481649if !refutable {
1650// Match checking ensures `otherwise_block` is actually unreachable in irrefutable
1651 // cases.
1652let source_info = self.source_info(scrutinee_span);
16531654// Matching on a scrutinee place of an uninhabited type doesn't generate any memory
1655 // reads by itself, and so if the place is uninitialized we wouldn't know. In order to
1656 // disallow the following:
1657 // ```rust
1658 // let x: !;
1659 // match x {}
1660 // ```
1661 // we add a dummy read on the place.
1662 //
1663 // NOTE: If we require never patterns for empty matches, those will check that the place
1664 // is initialized, and so this read would no longer be needed.
1665let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
16661667if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
1668self.cfg.push_fake_read(
1669otherwise_block,
1670source_info,
1671cause_matched_place,
1672scrutinee_place,
1673 );
1674 }
16751676self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable);
1677 }
16781679BuiltMatchTree {
1680 branches: candidates.into_iter().map(MatchTreeBranch::from_candidate).collect(),
1681otherwise_block,
1682fake_borrow_temps,
1683 }
1684 }
16851686/// The main match algorithm. It begins with a set of candidates `candidates` and has the job of
1687 /// generating code that branches to an appropriate block if the scrutinee matches one of these
1688 /// candidates. The
1689 /// candidates are ordered such that the first item in the list
1690 /// has the highest priority. When a candidate is found to match
1691 /// the value, we will set and generate a branch to the appropriate
1692 /// pre-binding block.
1693 ///
1694 /// If none of the candidates apply, we continue to the returned `otherwise_block`.
1695 ///
1696 /// Note that while `match` expressions in the Rust language are exhaustive,
1697 /// candidate lists passed to this method are often _non-exhaustive_.
1698 /// For example, the match lowering process will frequently divide up the
1699 /// list of candidates, and recursively call this method with a non-exhaustive
1700 /// subset of candidates.
1701 /// See [`Builder::test_candidates`] for more details on this
1702 /// "backtracking automata" approach.
1703 ///
1704 /// For an example of how we use `otherwise_block`, consider:
1705 /// ```
1706 /// # fn foo((x, y): (bool, bool)) -> u32 {
1707 /// match (x, y) {
1708 /// (true, true) => 1,
1709 /// (_, false) => 2,
1710 /// (false, true) => 3,
1711 /// }
1712 /// # }
1713 /// ```
1714 /// For this match, we generate something like:
1715 /// ```
1716 /// # fn foo((x, y): (bool, bool)) -> u32 {
1717 /// if x {
1718 /// if y {
1719 /// return 1
1720 /// } else {
1721 /// // continue
1722 /// }
1723 /// } else {
1724 /// // continue
1725 /// }
1726 /// if y {
1727 /// if x {
1728 /// // This is actually unreachable because the `(true, true)` case was handled above,
1729 /// // but we don't know that from within the lowering algorithm.
1730 /// // continue
1731 /// } else {
1732 /// return 3
1733 /// }
1734 /// } else {
1735 /// return 2
1736 /// }
1737 /// // this is the final `otherwise_block`, which is unreachable because the match was exhaustive.
1738 /// unreachable!()
1739 /// # }
1740 /// ```
1741 ///
1742 /// Every `continue` is an instance of branching to some `otherwise_block` somewhere deep within
1743 /// the algorithm. For more details on why we lower like this, see [`Builder::test_candidates`].
1744 ///
1745 /// Note how we test `x` twice. This is the tradeoff of backtracking automata: we prefer smaller
1746 /// code size so we accept non-optimal code paths.
1747#[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("match_candidates",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(1747u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["span",
"scrutinee_span", "start_block", "candidates"],
::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(&scrutinee_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(&start_block)
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(&candidates)
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: BasicBlock = loop {};
return __tracing_attr_fake_return;
}
{
ensure_sufficient_stack(||
{
self.match_candidates_inner(span, scrutinee_span,
start_block, candidates)
})
}
}
}#[instrument(skip(self), level = "debug")]1748fn match_candidates(
1749&mut self,
1750 span: Span,
1751 scrutinee_span: Span,
1752 start_block: BasicBlock,
1753 candidates: &mut [&mut Candidate<'tcx>],
1754 ) -> BasicBlock {
1755 ensure_sufficient_stack(|| {
1756self.match_candidates_inner(span, scrutinee_span, start_block, candidates)
1757 })
1758 }
17591760/// Construct the decision tree for `candidates`. Don't call this, call `match_candidates`
1761 /// instead to reserve sufficient stack space.
1762fn match_candidates_inner(
1763&mut self,
1764 span: Span,
1765 scrutinee_span: Span,
1766mut start_block: BasicBlock,
1767 candidates: &mut [&mut Candidate<'tcx>],
1768 ) -> BasicBlock {
1769if let [first, ..] = candidates {
1770if first.false_edge_start_block.is_none() {
1771first.false_edge_start_block = Some(start_block);
1772 }
1773 }
17741775// Process a prefix of the candidates.
1776let rest = match candidates {
1777 [] => {
1778// If there are no candidates that still need testing, we're done.
1779return start_block;
1780 }
1781 [first, remaining @ ..] if first.match_pairs.is_empty() => {
1782// The first candidate has satisfied all its match pairs.
1783 // We record the blocks that will be needed by match arm lowering,
1784 // and then continue with the remaining candidates.
1785let remainder_start = self.select_matched_candidate(first, start_block);
1786remainder_start.and(remaining)
1787 }
1788 candidates if candidates.iter().any(|candidate| candidate.starts_with_or_pattern()) => {
1789// If any candidate starts with an or-pattern, we want to expand or-patterns
1790 // before we do any more tests.
1791 //
1792 // The only candidate we strictly _need_ to expand here is the first one.
1793 // But by expanding other candidates as early as possible, we unlock more
1794 // opportunities to include them in test outcomes, making the match tree
1795 // smaller and simpler.
1796self.expand_and_match_or_candidates(span, scrutinee_span, start_block, candidates)
1797 }
1798 candidates => {
1799// The first candidate has some unsatisfied match pairs; we proceed to do more tests.
1800self.test_candidates(span, scrutinee_span, candidates, start_block)
1801 }
1802 };
18031804// Process any candidates that remain.
1805let remaining_candidates = { let BlockAnd(b, v) = rest; start_block = b; v }unpack!(start_block = rest);
1806self.match_candidates(span, scrutinee_span, start_block, remaining_candidates)
1807 }
18081809/// Link up matched candidates.
1810 ///
1811 /// For example, if we have something like this:
1812 ///
1813 /// ```ignore (illustrative)
1814 /// ...
1815 /// Some(x) if cond1 => ...
1816 /// Some(x) => ...
1817 /// Some(x) if cond2 => ...
1818 /// ...
1819 /// ```
1820 ///
1821 /// We generate real edges from:
1822 ///
1823 /// * `start_block` to the [pre-binding block] of the first pattern,
1824 /// * the [otherwise block] of the first pattern to the second pattern,
1825 /// * the [otherwise block] of the third pattern to a block with an
1826 /// [`Unreachable` terminator](TerminatorKind::Unreachable).
1827 ///
1828 /// In addition, we later add fake edges from the otherwise blocks to the
1829 /// pre-binding block of the next candidate in the original set of
1830 /// candidates.
1831 ///
1832 /// [pre-binding block]: Candidate::pre_binding_block
1833 /// [otherwise block]: Candidate::otherwise_block
1834fn select_matched_candidate(
1835&mut self,
1836 candidate: &mut Candidate<'tcx>,
1837 start_block: BasicBlock,
1838 ) -> BasicBlock {
1839if !candidate.otherwise_block.is_none() {
::core::panicking::panic("assertion failed: candidate.otherwise_block.is_none()")
};assert!(candidate.otherwise_block.is_none());
1840if !candidate.pre_binding_block.is_none() {
::core::panicking::panic("assertion failed: candidate.pre_binding_block.is_none()")
};assert!(candidate.pre_binding_block.is_none());
1841if !candidate.subcandidates.is_empty() {
::core::panicking::panic("assertion failed: candidate.subcandidates.is_empty()")
};assert!(candidate.subcandidates.is_empty());
18421843candidate.pre_binding_block = Some(start_block);
1844let otherwise_block = self.cfg.start_new_block();
1845// Create the otherwise block for this candidate, which is the
1846 // pre-binding block for the next candidate.
1847candidate.otherwise_block = Some(otherwise_block);
1848otherwise_block1849 }
18501851/// Takes a list of candidates such that some of the candidates' first match pairs are
1852 /// or-patterns. This expands as many or-patterns as possible and processes the resulting
1853 /// candidates. Returns the unprocessed candidates if any.
1854fn expand_and_match_or_candidates<'b, 'c>(
1855&mut self,
1856 span: Span,
1857 scrutinee_span: Span,
1858 start_block: BasicBlock,
1859 candidates: &'b mut [&'c mut Candidate<'tcx>],
1860 ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> {
1861// We can't expand or-patterns freely. The rule is:
1862 // - If a candidate doesn't start with an or-pattern, we include it in
1863 // the expansion list as-is (i.e. it "expands" to itself).
1864 // - If a candidate has an or-pattern as its only remaining match pair,
1865 // we can expand it.
1866 // - If it starts with an or-pattern but also has other match pairs,
1867 // we can expand it, but we can't process more candidates after it.
1868 //
1869 // If we didn't stop, the `otherwise` cases could get mixed up. E.g. in the
1870 // following, or-pattern simplification (in `merge_trivial_subcandidates`) makes it
1871 // so the `1` and `2` cases branch to a same block (which then tests `false`). If we
1872 // took `(2, _)` in the same set of candidates, when we reach the block that tests
1873 // `false` we don't know whether we came from `1` or `2`, hence we can't know where
1874 // to branch on failure.
1875 //
1876 // ```ignore(illustrative)
1877 // match (1, true) {
1878 // (1 | 2, false) => {},
1879 // (2, _) => {},
1880 // _ => {}
1881 // }
1882 // ```
1883 //
1884 // We therefore split the `candidates` slice in two, expand or-patterns in the first part,
1885 // and process the rest separately.
1886let expand_until = candidates1887 .iter()
1888 .position(|candidate| {
1889// If a candidate starts with an or-pattern and has more match pairs,
1890 // we can expand it, but we must stop expanding _after_ it.
1891candidate.match_pairs.len() > 1 && candidate.starts_with_or_pattern()
1892 })
1893 .map(|pos| pos + 1) // Stop _after_ the found candidate
1894.unwrap_or(candidates.len()); // Otherwise, include all candidates
1895let (candidates_to_expand, remaining_candidates) = candidates.split_at_mut(expand_until);
18961897// Expand one level of or-patterns for each candidate in `candidates_to_expand`.
1898 // We take care to preserve the relative ordering of candidates, so that
1899 // or-patterns are expanded in their parent's relative position.
1900let mut expanded_candidates = Vec::new();
1901for candidate in candidates_to_expand.iter_mut() {
1902if candidate.starts_with_or_pattern() {
1903let or_match_pair = candidate.match_pairs.remove(0);
1904// Expand the or-pattern into subcandidates.
1905self.create_or_subcandidates(candidate, or_match_pair);
1906// Collect the newly created subcandidates.
1907for subcandidate in candidate.subcandidates.iter_mut() {
1908 expanded_candidates.push(subcandidate);
1909 }
1910// Note that the subcandidates have been added to `expanded_candidates`,
1911 // but `candidate` itself has not. If the last candidate has more match pairs,
1912 // they are handled separately by `test_remaining_match_pairs_after_or`.
1913} else {
1914// A candidate that doesn't start with an or-pattern has nothing to
1915 // expand, so it is included in the post-expansion list as-is.
1916expanded_candidates.push(candidate);
1917 }
1918 }
19191920// Recursively lower the part of the match tree represented by the
1921 // expanded candidates. This is where subcandidates actually get lowered!
1922let remainder_start = self.match_candidates(
1923span,
1924scrutinee_span,
1925start_block,
1926expanded_candidates.as_mut_slice(),
1927 );
19281929// Postprocess subcandidates, and process any leftover match pairs.
1930 // (Only the last candidate can possibly have more match pairs.)
1931if true {
if !{
let mut all_except_last =
candidates_to_expand.iter().rev().skip(1);
all_except_last.all(|candidate|
candidate.match_pairs.is_empty())
} {
::core::panicking::panic("assertion failed: {\n let mut all_except_last = candidates_to_expand.iter().rev().skip(1);\n all_except_last.all(|candidate| candidate.match_pairs.is_empty())\n}")
};
};debug_assert!({
1932let mut all_except_last = candidates_to_expand.iter().rev().skip(1);
1933 all_except_last.all(|candidate| candidate.match_pairs.is_empty())
1934 });
1935for candidate in candidates_to_expand.iter_mut() {
1936if !candidate.subcandidates.is_empty() {
1937self.merge_trivial_subcandidates(candidate);
1938self.remove_never_subcandidates(candidate);
1939 }
1940 }
1941// It's important to perform the above simplifications _before_ dealing
1942 // with remaining match pairs, to avoid exponential blowup if possible
1943 // (for trivial or-patterns), and avoid useless work (for never patterns).
1944if let Some(last_candidate) = candidates_to_expand.last_mut() {
1945self.test_remaining_match_pairs_after_or(span, scrutinee_span, last_candidate);
1946 }
19471948remainder_start.and(remaining_candidates)
1949 }
19501951/// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1952 /// subcandidate. Any candidate that has been expanded this way should also be postprocessed
1953 /// at the end of [`Self::expand_and_match_or_candidates`].
1954fn create_or_subcandidates(
1955&mut self,
1956 candidate: &mut Candidate<'tcx>,
1957 match_pair: MatchPairTree<'tcx>,
1958 ) {
1959let TestableCase::Or { pats } = match_pair.testable_case else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
1960{
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/matches/mod.rs:1960",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(1960u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("expanding or-pattern: candidate={0:#?}\npats={1:#?}",
candidate, pats) as &dyn Value))])
});
} else { ; }
};debug!("expanding or-pattern: candidate={:#?}\npats={:#?}", candidate, pats);
1961candidate.or_span = Some(match_pair.pattern_span);
1962candidate.subcandidates = pats1963 .into_iter()
1964 .map(|flat_pat| Candidate::from_flat_pat(flat_pat, candidate.has_guard))
1965 .collect();
1966candidate.subcandidates[0].false_edge_start_block = candidate.false_edge_start_block;
1967 }
19681969/// Try to merge all of the subcandidates of the given candidate into one. This avoids
1970 /// exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. The candidate should have been
1971 /// expanded with `create_or_subcandidates`.
1972 ///
1973 /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
1974 /// so:
1975 ///
1976 /// ```text
1977 /// [ start ]
1978 /// |
1979 /// [ match P, Q ]
1980 /// |
1981 /// +----------------------------------------+------------------------------------+
1982 /// | | |
1983 /// V V V
1984 /// [ P matches ] [ Q matches ] [ otherwise ]
1985 /// | | |
1986 /// V V |
1987 /// [ match R, S ] [ match R, S ] |
1988 /// | | |
1989 /// +--------------+------------+ +--------------+------------+ |
1990 /// | | | | | | |
1991 /// V V V V V V |
1992 /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ] |
1993 /// | | | | | | |
1994 /// +--------------+------------|------------+--------------+ | |
1995 /// | | | |
1996 /// | +----------------------------------------+--------+
1997 /// | |
1998 /// V V
1999 /// [ Success ] [ Failure ]
2000 /// ```
2001 ///
2002 /// In practice there are some complications:
2003 ///
2004 /// * If there's a guard, then the otherwise branch of the first match on
2005 /// `R | S` goes to a test for whether `Q` matches, and the control flow
2006 /// doesn't merge into a single success block until after the guard is
2007 /// tested.
2008 /// * If neither `P` or `Q` has any bindings or type ascriptions and there
2009 /// isn't a match guard, then we create a smaller CFG like:
2010 ///
2011 /// ```text
2012 /// ...
2013 /// +---------------+------------+
2014 /// | | |
2015 /// [ P matches ] [ Q matches ] [ otherwise ]
2016 /// | | |
2017 /// +---------------+ |
2018 /// | ...
2019 /// [ match R, S ]
2020 /// |
2021 /// ...
2022 /// ```
2023 ///
2024 /// Note that this takes place _after_ the subcandidates have participated
2025 /// in match tree lowering.
2026fn merge_trivial_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) {
2027if !!candidate.subcandidates.is_empty() {
::core::panicking::panic("assertion failed: !candidate.subcandidates.is_empty()")
};assert!(!candidate.subcandidates.is_empty());
2028if candidate.has_guard {
2029// FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
2030return;
2031 }
20322033// FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
2034let can_merge = candidate.subcandidates.iter().all(|subcandidate| {
2035subcandidate.subcandidates.is_empty() && subcandidate.extra_data.is_empty()
2036 });
2037if !can_merge {
2038return;
2039 }
20402041let mut last_otherwise = None;
2042let shared_pre_binding_block = self.cfg.start_new_block();
2043// This candidate is about to become a leaf, so unset `or_span`.
2044let or_span = candidate.or_span.take().unwrap();
2045let source_info = self.source_info(or_span);
20462047if candidate.false_edge_start_block.is_none() {
2048candidate.false_edge_start_block = candidate.subcandidates[0].false_edge_start_block;
2049 }
20502051// Remove the (known-trivial) subcandidates from the candidate tree,
2052 // so that they aren't visible after match tree lowering, and wire them
2053 // all to join up at a single shared pre-binding block.
2054 // (Note that the subcandidates have already had their part of the match
2055 // tree lowered by this point, which is why we can add a goto to them.)
2056for subcandidate in mem::take(&mut candidate.subcandidates) {
2057let subcandidate_block = subcandidate.pre_binding_block.unwrap();
2058self.cfg.goto(subcandidate_block, source_info, shared_pre_binding_block);
2059 last_otherwise = subcandidate.otherwise_block;
2060 }
2061candidate.pre_binding_block = Some(shared_pre_binding_block);
2062if !last_otherwise.is_some() {
::core::panicking::panic("assertion failed: last_otherwise.is_some()")
};assert!(last_otherwise.is_some());
2063candidate.otherwise_block = last_otherwise;
2064 }
20652066/// Never subcandidates may have a set of bindings inconsistent with their siblings,
2067 /// which would break later code. So we filter them out. Note that we can't filter out
2068 /// top-level candidates this way.
2069fn remove_never_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) {
2070if candidate.subcandidates.is_empty() {
2071return;
2072 }
20732074let false_edge_start_block = candidate.subcandidates[0].false_edge_start_block;
2075candidate.subcandidates.retain_mut(|candidate| {
2076if candidate.extra_data.is_never {
2077candidate.visit_leaves(|subcandidate| {
2078let block = subcandidate.pre_binding_block.unwrap();
2079// That block is already unreachable but needs a terminator to make the MIR well-formed.
2080let source_info = self.source_info(subcandidate.extra_data.span);
2081self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
2082 });
2083false
2084} else {
2085true
2086}
2087 });
2088if candidate.subcandidates.is_empty() {
2089// If `candidate` has become a leaf candidate, ensure it has a `pre_binding_block` and `otherwise_block`.
2090let next_block = self.cfg.start_new_block();
2091candidate.pre_binding_block = Some(next_block);
2092candidate.otherwise_block = Some(next_block);
2093// In addition, if `candidate` doesn't have `false_edge_start_block`, it should be assigned here.
2094if candidate.false_edge_start_block.is_none() {
2095candidate.false_edge_start_block = false_edge_start_block;
2096 }
2097 }
2098 }
20992100/// If more match pairs remain, test them after each subcandidate.
2101 /// We could have added them to the or-candidates during or-pattern expansion, but that
2102 /// would make it impossible to detect simplifiable or-patterns. That would guarantee
2103 /// exponentially large CFGs for cases like `(1 | 2, 3 | 4, ...)`.
2104fn test_remaining_match_pairs_after_or(
2105&mut self,
2106 span: Span,
2107 scrutinee_span: Span,
2108 candidate: &mut Candidate<'tcx>,
2109 ) {
2110if candidate.match_pairs.is_empty() {
2111return;
2112 }
21132114let or_span = candidate.or_span.unwrap_or(candidate.extra_data.span);
2115let source_info = self.source_info(or_span);
2116let mut last_otherwise = None;
2117candidate.visit_leaves(|leaf_candidate| {
2118last_otherwise = leaf_candidate.otherwise_block;
2119 });
21202121let remaining_match_pairs = mem::take(&mut candidate.match_pairs);
2122// We're testing match pairs that remained after an `Or`, so the remaining
2123 // pairs should all be `Or` too, due to the sorting invariant.
2124if true {
if !remaining_match_pairs.iter().all(|match_pair|
#[allow(non_exhaustive_omitted_patterns)] match match_pair.testable_case
{
TestableCase::Or { .. } => true,
_ => false,
}) {
::core::panicking::panic("assertion failed: remaining_match_pairs.iter().all(|match_pair|\n matches!(match_pair.testable_case, TestableCase::Or { .. }))")
};
};debug_assert!(
2125 remaining_match_pairs
2126 .iter()
2127 .all(|match_pair| matches!(match_pair.testable_case, TestableCase::Or { .. }))
2128 );
21292130// Visit each leaf candidate within this subtree, add a copy of the remaining
2131 // match pairs to it, and then recursively lower the rest of the match tree
2132 // from that point.
2133candidate.visit_leaves(|leaf_candidate| {
2134// At this point the leaf's own match pairs have all been lowered
2135 // and removed, so `extend` and assignment are equivalent,
2136 // but extending can also recycle any existing vector capacity.
2137if !leaf_candidate.match_pairs.is_empty() {
::core::panicking::panic("assertion failed: leaf_candidate.match_pairs.is_empty()")
};assert!(leaf_candidate.match_pairs.is_empty());
2138leaf_candidate.match_pairs.extend(remaining_match_pairs.iter().cloned());
21392140let or_start = leaf_candidate.pre_binding_block.unwrap();
2141let otherwise =
2142self.match_candidates(span, scrutinee_span, or_start, &mut [leaf_candidate]);
2143// In a case like `(P | Q, R | S)`, if `P` succeeds and `R | S` fails, we know `(Q,
2144 // R | S)` will fail too. If there is no guard, we skip testing of `Q` by branching
2145 // directly to `last_otherwise`. If there is a guard,
2146 // `leaf_candidate.otherwise_block` can be reached by guard failure as well, so we
2147 // can't skip `Q`.
2148let or_otherwise = if leaf_candidate.has_guard {
2149leaf_candidate.otherwise_block.unwrap()
2150 } else {
2151last_otherwise.unwrap()
2152 };
2153self.cfg.goto(otherwise, source_info, or_otherwise);
2154 });
2155 }
21562157/// Pick a test to run. Which test doesn't matter as long as it is guaranteed to fully match at
2158 /// least one match pair. We currently simply pick the test corresponding to the first match
2159 /// pair of the first candidate in the list.
2160 ///
2161 /// *Note:* taking the first match pair is somewhat arbitrary, and we might do better here by
2162 /// choosing more carefully what to test.
2163 ///
2164 /// For example, consider the following possible match-pairs:
2165 ///
2166 /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has
2167 /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has
2168 /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in
2169 /// 4. etc.
2170 ///
2171 /// [`Switch`]: TestKind::Switch
2172 /// [`SwitchInt`]: TestKind::SwitchInt
2173 /// [`Range`]: TestKind::Range
2174fn pick_test(&mut self, candidates: &[&mut Candidate<'tcx>]) -> (Place<'tcx>, Test<'tcx>) {
2175// Extract the match-pair from the highest priority candidate
2176let match_pair = &candidates[0].match_pairs[0];
2177let test = self.pick_test_for_match_pair(match_pair);
2178// Unwrap is ok after simplification.
2179let match_place = match_pair.place.unwrap();
2180{
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/matches/mod.rs:2180",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2180u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["test",
"match_pair"],
::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(&test) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&match_pair)
as &dyn Value))])
});
} else { ; }
};debug!(?test, ?match_pair);
21812182 (match_place, test)
2183 }
21842185/// This is the most subtle part of the match lowering algorithm. At this point, there are
2186 /// no fully-satisfied candidates, and no or-patterns to expand, so we actually need to
2187 /// perform some sort of test to make progress.
2188 ///
2189 /// Once we pick what sort of test we are going to perform, this test will help us winnow down
2190 /// our candidates. So we walk over the candidates (from high to low priority) and check. We
2191 /// compute, for each outcome of the test, a list of (modified) candidates. If a candidate
2192 /// matches in exactly one branch of our test, we add it to the corresponding outcome. We also
2193 /// **mutate its list of match pairs** if appropriate, to reflect the fact that we know which
2194 /// outcome occurred.
2195 ///
2196 /// For example, if we are testing `x.0`'s variant, and we have a candidate `(x.0 @ Some(v), x.1
2197 /// @ 22)`, then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)` in the
2198 /// branch corresponding to `Some`. To ensure we make progress, we always pick a test that
2199 /// results in simplifying the first candidate.
2200 ///
2201 /// But there may also be candidates that the test doesn't
2202 /// apply to. The classical example is wildcards:
2203 ///
2204 /// ```
2205 /// # let (x, y, z) = (true, true, true);
2206 /// match (x, y, z) {
2207 /// (true , _ , true ) => true, // (0)
2208 /// (false, false, _ ) => false, // (1)
2209 /// (_ , true , _ ) => true, // (2)
2210 /// (true , _ , false) => false, // (3)
2211 /// }
2212 /// # ;
2213 /// ```
2214 ///
2215 /// Here, the traditional "decision tree" method would generate 2 separate code-paths for the 2
2216 /// possible values of `x`. This would however duplicate some candidates, which would need to be
2217 /// lowered several times.
2218 ///
2219 /// In some cases, this duplication can create an exponential amount of
2220 /// code. This is most easily seen by noticing that this method terminates
2221 /// with precisely the reachable arms being reachable - but that problem
2222 /// is trivially NP-complete:
2223 ///
2224 /// ```ignore (illustrative)
2225 /// match (var0, var1, var2, var3, ...) {
2226 /// (true , _ , _ , false, true, ...) => false,
2227 /// (_ , true, true , false, _ , ...) => false,
2228 /// (false, _ , false, false, _ , ...) => false,
2229 /// ...
2230 /// _ => true
2231 /// }
2232 /// ```
2233 ///
2234 /// Here the last arm is reachable only if there is an assignment to
2235 /// the variables that does not match any of the literals. Therefore,
2236 /// compilation would take an exponential amount of time in some cases.
2237 ///
2238 /// In rustc, we opt instead for the "backtracking automaton" approach. This guarantees we never
2239 /// duplicate a candidate (except in the presence of or-patterns). In fact this guarantee is
2240 /// ensured by the fact that we carry around `&mut Candidate`s which can't be duplicated.
2241 ///
2242 /// To make this work, whenever we decide to perform a test, if we encounter a candidate that
2243 /// could match in more than one branch of the test, we stop. We generate code for the test and
2244 /// for the candidates in its branches; the remaining candidates will be tested if the
2245 /// candidates in the branches fail to match.
2246 ///
2247 /// For example, if we test on `x` in the following:
2248 /// ```
2249 /// # fn foo((x, y, z): (bool, bool, bool)) -> u32 {
2250 /// match (x, y, z) {
2251 /// (true , _ , true ) => 0,
2252 /// (false, false, _ ) => 1,
2253 /// (_ , true , _ ) => 2,
2254 /// (true , _ , false) => 3,
2255 /// }
2256 /// # }
2257 /// ```
2258 /// this function generates code that looks more of less like:
2259 /// ```
2260 /// # fn foo((x, y, z): (bool, bool, bool)) -> u32 {
2261 /// if x {
2262 /// match (y, z) {
2263 /// (_, true) => return 0,
2264 /// _ => {} // continue matching
2265 /// }
2266 /// } else {
2267 /// match (y, z) {
2268 /// (false, _) => return 1,
2269 /// _ => {} // continue matching
2270 /// }
2271 /// }
2272 /// // the block here is `remainder_start`
2273 /// match (x, y, z) {
2274 /// (_ , true , _ ) => 2,
2275 /// (true , _ , false) => 3,
2276 /// _ => unreachable!(),
2277 /// }
2278 /// # }
2279 /// ```
2280 ///
2281 /// We return the unprocessed candidates.
2282fn test_candidates<'b, 'c>(
2283&mut self,
2284 span: Span,
2285 scrutinee_span: Span,
2286 candidates: &'b mut [&'c mut Candidate<'tcx>],
2287 start_block: BasicBlock,
2288 ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> {
2289// Choose a match pair from the first candidate, and use it to determine a
2290 // test to perform that will confirm or refute that match pair.
2291let (match_place, test) = self.pick_test(candidates);
22922293// For each of the N possible test outcomes, build the vector of candidates that applies if
2294 // the test has that particular outcome. This also mutates the candidates to remove match
2295 // pairs that are fully satisfied by the relevant outcome.
2296let PartitionedCandidates { target_candidates, remaining_candidates } =
2297self.partition_candidates_into_buckets(match_place, &test, candidates);
22982299// The block that we should branch to if none of the `target_candidates` match.
2300let remainder_start = self.cfg.start_new_block();
23012302// For each outcome of the test, recursively lower the rest of the match tree
2303 // from that point. (Note that we haven't lowered the actual test yet!)
2304let target_blocks: FxIndexMap<_, _> = target_candidates2305 .into_iter()
2306 .map(|(branch, mut candidates)| {
2307let branch_start = self.cfg.start_new_block();
2308// Recursively lower the rest of the match tree after the relevant outcome.
2309let branch_otherwise =
2310self.match_candidates(span, scrutinee_span, branch_start, &mut *candidates);
23112312// Link up the `otherwise` block of the subtree to `remainder_start`.
2313let source_info = self.source_info(span);
2314self.cfg.goto(branch_otherwise, source_info, remainder_start);
2315 (branch, branch_start)
2316 })
2317 .collect();
23182319// Perform the chosen test, branching to one of the N subtrees prepared above
2320 // (or to `remainder_start` if no outcome was satisfied).
2321self.perform_test(
2322span,
2323scrutinee_span,
2324start_block,
2325remainder_start,
2326match_place,
2327&test,
2328target_blocks,
2329 );
23302331remainder_start.and(remaining_candidates)
2332 }
2333}
23342335///////////////////////////////////////////////////////////////////////////
2336// Pat binding - used for `let` and function parameters as well.
23372338impl<'a, 'tcx> Builder<'a, 'tcx> {
2339/// Lowers a `let` expression that appears in a suitable context
2340 /// (e.g. an `if` condition or match guard).
2341 ///
2342 /// Also used for lowering let-else statements, since they have similar
2343 /// needs despite not actually using `let` expressions.
2344 ///
2345 /// Use [`DeclareLetBindings`] to control whether the `let` bindings are
2346 /// declared or not.
2347pub(crate) fn lower_let_expr(
2348&mut self,
2349mut block: BasicBlock,
2350 expr_id: ExprId,
2351 pat: &Pat<'tcx>,
2352 source_scope: Option<SourceScope>,
2353 scope_span: Span,
2354 declare_let_bindings: DeclareLetBindings,
2355 ) -> BlockAnd<()> {
2356let expr_span = self.thir[expr_id].span;
2357let scrutinee = {
let BlockAnd(b, v) = self.lower_scrutinee(block, expr_id, expr_span);
block = b;
v
}unpack!(block = self.lower_scrutinee(block, expr_id, expr_span));
2358let built_tree = self.lower_match_tree(
2359block,
2360expr_span,
2361&scrutinee,
2362pat.span,
2363::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(pat, HasMatchGuard::No)]))vec![(pat, HasMatchGuard::No)],
2364true,
2365 );
2366let [branch] = built_tree.branches.try_into().unwrap();
23672368self.break_for_else(built_tree.otherwise_block, self.source_info(expr_span));
23692370match declare_let_bindings {
2371 DeclareLetBindings::Yes => {
2372let expr_place = scrutinee.try_to_place(self);
2373let opt_expr_place = expr_place.as_ref().map(|place| (Some(place), expr_span));
2374self.declare_bindings(
2375source_scope,
2376pat.span.to(scope_span),
2377pat,
2378None,
2379opt_expr_place,
2380 );
2381 }
2382 DeclareLetBindings::No => {} // Caller is responsible for bindings.
2383DeclareLetBindings::LetNotPermitted => {
2384self.tcx.dcx().span_bug(expr_span, "let expression not expected in this context")
2385 }
2386 }
23872388let success = self.bind_pattern(self.source_info(pat.span), branch, &[], expr_span, None);
23892390// If branch coverage is enabled, record this branch.
2391self.visit_coverage_conditional_let(pat, success, built_tree.otherwise_block);
23922393success.unit()
2394 }
23952396/// Initializes each of the bindings from the candidate by
2397 /// moving/copying/ref'ing the source as appropriate. Tests the guard, if
2398 /// any, and then branches to the arm. Returns the block for the case where
2399 /// the guard succeeds.
2400 ///
2401 /// Note: we do not check earlier that if there is a guard,
2402 /// there cannot be move bindings. We avoid a use-after-move by only
2403 /// moving the binding once the guard has evaluated to true (see below).
2404fn bind_and_guard_matched_candidate(
2405&mut self,
2406 sub_branch: MatchTreeSubBranch<'tcx>,
2407 fake_borrows: &[(Place<'tcx>, Local, FakeBorrowKind)],
2408 scrutinee_span: Span,
2409 arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
2410 schedule_drops: ScheduleDrops,
2411 ) -> BasicBlock {
2412{
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/matches/mod.rs:2412",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2412u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("bind_and_guard_matched_candidate(subbranch={0:?})",
sub_branch) as &dyn Value))])
});
} else { ; }
};debug!("bind_and_guard_matched_candidate(subbranch={:?})", sub_branch);
24132414let block = sub_branch.success_block;
24152416if sub_branch.is_never {
2417// This arm has a dummy body, we don't need to generate code for it. `block` is already
2418 // unreachable (except via false edge).
2419let source_info = self.source_info(sub_branch.span);
2420self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
2421return self.cfg.start_new_block();
2422 }
24232424self.ascribe_types(block, sub_branch.ascriptions);
24252426// Lower an instance of the arm guard (if present) for this candidate,
2427 // and then perform bindings for the arm body.
2428if let Some((arm, match_scope)) = arm_match_scope2429 && let Some(guard) = arm.guard
2430 {
2431let tcx = self.tcx;
24322433// Bindings for guards require some extra handling to automatically
2434 // insert implicit references/dereferences.
2435 // This always schedules storage drops, so we may need to unschedule them below.
2436self.bind_matched_candidate_for_guard(block, sub_branch.bindings.iter());
2437let guard_frame = GuardFrame {
2438 locals: sub_branch2439 .bindings
2440 .iter()
2441 .map(|b| GuardFrameLocal::new(b.var_id))
2442 .collect(),
2443 };
2444{
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/matches/mod.rs:2444",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2444u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("entering guard building context: {0:?}",
guard_frame) as &dyn Value))])
});
} else { ; }
};debug!("entering guard building context: {:?}", guard_frame);
2445self.guard_context.push(guard_frame);
24462447let re_erased = tcx.lifetimes.re_erased;
2448let scrutinee_source_info = self.source_info(scrutinee_span);
2449for &(place, temp, kind) in fake_borrows {
2450let borrow = Rvalue::Ref(re_erased, BorrowKind::Fake(kind), place);
2451self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
2452 }
24532454let mut guard_span = rustc_span::DUMMY_SP;
24552456let (post_guard_block, otherwise_post_guard_block) =
2457self.in_if_then_scope(match_scope, guard_span, |this| {
2458guard_span = this.thir[guard].span;
2459this.then_else_break(
2460block,
2461guard,
2462None, // Use `self.local_scope()` as the temp scope
2463this.source_info(arm.span),
2464 DeclareLetBindings::No, // For guards, `let` bindings are declared separately
2465)
2466 });
24672468// If this isn't the final sub-branch being lowered, we need to unschedule drops of
2469 // bindings and temporaries created for and by the guard. As a result, the drop order
2470 // for the arm will correspond to the binding order of the final sub-branch lowered.
2471if #[allow(non_exhaustive_omitted_patterns)] match schedule_drops {
ScheduleDrops::No => true,
_ => false,
}matches!(schedule_drops, ScheduleDrops::No) {
2472self.clear_match_arm_and_guard_scopes(arm.scope);
2473 }
24742475let source_info = self.source_info(guard_span);
2476let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
2477let guard_frame = self.guard_context.pop().unwrap();
2478{
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/matches/mod.rs:2478",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2478u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("Exiting guard building context with locals: {0:?}",
guard_frame) as &dyn Value))])
});
} else { ; }
};debug!("Exiting guard building context with locals: {:?}", guard_frame);
24792480for &(_, temp, _) in fake_borrows {
2481let cause = FakeReadCause::ForMatchGuard;
2482self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp));
2483 }
24842485self.cfg.goto(otherwise_post_guard_block, source_info, sub_branch.otherwise_block);
24862487// We want to ensure that the matched candidates are bound
2488 // after we have confirmed this candidate *and* any
2489 // associated guard; Binding them on `block` is too soon,
2490 // because that would be before we've checked the result
2491 // from the guard.
2492 //
2493 // But binding them on the arm is *too late*, because
2494 // then all of the candidates for a single arm would be
2495 // bound in the same place, that would cause a case like:
2496 //
2497 // ```rust
2498 // match (30, 2) {
2499 // (mut x, 1) | (2, mut x) if { true } => { ... }
2500 // ... // ^^^^^^^ (this is `arm_block`)
2501 // }
2502 // ```
2503 //
2504 // would yield an `arm_block` something like:
2505 //
2506 // ```
2507 // StorageLive(_4); // _4 is `x`
2508 // _4 = &mut (_1.0: i32); // this is handling `(mut x, 1)` case
2509 // _4 = &mut (_1.1: i32); // this is handling `(2, mut x)` case
2510 // ```
2511 //
2512 // and that is clearly not correct.
2513let by_value_bindings = sub_branch2514 .bindings
2515 .iter()
2516 .filter(|binding| #[allow(non_exhaustive_omitted_patterns)] match binding.binding_mode.0 {
ByRef::No => true,
_ => false,
}matches!(binding.binding_mode.0, ByRef::No));
2517// Read all of the by reference bindings to ensure that the
2518 // place they refer to can't be modified by the guard.
2519for binding in by_value_bindings.clone() {
2520let local_id = self.var_local_id(binding.var_id, RefWithinGuard);
2521let cause = FakeReadCause::ForGuardBinding;
2522self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id));
2523 }
2524// Only schedule drops for the last sub-branch we lower.
2525self.bind_matched_candidate_for_arm_body(
2526post_guard_block,
2527schedule_drops,
2528by_value_bindings,
2529 );
25302531post_guard_block2532 } else {
2533// (Here, it is not too early to bind the matched
2534 // candidate on `block`, because there is no guard result
2535 // that we have to inspect before we bind them.)
2536self.bind_matched_candidate_for_arm_body(
2537block,
2538schedule_drops,
2539sub_branch.bindings.iter(),
2540 );
2541block2542 }
2543 }
25442545/// Append `AscribeUserType` statements onto the end of `block`
2546 /// for each ascription
2547fn ascribe_types(
2548&mut self,
2549 block: BasicBlock,
2550 ascriptions: impl IntoIterator<Item = Ascription<'tcx>>,
2551 ) {
2552for ascription in ascriptions {
2553let source_info = self.source_info(ascription.annotation.span);
25542555let base = self.canonical_user_type_annotations.push(ascription.annotation);
2556self.cfg.push(
2557 block,
2558 Statement::new(
2559 source_info,
2560 StatementKind::AscribeUserType(
2561 Box::new((
2562 ascription.source,
2563 UserTypeProjection { base, projs: Vec::new() },
2564 )),
2565 ascription.variance,
2566 ),
2567 ),
2568 );
2569 }
2570 }
25712572/// Binding for guards is a bit different from binding for the arm body,
2573 /// because an extra layer of implicit reference/dereference is added.
2574 ///
2575 /// The idea is that any pattern bindings of type T will map to a `&T` within
2576 /// the context of the guard expression, but will continue to map to a `T`
2577 /// in the context of the arm body. To avoid surfacing this distinction in
2578 /// the user source code (which would be a severe change to the language and
2579 /// require far more revision to the compiler), any occurrence of the
2580 /// identifier in the guard expression will automatically get a deref op
2581 /// applied to it. (See the caller of [`Self::is_bound_var_in_guard`].)
2582 ///
2583 /// So an input like:
2584 ///
2585 /// ```ignore (illustrative)
2586 /// let place = Foo::new();
2587 /// match place { foo if inspect(foo)
2588 /// => feed(foo), ... }
2589 /// ```
2590 ///
2591 /// will be treated as if it were really something like:
2592 ///
2593 /// ```ignore (illustrative)
2594 /// let place = Foo::new();
2595 /// match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
2596 /// => { let tmp2 = place; feed(tmp2) }, ... }
2597 /// ```
2598 ///
2599 /// And an input like:
2600 ///
2601 /// ```ignore (illustrative)
2602 /// let place = Foo::new();
2603 /// match place { ref mut foo if inspect(foo)
2604 /// => feed(foo), ... }
2605 /// ```
2606 ///
2607 /// will be treated as if it were really something like:
2608 ///
2609 /// ```ignore (illustrative)
2610 /// let place = Foo::new();
2611 /// match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
2612 /// => { let tmp2 = &mut place; feed(tmp2) }, ... }
2613 /// ```
2614 /// ---
2615 ///
2616 /// ## Implementation notes
2617 ///
2618 /// To encode the distinction above, we must inject the
2619 /// temporaries `tmp1` and `tmp2`.
2620 ///
2621 /// There are two cases of interest: binding by-value, and binding by-ref.
2622 ///
2623 /// 1. Binding by-value: Things are simple.
2624 ///
2625 /// * Establishing `tmp1` creates a reference into the
2626 /// matched place. This code is emitted by
2627 /// [`Self::bind_matched_candidate_for_guard`].
2628 ///
2629 /// * `tmp2` is only initialized "lazily", after we have
2630 /// checked the guard. Thus, the code that can trigger
2631 /// moves out of the candidate can only fire after the
2632 /// guard evaluated to true. This initialization code is
2633 /// emitted by [`Self::bind_matched_candidate_for_arm_body`].
2634 ///
2635 /// 2. Binding by-reference: Things are tricky.
2636 ///
2637 /// * Here, the guard expression wants a `&&` or `&&mut`
2638 /// into the original input. This means we need to borrow
2639 /// the reference that we create for the arm.
2640 /// * So we eagerly create the reference for the arm and then take a
2641 /// reference to that.
2642 ///
2643 /// ---
2644 ///
2645 /// See these PRs for some historical context:
2646 /// - <https://github.com/rust-lang/rust/pull/49870> (introduction of autoref)
2647 /// - <https://github.com/rust-lang/rust/pull/59114> (always use autoref)
2648fn bind_matched_candidate_for_guard<'b>(
2649&mut self,
2650 block: BasicBlock,
2651 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2652 ) where
2653'tcx: 'b,
2654 {
2655{
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/matches/mod.rs:2655",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2655u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("bind_matched_candidate_for_guard(block={0:?})",
block) as &dyn Value))])
});
} else { ; }
};debug!("bind_matched_candidate_for_guard(block={:?})", block);
26562657// Assign each of the bindings. Since we are binding for a
2658 // guard expression, this will never trigger moves out of the
2659 // candidate.
2660let re_erased = self.tcx.lifetimes.re_erased;
2661for binding in bindings {
2662{
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/matches/mod.rs:2662",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2662u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("bind_matched_candidate_for_guard(binding={0:?})",
binding) as &dyn Value))])
});
} else { ; }
};debug!("bind_matched_candidate_for_guard(binding={:?})", binding);
2663let source_info = self.source_info(binding.span);
26642665// For each pattern ident P of type T, `ref_for_guard` is
2666 // a reference R: &T pointing to the location matched by
2667 // the pattern, and every occurrence of P within a guard
2668 // denotes *R.
2669 // Drops must be scheduled to emit `StorageDead` on the guard's failure/break branches.
2670let ref_for_guard = self.storage_live_binding(
2671 block,
2672 binding.var_id,
2673 binding.span,
2674 binding.is_shorthand,
2675 RefWithinGuard,
2676 ScheduleDrops::Yes,
2677 );
2678match binding.binding_mode.0 {
2679 ByRef::No => {
2680// The arm binding will be by value, so for the guard binding
2681 // just take a shared reference to the matched place.
2682let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source);
2683self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2684 }
2685 ByRef::Yes(pinnedness, mutbl) => {
2686// The arm binding will be by reference, so eagerly create it now // be scheduled to emit `StorageDead` on the guard's failure/break branches.
2687let value_for_arm = self.storage_live_binding(
2688 block,
2689 binding.var_id,
2690 binding.span,
2691 binding.is_shorthand,
2692 OutsideGuard,
2693 ScheduleDrops::Yes,
2694 );
26952696let rvalue =
2697 Rvalue::Ref(re_erased, util::ref_pat_borrow_kind(mutbl), binding.source);
2698let rvalue = match pinnedness {
2699 ty::Pinnedness::Not => rvalue,
2700 ty::Pinnedness::Pinned => {
2701self.pin_borrowed_local(block, value_for_arm.local, rvalue, source_info)
2702 }
2703 };
2704self.cfg.push_assign(block, source_info, value_for_arm, rvalue);
2705// For the guard binding, take a shared reference to that reference.
2706let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm);
2707self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2708 }
2709 }
2710 }
2711 }
27122713fn bind_matched_candidate_for_arm_body<'b>(
2714&mut self,
2715 block: BasicBlock,
2716 schedule_drops: ScheduleDrops,
2717 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2718 ) where
2719'tcx: 'b,
2720 {
2721{
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/matches/mod.rs:2721",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2721u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::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!("bind_matched_candidate_for_arm_body(block={0:?})",
block) as &dyn Value))])
});
} else { ; }
};debug!("bind_matched_candidate_for_arm_body(block={:?})", block);
27222723let re_erased = self.tcx.lifetimes.re_erased;
2724// Assign each of the bindings. This may trigger moves out of the candidate.
2725for binding in bindings {
2726let source_info = self.source_info(binding.span);
2727let local = self.storage_live_binding(
2728 block,
2729 binding.var_id,
2730 binding.span,
2731 binding.is_shorthand,
2732 OutsideGuard,
2733 schedule_drops,
2734 );
2735if #[allow(non_exhaustive_omitted_patterns)] match schedule_drops {
ScheduleDrops::Yes => true,
_ => false,
}matches!(schedule_drops, ScheduleDrops::Yes) {
2736self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
2737 }
2738let rvalue = match binding.binding_mode.0 {
2739 ByRef::No => Rvalue::Use(self.consume_by_copy_or_move(binding.source)),
2740 ByRef::Yes(pinnedness, mutbl) => {
2741let rvalue =
2742 Rvalue::Ref(re_erased, util::ref_pat_borrow_kind(mutbl), binding.source);
2743match pinnedness {
2744 ty::Pinnedness::Not => rvalue,
2745 ty::Pinnedness::Pinned => {
2746self.pin_borrowed_local(block, local.local, rvalue, source_info)
2747 }
2748 }
2749 }
2750 };
2751self.cfg.push_assign(block, source_info, local, rvalue);
2752 }
2753 }
27542755/// Given an rvalue `&[mut]borrow` and a local `local`, generate the pinned borrow for it:
2756 /// ```ignore (illustrative)
2757 /// pinned_temp = &borrow;
2758 /// local = Pin { __pointer: move pinned_temp };
2759 /// ```
2760fn pin_borrowed_local(
2761&mut self,
2762 block: BasicBlock,
2763 local: Local,
2764 borrow: Rvalue<'tcx>,
2765 source_info: SourceInfo,
2766 ) -> Rvalue<'tcx> {
2767if true {
match borrow {
Rvalue::Ref(..) => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"Rvalue::Ref(..)", ::core::option::Option::None);
}
};
};debug_assert_matches!(borrow, Rvalue::Ref(..));
27682769let local_ty = self.local_decls[local].ty;
27702771let pinned_ty = local_ty.pinned_ty().unwrap_or_else(|| {
2772::rustc_middle::util::bug::span_bug_fmt(source_info.span,
format_args!("expect type `Pin` for a pinned binding, found type {0:?}",
local_ty))span_bug!(
2773source_info.span,
2774"expect type `Pin` for a pinned binding, found type {:?}",
2775 local_ty
2776 )2777 });
2778let pinned_temp =
2779Place::from(self.local_decls.push(LocalDecl::new(pinned_ty, source_info.span)));
2780self.cfg.push_assign(block, source_info, pinned_temp, borrow);
2781 Rvalue::Aggregate(
2782Box::new(AggregateKind::Adt(
2783self.tcx.require_lang_item(LangItem::Pin, source_info.span),
2784FIRST_VARIANT,
2785self.tcx.mk_args(&[pinned_ty.into()]),
2786None,
2787None,
2788 )),
2789 std::iter::once(Operand::Move(pinned_temp)).collect(),
2790 )
2791 }
27922793/// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound
2794 /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The
2795 /// first local is a binding for occurrences of `var` in the guard, which
2796 /// will have type `&T`. The second local is a binding for occurrences of
2797 /// `var` in the arm body, which will have type `T`.
2798#[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("declare_binding",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2798u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["source_info",
"visibility_scope", "name", "mode", "var_id", "var_ty",
"user_ty", "has_guard", "opt_match_place", "pat_span"],
::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(&source_info)
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(&visibility_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(&name)
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(&mode)
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(&var_id)
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(&var_ty)
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(&user_ty)
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(&has_guard)
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(&opt_match_place)
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(&pat_span)
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;
}
{
let tcx = self.tcx;
let debug_source_info =
SourceInfo {
span: source_info.span,
scope: visibility_scope,
};
let local =
LocalDecl {
mutability: mode.1,
ty: var_ty,
user_ty,
source_info,
local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(BindingForm::Var(VarBindingForm {
binding_mode: mode,
opt_ty_info: None,
opt_match_place,
pat_span,
introductions: Vec::new(),
})))),
};
let for_arm_body = self.local_decls.push(local);
if self.should_emit_debug_info_for_binding(name, var_id) {
self.var_debug_info.push(VarDebugInfo {
name,
source_info: debug_source_info,
value: VarDebugInfoContents::Place(for_arm_body.into()),
composite: None,
argument_index: None,
});
}
let locals =
if has_guard.0 {
let ref_for_guard =
self.local_decls.push(LocalDecl::<'tcx> {
mutability: Mutability::Not,
ty: Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, var_ty),
user_ty: None,
source_info,
local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(BindingForm::RefForGuard(for_arm_body)))),
});
if self.should_emit_debug_info_for_binding(name, var_id) {
self.var_debug_info.push(VarDebugInfo {
name,
source_info: debug_source_info,
value: VarDebugInfoContents::Place(ref_for_guard.into()),
composite: None,
argument_index: None,
});
}
LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
} else { LocalsForNode::One(for_arm_body) };
{
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/matches/mod.rs:2868",
"rustc_mir_build::builder::matches",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/matches/mod.rs"),
::tracing_core::__macro_support::Option::Some(2868u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::matches"),
::tracing_core::field::FieldSet::new(&["locals"],
::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(&locals) as
&dyn Value))])
});
} else { ; }
};
self.var_indices.insert(var_id, locals);
}
}
}#[instrument(skip(self), level = "debug")]2799fn declare_binding(
2800&mut self,
2801 source_info: SourceInfo,
2802 visibility_scope: SourceScope,
2803 name: Symbol,
2804 mode: BindingMode,
2805 var_id: LocalVarId,
2806 var_ty: Ty<'tcx>,
2807 user_ty: Option<Box<UserTypeProjections>>,
2808 has_guard: ArmHasGuard,
2809 opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
2810 pat_span: Span,
2811 ) {
2812let tcx = self.tcx;
2813let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope };
2814let local = LocalDecl {
2815 mutability: mode.1,
2816 ty: var_ty,
2817 user_ty,
2818 source_info,
2819 local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(BindingForm::Var(
2820 VarBindingForm {
2821 binding_mode: mode,
2822// hypothetically, `visit_primary_bindings` could try to unzip
2823 // an outermost hir::Ty as we descend, matching up
2824 // idents in pat; but complex w/ unclear UI payoff.
2825 // Instead, just abandon providing diagnostic info.
2826opt_ty_info: None,
2827 opt_match_place,
2828 pat_span,
2829 introductions: Vec::new(),
2830 },
2831 )))),
2832 };
2833let for_arm_body = self.local_decls.push(local);
2834if self.should_emit_debug_info_for_binding(name, var_id) {
2835self.var_debug_info.push(VarDebugInfo {
2836 name,
2837 source_info: debug_source_info,
2838 value: VarDebugInfoContents::Place(for_arm_body.into()),
2839 composite: None,
2840 argument_index: None,
2841 });
2842 }
2843let locals = if has_guard.0 {
2844let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
2845// This variable isn't mutated but has a name, so has to be
2846 // immutable to avoid the unused mut lint.
2847mutability: Mutability::Not,
2848 ty: Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, var_ty),
2849 user_ty: None,
2850 source_info,
2851 local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(
2852 BindingForm::RefForGuard(for_arm_body),
2853 ))),
2854 });
2855if self.should_emit_debug_info_for_binding(name, var_id) {
2856self.var_debug_info.push(VarDebugInfo {
2857 name,
2858 source_info: debug_source_info,
2859 value: VarDebugInfoContents::Place(ref_for_guard.into()),
2860 composite: None,
2861 argument_index: None,
2862 });
2863 }
2864 LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
2865 } else {
2866 LocalsForNode::One(for_arm_body)
2867 };
2868debug!(?locals);
2869self.var_indices.insert(var_id, locals);
2870 }
28712872/// Some bindings are introduced when producing HIR from the AST and don't
2873 /// actually exist in the source. Skip producing debug info for those when
2874 /// we can recognize them.
2875fn should_emit_debug_info_for_binding(&self, name: Symbol, var_id: LocalVarId) -> bool {
2876// For now we only recognize the output of desugaring assigns.
2877if name != sym::lhs {
2878return true;
2879 }
28802881let tcx = self.tcx;
2882for (_, node) in tcx.hir_parent_iter(var_id.0) {
2883// FIXME(khuey) at what point is it safe to bail on the iterator?
2884 // Can we stop at the first non-Pat node?
2885if #[allow(non_exhaustive_omitted_patterns)] match node {
Node::LetStmt(&LetStmt { source: LocalSource::AssignDesugar, .. }) =>
true,
_ => false,
}matches!(node, Node::LetStmt(&LetStmt { source: LocalSource::AssignDesugar, .. })) {
2886return false;
2887 }
2888 }
28892890true
2891}
28922893/// Attempt to statically pick the `BasicBlock` that a value would resolve to at runtime.
2894pub(crate) fn static_pattern_match(
2895&self,
2896 cx: &RustcPatCtxt<'_, 'tcx>,
2897 valtree: ValTree<'tcx>,
2898 arms: &[ArmId],
2899 built_match_tree: &BuiltMatchTree<'tcx>,
2900 ) -> Option<BasicBlock> {
2901let it = arms.iter().zip(built_match_tree.branches.iter());
2902for (&arm_id, branch) in it {
2903let pat = cx.lower_pat(&*self.thir.arms[arm_id].pattern);
29042905// Peel off or-patterns if they exist.
2906if let rustc_pattern_analysis::rustc::Constructor::Or = pat.ctor() {
2907for pat in pat.iter_fields() {
2908// For top-level or-patterns (the only ones we accept right now), when the
2909 // bindings are the same (e.g. there are none), the sub_branch is stored just
2910 // once.
2911let sub_branch = branch
2912 .sub_branches
2913 .get(pat.idx)
2914 .or_else(|| branch.sub_branches.last())
2915 .unwrap();
29162917match self.static_pattern_match_inner(valtree, &pat.pat) {
2918true => return Some(sub_branch.success_block),
2919false => continue,
2920 }
2921 }
2922 } else if self.static_pattern_match_inner(valtree, &pat) {
2923return Some(branch.sub_branches[0].success_block);
2924 }
2925 }
29262927None2928 }
29292930/// Helper for [`Self::static_pattern_match`], checking whether the value represented by the
2931 /// `ValTree` matches the given pattern. This function does not recurse, meaning that it does
2932 /// not handle or-patterns, or patterns for types with fields.
2933fn static_pattern_match_inner(
2934&self,
2935 valtree: ty::ValTree<'tcx>,
2936 pat: &DeconstructedPat<'_, 'tcx>,
2937 ) -> bool {
2938use rustc_pattern_analysis::constructor::{IntRange, MaybeInfiniteInt};
2939use rustc_pattern_analysis::rustc::Constructor;
29402941match pat.ctor() {
2942 Constructor::Variant(variant_index) => {
2943let ValTreeKind::Branch(branch) = *valtreeelse {
2944::rustc_middle::util::bug::bug_fmt(format_args!("malformed valtree for an enum"))bug!("malformed valtree for an enum")2945 };
2946if branch.len() != 1 {
2947::rustc_middle::util::bug::bug_fmt(format_args!("malformed valtree for an enum"))bug!("malformed valtree for an enum")2948 };
2949let ValTreeKind::Leaf(actual_variant_idx) = **branch[0].to_value().valtree else {
2950::rustc_middle::util::bug::bug_fmt(format_args!("malformed valtree for an enum"))bug!("malformed valtree for an enum")2951 };
29522953*variant_index == VariantIdx::from_u32(actual_variant_idx.to_u32())
2954 }
2955 Constructor::IntRange(int_range) => {
2956let size = pat.ty().primitive_size(self.tcx);
2957let actual_int = valtree.to_leaf().to_bits(size);
2958let actual_int = if pat.ty().is_signed() {
2959 MaybeInfiniteInt::new_finite_int(actual_int, size.bits())
2960 } else {
2961 MaybeInfiniteInt::new_finite_uint(actual_int)
2962 };
2963 IntRange::from_singleton(actual_int).is_subrange(int_range)
2964 }
2965 Constructor::Bool(pattern_value) => match valtree.to_leaf().try_to_bool() {
2966Ok(actual_value) => *pattern_value == actual_value,
2967Err(()) => ::rustc_middle::util::bug::bug_fmt(format_args!("bool value with invalid bits"))bug!("bool value with invalid bits"),
2968 },
2969 Constructor::F16Range(l, h, end) => {
2970let actual = valtree.to_leaf().to_f16();
2971match end {
2972 RangeEnd::Included => (*l..=*h).contains(&actual),
2973 RangeEnd::Excluded => (*l..*h).contains(&actual),
2974 }
2975 }
2976 Constructor::F32Range(l, h, end) => {
2977let actual = valtree.to_leaf().to_f32();
2978match end {
2979 RangeEnd::Included => (*l..=*h).contains(&actual),
2980 RangeEnd::Excluded => (*l..*h).contains(&actual),
2981 }
2982 }
2983 Constructor::F64Range(l, h, end) => {
2984let actual = valtree.to_leaf().to_f64();
2985match end {
2986 RangeEnd::Included => (*l..=*h).contains(&actual),
2987 RangeEnd::Excluded => (*l..*h).contains(&actual),
2988 }
2989 }
2990 Constructor::F128Range(l, h, end) => {
2991let actual = valtree.to_leaf().to_f128();
2992match end {
2993 RangeEnd::Included => (*l..=*h).contains(&actual),
2994 RangeEnd::Excluded => (*l..*h).contains(&actual),
2995 }
2996 }
2997 Constructor::Wildcard => true,
29982999// Opaque patterns must not be matched on structurally.
3000Constructor::Opaque(_) => false,
30013002// These we may eventually support:
3003Constructor::Struct
3004 | Constructor::Ref
3005 | Constructor::DerefPattern(_)
3006 | Constructor::Slice(_)
3007 | Constructor::UnionField
3008 | Constructor::Or
3009 | Constructor::Str(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("unsupported pattern constructor {0:?}",
pat.ctor()))bug!("unsupported pattern constructor {:?}", pat.ctor()),
30103011// These should never occur here:
3012Constructor::Never
3013 | Constructor::NonExhaustive
3014 | Constructor::Hidden
3015 | Constructor::Missing
3016 | Constructor::PrivateUninhabited => {
3017::rustc_middle::util::bug::bug_fmt(format_args!("unsupported pattern constructor {0:?}",
pat.ctor()))bug!("unsupported pattern constructor {:?}", pat.ctor())3018 }
3019 }
3020 }
3021}