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, Exhaustive::Yes);
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,
379 Exhaustive::Yes,
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)],
652 Exhaustive::Yes,
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);
}
}
PatKind::Guard { ref subpattern, .. } => {
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 PatKind::Guard { ref subpattern, .. } => {
948 visit_subpat(self, subpattern, user_tys, f);
949 }
950 }
951 }
952}
953954/// Data extracted from a pattern that doesn't affect which branch is taken. Collected during
955/// pattern simplification and not mutated later.
956#[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)]
957struct PatternExtraData<'tcx> {
958/// [`Span`] of the original pattern.
959span: Span,
960961/// Bindings that must be established.
962bindings: Vec<SubpatternBindings<'tcx>>,
963964/// Types that must be asserted.
965ascriptions: Vec<Ascription<'tcx>>,
966967/// Whether this corresponds to a never pattern.
968is_never: bool,
969}
970971impl<'tcx> PatternExtraData<'tcx> {
972fn is_empty(&self) -> bool {
973self.bindings.is_empty() && self.ascriptions.is_empty()
974 }
975}
976977#[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)]
978enum SubpatternBindings<'tcx> {
979/// A single binding.
980One(Binding<'tcx>),
981/// Holds the place for an or-pattern's bindings. This ensures their drops are scheduled in the
982 /// order the primary bindings appear. See rust-lang/rust#142163 for more information.
983FromOrPattern,
984}
985986/// A pattern in a form suitable for lowering the match tree, with all irrefutable
987/// patterns simplified away.
988///
989/// Here, "flat" indicates that irrefutable nodes in the pattern tree have been
990/// recursively replaced with their refutable subpatterns. They are not
991/// necessarily flat in an absolute sense.
992///
993/// Will typically be incorporated into a [`Candidate`].
994#[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)]
995struct FlatPat<'tcx> {
996/// To match the pattern, all of these must be satisfied...
997match_pairs: Vec<MatchPairTree<'tcx>>,
998999 extra_data: PatternExtraData<'tcx>,
1000}
10011002/// Candidates are a generalization of (a) top-level match arms, and
1003/// (b) sub-branches of or-patterns, allowing the match-lowering process to handle
1004/// them both in a mostly-uniform way. For example, the list of candidates passed
1005/// to [`Builder::match_candidates`] will often contain a mixture of top-level
1006/// candidates and or-pattern subcandidates.
1007///
1008/// At the start of match lowering, there is one candidate for each match arm.
1009/// During match lowering, arms with or-patterns will be expanded into a tree
1010/// of candidates, where each "leaf" candidate represents one of the ways for
1011/// the arm pattern to successfully match.
1012#[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)]
1013struct Candidate<'tcx> {
1014/// For the candidate to match, all of these must be satisfied...
1015 ///
1016 /// ---
1017 /// Initially contains a list of match pairs created by [`FlatPat`], but is
1018 /// subsequently mutated (in a queue-like way) while lowering the match tree.
1019 /// When this list becomes empty, the candidate is fully matched and becomes
1020 /// a leaf (see [`Builder::select_matched_candidate`]).
1021 ///
1022 /// Key mutations include:
1023 ///
1024 /// - When a match pair is fully satisfied by a test, it is removed from the
1025 /// list, and its subpairs are added instead (see [`Builder::choose_bucket_for_candidate`]).
1026 /// - During or-pattern expansion, any leading or-pattern is removed, and is
1027 /// converted into subcandidates (see [`Builder::expand_and_match_or_candidates`]).
1028 /// - After a candidate's subcandidates have been lowered, a copy of any remaining
1029 /// or-patterns is added to each leaf subcandidate
1030 /// (see [`Builder::test_remaining_match_pairs_after_or`]).
1031 ///
1032 /// Invariants:
1033 /// - All or-patterns ([`TestableCase::Or`]) have been sorted to the end.
1034match_pairs: Vec<MatchPairTree<'tcx>>,
10351036/// ...and if this is non-empty, one of these subcandidates also has to match...
1037 ///
1038 /// ---
1039 /// Initially a candidate has no subcandidates; they are added (and then immediately
1040 /// lowered) during or-pattern expansion. Their main function is to serve as _output_
1041 /// of match tree lowering, allowing later steps to see the leaf candidates that
1042 /// represent a match of the entire match arm.
1043 ///
1044 /// A candidate no subcandidates is either incomplete (if it has match pairs left),
1045 /// or is a leaf in the match tree. A candidate with one or more subcandidates is
1046 /// an internal node in the match tree.
1047 ///
1048 /// Invariant: at the end of match tree lowering, this must not contain an
1049 /// `is_never` candidate, because that would break binding consistency.
1050 /// - See [`Builder::remove_never_subcandidates`].
1051subcandidates: Vec<Candidate<'tcx>>,
10521053/// ...and if there is a guard it must be evaluated; if it's `false` then branch to `otherwise_block`.
1054 ///
1055 /// ---
1056 /// For subcandidates, this is copied from the parent candidate, so it indicates
1057 /// whether the enclosing match arm has a guard.
1058has_guard: bool,
10591060/// Holds extra pattern data that was prepared by [`FlatPat`], including bindings and
1061 /// ascriptions that must be established if this candidate succeeds.
1062extra_data: PatternExtraData<'tcx>,
10631064/// When setting `self.subcandidates`, we store here the span of the or-pattern they came from.
1065 ///
1066 /// ---
1067 /// Invariant: it is `None` iff `subcandidates.is_empty()`.
1068 /// - FIXME: We sometimes don't unset this when clearing `subcandidates`.
1069or_span: Option<Span>,
10701071/// The block before the `bindings` have been established.
1072 ///
1073 /// After the match tree has been lowered, [`Builder::lower_match_arms`]
1074 /// will use this as the start point for lowering bindings and guards, and
1075 /// then jump to a shared block containing the arm body.
1076pre_binding_block: Option<BasicBlock>,
10771078/// The block to branch to if the guard or a nested candidate fails to match.
1079otherwise_block: Option<BasicBlock>,
10801081/// The earliest block that has only candidates >= this one as descendents. Used for false
1082 /// edges, see the doc for [`Builder::match_expr`].
1083false_edge_start_block: Option<BasicBlock>,
1084}
10851086impl<'tcx> Candidate<'tcx> {
1087fn new(
1088 place: PlaceBuilder<'tcx>,
1089 pattern: &Pat<'tcx>,
1090 has_guard: HasMatchGuard,
1091 cx: &mut Builder<'_, 'tcx>,
1092 ) -> Self {
1093// Use `FlatPat` to build simplified match pairs, then immediately
1094 // incorporate them into a new candidate.
1095Self::from_flat_pat(
1096FlatPat::new(place, pattern, cx),
1097#[allow(non_exhaustive_omitted_patterns)] match has_guard {
HasMatchGuard::Yes => true,
_ => false,
}matches!(has_guard, HasMatchGuard::Yes),
1098 )
1099 }
11001101/// Incorporates an already-simplified [`FlatPat`] into a new candidate.
1102fn from_flat_pat(flat_pat: FlatPat<'tcx>, has_guard: bool) -> Self {
1103let mut this = Candidate {
1104 match_pairs: flat_pat.match_pairs,
1105 extra_data: flat_pat.extra_data,
1106has_guard,
1107 subcandidates: Vec::new(),
1108 or_span: None,
1109 otherwise_block: None,
1110 pre_binding_block: None,
1111 false_edge_start_block: None,
1112 };
1113this.sort_match_pairs();
1114this1115 }
11161117/// Restores the invariant that or-patterns must be sorted to the end.
1118fn sort_match_pairs(&mut self) {
1119self.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 { .. }));
1120 }
11211122/// Returns whether the first match pair of this candidate is an or-pattern.
1123fn starts_with_or_pattern(&self) -> bool {
1124#[allow(non_exhaustive_omitted_patterns)] match &*self.match_pairs {
[MatchPairTree { testable_case: TestableCase::Or { .. }, .. }, ..] =>
true,
_ => false,
}matches!(
1125&*self.match_pairs,
1126 [MatchPairTree { testable_case: TestableCase::Or { .. }, .. }, ..]
1127 )1128 }
11291130/// Visit the leaf candidates (those with no subcandidates) contained in
1131 /// this candidate.
1132fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1133traverse_candidate(
1134self,
1135&mut (),
1136&mut move |c, _| visit_leaf(c),
1137move |c, _| c.subcandidates.iter_mut(),
1138 |_| {},
1139 );
1140 }
11411142/// Visit the leaf candidates in reverse order.
1143fn visit_leaves_rev<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1144traverse_candidate(
1145self,
1146&mut (),
1147&mut move |c, _| visit_leaf(c),
1148move |c, _| c.subcandidates.iter_mut().rev(),
1149 |_| {},
1150 );
1151 }
1152}
11531154/// A depth-first traversal of the `Candidate` and all of its recursive
1155/// subcandidates.
1156///
1157/// This signature is very generic, to support traversing candidate trees by
1158/// reference or by value, and to allow a mutable "context" to be shared by the
1159/// traversal callbacks. Most traversals can use the simpler
1160/// [`Candidate::visit_leaves`] wrapper instead.
1161fn traverse_candidate<'tcx, C, T, I>(
1162 candidate: C,
1163 context: &mut T,
1164// Called when visiting a "leaf" candidate (with no subcandidates).
1165visit_leaf: &mut impl FnMut(C, &mut T),
1166// Called when visiting a "node" candidate (with one or more subcandidates).
1167 // Returns an iterator over the candidate's children (by value or reference).
1168 // Can perform setup before visiting the node's children.
1169get_children: impl Copy + Fn(C, &mut T) -> I,
1170// Called after visiting a "node" candidate's children.
1171complete_children: impl Copy + Fn(&mut T),
1172) where
1173C: Borrow<Candidate<'tcx>>, // Typically `Candidate` or `&mut Candidate`
1174I: Iterator<Item = C>,
1175{
1176if candidate.borrow().subcandidates.is_empty() {
1177visit_leaf(candidate, context)
1178 } else {
1179for child in get_children(candidate, context) {
1180 traverse_candidate(child, context, visit_leaf, get_children, complete_children);
1181 }
1182complete_children(context)
1183 }
1184}
11851186#[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)]
1187struct Binding<'tcx> {
1188 span: Span,
1189 source: Place<'tcx>,
1190 var_id: LocalVarId,
1191 binding_mode: BindingMode,
1192 is_shorthand: bool,
1193}
11941195/// Indicates that the type of `source` must be a subtype of the
1196/// user-given type `user_ty`; this is basically a no-op but can
1197/// influence region inference.
1198#[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)]
1199struct Ascription<'tcx> {
1200 source: Place<'tcx>,
1201 annotation: CanonicalUserTypeAnnotation<'tcx>,
1202 variance: ty::Variance,
1203}
12041205/// Partial summary of a [`thir::Pat`], indicating what sort of test should be
1206/// performed to match/reject the pattern, and what the desired test outcome is.
1207/// This avoids having to perform a full match on [`thir::PatKind`] in some places,
1208/// and helps [`TestKind::Switch`] and [`TestKind::SwitchInt`] know what target
1209/// values to use.
1210///
1211/// Created by [`MatchPairTree`], and then inspected primarily by:
1212/// - [`Builder::pick_test_for_match_pair`] (to choose a test)
1213/// - [`Builder::choose_bucket_for_candidate`] (to see how the test interacts with a match pair)
1214///
1215/// Note that or-patterns are not tested directly like the other variants.
1216/// Instead they participate in or-pattern expansion, where they are transformed into
1217/// subcandidates. See [`Builder::expand_and_match_or_candidates`].
1218#[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)]
1219enum TestableCase<'tcx> {
1220 Variant { adt_def: ty::AdtDef<'tcx>, variant_index: VariantIdx },
1221 Constant { value: ty::Value<'tcx>, kind: PatConstKind },
1222 Range(Arc<PatRange<'tcx>>),
1223 Slice { len: u64, op: SliceLenOp },
1224 Deref { temp: Place<'tcx>, mutability: Mutability },
1225 Never,
1226 Or { pats: Box<[FlatPat<'tcx>]> },
1227}
12281229impl<'tcx> TestableCase<'tcx> {
1230fn as_range(&self) -> Option<&PatRange<'tcx>> {
1231if let Self::Range(v) = self { Some(v.as_ref()) } else { None }
1232 }
1233}
12341235/// Sub-classification of [`TestableCase::Constant`], which helps to avoid
1236/// some redundant ad-hoc checks when preparing and lowering tests.
1237#[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)]
1238enum PatConstKind {
1239/// The primitive `bool` type, which is like an integer but simpler,
1240 /// having only two values.
1241Bool,
1242/// Primitive unsigned/signed integer types, plus `char`.
1243 /// These types interact nicely with `SwitchInt`.
1244IntOrChar,
1245/// Floating-point primitives, e.g. `f32`, `f64`.
1246 /// These types don't support `SwitchInt` and require an equality test,
1247 /// but can also interact with range pattern tests.
1248Float,
1249/// Constant string values, tested via string equality.
1250String,
1251/// Any other constant-pattern is usually tested via some kind of equality
1252 /// check. Types that might be encountered here include:
1253 /// - raw pointers derived from integer values
1254 /// - pattern types, e.g. `pattern_type!(u32 is 1..)`
1255Other,
1256}
12571258/// Node in a tree of "match pairs", where each pair consists of a place to be
1259/// tested, and a test to perform on that place.
1260///
1261/// Each node also has a list of subpairs (possibly empty) that must also match,
1262/// and some additional information from the THIR pattern it represents.
1263#[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_field4_finish(f, "MatchPairTree",
"place", &self.place, "testable_case", &self.testable_case,
"subpairs", &self.subpairs, "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_span: ::core::clone::Clone::clone(&self.pattern_span),
}
}
}Clone)]
1264pub(crate) struct MatchPairTree<'tcx> {
1265/// This place...
1266 ///
1267 /// ---
1268 /// This can be `None` if it referred to a non-captured place in a closure.
1269 ///
1270 /// Invariant: Can only be `None` when `testable_case` is `Or`.
1271 /// Therefore this must be `Some(_)` after or-pattern expansion.
1272place: Option<Place<'tcx>>,
12731274/// ... must pass this test...
1275testable_case: TestableCase<'tcx>,
12761277/// ... and these subpairs must match.
1278 ///
1279 /// ---
1280 /// Subpairs typically represent tests that can only be performed after their
1281 /// parent has succeeded. For example, the pattern `Some(3)` might have an
1282 /// outer match pair that tests for the variant `Some`, and then a subpair
1283 /// that tests its field for the value `3`.
1284subpairs: Vec<Self>,
12851286/// Span field of the THIR pattern this node was created from.
1287pattern_span: Span,
1288}
12891290/// A runtime test to perform to determine which candidates match a scrutinee place.
1291///
1292/// The kind of test to perform is indicated by [`TestKind`].
1293#[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)]
1294pub(crate) struct Test<'tcx> {
1295 span: Span,
1296 kind: TestKind<'tcx>,
1297}
12981299/// The kind of runtime test to perform to determine which candidates match a
1300/// scrutinee place. This is the main component of [`Test`].
1301///
1302/// Some of these variants don't contain the constant value(s) being tested
1303/// against, because those values are stored in the corresponding bucketed
1304/// candidates instead.
1305#[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)]
1306enum TestKind<'tcx> {
1307/// Test what enum variant a value is.
1308 ///
1309 /// The subset of expected variants is not stored here; instead they are
1310 /// extracted from the [`TestableCase`]s of the candidates participating in the
1311 /// test.
1312Switch {
1313/// The enum type being tested.
1314adt_def: ty::AdtDef<'tcx>,
1315 },
13161317/// Test what value an integer or `char` has.
1318 ///
1319 /// The test's target values are not stored here; instead they are extracted
1320 /// from the [`TestableCase`]s of the candidates participating in the test.
1321SwitchInt,
13221323/// Test whether a `bool` is `true` or `false`.
1324If,
13251326/// Tests the place against a string constant using string equality.
1327StringEq {
1328/// Constant string value to test against.
1329 /// Note that this value has type `str` (not `&str`).
1330value: ty::Value<'tcx>,
1331 },
13321333/// Tests the place against a constant using scalar equality.
1334ScalarEq { value: ty::Value<'tcx> },
13351336/// Test whether the value falls within an inclusive or exclusive range.
1337Range(Arc<PatRange<'tcx>>),
13381339/// Test that the length of the slice is `== len` or `>= len`.
1340SliceLen { len: u64, op: SliceLenOp },
13411342/// Call `Deref::deref[_mut]` on the value.
1343Deref {
1344/// Temporary to store the result of `deref()`/`deref_mut()`.
1345temp: Place<'tcx>,
1346 mutability: Mutability,
1347 },
13481349/// Assert unreachability of never patterns.
1350Never,
1351}
13521353/// Indicates the kind of slice-length constraint imposed by a slice pattern,
1354/// or its corresponding test.
1355#[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)]
1356enum SliceLenOp {
1357/// The slice pattern can only match a slice with exactly `len` elements.
1358Equal,
1359/// The slice pattern can match a slice with `len` or more elements
1360 /// (i.e. it contains a `..` subpattern in the middle).
1361GreaterOrEqual,
1362}
13631364/// The branch to be taken after a test.
1365#[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)]
1366enum TestBranch<'tcx> {
1367/// Success branch, used for tests with two possible outcomes.
1368Success,
1369/// Branch corresponding to this constant. Must be a scalar.
1370Constant(ty::Value<'tcx>),
1371/// Branch corresponding to this variant.
1372Variant(VariantIdx),
1373/// Failure branch for tests with two possible outcomes, and "otherwise" branch for other tests.
1374Failure,
1375}
13761377impl<'tcx> TestBranch<'tcx> {
1378fn as_constant(&self) -> Option<ty::Value<'tcx>> {
1379if let Self::Constant(v) = self { Some(*v) } else { None }
1380 }
1381}
13821383/// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether
1384/// a match arm has a guard expression attached to it.
1385#[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)]
1386pub(crate) struct ArmHasGuard(pub(crate) bool);
13871388///////////////////////////////////////////////////////////////////////////
1389// Main matching algorithm
13901391/// A sub-branch in the output of match lowering. Match lowering has generated MIR code that will
1392/// branch to `success_block` when the matched value matches the corresponding pattern. If there is
1393/// a guard, its failure must continue to `otherwise_block`, which will resume testing patterns.
1394#[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)]
1395struct MatchTreeSubBranch<'tcx> {
1396 span: Span,
1397/// The block that is branched to if the corresponding subpattern matches.
1398success_block: BasicBlock,
1399/// The block to branch to if this arm had a guard and the guard fails.
1400otherwise_block: BasicBlock,
1401/// The bindings to set up in this sub-branch.
1402bindings: Vec<Binding<'tcx>>,
1403/// The ascriptions to set up in this sub-branch.
1404ascriptions: Vec<Ascription<'tcx>>,
1405/// Whether the sub-branch corresponds to a never pattern.
1406is_never: bool,
1407}
14081409/// A branch in the output of match lowering.
1410#[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)]
1411struct MatchTreeBranch<'tcx> {
1412 sub_branches: Vec<MatchTreeSubBranch<'tcx>>,
1413}
14141415/// The result of generating MIR for a pattern-matching expression. Each input branch/arm/pattern
1416/// gives rise to an output `MatchTreeBranch`. If one of the patterns matches, we branch to the
1417/// corresponding `success_block`. If none of the patterns matches, we branch to `otherwise_block`.
1418///
1419/// Each branch is made of one of more sub-branches, corresponding to or-patterns. E.g.
1420/// ```ignore(illustrative)
1421/// match foo {
1422/// (x, false) | (false, x) => {}
1423/// (true, true) => {}
1424/// }
1425/// ```
1426/// Here the first arm gives the first `MatchTreeBranch`, which has two sub-branches, one for each
1427/// alternative of the or-pattern. They are kept separate because each needs to bind `x` to a
1428/// different place.
1429#[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)]
1430pub(crate) struct BuiltMatchTree<'tcx> {
1431 branches: Vec<MatchTreeBranch<'tcx>>,
1432 otherwise_block: BasicBlock,
1433/// If any of the branches had a guard, we collect here the places and locals to fakely borrow
1434 /// to ensure match guards can't modify the values as we match them. For more details, see
1435 /// [`util::collect_fake_borrows`].
1436fake_borrow_temps: Vec<(Place<'tcx>, Local, FakeBorrowKind)>,
1437}
14381439impl<'tcx> MatchTreeSubBranch<'tcx> {
1440fn from_sub_candidate(
1441 candidate: Candidate<'tcx>,
1442 parent_data: &Vec<PatternExtraData<'tcx>>,
1443 ) -> Self {
1444if true {
if !candidate.match_pairs.is_empty() {
::core::panicking::panic("assertion failed: candidate.match_pairs.is_empty()")
};
};debug_assert!(candidate.match_pairs.is_empty());
1445MatchTreeSubBranch {
1446 span: candidate.extra_data.span,
1447 success_block: candidate.pre_binding_block.unwrap(),
1448 otherwise_block: candidate.otherwise_block.unwrap(),
1449 bindings: sub_branch_bindings(parent_data, &candidate.extra_data.bindings),
1450 ascriptions: parent_data1451 .iter()
1452 .flat_map(|d| &d.ascriptions)
1453 .cloned()
1454 .chain(candidate.extra_data.ascriptions)
1455 .collect(),
1456 is_never: candidate.extra_data.is_never,
1457 }
1458 }
1459}
14601461impl<'tcx> MatchTreeBranch<'tcx> {
1462fn from_candidate(candidate: Candidate<'tcx>) -> Self {
1463let mut sub_branches = Vec::new();
1464traverse_candidate(
1465candidate,
1466&mut Vec::new(),
1467&mut |candidate: Candidate<'_>, parent_data: &mut Vec<PatternExtraData<'_>>| {
1468sub_branches.push(MatchTreeSubBranch::from_sub_candidate(candidate, parent_data));
1469 },
1470 |inner_candidate, parent_data| {
1471parent_data.push(inner_candidate.extra_data);
1472inner_candidate.subcandidates.into_iter()
1473 },
1474 |parent_data| {
1475parent_data.pop();
1476 },
1477 );
1478MatchTreeBranch { sub_branches }
1479 }
1480}
14811482/// Collects the bindings for a [`MatchTreeSubBranch`], preserving the order they appear in the
1483/// pattern, as though the or-alternatives chosen in this sub-branch were inlined.
1484fn sub_branch_bindings<'tcx>(
1485 parents: &[PatternExtraData<'tcx>],
1486 leaf_bindings: &[SubpatternBindings<'tcx>],
1487) -> Vec<Binding<'tcx>> {
1488// In the common case, all bindings will be in leaves. Allocate to fit the leaf's bindings.
1489let mut all_bindings = Vec::with_capacity(leaf_bindings.len());
1490let mut remainder = parents1491 .iter()
1492 .map(|parent| parent.bindings.as_slice())
1493 .chain([leaf_bindings])
1494// Skip over unsimplified or-patterns without bindings.
1495.filter(|bindings| !bindings.is_empty());
1496if let Some(candidate_bindings) = remainder.next() {
1497push_sub_branch_bindings(&mut all_bindings, candidate_bindings, &mut remainder);
1498 }
1499// Make sure we've included all bindings. For ill-formed patterns like `(x, _ | y)`, we may not
1500 // have collected all bindings yet, since we only check the first alternative when determining
1501 // whether to inline subcandidates' bindings.
1502 // FIXME(@dianne): prevent ill-formed patterns from getting here
1503while let Some(candidate_bindings) = remainder.next() {
1504 ty::tls::with(|tcx| {
1505 tcx.dcx().delayed_bug("mismatched or-pattern bindings but no error emitted")
1506 });
1507// To recover, we collect the rest in an arbitrary order.
1508push_sub_branch_bindings(&mut all_bindings, candidate_bindings, &mut remainder);
1509 }
1510all_bindings1511}
15121513/// Helper for [`sub_branch_bindings`]. Collects bindings from `candidate_bindings` into
1514/// `flattened`. Bindings in or-patterns are collected recursively from `remainder`.
1515fn push_sub_branch_bindings<'c, 'tcx: 'c>(
1516 flattened: &mut Vec<Binding<'tcx>>,
1517 candidate_bindings: &'c [SubpatternBindings<'tcx>],
1518 remainder: &mut impl Iterator<Item = &'c [SubpatternBindings<'tcx>]>,
1519) {
1520for subpat_bindings in candidate_bindings {
1521match subpat_bindings {
1522 SubpatternBindings::One(binding) => flattened.push(*binding),
1523 SubpatternBindings::FromOrPattern => {
1524// Inline bindings from an or-pattern. By construction, this always
1525 // corresponds to a subcandidate and its closest descendants (i.e. those
1526 // from nested or-patterns, but not adjacent or-patterns). To handle
1527 // adjacent or-patterns, e.g. `(x | x, y | y)`, we update the `remainder` to
1528 // point to the first descendant candidate from outside this or-pattern.
1529if let Some(subcandidate_bindings) = remainder.next() {
1530 push_sub_branch_bindings(flattened, subcandidate_bindings, remainder);
1531 } else {
1532// For ill-formed patterns like `x | _`, we may not have any subcandidates left
1533 // to inline bindings from.
1534 // FIXME(@dianne): prevent ill-formed patterns from getting here
1535ty::tls::with(|tcx| {
1536 tcx.dcx().delayed_bug("mismatched or-pattern bindings but no error emitted")
1537 });
1538 };
1539 }
1540 }
1541 }
1542}
15431544#[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)]
1545pub(crate) enum HasMatchGuard {
1546 Yes,
1547 No,
1548}
15491550#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Exhaustive {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self { Exhaustive::Yes => "Yes", Exhaustive::No => "No", })
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Exhaustive {
#[inline]
fn clone(&self) -> Exhaustive { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Exhaustive { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for Exhaustive {
#[inline]
fn eq(&self, other: &Exhaustive) -> 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 Exhaustive {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
1551pub(crate) enum Exhaustive {
1552/// `let` and `match` are exhaustive.
1553Yes,
1554/// `if let` and `let else` are not exhaustive.
1555No,
1556}
15571558impl<'a, 'tcx> Builder<'a, 'tcx> {
1559/// The entrypoint of the matching algorithm. Create the decision tree for the match expression,
1560 /// starting from `block`.
1561 ///
1562 /// `patterns` is a list of patterns, one for each arm. The associated boolean indicates whether
1563 /// the arm has a guard.
1564 ///
1565 /// `exhaustive` indicates whether the candidate list is exhaustive (for `if let` and `let else`)
1566 /// or not (for `let` and `match`). In the non-exhaustive case we return the block to which we
1567 /// branch on failure.
1568pub(crate) fn lower_match_tree(
1569&mut self,
1570 block: BasicBlock,
1571 scrutinee_span: Span,
1572 scrutinee_place_builder: &PlaceBuilder<'tcx>,
1573 match_start_span: Span,
1574 patterns: Vec<(&Pat<'tcx>, HasMatchGuard)>,
1575 exhaustive: Exhaustive,
1576 ) -> BuiltMatchTree<'tcx> {
1577// Assemble the initial list of candidates. These top-level candidates are 1:1 with the
1578 // input patterns, but other parts of match lowering also introduce subcandidates (for
1579 // sub-or-patterns). So inside the algorithm, the candidates list may not correspond to
1580 // match arms directly.
1581let mut candidates: Vec<Candidate<'_>> = patterns1582 .into_iter()
1583 .map(|(pat, has_guard)| {
1584Candidate::new(scrutinee_place_builder.clone(), pat, has_guard, self)
1585 })
1586 .collect();
15871588let fake_borrow_temps = util::collect_fake_borrows(
1589self,
1590&candidates,
1591scrutinee_span,
1592scrutinee_place_builder.base(),
1593 );
15941595// This will generate code to test scrutinee_place and branch to the appropriate arm block.
1596 // If none of the arms match, we branch to `otherwise_block`. When lowering a `match`
1597 // expression, exhaustiveness checking ensures that this block is unreachable.
1598let mut candidate_refs = candidates.iter_mut().collect::<Vec<_>>();
1599let otherwise_block =
1600self.match_candidates(match_start_span, scrutinee_span, block, &mut candidate_refs);
16011602// Set up false edges so that the borrow-checker cannot make use of the specific CFG we
1603 // generated. We falsely branch from each candidate to the one below it to make it as if we
1604 // were testing match branches one by one in order. In the non-exhaustive case we also want a
1605 // false edge to the final failure block.
1606let mut next_candidate_start_block = match exhaustive {
1607 Exhaustive::Yes => None,
1608 Exhaustive::No => Some(otherwise_block),
1609 };
1610for candidate in candidates.iter_mut().rev() {
1611let has_guard = candidate.has_guard;
1612 candidate.visit_leaves_rev(|leaf_candidate| {
1613if let Some(next_candidate_start_block) = next_candidate_start_block {
1614let source_info = self.source_info(leaf_candidate.extra_data.span);
1615// Falsely branch to `next_candidate_start_block` before reaching pre_binding.
1616let old_pre_binding = leaf_candidate.pre_binding_block.unwrap();
1617let new_pre_binding = self.cfg.start_new_block();
1618self.false_edges(
1619 old_pre_binding,
1620 new_pre_binding,
1621 next_candidate_start_block,
1622 source_info,
1623 );
1624 leaf_candidate.pre_binding_block = Some(new_pre_binding);
1625if has_guard {
1626// Falsely branch to `next_candidate_start_block` also if the guard fails.
1627let new_otherwise = self.cfg.start_new_block();
1628let old_otherwise = leaf_candidate.otherwise_block.unwrap();
1629self.false_edges(
1630 new_otherwise,
1631 old_otherwise,
1632 next_candidate_start_block,
1633 source_info,
1634 );
1635 leaf_candidate.otherwise_block = Some(new_otherwise);
1636 }
1637 }
1638if !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());
1639 next_candidate_start_block = leaf_candidate.false_edge_start_block;
1640 });
1641 }
16421643if exhaustive == Exhaustive::Yes {
1644// Match checking ensures `otherwise_block` is actually unreachable in exhaustive
1645 // cases.
1646let source_info = self.source_info(scrutinee_span);
16471648// Matching on a scrutinee place of an uninhabited type doesn't generate any memory
1649 // reads by itself, and so if the place is uninitialized we wouldn't know. In order to
1650 // disallow the following:
1651 // ```rust
1652 // let x: !;
1653 // match x {}
1654 // ```
1655 // we add a dummy read on the place.
1656 //
1657 // NOTE: If we require never patterns for empty matches, those will check that the place
1658 // is initialized, and so this read would no longer be needed.
1659let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
16601661if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
1662self.cfg.push_fake_read(
1663otherwise_block,
1664source_info,
1665cause_matched_place,
1666scrutinee_place,
1667 );
1668 }
16691670self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable);
1671 }
16721673BuiltMatchTree {
1674 branches: candidates.into_iter().map(MatchTreeBranch::from_candidate).collect(),
1675otherwise_block,
1676fake_borrow_temps,
1677 }
1678 }
16791680/// The main match algorithm. It begins with a set of candidates `candidates` and has the job of
1681 /// generating code that branches to an appropriate block if the scrutinee matches one of these
1682 /// candidates. The
1683 /// candidates are ordered such that the first item in the list
1684 /// has the highest priority. When a candidate is found to match
1685 /// the value, we will set and generate a branch to the appropriate
1686 /// pre-binding block.
1687 ///
1688 /// If none of the candidates apply, we continue to the returned `otherwise_block`.
1689 ///
1690 /// Note that while `match` expressions in the Rust language are exhaustive,
1691 /// candidate lists passed to this method are often _non-exhaustive_.
1692 /// For example, the match lowering process will frequently divide up the
1693 /// list of candidates, and recursively call this method with a non-exhaustive
1694 /// subset of candidates.
1695 /// See [`Builder::test_candidates`] for more details on this
1696 /// "backtracking automata" approach.
1697 ///
1698 /// For an example of how we use `otherwise_block`, consider:
1699 /// ```
1700 /// # fn foo((x, y): (bool, bool)) -> u32 {
1701 /// match (x, y) {
1702 /// (true, true) => 1,
1703 /// (_, false) => 2,
1704 /// (false, true) => 3,
1705 /// }
1706 /// # }
1707 /// ```
1708 /// For this match, we generate something like:
1709 /// ```
1710 /// # fn foo((x, y): (bool, bool)) -> u32 {
1711 /// if x {
1712 /// if y {
1713 /// return 1
1714 /// } else {
1715 /// // continue
1716 /// }
1717 /// } else {
1718 /// // continue
1719 /// }
1720 /// if y {
1721 /// if x {
1722 /// // This is actually unreachable because the `(true, true)` case was handled above,
1723 /// // but we don't know that from within the lowering algorithm.
1724 /// // continue
1725 /// } else {
1726 /// return 3
1727 /// }
1728 /// } else {
1729 /// return 2
1730 /// }
1731 /// // this is the final `otherwise_block`, which is unreachable because the match was exhaustive.
1732 /// unreachable!()
1733 /// # }
1734 /// ```
1735 ///
1736 /// Every `continue` is an instance of branching to some `otherwise_block` somewhere deep within
1737 /// the algorithm. For more details on why we lower like this, see [`Builder::test_candidates`].
1738 ///
1739 /// Note how we test `x` twice. This is the tradeoff of backtracking automata: we prefer smaller
1740 /// code size so we accept non-optimal code paths.
1741#[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(1741u32),
::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")]1742fn match_candidates(
1743&mut self,
1744 span: Span,
1745 scrutinee_span: Span,
1746 start_block: BasicBlock,
1747 candidates: &mut [&mut Candidate<'tcx>],
1748 ) -> BasicBlock {
1749 ensure_sufficient_stack(|| {
1750self.match_candidates_inner(span, scrutinee_span, start_block, candidates)
1751 })
1752 }
17531754/// Construct the decision tree for `candidates`. Don't call this, call `match_candidates`
1755 /// instead to reserve sufficient stack space.
1756fn match_candidates_inner(
1757&mut self,
1758 span: Span,
1759 scrutinee_span: Span,
1760mut start_block: BasicBlock,
1761 candidates: &mut [&mut Candidate<'tcx>],
1762 ) -> BasicBlock {
1763if let [first, ..] = candidates {
1764if first.false_edge_start_block.is_none() {
1765first.false_edge_start_block = Some(start_block);
1766 }
1767 }
17681769// Process a prefix of the candidates.
1770let rest = match candidates {
1771 [] => {
1772// If there are no candidates that still need testing, we're done.
1773return start_block;
1774 }
1775 [first, remaining @ ..] if first.match_pairs.is_empty() => {
1776// The first candidate has satisfied all its match pairs.
1777 // We record the blocks that will be needed by match arm lowering,
1778 // and then continue with the remaining candidates.
1779let remainder_start = self.select_matched_candidate(first, start_block);
1780remainder_start.and(remaining)
1781 }
1782 candidates if candidates.iter().any(|candidate| candidate.starts_with_or_pattern()) => {
1783// If any candidate starts with an or-pattern, we want to expand or-patterns
1784 // before we do any more tests.
1785 //
1786 // The only candidate we strictly _need_ to expand here is the first one.
1787 // But by expanding other candidates as early as possible, we unlock more
1788 // opportunities to include them in test outcomes, making the match tree
1789 // smaller and simpler.
1790self.expand_and_match_or_candidates(span, scrutinee_span, start_block, candidates)
1791 }
1792 candidates => {
1793// The first candidate has some unsatisfied match pairs; we proceed to do more tests.
1794self.test_candidates(span, scrutinee_span, candidates, start_block)
1795 }
1796 };
17971798// Process any candidates that remain.
1799let remaining_candidates = { let BlockAnd(b, v) = rest; start_block = b; v }unpack!(start_block = rest);
1800self.match_candidates(span, scrutinee_span, start_block, remaining_candidates)
1801 }
18021803/// Link up matched candidates.
1804 ///
1805 /// For example, if we have something like this:
1806 ///
1807 /// ```ignore (illustrative)
1808 /// ...
1809 /// Some(x) if cond1 => ...
1810 /// Some(x) => ...
1811 /// Some(x) if cond2 => ...
1812 /// ...
1813 /// ```
1814 ///
1815 /// We generate real edges from:
1816 ///
1817 /// * `start_block` to the [pre-binding block] of the first pattern,
1818 /// * the [otherwise block] of the first pattern to the second pattern,
1819 /// * the [otherwise block] of the third pattern to a block with an
1820 /// [`Unreachable` terminator](TerminatorKind::Unreachable).
1821 ///
1822 /// In addition, we later add fake edges from the otherwise blocks to the
1823 /// pre-binding block of the next candidate in the original set of
1824 /// candidates.
1825 ///
1826 /// [pre-binding block]: Candidate::pre_binding_block
1827 /// [otherwise block]: Candidate::otherwise_block
1828fn select_matched_candidate(
1829&mut self,
1830 candidate: &mut Candidate<'tcx>,
1831 start_block: BasicBlock,
1832 ) -> BasicBlock {
1833if !candidate.otherwise_block.is_none() {
::core::panicking::panic("assertion failed: candidate.otherwise_block.is_none()")
};assert!(candidate.otherwise_block.is_none());
1834if !candidate.pre_binding_block.is_none() {
::core::panicking::panic("assertion failed: candidate.pre_binding_block.is_none()")
};assert!(candidate.pre_binding_block.is_none());
1835if !candidate.subcandidates.is_empty() {
::core::panicking::panic("assertion failed: candidate.subcandidates.is_empty()")
};assert!(candidate.subcandidates.is_empty());
18361837candidate.pre_binding_block = Some(start_block);
1838let otherwise_block = self.cfg.start_new_block();
1839// Create the otherwise block for this candidate, which is the
1840 // pre-binding block for the next candidate.
1841candidate.otherwise_block = Some(otherwise_block);
1842otherwise_block1843 }
18441845/// Takes a list of candidates such that some of the candidates' first match pairs are
1846 /// or-patterns. This expands as many or-patterns as possible and processes the resulting
1847 /// candidates. Returns the unprocessed candidates if any.
1848fn expand_and_match_or_candidates<'b, 'c>(
1849&mut self,
1850 span: Span,
1851 scrutinee_span: Span,
1852 start_block: BasicBlock,
1853 candidates: &'b mut [&'c mut Candidate<'tcx>],
1854 ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> {
1855// We can't expand or-patterns freely. The rule is:
1856 // - If a candidate doesn't start with an or-pattern, we include it in
1857 // the expansion list as-is (i.e. it "expands" to itself).
1858 // - If a candidate has an or-pattern as its only remaining match pair,
1859 // we can expand it.
1860 // - If it starts with an or-pattern but also has other match pairs,
1861 // we can expand it, but we can't process more candidates after it.
1862 //
1863 // If we didn't stop, the `otherwise` cases could get mixed up. E.g. in the
1864 // following, or-pattern simplification (in `merge_trivial_subcandidates`) makes it
1865 // so the `1` and `2` cases branch to a same block (which then tests `false`). If we
1866 // took `(2, _)` in the same set of candidates, when we reach the block that tests
1867 // `false` we don't know whether we came from `1` or `2`, hence we can't know where
1868 // to branch on failure.
1869 //
1870 // ```ignore(illustrative)
1871 // match (1, true) {
1872 // (1 | 2, false) => {},
1873 // (2, _) => {},
1874 // _ => {}
1875 // }
1876 // ```
1877 //
1878 // We therefore split the `candidates` slice in two, expand or-patterns in the first part,
1879 // and process the rest separately.
1880let expand_until = candidates1881 .iter()
1882 .position(|candidate| {
1883// If a candidate starts with an or-pattern and has more match pairs,
1884 // we can expand it, but we must stop expanding _after_ it.
1885candidate.match_pairs.len() > 1 && candidate.starts_with_or_pattern()
1886 })
1887 .map(|pos| pos + 1) // Stop _after_ the found candidate
1888.unwrap_or(candidates.len()); // Otherwise, include all candidates
1889let (candidates_to_expand, remaining_candidates) = candidates.split_at_mut(expand_until);
18901891// Expand one level of or-patterns for each candidate in `candidates_to_expand`.
1892 // We take care to preserve the relative ordering of candidates, so that
1893 // or-patterns are expanded in their parent's relative position.
1894let mut expanded_candidates = Vec::new();
1895for candidate in candidates_to_expand.iter_mut() {
1896if candidate.starts_with_or_pattern() {
1897let or_match_pair = candidate.match_pairs.remove(0);
1898// Expand the or-pattern into subcandidates.
1899self.create_or_subcandidates(candidate, or_match_pair);
1900// Collect the newly created subcandidates.
1901for subcandidate in candidate.subcandidates.iter_mut() {
1902 expanded_candidates.push(subcandidate);
1903 }
1904// Note that the subcandidates have been added to `expanded_candidates`,
1905 // but `candidate` itself has not. If the last candidate has more match pairs,
1906 // they are handled separately by `test_remaining_match_pairs_after_or`.
1907} else {
1908// A candidate that doesn't start with an or-pattern has nothing to
1909 // expand, so it is included in the post-expansion list as-is.
1910expanded_candidates.push(candidate);
1911 }
1912 }
19131914// Recursively lower the part of the match tree represented by the
1915 // expanded candidates. This is where subcandidates actually get lowered!
1916let remainder_start = self.match_candidates(
1917span,
1918scrutinee_span,
1919start_block,
1920expanded_candidates.as_mut_slice(),
1921 );
19221923// Postprocess subcandidates, and process any leftover match pairs.
1924 // (Only the last candidate can possibly have more match pairs.)
1925if 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!({
1926let mut all_except_last = candidates_to_expand.iter().rev().skip(1);
1927 all_except_last.all(|candidate| candidate.match_pairs.is_empty())
1928 });
1929for candidate in candidates_to_expand.iter_mut() {
1930if !candidate.subcandidates.is_empty() {
1931self.merge_trivial_subcandidates(candidate);
1932self.remove_never_subcandidates(candidate);
1933 }
1934 }
1935// It's important to perform the above simplifications _before_ dealing
1936 // with remaining match pairs, to avoid exponential blowup if possible
1937 // (for trivial or-patterns), and avoid useless work (for never patterns).
1938if let Some(last_candidate) = candidates_to_expand.last_mut() {
1939self.test_remaining_match_pairs_after_or(span, scrutinee_span, last_candidate);
1940 }
19411942remainder_start.and(remaining_candidates)
1943 }
19441945/// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1946 /// subcandidate. Any candidate that has been expanded this way should also be postprocessed
1947 /// at the end of [`Self::expand_and_match_or_candidates`].
1948fn create_or_subcandidates(
1949&mut self,
1950 candidate: &mut Candidate<'tcx>,
1951 match_pair: MatchPairTree<'tcx>,
1952 ) {
1953let TestableCase::Or { pats } = match_pair.testable_case else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
1954{
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:1954",
"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(1954u32),
::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);
1955candidate.or_span = Some(match_pair.pattern_span);
1956candidate.subcandidates = pats1957 .into_iter()
1958 .map(|flat_pat| Candidate::from_flat_pat(flat_pat, candidate.has_guard))
1959 .collect();
1960candidate.subcandidates[0].false_edge_start_block = candidate.false_edge_start_block;
1961 }
19621963/// Try to merge all of the subcandidates of the given candidate into one. This avoids
1964 /// exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. The candidate should have been
1965 /// expanded with `create_or_subcandidates`.
1966 ///
1967 /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
1968 /// so:
1969 ///
1970 /// ```text
1971 /// [ start ]
1972 /// |
1973 /// [ match P, Q ]
1974 /// |
1975 /// +----------------------------------------+------------------------------------+
1976 /// | | |
1977 /// V V V
1978 /// [ P matches ] [ Q matches ] [ otherwise ]
1979 /// | | |
1980 /// V V |
1981 /// [ match R, S ] [ match R, S ] |
1982 /// | | |
1983 /// +--------------+------------+ +--------------+------------+ |
1984 /// | | | | | | |
1985 /// V V V V V V |
1986 /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ] |
1987 /// | | | | | | |
1988 /// +--------------+------------|------------+--------------+ | |
1989 /// | | | |
1990 /// | +----------------------------------------+--------+
1991 /// | |
1992 /// V V
1993 /// [ Success ] [ Failure ]
1994 /// ```
1995 ///
1996 /// In practice there are some complications:
1997 ///
1998 /// * If there's a guard, then the otherwise branch of the first match on
1999 /// `R | S` goes to a test for whether `Q` matches, and the control flow
2000 /// doesn't merge into a single success block until after the guard is
2001 /// tested.
2002 /// * If neither `P` or `Q` has any bindings or type ascriptions and there
2003 /// isn't a match guard, then we create a smaller CFG like:
2004 ///
2005 /// ```text
2006 /// ...
2007 /// +---------------+------------+
2008 /// | | |
2009 /// [ P matches ] [ Q matches ] [ otherwise ]
2010 /// | | |
2011 /// +---------------+ |
2012 /// | ...
2013 /// [ match R, S ]
2014 /// |
2015 /// ...
2016 /// ```
2017 ///
2018 /// Note that this takes place _after_ the subcandidates have participated
2019 /// in match tree lowering.
2020fn merge_trivial_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) {
2021if !!candidate.subcandidates.is_empty() {
::core::panicking::panic("assertion failed: !candidate.subcandidates.is_empty()")
};assert!(!candidate.subcandidates.is_empty());
2022if candidate.has_guard {
2023// FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
2024return;
2025 }
20262027// FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
2028let can_merge = candidate.subcandidates.iter().all(|subcandidate| {
2029subcandidate.subcandidates.is_empty() && subcandidate.extra_data.is_empty()
2030 });
2031if !can_merge {
2032return;
2033 }
20342035let mut last_otherwise = None;
2036let shared_pre_binding_block = self.cfg.start_new_block();
2037// This candidate is about to become a leaf, so unset `or_span`.
2038let or_span = candidate.or_span.take().unwrap();
2039let source_info = self.source_info(or_span);
20402041if candidate.false_edge_start_block.is_none() {
2042candidate.false_edge_start_block = candidate.subcandidates[0].false_edge_start_block;
2043 }
20442045// Remove the (known-trivial) subcandidates from the candidate tree,
2046 // so that they aren't visible after match tree lowering, and wire them
2047 // all to join up at a single shared pre-binding block.
2048 // (Note that the subcandidates have already had their part of the match
2049 // tree lowered by this point, which is why we can add a goto to them.)
2050for subcandidate in mem::take(&mut candidate.subcandidates) {
2051let subcandidate_block = subcandidate.pre_binding_block.unwrap();
2052self.cfg.goto(subcandidate_block, source_info, shared_pre_binding_block);
2053 last_otherwise = subcandidate.otherwise_block;
2054 }
2055candidate.pre_binding_block = Some(shared_pre_binding_block);
2056if !last_otherwise.is_some() {
::core::panicking::panic("assertion failed: last_otherwise.is_some()")
};assert!(last_otherwise.is_some());
2057candidate.otherwise_block = last_otherwise;
2058 }
20592060/// Never subcandidates may have a set of bindings inconsistent with their siblings,
2061 /// which would break later code. So we filter them out. Note that we can't filter out
2062 /// top-level candidates this way.
2063fn remove_never_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) {
2064if candidate.subcandidates.is_empty() {
2065return;
2066 }
20672068let false_edge_start_block = candidate.subcandidates[0].false_edge_start_block;
2069candidate.subcandidates.retain_mut(|candidate| {
2070if candidate.extra_data.is_never {
2071candidate.visit_leaves(|subcandidate| {
2072let block = subcandidate.pre_binding_block.unwrap();
2073// That block is already unreachable but needs a terminator to make the MIR well-formed.
2074let source_info = self.source_info(subcandidate.extra_data.span);
2075self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
2076 });
2077false
2078} else {
2079true
2080}
2081 });
2082if candidate.subcandidates.is_empty() {
2083// If `candidate` has become a leaf candidate, ensure it has a `pre_binding_block` and `otherwise_block`.
2084let next_block = self.cfg.start_new_block();
2085candidate.pre_binding_block = Some(next_block);
2086candidate.otherwise_block = Some(next_block);
2087// In addition, if `candidate` doesn't have `false_edge_start_block`, it should be assigned here.
2088if candidate.false_edge_start_block.is_none() {
2089candidate.false_edge_start_block = false_edge_start_block;
2090 }
2091 }
2092 }
20932094/// If more match pairs remain, test them after each subcandidate.
2095 /// We could have added them to the or-candidates during or-pattern expansion, but that
2096 /// would make it impossible to detect simplifiable or-patterns. That would guarantee
2097 /// exponentially large CFGs for cases like `(1 | 2, 3 | 4, ...)`.
2098fn test_remaining_match_pairs_after_or(
2099&mut self,
2100 span: Span,
2101 scrutinee_span: Span,
2102 candidate: &mut Candidate<'tcx>,
2103 ) {
2104if candidate.match_pairs.is_empty() {
2105return;
2106 }
21072108let or_span = candidate.or_span.unwrap_or(candidate.extra_data.span);
2109let source_info = self.source_info(or_span);
2110let mut last_otherwise = None;
2111candidate.visit_leaves(|leaf_candidate| {
2112last_otherwise = leaf_candidate.otherwise_block;
2113 });
21142115let remaining_match_pairs = mem::take(&mut candidate.match_pairs);
2116// We're testing match pairs that remained after an `Or`, so the remaining
2117 // pairs should all be `Or` too, due to the sorting invariant.
2118if 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!(
2119 remaining_match_pairs
2120 .iter()
2121 .all(|match_pair| matches!(match_pair.testable_case, TestableCase::Or { .. }))
2122 );
21232124// Visit each leaf candidate within this subtree, add a copy of the remaining
2125 // match pairs to it, and then recursively lower the rest of the match tree
2126 // from that point.
2127candidate.visit_leaves(|leaf_candidate| {
2128// At this point the leaf's own match pairs have all been lowered
2129 // and removed, so `extend` and assignment are equivalent,
2130 // but extending can also recycle any existing vector capacity.
2131if !leaf_candidate.match_pairs.is_empty() {
::core::panicking::panic("assertion failed: leaf_candidate.match_pairs.is_empty()")
};assert!(leaf_candidate.match_pairs.is_empty());
2132leaf_candidate.match_pairs.extend(remaining_match_pairs.iter().cloned());
21332134let or_start = leaf_candidate.pre_binding_block.unwrap();
2135let otherwise =
2136self.match_candidates(span, scrutinee_span, or_start, &mut [leaf_candidate]);
2137// In a case like `(P | Q, R | S)`, if `P` succeeds and `R | S` fails, we know `(Q,
2138 // R | S)` will fail too. If there is no guard, we skip testing of `Q` by branching
2139 // directly to `last_otherwise`. If there is a guard,
2140 // `leaf_candidate.otherwise_block` can be reached by guard failure as well, so we
2141 // can't skip `Q`.
2142let or_otherwise = if leaf_candidate.has_guard {
2143leaf_candidate.otherwise_block.unwrap()
2144 } else {
2145last_otherwise.unwrap()
2146 };
2147self.cfg.goto(otherwise, source_info, or_otherwise);
2148 });
2149 }
21502151/// Pick a test to run. Which test doesn't matter as long as it is guaranteed to fully match at
2152 /// least one match pair. We currently simply pick the test corresponding to the first match
2153 /// pair of the first candidate in the list.
2154 ///
2155 /// *Note:* taking the first match pair is somewhat arbitrary, and we might do better here by
2156 /// choosing more carefully what to test.
2157 ///
2158 /// For example, consider the following possible match-pairs:
2159 ///
2160 /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has
2161 /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has
2162 /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in
2163 /// 4. etc.
2164 ///
2165 /// [`Switch`]: TestKind::Switch
2166 /// [`SwitchInt`]: TestKind::SwitchInt
2167 /// [`Range`]: TestKind::Range
2168fn pick_test(&mut self, candidates: &[&mut Candidate<'tcx>]) -> (Place<'tcx>, Test<'tcx>) {
2169// Extract the match-pair from the highest priority candidate
2170let match_pair = &candidates[0].match_pairs[0];
2171let test = self.pick_test_for_match_pair(match_pair);
2172// Unwrap is ok after simplification.
2173let match_place = match_pair.place.unwrap();
2174{
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:2174",
"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(2174u32),
::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);
21752176 (match_place, test)
2177 }
21782179/// This is the most subtle part of the match lowering algorithm. At this point, there are
2180 /// no fully-satisfied candidates, and no or-patterns to expand, so we actually need to
2181 /// perform some sort of test to make progress.
2182 ///
2183 /// Once we pick what sort of test we are going to perform, this test will help us winnow down
2184 /// our candidates. So we walk over the candidates (from high to low priority) and check. We
2185 /// compute, for each outcome of the test, a list of (modified) candidates. If a candidate
2186 /// matches in exactly one branch of our test, we add it to the corresponding outcome. We also
2187 /// **mutate its list of match pairs** if appropriate, to reflect the fact that we know which
2188 /// outcome occurred.
2189 ///
2190 /// For example, if we are testing `x.0`'s variant, and we have a candidate `(x.0 @ Some(v), x.1
2191 /// @ 22)`, then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)` in the
2192 /// branch corresponding to `Some`. To ensure we make progress, we always pick a test that
2193 /// results in simplifying the first candidate.
2194 ///
2195 /// But there may also be candidates that the test doesn't
2196 /// apply to. The classical example is wildcards:
2197 ///
2198 /// ```
2199 /// # let (x, y, z) = (true, true, true);
2200 /// match (x, y, z) {
2201 /// (true , _ , true ) => true, // (0)
2202 /// (false, false, _ ) => false, // (1)
2203 /// (_ , true , _ ) => true, // (2)
2204 /// (true , _ , false) => false, // (3)
2205 /// }
2206 /// # ;
2207 /// ```
2208 ///
2209 /// Here, the traditional "decision tree" method would generate 2 separate code-paths for the 2
2210 /// possible values of `x`. This would however duplicate some candidates, which would need to be
2211 /// lowered several times.
2212 ///
2213 /// In some cases, this duplication can create an exponential amount of
2214 /// code. This is most easily seen by noticing that this method terminates
2215 /// with precisely the reachable arms being reachable - but that problem
2216 /// is trivially NP-complete:
2217 ///
2218 /// ```ignore (illustrative)
2219 /// match (var0, var1, var2, var3, ...) {
2220 /// (true , _ , _ , false, true, ...) => false,
2221 /// (_ , true, true , false, _ , ...) => false,
2222 /// (false, _ , false, false, _ , ...) => false,
2223 /// ...
2224 /// _ => true
2225 /// }
2226 /// ```
2227 ///
2228 /// Here the last arm is reachable only if there is an assignment to
2229 /// the variables that does not match any of the literals. Therefore,
2230 /// compilation would take an exponential amount of time in some cases.
2231 ///
2232 /// In rustc, we opt instead for the "backtracking automaton" approach. This guarantees we never
2233 /// duplicate a candidate (except in the presence of or-patterns). In fact this guarantee is
2234 /// ensured by the fact that we carry around `&mut Candidate`s which can't be duplicated.
2235 ///
2236 /// To make this work, whenever we decide to perform a test, if we encounter a candidate that
2237 /// could match in more than one branch of the test, we stop. We generate code for the test and
2238 /// for the candidates in its branches; the remaining candidates will be tested if the
2239 /// candidates in the branches fail to match.
2240 ///
2241 /// For example, if we test on `x` in the following:
2242 /// ```
2243 /// # fn foo((x, y, z): (bool, bool, bool)) -> u32 {
2244 /// match (x, y, z) {
2245 /// (true , _ , true ) => 0,
2246 /// (false, false, _ ) => 1,
2247 /// (_ , true , _ ) => 2,
2248 /// (true , _ , false) => 3,
2249 /// }
2250 /// # }
2251 /// ```
2252 /// this function generates code that looks more of less like:
2253 /// ```
2254 /// # fn foo((x, y, z): (bool, bool, bool)) -> u32 {
2255 /// if x {
2256 /// match (y, z) {
2257 /// (_, true) => return 0,
2258 /// _ => {} // continue matching
2259 /// }
2260 /// } else {
2261 /// match (y, z) {
2262 /// (false, _) => return 1,
2263 /// _ => {} // continue matching
2264 /// }
2265 /// }
2266 /// // the block here is `remainder_start`
2267 /// match (x, y, z) {
2268 /// (_ , true , _ ) => 2,
2269 /// (true , _ , false) => 3,
2270 /// _ => unreachable!(),
2271 /// }
2272 /// # }
2273 /// ```
2274 ///
2275 /// We return the unprocessed candidates.
2276fn test_candidates<'b, 'c>(
2277&mut self,
2278 span: Span,
2279 scrutinee_span: Span,
2280 candidates: &'b mut [&'c mut Candidate<'tcx>],
2281 start_block: BasicBlock,
2282 ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> {
2283// Choose a match pair from the first candidate, and use it to determine a
2284 // test to perform that will confirm or refute that match pair.
2285let (match_place, test) = self.pick_test(candidates);
22862287// For each of the N possible test outcomes, build the vector of candidates that applies if
2288 // the test has that particular outcome. This also mutates the candidates to remove match
2289 // pairs that are fully satisfied by the relevant outcome.
2290let PartitionedCandidates { target_candidates, remaining_candidates } =
2291self.partition_candidates_into_buckets(match_place, &test, candidates);
22922293// The block that we should branch to if none of the `target_candidates` match.
2294let remainder_start = self.cfg.start_new_block();
22952296// For each outcome of the test, recursively lower the rest of the match tree
2297 // from that point. (Note that we haven't lowered the actual test yet!)
2298let target_blocks: FxIndexMap<_, _> = target_candidates2299 .into_iter()
2300 .map(|(branch, mut candidates)| {
2301let branch_start = self.cfg.start_new_block();
2302// Recursively lower the rest of the match tree after the relevant outcome.
2303let branch_otherwise =
2304self.match_candidates(span, scrutinee_span, branch_start, &mut *candidates);
23052306// Link up the `otherwise` block of the subtree to `remainder_start`.
2307let source_info = self.source_info(span);
2308self.cfg.goto(branch_otherwise, source_info, remainder_start);
2309 (branch, branch_start)
2310 })
2311 .collect();
23122313// Perform the chosen test, branching to one of the N subtrees prepared above
2314 // (or to `remainder_start` if no outcome was satisfied).
2315self.perform_test(
2316span,
2317scrutinee_span,
2318start_block,
2319remainder_start,
2320match_place,
2321&test,
2322target_blocks,
2323 );
23242325remainder_start.and(remaining_candidates)
2326 }
2327}
23282329///////////////////////////////////////////////////////////////////////////
2330// Pat binding - used for `let` and function parameters as well.
23312332impl<'a, 'tcx> Builder<'a, 'tcx> {
2333/// Lowers a `let` expression that appears in a suitable context
2334 /// (e.g. an `if` condition or match guard).
2335 ///
2336 /// Also used for lowering let-else statements, since they have similar
2337 /// needs despite not actually using `let` expressions.
2338 ///
2339 /// Use [`DeclareLetBindings`] to control whether the `let` bindings are
2340 /// declared or not.
2341pub(crate) fn lower_let_expr(
2342&mut self,
2343mut block: BasicBlock,
2344 expr_id: ExprId,
2345 pat: &Pat<'tcx>,
2346 source_scope: Option<SourceScope>,
2347 scope_span: Span,
2348 declare_let_bindings: DeclareLetBindings,
2349 ) -> BlockAnd<()> {
2350let expr_span = self.thir[expr_id].span;
2351let 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));
2352let built_tree = self.lower_match_tree(
2353block,
2354expr_span,
2355&scrutinee,
2356pat.span,
2357::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)],
2358 Exhaustive::No,
2359 );
2360let [branch] = built_tree.branches.try_into().unwrap();
23612362self.break_for_else(built_tree.otherwise_block, self.source_info(expr_span));
23632364match declare_let_bindings {
2365 DeclareLetBindings::Yes => {
2366let expr_place = scrutinee.try_to_place(self);
2367let opt_expr_place = expr_place.as_ref().map(|place| (Some(place), expr_span));
2368self.declare_bindings(
2369source_scope,
2370pat.span.to(scope_span),
2371pat,
2372None,
2373opt_expr_place,
2374 );
2375 }
2376 DeclareLetBindings::No => {} // Caller is responsible for bindings.
2377DeclareLetBindings::LetNotPermitted => {
2378self.tcx.dcx().span_bug(expr_span, "let expression not expected in this context")
2379 }
2380 }
23812382let success = self.bind_pattern(self.source_info(pat.span), branch, &[], expr_span, None);
23832384// If branch coverage is enabled, record this branch.
2385self.visit_coverage_conditional_let(pat, success, built_tree.otherwise_block);
23862387success.unit()
2388 }
23892390/// Initializes each of the bindings from the candidate by
2391 /// moving/copying/ref'ing the source as appropriate. Tests the guard, if
2392 /// any, and then branches to the arm. Returns the block for the case where
2393 /// the guard succeeds.
2394 ///
2395 /// Note: we do not check earlier that if there is a guard,
2396 /// there cannot be move bindings. We avoid a use-after-move by only
2397 /// moving the binding once the guard has evaluated to true (see below).
2398fn bind_and_guard_matched_candidate(
2399&mut self,
2400 sub_branch: MatchTreeSubBranch<'tcx>,
2401 fake_borrows: &[(Place<'tcx>, Local, FakeBorrowKind)],
2402 scrutinee_span: Span,
2403 arm_match_scope: Option<(&Arm<'tcx>, region::Scope)>,
2404 schedule_drops: ScheduleDrops,
2405 ) -> BasicBlock {
2406{
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:2406",
"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(2406u32),
::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);
24072408let block = sub_branch.success_block;
24092410if sub_branch.is_never {
2411// This arm has a dummy body, we don't need to generate code for it. `block` is already
2412 // unreachable (except via false edge).
2413let source_info = self.source_info(sub_branch.span);
2414self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
2415return self.cfg.start_new_block();
2416 }
24172418self.ascribe_types(block, sub_branch.ascriptions);
24192420// Lower an instance of the arm guard (if present) for this candidate,
2421 // and then perform bindings for the arm body.
2422if let Some((arm, match_scope)) = arm_match_scope2423 && let Some(guard) = arm.guard
2424 {
2425let tcx = self.tcx;
24262427// Bindings for guards require some extra handling to automatically
2428 // insert implicit references/dereferences.
2429 // This always schedules storage drops, so we may need to unschedule them below.
2430self.bind_matched_candidate_for_guard(block, sub_branch.bindings.iter());
2431let guard_frame = GuardFrame {
2432 locals: sub_branch2433 .bindings
2434 .iter()
2435 .map(|b| GuardFrameLocal::new(b.var_id))
2436 .collect(),
2437 };
2438{
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:2438",
"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(2438u32),
::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);
2439self.guard_context.push(guard_frame);
24402441let re_erased = tcx.lifetimes.re_erased;
2442let scrutinee_source_info = self.source_info(scrutinee_span);
2443for &(place, temp, kind) in fake_borrows {
2444let borrow = Rvalue::Ref(re_erased, BorrowKind::Fake(kind), place);
2445self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
2446 }
24472448let mut guard_span = rustc_span::DUMMY_SP;
24492450let (post_guard_block, otherwise_post_guard_block) =
2451self.in_if_then_scope(match_scope, guard_span, |this| {
2452guard_span = this.thir[guard].span;
2453this.then_else_break(
2454block,
2455guard,
2456None, // Use `self.local_scope()` as the temp scope
2457this.source_info(arm.span),
2458 DeclareLetBindings::No, // For guards, `let` bindings are declared separately
2459)
2460 });
24612462// If this isn't the final sub-branch being lowered, we need to unschedule drops of
2463 // bindings and temporaries created for and by the guard. As a result, the drop order
2464 // for the arm will correspond to the binding order of the final sub-branch lowered.
2465if #[allow(non_exhaustive_omitted_patterns)] match schedule_drops {
ScheduleDrops::No => true,
_ => false,
}matches!(schedule_drops, ScheduleDrops::No) {
2466self.clear_match_arm_and_guard_scopes(arm.scope);
2467 }
24682469let source_info = self.source_info(guard_span);
2470let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
2471let guard_frame = self.guard_context.pop().unwrap();
2472{
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:2472",
"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(2472u32),
::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);
24732474for &(_, temp, _) in fake_borrows {
2475let cause = FakeReadCause::ForMatchGuard;
2476self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp));
2477 }
24782479self.cfg.goto(otherwise_post_guard_block, source_info, sub_branch.otherwise_block);
24802481// We want to ensure that the matched candidates are bound
2482 // after we have confirmed this candidate *and* any
2483 // associated guard; Binding them on `block` is too soon,
2484 // because that would be before we've checked the result
2485 // from the guard.
2486 //
2487 // But binding them on the arm is *too late*, because
2488 // then all of the candidates for a single arm would be
2489 // bound in the same place, that would cause a case like:
2490 //
2491 // ```rust
2492 // match (30, 2) {
2493 // (mut x, 1) | (2, mut x) if { true } => { ... }
2494 // ... // ^^^^^^^ (this is `arm_block`)
2495 // }
2496 // ```
2497 //
2498 // would yield an `arm_block` something like:
2499 //
2500 // ```
2501 // StorageLive(_4); // _4 is `x`
2502 // _4 = &mut (_1.0: i32); // this is handling `(mut x, 1)` case
2503 // _4 = &mut (_1.1: i32); // this is handling `(2, mut x)` case
2504 // ```
2505 //
2506 // and that is clearly not correct.
2507let by_value_bindings = sub_branch2508 .bindings
2509 .iter()
2510 .filter(|binding| #[allow(non_exhaustive_omitted_patterns)] match binding.binding_mode.0 {
ByRef::No => true,
_ => false,
}matches!(binding.binding_mode.0, ByRef::No));
2511// Read all of the by reference bindings to ensure that the
2512 // place they refer to can't be modified by the guard.
2513for binding in by_value_bindings.clone() {
2514let local_id = self.var_local_id(binding.var_id, RefWithinGuard);
2515let cause = FakeReadCause::ForGuardBinding;
2516self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id));
2517 }
2518// Only schedule drops for the last sub-branch we lower.
2519self.bind_matched_candidate_for_arm_body(
2520post_guard_block,
2521schedule_drops,
2522by_value_bindings,
2523 );
25242525post_guard_block2526 } else {
2527// (Here, it is not too early to bind the matched
2528 // candidate on `block`, because there is no guard result
2529 // that we have to inspect before we bind them.)
2530self.bind_matched_candidate_for_arm_body(
2531block,
2532schedule_drops,
2533sub_branch.bindings.iter(),
2534 );
2535block2536 }
2537 }
25382539/// Append `AscribeUserType` statements onto the end of `block`
2540 /// for each ascription
2541fn ascribe_types(
2542&mut self,
2543 block: BasicBlock,
2544 ascriptions: impl IntoIterator<Item = Ascription<'tcx>>,
2545 ) {
2546for ascription in ascriptions {
2547let source_info = self.source_info(ascription.annotation.span);
25482549let base = self.canonical_user_type_annotations.push(ascription.annotation);
2550self.cfg.push(
2551 block,
2552 Statement::new(
2553 source_info,
2554 StatementKind::AscribeUserType(
2555 Box::new((
2556 ascription.source,
2557 UserTypeProjection { base, projs: Vec::new() },
2558 )),
2559 ascription.variance,
2560 ),
2561 ),
2562 );
2563 }
2564 }
25652566/// Binding for guards is a bit different from binding for the arm body,
2567 /// because an extra layer of implicit reference/dereference is added.
2568 ///
2569 /// The idea is that any pattern bindings of type T will map to a `&T` within
2570 /// the context of the guard expression, but will continue to map to a `T`
2571 /// in the context of the arm body. To avoid surfacing this distinction in
2572 /// the user source code (which would be a severe change to the language and
2573 /// require far more revision to the compiler), any occurrence of the
2574 /// identifier in the guard expression will automatically get a deref op
2575 /// applied to it. (See the caller of [`Self::is_bound_var_in_guard`].)
2576 ///
2577 /// So an input like:
2578 ///
2579 /// ```ignore (illustrative)
2580 /// let place = Foo::new();
2581 /// match place { foo if inspect(foo)
2582 /// => feed(foo), ... }
2583 /// ```
2584 ///
2585 /// will be treated as if it were really something like:
2586 ///
2587 /// ```ignore (illustrative)
2588 /// let place = Foo::new();
2589 /// match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
2590 /// => { let tmp2 = place; feed(tmp2) }, ... }
2591 /// ```
2592 ///
2593 /// And an input like:
2594 ///
2595 /// ```ignore (illustrative)
2596 /// let place = Foo::new();
2597 /// match place { ref mut foo if inspect(foo)
2598 /// => feed(foo), ... }
2599 /// ```
2600 ///
2601 /// will be treated as if it were really something like:
2602 ///
2603 /// ```ignore (illustrative)
2604 /// let place = Foo::new();
2605 /// match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
2606 /// => { let tmp2 = &mut place; feed(tmp2) }, ... }
2607 /// ```
2608 /// ---
2609 ///
2610 /// ## Implementation notes
2611 ///
2612 /// To encode the distinction above, we must inject the
2613 /// temporaries `tmp1` and `tmp2`.
2614 ///
2615 /// There are two cases of interest: binding by-value, and binding by-ref.
2616 ///
2617 /// 1. Binding by-value: Things are simple.
2618 ///
2619 /// * Establishing `tmp1` creates a reference into the
2620 /// matched place. This code is emitted by
2621 /// [`Self::bind_matched_candidate_for_guard`].
2622 ///
2623 /// * `tmp2` is only initialized "lazily", after we have
2624 /// checked the guard. Thus, the code that can trigger
2625 /// moves out of the candidate can only fire after the
2626 /// guard evaluated to true. This initialization code is
2627 /// emitted by [`Self::bind_matched_candidate_for_arm_body`].
2628 ///
2629 /// 2. Binding by-reference: Things are tricky.
2630 ///
2631 /// * Here, the guard expression wants a `&&` or `&&mut`
2632 /// into the original input. This means we need to borrow
2633 /// the reference that we create for the arm.
2634 /// * So we eagerly create the reference for the arm and then take a
2635 /// reference to that.
2636 ///
2637 /// ---
2638 ///
2639 /// See these PRs for some historical context:
2640 /// - <https://github.com/rust-lang/rust/pull/49870> (introduction of autoref)
2641 /// - <https://github.com/rust-lang/rust/pull/59114> (always use autoref)
2642fn bind_matched_candidate_for_guard<'b>(
2643&mut self,
2644 block: BasicBlock,
2645 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2646 ) where
2647'tcx: 'b,
2648 {
2649{
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:2649",
"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(2649u32),
::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);
26502651// Assign each of the bindings. Since we are binding for a
2652 // guard expression, this will never trigger moves out of the
2653 // candidate.
2654let re_erased = self.tcx.lifetimes.re_erased;
2655for binding in bindings {
2656{
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:2656",
"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(2656u32),
::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);
2657let source_info = self.source_info(binding.span);
26582659// For each pattern ident P of type T, `ref_for_guard` is
2660 // a reference R: &T pointing to the location matched by
2661 // the pattern, and every occurrence of P within a guard
2662 // denotes *R.
2663 // Drops must be scheduled to emit `StorageDead` on the guard's failure/break branches.
2664let ref_for_guard = self.storage_live_binding(
2665 block,
2666 binding.var_id,
2667 binding.span,
2668 binding.is_shorthand,
2669 RefWithinGuard,
2670 ScheduleDrops::Yes,
2671 );
2672match binding.binding_mode.0 {
2673 ByRef::No => {
2674// The arm binding will be by value, so for the guard binding
2675 // just take a shared reference to the matched place.
2676let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source);
2677self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2678 }
2679 ByRef::Yes(pinnedness, mutbl) => {
2680// The arm binding will be by reference, so eagerly create it now // be scheduled to emit `StorageDead` on the guard's failure/break branches.
2681let value_for_arm = self.storage_live_binding(
2682 block,
2683 binding.var_id,
2684 binding.span,
2685 binding.is_shorthand,
2686 OutsideGuard,
2687 ScheduleDrops::Yes,
2688 );
26892690let rvalue =
2691 Rvalue::Ref(re_erased, util::ref_pat_borrow_kind(mutbl), binding.source);
2692let rvalue = match pinnedness {
2693 ty::Pinnedness::Not => rvalue,
2694 ty::Pinnedness::Pinned => {
2695self.pin_borrowed_local(block, value_for_arm.local, rvalue, source_info)
2696 }
2697 };
2698self.cfg.push_assign(block, source_info, value_for_arm, rvalue);
2699// For the guard binding, take a shared reference to that reference.
2700let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm);
2701self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
2702 }
2703 }
2704 }
2705 }
27062707fn bind_matched_candidate_for_arm_body<'b>(
2708&mut self,
2709 block: BasicBlock,
2710 schedule_drops: ScheduleDrops,
2711 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2712 ) where
2713'tcx: 'b,
2714 {
2715{
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:2715",
"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(2715u32),
::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);
27162717let re_erased = self.tcx.lifetimes.re_erased;
2718// Assign each of the bindings. This may trigger moves out of the candidate.
2719for binding in bindings {
2720let source_info = self.source_info(binding.span);
2721let local = self.storage_live_binding(
2722 block,
2723 binding.var_id,
2724 binding.span,
2725 binding.is_shorthand,
2726 OutsideGuard,
2727 schedule_drops,
2728 );
2729if #[allow(non_exhaustive_omitted_patterns)] match schedule_drops {
ScheduleDrops::Yes => true,
_ => false,
}matches!(schedule_drops, ScheduleDrops::Yes) {
2730self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
2731 }
2732let rvalue = match binding.binding_mode.0 {
2733 ByRef::No => {
2734 Rvalue::Use(self.consume_by_copy_or_move(binding.source), WithRetag::Yes)
2735 }
2736 ByRef::Yes(pinnedness, mutbl) => {
2737let rvalue =
2738 Rvalue::Ref(re_erased, util::ref_pat_borrow_kind(mutbl), binding.source);
2739match pinnedness {
2740 ty::Pinnedness::Not => rvalue,
2741 ty::Pinnedness::Pinned => {
2742self.pin_borrowed_local(block, local.local, rvalue, source_info)
2743 }
2744 }
2745 }
2746 };
2747self.cfg.push_assign(block, source_info, local, rvalue);
2748 }
2749 }
27502751/// Given an rvalue `&[mut]borrow` and a local `local`, generate the pinned borrow for it:
2752 /// ```ignore (illustrative)
2753 /// pinned_temp = &borrow;
2754 /// local = Pin { __pointer: move pinned_temp };
2755 /// ```
2756fn pin_borrowed_local(
2757&mut self,
2758 block: BasicBlock,
2759 local: Local,
2760 borrow: Rvalue<'tcx>,
2761 source_info: SourceInfo,
2762 ) -> Rvalue<'tcx> {
2763if 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(..));
27642765let local_ty = self.local_decls[local].ty;
27662767let pinned_ty = local_ty.pinned_ty().unwrap_or_else(|| {
2768::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!(
2769 source_info.span,
2770"expect type `Pin` for a pinned binding, found type {:?}",
2771 local_ty
2772 )2773 });
2774let pinned_temp =
2775Place::from(self.local_decls.push(LocalDecl::new(pinned_ty, source_info.span)));
2776self.cfg.push_assign(block, source_info, pinned_temp, borrow);
2777 Rvalue::Aggregate(
2778Box::new(AggregateKind::Adt(
2779self.tcx.require_lang_item(LangItem::Pin, source_info.span),
2780FIRST_VARIANT,
2781self.tcx.mk_args(&[pinned_ty.into()]),
2782None,
2783None,
2784 )),
2785 std::iter::once(Operand::Move(pinned_temp)).collect(),
2786 )
2787 }
27882789/// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound
2790 /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The
2791 /// first local is a binding for occurrences of `var` in the guard, which
2792 /// will have type `&T`. The second local is a binding for occurrences of
2793 /// `var` in the arm body, which will have type `T`.
2794#[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(2794u32),
::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:2864",
"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(2864u32),
::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")]2795fn declare_binding(
2796&mut self,
2797 source_info: SourceInfo,
2798 visibility_scope: SourceScope,
2799 name: Symbol,
2800 mode: BindingMode,
2801 var_id: LocalVarId,
2802 var_ty: Ty<'tcx>,
2803 user_ty: Option<Box<UserTypeProjections>>,
2804 has_guard: ArmHasGuard,
2805 opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
2806 pat_span: Span,
2807 ) {
2808let tcx = self.tcx;
2809let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope };
2810let local = LocalDecl {
2811 mutability: mode.1,
2812 ty: var_ty,
2813 user_ty,
2814 source_info,
2815 local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(BindingForm::Var(
2816 VarBindingForm {
2817 binding_mode: mode,
2818// hypothetically, `visit_primary_bindings` could try to unzip
2819 // an outermost hir::Ty as we descend, matching up
2820 // idents in pat; but complex w/ unclear UI payoff.
2821 // Instead, just abandon providing diagnostic info.
2822opt_ty_info: None,
2823 opt_match_place,
2824 pat_span,
2825 introductions: Vec::new(),
2826 },
2827 )))),
2828 };
2829let for_arm_body = self.local_decls.push(local);
2830if self.should_emit_debug_info_for_binding(name, var_id) {
2831self.var_debug_info.push(VarDebugInfo {
2832 name,
2833 source_info: debug_source_info,
2834 value: VarDebugInfoContents::Place(for_arm_body.into()),
2835 composite: None,
2836 argument_index: None,
2837 });
2838 }
2839let locals = if has_guard.0 {
2840let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
2841// This variable isn't mutated but has a name, so has to be
2842 // immutable to avoid the unused mut lint.
2843mutability: Mutability::Not,
2844 ty: Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, var_ty),
2845 user_ty: None,
2846 source_info,
2847 local_info: ClearCrossCrate::Set(Box::new(LocalInfo::User(
2848 BindingForm::RefForGuard(for_arm_body),
2849 ))),
2850 });
2851if self.should_emit_debug_info_for_binding(name, var_id) {
2852self.var_debug_info.push(VarDebugInfo {
2853 name,
2854 source_info: debug_source_info,
2855 value: VarDebugInfoContents::Place(ref_for_guard.into()),
2856 composite: None,
2857 argument_index: None,
2858 });
2859 }
2860 LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
2861 } else {
2862 LocalsForNode::One(for_arm_body)
2863 };
2864debug!(?locals);
2865self.var_indices.insert(var_id, locals);
2866 }
28672868/// Some bindings are introduced when producing HIR from the AST and don't
2869 /// actually exist in the source. Skip producing debug info for those when
2870 /// we can recognize them.
2871fn should_emit_debug_info_for_binding(&self, name: Symbol, var_id: LocalVarId) -> bool {
2872// For now we only recognize the output of desugaring assigns.
2873if name != sym::lhs {
2874return true;
2875 }
28762877let tcx = self.tcx;
2878for (_, node) in tcx.hir_parent_iter(var_id.0) {
2879// FIXME(khuey) at what point is it safe to bail on the iterator?
2880 // Can we stop at the first non-Pat node?
2881if #[allow(non_exhaustive_omitted_patterns)] match node {
Node::LetStmt(&LetStmt { source: LocalSource::AssignDesugar, .. }) =>
true,
_ => false,
}matches!(node, Node::LetStmt(&LetStmt { source: LocalSource::AssignDesugar, .. })) {
2882return false;
2883 }
2884 }
28852886true
2887}
28882889/// Attempt to statically pick the `BasicBlock` that a value would resolve to at runtime.
2890pub(crate) fn static_pattern_match(
2891&self,
2892 cx: &RustcPatCtxt<'_, 'tcx>,
2893 valtree: ValTree<'tcx>,
2894 arms: &[ArmId],
2895 built_match_tree: &BuiltMatchTree<'tcx>,
2896 ) -> Option<BasicBlock> {
2897let it = arms.iter().zip(built_match_tree.branches.iter());
2898for (&arm_id, branch) in it {
2899let pat = cx.lower_pat(&*self.thir.arms[arm_id].pattern);
29002901// Peel off or-patterns if they exist.
2902if let rustc_pattern_analysis::rustc::Constructor::Or = pat.ctor() {
2903for pat in pat.iter_fields() {
2904// For top-level or-patterns (the only ones we accept right now), when the
2905 // bindings are the same (e.g. there are none), the sub_branch is stored just
2906 // once.
2907let sub_branch = branch
2908 .sub_branches
2909 .get(pat.idx)
2910 .or_else(|| branch.sub_branches.last())
2911 .unwrap();
29122913match self.static_pattern_match_inner(valtree, &pat.pat) {
2914true => return Some(sub_branch.success_block),
2915false => continue,
2916 }
2917 }
2918 } else if self.static_pattern_match_inner(valtree, &pat) {
2919return Some(branch.sub_branches[0].success_block);
2920 }
2921 }
29222923None2924 }
29252926/// Helper for [`Self::static_pattern_match`], checking whether the value represented by the
2927 /// `ValTree` matches the given pattern. This function does not recurse, meaning that it does
2928 /// not handle or-patterns, or patterns for types with fields.
2929fn static_pattern_match_inner(
2930&self,
2931 valtree: ty::ValTree<'tcx>,
2932 pat: &DeconstructedPat<'_, 'tcx>,
2933 ) -> bool {
2934use rustc_pattern_analysis::constructor::{IntRange, MaybeInfiniteInt};
2935use rustc_pattern_analysis::rustc::Constructor;
29362937match pat.ctor() {
2938 Constructor::Variant(variant_index) => {
2939let ValTreeKind::Branch(branch) = *valtreeelse {
2940::rustc_middle::util::bug::bug_fmt(format_args!("malformed valtree for an enum"))bug!("malformed valtree for an enum")2941 };
2942if branch.len() != 1 {
2943::rustc_middle::util::bug::bug_fmt(format_args!("malformed valtree for an enum"))bug!("malformed valtree for an enum")2944 };
2945let ValTreeKind::Leaf(actual_variant_idx) = **branch[0].to_value().valtree else {
2946::rustc_middle::util::bug::bug_fmt(format_args!("malformed valtree for an enum"))bug!("malformed valtree for an enum")2947 };
29482949*variant_index == VariantIdx::from_u32(actual_variant_idx.to_u32())
2950 }
2951 Constructor::IntRange(int_range) => {
2952let size = pat.ty().primitive_size(self.tcx);
2953let actual_int = valtree.to_leaf().to_bits(size);
2954let actual_int = if pat.ty().is_signed() {
2955 MaybeInfiniteInt::new_finite_int(actual_int, size.bits())
2956 } else {
2957 MaybeInfiniteInt::new_finite_uint(actual_int)
2958 };
2959 IntRange::from_singleton(actual_int).is_subrange(int_range)
2960 }
2961 Constructor::Bool(pattern_value) => match valtree.to_leaf().try_to_bool() {
2962Ok(actual_value) => *pattern_value == actual_value,
2963Err(()) => ::rustc_middle::util::bug::bug_fmt(format_args!("bool value with invalid bits"))bug!("bool value with invalid bits"),
2964 },
2965 Constructor::F16Range(l, h, end) => {
2966let actual = valtree.to_leaf().to_f16();
2967match end {
2968 RangeEnd::Included => (*l..=*h).contains(&actual),
2969 RangeEnd::Excluded => (*l..*h).contains(&actual),
2970 }
2971 }
2972 Constructor::F32Range(l, h, end) => {
2973let actual = valtree.to_leaf().to_f32();
2974match end {
2975 RangeEnd::Included => (*l..=*h).contains(&actual),
2976 RangeEnd::Excluded => (*l..*h).contains(&actual),
2977 }
2978 }
2979 Constructor::F64Range(l, h, end) => {
2980let actual = valtree.to_leaf().to_f64();
2981match end {
2982 RangeEnd::Included => (*l..=*h).contains(&actual),
2983 RangeEnd::Excluded => (*l..*h).contains(&actual),
2984 }
2985 }
2986 Constructor::F128Range(l, h, end) => {
2987let actual = valtree.to_leaf().to_f128();
2988match end {
2989 RangeEnd::Included => (*l..=*h).contains(&actual),
2990 RangeEnd::Excluded => (*l..*h).contains(&actual),
2991 }
2992 }
2993 Constructor::Wildcard => true,
29942995// Opaque patterns must not be matched on structurally.
2996Constructor::Opaque(_) => false,
29972998// These we may eventually support:
2999Constructor::Struct
3000 | Constructor::Ref
3001 | Constructor::DerefPattern(_)
3002 | Constructor::Slice(_)
3003 | Constructor::UnionField
3004 | Constructor::Or
3005 | Constructor::Str(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("unsupported pattern constructor {0:?}",
pat.ctor()))bug!("unsupported pattern constructor {:?}", pat.ctor()),
30063007// These should never occur here:
3008Constructor::Never
3009 | Constructor::NonExhaustive
3010 | Constructor::Hidden
3011 | Constructor::Missing
3012 | Constructor::PrivateUninhabited => {
3013::rustc_middle::util::bug::bug_fmt(format_args!("unsupported pattern constructor {0:?}",
pat.ctor()))bug!("unsupported pattern constructor {:?}", pat.ctor())3014 }
3015 }
3016 }
3017}