]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/build/matches/mod.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / compiler / rustc_mir_build / src / build / matches / mod.rs
CommitLineData
9fa01778
XL
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.
7
6a06907d 8use crate::build::expr::as_place::PlaceBuilder;
dc9dc135 9use crate::build::scope::DropKind;
9fa01778
XL
10use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard};
11use crate::build::{BlockAnd, BlockAndExtension, Builder};
12use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode};
3dfed10e 13use rustc_data_structures::{
04454e1e 14 fx::{FxHashSet, FxIndexMap, FxIndexSet},
3dfed10e
XL
15 stack::ensure_sufficient_stack,
16};
dfeec247 17use rustc_index::bit_set::BitSet;
ba9703b0
XL
18use rustc_middle::middle::region;
19use rustc_middle::mir::*;
17df50a5 20use rustc_middle::thir::{self, *};
ba9703b0 21use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
f9f354fc 22use rustc_span::symbol::Symbol;
94222f64 23use rustc_span::{BytePos, Pos, Span};
ba9703b0 24use rustc_target::abi::VariantIdx;
dfeec247 25use smallvec::{smallvec, SmallVec};
e9174d1e
SL
26
27// helper functions, broken out by category:
28mod simplify;
29mod test;
30mod util;
31
74b04a01 32use std::borrow::Borrow;
0bf4aa26 33use std::convert::TryFrom;
74b04a01 34use std::mem;
0bf4aa26 35
dc9dc135 36impl<'a, 'tcx> Builder<'a, 'tcx> {
94222f64
XL
37 pub(crate) fn then_else_break(
38 &mut self,
39 mut block: BasicBlock,
40 expr: &Expr<'tcx>,
41 temp_scope_override: Option<region::Scope>,
42 break_scope: region::Scope,
923072b8 43 variable_source_info: SourceInfo,
94222f64
XL
44 ) -> BlockAnd<()> {
45 let this = self;
46 let expr_span = expr.span;
47
48 match expr.kind {
5099ac24
FG
49 ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
50 let lhs_then_block = unpack!(this.then_else_break(
51 block,
52 &this.thir[lhs],
53 temp_scope_override,
54 break_scope,
923072b8 55 variable_source_info,
5099ac24
FG
56 ));
57
58 let rhs_then_block = unpack!(this.then_else_break(
59 lhs_then_block,
60 &this.thir[rhs],
61 temp_scope_override,
62 break_scope,
923072b8 63 variable_source_info,
5099ac24
FG
64 ));
65
66 rhs_then_block.unit()
67 }
94222f64
XL
68 ExprKind::Scope { region_scope, lint_level, value } => {
69 let region_scope = (region_scope, this.source_info(expr_span));
70 this.in_scope(region_scope, lint_level, |this| {
71 this.then_else_break(
72 block,
73 &this.thir[value],
74 temp_scope_override,
75 break_scope,
923072b8 76 variable_source_info,
94222f64
XL
77 )
78 })
79 }
923072b8
FG
80 ExprKind::Let { expr, ref pat } => this.lower_let_expr(
81 block,
82 &this.thir[expr],
83 pat,
84 break_scope,
85 Some(variable_source_info.scope),
86 variable_source_info.span,
87 ),
94222f64
XL
88 _ => {
89 let temp_scope = temp_scope_override.unwrap_or_else(|| this.local_scope());
90 let mutability = Mutability::Mut;
91 let place =
92 unpack!(block = this.as_temp(block, Some(temp_scope), expr, mutability));
93 let operand = Operand::Move(Place::from(place));
94
95 let then_block = this.cfg.start_new_block();
96 let else_block = this.cfg.start_new_block();
97 let term = TerminatorKind::if_(this.tcx, operand, then_block, else_block);
98
99 let source_info = this.source_info(expr_span);
100 this.cfg.terminate(block, source_info, term);
101 this.break_for_else(else_block, break_scope, source_info);
102
103 then_block.unit()
104 }
105 }
106 }
107
9fa01778
XL
108 /// Generates MIR for a `match` expression.
109 ///
110 /// The MIR that we generate for a match looks like this.
111 ///
112 /// ```text
113 /// [ 0. Pre-match ]
114 /// |
115 /// [ 1. Evaluate Scrutinee (expression being matched on) ]
116 /// [ (fake read of scrutinee) ]
117 /// |
118 /// [ 2. Decision tree -- check discriminants ] <--------+
119 /// | |
120 /// | (once a specific arm is chosen) |
121 /// | |
122 /// [pre_binding_block] [otherwise_block]
123 /// | |
124 /// [ 3. Create "guard bindings" for arm ] |
125 /// [ (create fake borrows) ] |
126 /// | |
127 /// [ 4. Execute guard code ] |
128 /// [ (read fake borrows) ] --(guard is false)-----------+
129 /// |
130 /// | (guard results in true)
131 /// |
132 /// [ 5. Create real bindings and execute arm ]
133 /// |
134 /// [ Exit match ]
135 /// ```
136 ///
137 /// All of the different arms have been stacked on top of each other to
138 /// simplify the diagram. For an arm with no guard the blocks marked 3 and
139 /// 4 and the fake borrows are omitted.
140 ///
141 /// We generate MIR in the following steps:
142 ///
dfeec247 143 /// 1. Evaluate the scrutinee and add the fake read of it ([Builder::lower_scrutinee]).
74b04a01
XL
144 /// 2. Create the decision tree ([Builder::lower_match_tree]).
145 /// 3. Determine the fake borrows that are needed from the places that were
dfeec247
XL
146 /// matched against and create the required temporaries for them
147 /// ([Builder::calculate_fake_borrows]).
74b04a01 148 /// 4. Create everything else: the guards and the arms ([Builder::lower_match_arms]).
9fa01778
XL
149 ///
150 /// ## False edges
151 ///
152 /// We don't want to have the exact structure of the decision tree be
153 /// visible through borrow checking. False edges ensure that the CFG as
154 /// seen by borrow checking doesn't encode this. False edges are added:
155 ///
5869c6ff
XL
156 /// * From each pre-binding block to the next pre-binding block.
157 /// * From each otherwise block to the next pre-binding block.
923072b8
FG
158 #[tracing::instrument(level = "debug", skip(self, arms))]
159 pub(crate) fn match_expr(
b7449926 160 &mut self,
ba9703b0 161 destination: Place<'tcx>,
b7449926
XL
162 span: Span,
163 mut block: BasicBlock,
17df50a5
XL
164 scrutinee: &Expr<'tcx>,
165 arms: &[ArmId],
b7449926 166 ) -> BlockAnd<()> {
6a06907d 167 let scrutinee_span = scrutinee.span;
dfeec247
XL
168 let scrutinee_place =
169 unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,));
ff7c6d11 170
6a06907d 171 let mut arm_candidates = self.create_match_candidates(scrutinee_place.clone(), &arms);
9fa01778 172
17df50a5 173 let match_has_guard = arms.iter().copied().any(|arm| self.thir[arm].guard.is_some());
74b04a01
XL
174 let mut candidates =
175 arm_candidates.iter_mut().map(|(_, candidate)| candidate).collect::<Vec<_>>();
dfeec247 176
94222f64
XL
177 let match_start_span = span.shrink_to_lo().to(scrutinee.span);
178
179 let fake_borrow_temps = self.lower_match_tree(
180 block,
181 scrutinee_span,
182 match_start_span,
183 match_has_guard,
184 &mut candidates,
185 );
dfeec247
XL
186
187 self.lower_match_arms(
74b04a01 188 destination,
dfeec247
XL
189 scrutinee_place,
190 scrutinee_span,
191 arm_candidates,
192 self.source_info(span),
193 fake_borrow_temps,
194 )
195 }
9fa01778 196
dfeec247
XL
197 /// Evaluate the scrutinee and add the fake read of it.
198 fn lower_scrutinee(
199 &mut self,
200 mut block: BasicBlock,
17df50a5 201 scrutinee: &Expr<'tcx>,
dfeec247 202 scrutinee_span: Span,
6a06907d
XL
203 ) -> BlockAnd<PlaceBuilder<'tcx>> {
204 let scrutinee_place_builder = unpack!(block = self.as_place_builder(block, scrutinee));
9fa01778 205 // Matching on a `scrutinee_place` with an uninhabited type doesn't
ff7c6d11
XL
206 // generate any memory reads by itself, and so if the place "expression"
207 // contains unsafe operations like raw pointer dereferences or union
208 // field projections, we wouldn't know to require an `unsafe` block
209 // around a `match` equivalent to `std::intrinsics::unreachable()`.
210 // See issue #47412 for this hole being discovered in the wild.
211 //
212 // HACK(eddyb) Work around the above issue by adding a dummy inspection
9fa01778 213 // of `scrutinee_place`, specifically by applying `ReadForMatch`.
94b46f34 214 //
9fa01778 215 // NOTE: ReadForMatch also checks that the scrutinee is initialized.
0bf4aa26
XL
216 // This is currently needed to not allow matching on an uninitialized,
217 // uninhabited value. If we get never patterns, those will check that
218 // the place is initialized, and so this read would only be used to
219 // check safety.
cdc7bbd5 220 let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
dfeec247 221 let source_info = self.source_info(scrutinee_span);
94b46f34 222
6a06907d
XL
223 if let Ok(scrutinee_builder) =
224 scrutinee_place_builder.clone().try_upvars_resolved(self.tcx, self.typeck_results)
225 {
226 let scrutinee_place = scrutinee_builder.into_place(self.tcx, self.typeck_results);
227 self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
228 }
229
230 block.and(scrutinee_place_builder)
dfeec247 231 }
e9174d1e 232
dfeec247
XL
233 /// Create the initial `Candidate`s for a `match` expression.
234 fn create_match_candidates<'pat>(
235 &mut self,
6a06907d 236 scrutinee: PlaceBuilder<'tcx>,
17df50a5
XL
237 arms: &'pat [ArmId],
238 ) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)>
239 where
240 'a: 'pat,
241 {
9fa01778
XL
242 // Assemble a list of candidates: there is one candidate per pattern,
243 // which means there may be more than one candidate *per arm*.
dfeec247 244 arms.iter()
17df50a5 245 .copied()
9fa01778 246 .map(|arm| {
17df50a5 247 let arm = &self.thir[arm];
9fa01778 248 let arm_has_guard = arm.guard.is_some();
6a06907d 249 let arm_candidate = Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard);
74b04a01 250 (arm, arm_candidate)
9fa01778 251 })
dfeec247
XL
252 .collect()
253 }
9fa01778 254
dfeec247
XL
255 /// Create the decision tree for the match expression, starting from `block`.
256 ///
257 /// Modifies `candidates` to store the bindings and type ascriptions for
258 /// that candidate.
259 ///
260 /// Returns the places that need fake borrows because we bind or test them.
261 fn lower_match_tree<'pat>(
262 &mut self,
263 block: BasicBlock,
264 scrutinee_span: Span,
94222f64 265 match_start_span: Span,
dfeec247 266 match_has_guard: bool,
74b04a01 267 candidates: &mut [&mut Candidate<'pat, 'tcx>],
dfeec247 268 ) -> Vec<(Place<'tcx>, Local)> {
9fa01778
XL
269 // The set of places that we are creating fake borrows of. If there are
270 // no match guards then we don't need any fake borrows, so don't track
271 // them.
04454e1e 272 let mut fake_borrows = match_has_guard.then(FxIndexSet::default);
0bf4aa26 273
74b04a01
XL
274 let mut otherwise = None;
275
dfeec247 276 // This will generate code to test scrutinee_place and
e9174d1e 277 // branch to the appropriate arm block
94222f64
XL
278 self.match_candidates(
279 match_start_span,
280 scrutinee_span,
281 block,
282 &mut otherwise,
283 candidates,
284 &mut fake_borrows,
285 );
74b04a01
XL
286
287 if let Some(otherwise_block) = otherwise {
288 // See the doc comment on `match_candidates` for why we may have an
289 // otherwise block. Match checking will ensure this is actually
290 // unreachable.
291 let source_info = self.source_info(scrutinee_span);
292 self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable);
293 }
294
295 // Link each leaf candidate to the `pre_binding_block` of the next one.
296 let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None;
297
298 for candidate in candidates {
299 candidate.visit_leaves(|leaf_candidate| {
300 if let Some(ref mut prev) = previous_candidate {
301 prev.next_candidate_pre_binding_block = leaf_candidate.pre_binding_block;
302 }
303 previous_candidate = Some(leaf_candidate);
304 });
305 }
92a42be0 306
dfeec247 307 if let Some(ref borrows) = fake_borrows {
9fa01778
XL
308 self.calculate_fake_borrows(borrows, scrutinee_span)
309 } else {
310 Vec::new()
dfeec247
XL
311 }
312 }
9fa01778 313
dfeec247
XL
314 /// Lower the bindings, guards and arm bodies of a `match` expression.
315 ///
316 /// The decision tree should have already been created
317 /// (by [Builder::lower_match_tree]).
318 ///
319 /// `outer_source_info` is the SourceInfo for the whole match.
320 fn lower_match_arms(
321 &mut self,
ba9703b0 322 destination: Place<'tcx>,
6a06907d 323 scrutinee_place_builder: PlaceBuilder<'tcx>,
dfeec247 324 scrutinee_span: Span,
17df50a5 325 arm_candidates: Vec<(&'_ Arm<'tcx>, Candidate<'_, 'tcx>)>,
dfeec247
XL
326 outer_source_info: SourceInfo,
327 fake_borrow_temps: Vec<(Place<'tcx>, Local)>,
328 ) -> BlockAnd<()> {
dfeec247
XL
329 let arm_end_blocks: Vec<_> = arm_candidates
330 .into_iter()
74b04a01 331 .map(|(arm, candidate)| {
29967ef6 332 debug!("lowering arm {:?}\ncandidate = {:?}", arm, candidate);
dfeec247
XL
333
334 let arm_source_info = self.source_info(arm.span);
335 let arm_scope = (arm.scope, arm_source_info);
94222f64 336 let match_scope = self.local_scope();
dfeec247 337 self.in_scope(arm_scope, arm.lint_level, |this| {
6a06907d
XL
338 // `try_upvars_resolved` may fail if it is unable to resolve the given
339 // `PlaceBuilder` inside a closure. In this case, we don't want to include
340 // a scrutinee place. `scrutinee_place_builder` will fail to be resolved
341 // if the only match arm is a wildcard (`_`).
342 // Example:
343 // ```
344 // let foo = (0, 1);
345 // let c = || {
346 // match foo { _ => () };
347 // };
348 // ```
349 let mut opt_scrutinee_place: Option<(Option<&Place<'tcx>>, Span)> = None;
350 let scrutinee_place: Place<'tcx>;
351 if let Ok(scrutinee_builder) = scrutinee_place_builder
352 .clone()
353 .try_upvars_resolved(this.tcx, this.typeck_results)
354 {
355 scrutinee_place =
356 scrutinee_builder.into_place(this.tcx, this.typeck_results);
357 opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span));
358 }
dfeec247
XL
359 let scope = this.declare_bindings(
360 None,
361 arm.span,
74b04a01 362 &arm.pattern,
dfeec247 363 ArmHasGuard(arm.guard.is_some()),
6a06907d 364 opt_scrutinee_place,
dfeec247 365 );
9fa01778 366
dfeec247
XL
367 let arm_block = this.bind_pattern(
368 outer_source_info,
74b04a01 369 candidate,
29967ef6 370 arm.guard.as_ref(),
dc9dc135
XL
371 &fake_borrow_temps,
372 scrutinee_span,
fc512014 373 Some(arm.span),
74b04a01 374 Some(arm.scope),
94222f64 375 Some(match_scope),
dc9dc135 376 );
9fa01778 377
dfeec247
XL
378 if let Some(source_scope) = scope {
379 this.source_scope = source_scope;
380 }
9fa01778 381
17df50a5 382 this.expr_into_dest(destination, arm_block, &&this.thir[arm.body])
dfeec247 383 })
dc9dc135 384 })
dfeec247 385 .collect();
9fa01778
XL
386
387 // all the arm blocks will rejoin here
388 let end_block = self.cfg.start_new_block();
389
94222f64
XL
390 let end_brace = self.source_info(
391 outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)),
392 );
9fa01778 393 for arm_block in arm_end_blocks {
94222f64
XL
394 let block = &self.cfg.basic_blocks[arm_block.0];
395 let last_location = block.statements.last().map(|s| s.source_info);
396
397 self.cfg.goto(unpack!(arm_block), last_location.unwrap_or(end_brace), end_block);
e9174d1e 398 }
9fa01778 399
94b46f34 400 self.source_scope = outer_source_info.scope;
e9174d1e
SL
401
402 end_block.unit()
403 }
404
74b04a01
XL
405 /// Binds the variables and ascribes types for a given `match` arm or
406 /// `let` binding.
dfeec247
XL
407 ///
408 /// Also check if the guard matches, if it's provided.
74b04a01
XL
409 /// `arm_scope` should be `Some` if and only if this is called for a
410 /// `match` arm.
dfeec247
XL
411 fn bind_pattern(
412 &mut self,
413 outer_source_info: SourceInfo,
74b04a01 414 candidate: Candidate<'_, 'tcx>,
17df50a5 415 guard: Option<&Guard<'tcx>>,
923072b8 416 fake_borrow_temps: &[(Place<'tcx>, Local)],
dfeec247 417 scrutinee_span: Span,
fc512014 418 arm_span: Option<Span>,
74b04a01 419 arm_scope: Option<region::Scope>,
94222f64 420 match_scope: Option<region::Scope>,
dfeec247 421 ) -> BasicBlock {
74b04a01 422 if candidate.subcandidates.is_empty() {
dfeec247
XL
423 // Avoid generating another `BasicBlock` when we only have one
424 // candidate.
425 self.bind_and_guard_matched_candidate(
74b04a01
XL
426 candidate,
427 &[],
dfeec247
XL
428 guard,
429 fake_borrow_temps,
430 scrutinee_span,
fc512014 431 arm_span,
94222f64 432 match_scope,
74b04a01 433 true,
dfeec247
XL
434 )
435 } else {
74b04a01
XL
436 // It's helpful to avoid scheduling drops multiple times to save
437 // drop elaboration from having to clean up the extra drops.
438 //
439 // If we are in a `let` then we only schedule drops for the first
440 // candidate.
441 //
442 // If we're in a `match` arm then we could have a case like so:
443 //
444 // Ok(x) | Err(x) if return => { /* ... */ }
445 //
446 // In this case we don't want a drop of `x` scheduled when we
447 // return: it isn't bound by move until right before enter the arm.
448 // To handle this we instead unschedule it's drop after each time
449 // we lower the guard.
450 let target_block = self.cfg.start_new_block();
451 let mut schedule_drops = true;
5e7ed085 452 // We keep a stack of all of the bindings and type ascriptions
1b1a35ee 453 // from the parent candidates that we visit, that also need to
74b04a01
XL
454 // be bound for each candidate.
455 traverse_candidate(
456 candidate,
457 &mut Vec::new(),
458 &mut |leaf_candidate, parent_bindings| {
459 if let Some(arm_scope) = arm_scope {
460 self.clear_top_scope(arm_scope);
461 }
462 let binding_end = self.bind_and_guard_matched_candidate(
463 leaf_candidate,
464 parent_bindings,
465 guard,
466 &fake_borrow_temps,
467 scrutinee_span,
fc512014 468 arm_span,
94222f64 469 match_scope,
74b04a01
XL
470 schedule_drops,
471 );
472 if arm_scope.is_none() {
473 schedule_drops = false;
474 }
475 self.cfg.goto(binding_end, outer_source_info, target_block);
476 },
477 |inner_candidate, parent_bindings| {
478 parent_bindings.push((inner_candidate.bindings, inner_candidate.ascriptions));
479 inner_candidate.subcandidates.into_iter()
480 },
481 |parent_bindings| {
482 parent_bindings.pop();
483 },
484 );
485
486 target_block
dfeec247
XL
487 }
488 }
489
0bf4aa26 490 pub(super) fn expr_into_pattern(
b7449926
XL
491 &mut self,
492 mut block: BasicBlock,
e74abb32 493 irrefutable_pat: Pat<'tcx>,
17df50a5 494 initializer: &Expr<'tcx>,
b7449926 495 ) -> BlockAnd<()> {
92a42be0 496 match *irrefutable_pat.kind {
b7449926 497 // Optimize the case of `let x = ...` to write directly into `x`
dfeec247 498 PatKind::Binding { mode: BindingMode::ByValue, var, subpattern: None, .. } => {
b7449926 499 let place =
74b04a01 500 self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
6a06907d 501 unpack!(block = self.expr_into_dest(place, block, initializer));
0bf4aa26 502
0bf4aa26
XL
503 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
504 let source_info = self.source_info(irrefutable_pat.span);
cdc7bbd5 505 self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLet(None), place);
0bf4aa26 506
b7449926
XL
507 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
508 block.unit()
509 }
0531ce1d 510
b7449926
XL
511 // Optimize the case of `let x: T = ...` to write directly
512 // into `x` and then require that `T == typeof(x)`.
513 //
514 // Weirdly, this is needed to prevent the
515 // `intrinsic-move-val.rs` test case from crashing. That
516 // test works with uninitialized values in a rather
517 // dubious way, so it may be that the test is kind of
518 // broken.
e74abb32 519 PatKind::AscribeUserType {
dfeec247
XL
520 subpattern:
521 Pat {
522 kind:
523 box PatKind::Binding {
524 mode: BindingMode::ByValue,
525 var,
526 subpattern: None,
527 ..
528 },
b7449926
XL
529 ..
530 },
923072b8 531 ascription: thir::Ascription { annotation, variance: _ },
b7449926
XL
532 } => {
533 let place =
74b04a01 534 self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
6a06907d 535 unpack!(block = self.expr_into_dest(place, block, initializer));
b7449926 536
0bf4aa26
XL
537 // Inject a fake read, see comments on `FakeReadCause::ForLet`.
538 let pattern_source_info = self.source_info(irrefutable_pat.span);
cdc7bbd5 539 let cause_let = FakeReadCause::ForLet(None);
74b04a01 540 self.cfg.push_fake_read(block, pattern_source_info, cause_let, place);
0bf4aa26 541
923072b8
FG
542 let ty_source_info = self.source_info(annotation.span);
543
544 let base = self.canonical_user_type_annotations.push(annotation);
0bf4aa26
XL
545 self.cfg.push(
546 block,
547 Statement {
548 source_info: ty_source_info,
b7449926 549 kind: StatementKind::AscribeUserType(
923072b8 550 Box::new((place, UserTypeProjection { base, projs: Vec::new() })),
0731742a
XL
551 // We always use invariant as the variance here. This is because the
552 // variance field from the ascription refers to the variance to use
553 // when applying the type to the value being matched, but this
554 // ascription applies rather to the type of the binding. e.g., in this
555 // example:
556 //
557 // ```
558 // let x: T = <expr>
559 // ```
560 //
561 // We are creating an ascription that defines the type of `x` to be
562 // exactly `T` (i.e., with invariance). The variance field, in
563 // contrast, is intended to be used to relate `T` to the type of
564 // `<expr>`.
b7449926 565 ty::Variance::Invariant,
b7449926
XL
566 ),
567 },
568 );
569
83c7162d 570 self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
8bb4bdeb
XL
571 block.unit()
572 }
9fa01778 573
8bb4bdeb 574 _ => {
6a06907d
XL
575 let place_builder = unpack!(block = self.as_place_builder(block, initializer));
576 self.place_into_pattern(block, irrefutable_pat, place_builder, true)
e9174d1e 577 }
e9174d1e 578 }
e9174d1e
SL
579 }
580
923072b8 581 pub(crate) fn place_into_pattern(
b7449926 582 &mut self,
0731742a 583 block: BasicBlock,
e74abb32 584 irrefutable_pat: Pat<'tcx>,
6a06907d 585 initializer: PlaceBuilder<'tcx>,
b7449926
XL
586 set_match_place: bool,
587 ) -> BlockAnd<()> {
6a06907d 588 let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false);
94222f64
XL
589 let fake_borrow_temps = self.lower_match_tree(
590 block,
591 irrefutable_pat.span,
592 irrefutable_pat.span,
593 false,
594 &mut [&mut candidate],
595 );
74b04a01 596 // For matches and function arguments, the place that is being matched
8faf50e0
XL
597 // can be set when creating the variables. But the place for
598 // let PATTERN = ... might not even exist until we do the assignment.
74b04a01 599 // so we set it here instead.
8faf50e0 600 if set_match_place {
74b04a01
XL
601 let mut candidate_ref = &candidate;
602 while let Some(next) = {
603 for binding in &candidate_ref.bindings {
604 let local = self.var_local_id(binding.var_id, OutsideGuard);
605
5099ac24 606 let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
74b04a01 607 VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
5099ac24 608 )))) = self.local_decls[local].local_info else {
74b04a01 609 bug!("Let binding to non-user variable.")
5099ac24
FG
610 };
611 // `try_upvars_resolved` may fail if it is unable to resolve the given
612 // `PlaceBuilder` inside a closure. In this case, we don't want to include
613 // a scrutinee place. `scrutinee_place_builder` will fail for destructured
614 // assignments. This is because a closure only captures the precise places
615 // that it will read and as a result a closure may not capture the entire
616 // tuple/struct and rather have individual places that will be read in the
617 // final MIR.
618 // Example:
619 // ```
620 // let foo = (0, 1);
621 // let c = || {
622 // let (v1, v2) = foo;
623 // };
624 // ```
625 if let Ok(match_pair_resolved) =
626 initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results)
627 {
628 let place = match_pair_resolved.into_place(self.tcx, self.typeck_results);
629 *match_place = Some(place);
74b04a01 630 }
8faf50e0 631 }
74b04a01
XL
632 // All of the subcandidates should bind the same locals, so we
633 // only visit the first one.
634 candidate_ref.subcandidates.get(0)
635 } {
636 candidate_ref = next;
8faf50e0
XL
637 }
638 }
639
74b04a01
XL
640 self.bind_pattern(
641 self.source_info(irrefutable_pat.span),
642 candidate,
643 None,
644 &fake_borrow_temps,
645 irrefutable_pat.span,
646 None,
fc512014 647 None,
94222f64 648 None,
74b04a01
XL
649 )
650 .unit()
e9174d1e
SL
651 }
652
b7449926
XL
653 /// Declares the bindings of the given patterns and returns the visibility
654 /// scope for the bindings in these patterns, if such a scope had to be
655 /// created. NOTE: Declaring the bindings should always be done in their
656 /// drop scope.
923072b8 657 pub(crate) fn declare_bindings(
b7449926
XL
658 &mut self,
659 mut visibility_scope: Option<SourceScope>,
660 scope_span: Span,
e74abb32 661 pattern: &Pat<'tcx>,
b7449926
XL
662 has_guard: ArmHasGuard,
663 opt_match_place: Option<(Option<&Place<'tcx>>, Span)>,
664 ) -> Option<SourceScope> {
9fa01778 665 debug!("declare_bindings: pattern={:?}", pattern);
f9f354fc 666 self.visit_primary_bindings(
9fa01778 667 &pattern,
0731742a 668 UserTypeProjections::none(),
b7449926
XL
669 &mut |this, mutability, name, mode, var, span, ty, user_ty| {
670 if visibility_scope.is_none() {
dc9dc135
XL
671 visibility_scope =
672 Some(this.new_source_scope(scope_span, LintLevel::Inherited, None));
ea8adc8c 673 }
dc9dc135 674 let source_info = SourceInfo { span, scope: this.source_scope };
b7449926
XL
675 let visibility_scope = visibility_scope.unwrap();
676 this.declare_binding(
677 source_info,
678 visibility_scope,
679 mutability,
680 name,
681 mode,
b7449926
XL
682 var,
683 ty,
684 user_ty,
685 has_guard,
686 opt_match_place.map(|(x, y)| (x.cloned(), y)),
9fa01778 687 pattern.span,
b7449926
XL
688 );
689 },
690 );
94b46f34 691 visibility_scope
e9174d1e 692 }
5bcae85e 693
923072b8 694 pub(crate) fn storage_live_binding(
b7449926
XL
695 &mut self,
696 block: BasicBlock,
923072b8 697 var: LocalVarId,
b7449926
XL
698 span: Span,
699 for_guard: ForGuard,
74b04a01 700 schedule_drop: bool,
b7449926 701 ) -> Place<'tcx> {
83c7162d 702 let local_id = self.var_local_id(var, for_guard);
8bb4bdeb 703 let source_info = self.source_info(span);
dfeec247 704 self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) });
04454e1e
FG
705 // Altough there is almost always scope for given variable in corner cases
706 // like #92893 we might get variable with no scope.
923072b8 707 if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop{
74b04a01
XL
708 self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
709 }
416331ca 710 Place::from(local_id)
8bb4bdeb 711 }
5bcae85e 712
923072b8
FG
713 pub(crate) fn schedule_drop_for_binding(
714 &mut self,
715 var: LocalVarId,
716 span: Span,
717 for_guard: ForGuard,
718 ) {
83c7162d 719 let local_id = self.var_local_id(var, for_guard);
923072b8 720 if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) {
04454e1e
FG
721 self.schedule_drop(span, region_scope, local_id, DropKind::Value);
722 }
8bb4bdeb
XL
723 }
724
f9f354fc
XL
725 /// Visit all of the primary bindings in a patterns, that is, visit the
726 /// leftmost occurrence of each variable bound in a pattern. A variable
727 /// will occur more than once in an or-pattern.
728 pub(super) fn visit_primary_bindings(
b7449926 729 &mut self,
e74abb32 730 pattern: &Pat<'tcx>,
532ac7d7 731 pattern_user_ty: UserTypeProjections,
b7449926
XL
732 f: &mut impl FnMut(
733 &mut Self,
734 Mutability,
f9f354fc 735 Symbol,
b7449926 736 BindingMode,
923072b8 737 LocalVarId,
b7449926
XL
738 Span,
739 Ty<'tcx>,
532ac7d7 740 UserTypeProjections,
b7449926
XL
741 ),
742 ) {
f9f354fc
XL
743 debug!(
744 "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}",
745 pattern, pattern_user_ty
746 );
8bb4bdeb 747 match *pattern.kind {
f9f354fc
XL
748 PatKind::Binding {
749 mutability,
750 name,
751 mode,
752 var,
753 ty,
754 ref subpattern,
755 is_primary,
756 ..
757 } => {
758 if is_primary {
759 f(self, mutability, name, mode, var, pattern.span, ty, pattern_user_ty.clone());
760 }
5bcae85e 761 if let Some(subpattern) = subpattern.as_ref() {
f9f354fc 762 self.visit_primary_bindings(subpattern, pattern_user_ty, f);
5bcae85e
SL
763 }
764 }
9fa01778 765
dfeec247
XL
766 PatKind::Array { ref prefix, ref slice, ref suffix }
767 | PatKind::Slice { ref prefix, ref slice, ref suffix } => {
1b1a35ee
XL
768 let from = u64::try_from(prefix.len()).unwrap();
769 let to = u64::try_from(suffix.len()).unwrap();
0bf4aa26 770 for subpattern in prefix {
f9f354fc 771 self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
0bf4aa26
XL
772 }
773 for subpattern in slice {
f9f354fc
XL
774 self.visit_primary_bindings(
775 subpattern,
776 pattern_user_ty.clone().subslice(from, to),
777 f,
778 );
0bf4aa26
XL
779 }
780 for subpattern in suffix {
f9f354fc 781 self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f);
5bcae85e
SL
782 }
783 }
9fa01778 784
e74abb32 785 PatKind::Constant { .. } | PatKind::Range { .. } | PatKind::Wild => {}
9fa01778 786
e74abb32 787 PatKind::Deref { ref subpattern } => {
f9f354fc 788 self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f);
5bcae85e 789 }
9fa01778 790
e74abb32 791 PatKind::AscribeUserType {
0731742a 792 ref subpattern,
923072b8 793 ascription: thir::Ascription { ref annotation, variance: _ },
0731742a 794 } => {
b7449926
XL
795 // This corresponds to something like
796 //
797 // ```
0bf4aa26 798 // let A::<'a>(_): A<'static> = ...;
b7449926 799 // ```
0731742a
XL
800 //
801 // Note that the variance doesn't apply here, as we are tracking the effect
802 // of `user_ty` on any bindings contained with subpattern.
923072b8 803
0731742a 804 let projection = UserTypeProjection {
923072b8 805 base: self.canonical_user_type_annotations.push(annotation.clone()),
9fa01778 806 projs: Vec::new(),
0731742a 807 };
923072b8
FG
808 let subpattern_user_ty =
809 pattern_user_ty.push_projection(&projection, annotation.span);
f9f354fc 810 self.visit_primary_bindings(subpattern, subpattern_user_ty, f)
b7449926 811 }
0bf4aa26 812
e74abb32 813 PatKind::Leaf { ref subpatterns } => {
5bcae85e 814 for subpattern in subpatterns {
0731742a 815 let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field);
f9f354fc
XL
816 debug!("visit_primary_bindings: subpattern_user_ty={:?}", subpattern_user_ty);
817 self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
0bf4aa26
XL
818 }
819 }
820
e74abb32 821 PatKind::Variant { adt_def, substs: _, variant_index, ref subpatterns } => {
0bf4aa26 822 for subpattern in subpatterns {
dfeec247
XL
823 let subpattern_user_ty =
824 pattern_user_ty.clone().variant(adt_def, variant_index, subpattern.field);
f9f354fc 825 self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f);
5bcae85e
SL
826 }
827 }
e74abb32 828 PatKind::Or { ref pats } => {
f9f354fc
XL
829 // In cases where we recover from errors the primary bindings
830 // may not all be in the leftmost subpattern. For example in
831 // `let (x | y) = ...`, the primary binding of `y` occurs in
832 // the right subpattern
833 for subpattern in pats {
834 self.visit_primary_bindings(subpattern, pattern_user_ty.clone(), f);
835 }
e1599b0c 836 }
5bcae85e
SL
837 }
838 }
e9174d1e
SL
839}
840
9fa01778 841#[derive(Debug)]
74b04a01 842struct Candidate<'pat, 'tcx> {
5869c6ff 843 /// [`Span`] of the original pattern that gave rise to this candidate.
54a0048b
SL
844 span: Span,
845
5869c6ff 846 /// Whether this `Candidate` has a guard.
74b04a01
XL
847 has_guard: bool,
848
849 /// All of these must be satisfied...
60c5eb7d 850 match_pairs: SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
e9174d1e 851
74b04a01 852 /// ...these bindings established...
b039eaaf 853 bindings: Vec<Binding<'tcx>>,
e9174d1e 854
74b04a01 855 /// ...and these types asserted...
b7449926
XL
856 ascriptions: Vec<Ascription<'tcx>>,
857
5869c6ff 858 /// ...and if this is non-empty, one of these subcandidates also has to match...
74b04a01
XL
859 subcandidates: Vec<Candidate<'pat, 'tcx>>,
860
5869c6ff 861 /// ...and the guard must be evaluated; if it's `false` then branch to `otherwise_block`.
9fa01778 862 otherwise_block: Option<BasicBlock>,
abe05a73 863
5869c6ff 864 /// The block before the `bindings` have been established.
74b04a01 865 pre_binding_block: Option<BasicBlock>,
5869c6ff 866 /// The pre-binding block of the next candidate.
dc9dc135 867 next_candidate_pre_binding_block: Option<BasicBlock>,
e9174d1e
SL
868}
869
74b04a01 870impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
6a06907d 871 fn new(place: PlaceBuilder<'tcx>, pattern: &'pat Pat<'tcx>, has_guard: bool) -> Self {
74b04a01
XL
872 Candidate {
873 span: pattern.span,
874 has_guard,
875 match_pairs: smallvec![MatchPair { place, pattern }],
876 bindings: Vec::new(),
877 ascriptions: Vec::new(),
878 subcandidates: Vec::new(),
879 otherwise_block: None,
880 pre_binding_block: None,
881 next_candidate_pre_binding_block: None,
882 }
883 }
884
885 /// Visit the leaf candidates (those with no subcandidates) contained in
886 /// this candidate.
887 fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
888 traverse_candidate(
889 self,
890 &mut (),
891 &mut move |c, _| visit_leaf(c),
892 move |c, _| c.subcandidates.iter_mut(),
893 |_| {},
894 );
895 }
896}
897
898/// A depth-first traversal of the `Candidate` and all of its recursive
899/// subcandidates.
900fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>(
901 candidate: C,
902 context: &mut T,
903 visit_leaf: &mut impl FnMut(C, &mut T),
904 get_children: impl Copy + Fn(C, &mut T) -> I,
905 complete_children: impl Copy + Fn(&mut T),
906) where
907 C: Borrow<Candidate<'pat, 'tcx>>,
908 I: Iterator<Item = C>,
909{
910 if candidate.borrow().subcandidates.is_empty() {
911 visit_leaf(candidate, context)
912 } else {
913 for child in get_children(candidate, context) {
914 traverse_candidate(child, context, visit_leaf, get_children, complete_children);
915 }
916 complete_children(context)
917 }
918}
919
e9174d1e 920#[derive(Clone, Debug)]
b039eaaf
SL
921struct Binding<'tcx> {
922 span: Span,
ff7c6d11 923 source: Place<'tcx>,
923072b8 924 var_id: LocalVarId,
0731742a 925 binding_mode: BindingMode,
e9174d1e
SL
926}
927
b7449926
XL
928/// Indicates that the type of `source` must be a subtype of the
929/// user-given type `user_ty`; this is basically a no-op but can
930/// influence region inference.
931#[derive(Clone, Debug)]
932struct Ascription<'tcx> {
b7449926 933 source: Place<'tcx>,
923072b8 934 annotation: CanonicalUserTypeAnnotation<'tcx>,
0731742a 935 variance: ty::Variance,
b7449926
XL
936}
937
e9174d1e 938#[derive(Clone, Debug)]
923072b8 939pub(crate) struct MatchPair<'pat, 'tcx> {
ff7c6d11 940 // this place...
6a06907d 941 place: PlaceBuilder<'tcx>,
e9174d1e
SL
942
943 // ... must match this pattern.
e74abb32 944 pattern: &'pat Pat<'tcx>,
e9174d1e
SL
945}
946
5869c6ff 947/// See [`Test`] for more.
e9174d1e 948#[derive(Clone, Debug, PartialEq)]
b039eaaf 949enum TestKind<'tcx> {
5869c6ff 950 /// Test what enum variant a value is.
b039eaaf 951 Switch {
5869c6ff 952 /// The enum type being tested.
5e7ed085 953 adt_def: ty::AdtDef<'tcx>,
dc9dc135
XL
954 /// The set of variants that we should create a branch for. We also
955 /// create an additional "otherwise" case.
a1dfa0c6 956 variants: BitSet<VariantIdx>,
b039eaaf 957 },
e9174d1e 958
5869c6ff 959 /// Test what value an integer, `bool`, or `char` has.
92a42be0 960 SwitchInt {
dc9dc135 961 /// The type of the value that we're testing.
92a42be0 962 switch_ty: Ty<'tcx>,
dc9dc135
XL
963 /// The (ordered) set of values that we test for.
964 ///
965 /// For integers and `char`s we create a branch to each of the values in
966 /// `options`, as well as an "otherwise" branch for all other values, even
5869c6ff 967 /// in the (rare) case that `options` is exhaustive.
dc9dc135
XL
968 ///
969 /// For `bool` we always generate two edges, one for `true` and one for
970 /// `false`.
923072b8 971 options: FxIndexMap<ConstantKind<'tcx>, u128>,
92a42be0
SL
972 },
973
dc9dc135
XL
974 /// Test for equality with value, possibly after an unsizing coercion to
975 /// `ty`,
b039eaaf 976 Eq {
923072b8 977 value: ConstantKind<'tcx>,
dc9dc135
XL
978 // Integer types are handled by `SwitchInt`, and constants with ADT
979 // types are converted back into patterns, so this can only be `&str`,
980 // `&[T]`, `f32` or `f64`.
b039eaaf
SL
981 ty: Ty<'tcx>,
982 },
e9174d1e 983
dc9dc135 984 /// Test whether the value falls within an inclusive or exclusive range
e74abb32 985 Range(PatRange<'tcx>),
e9174d1e 986
5869c6ff 987 /// Test that the length of the slice is equal to `len`.
dfeec247 988 Len { len: u64, op: BinOp },
e9174d1e
SL
989}
990
5869c6ff
XL
991/// A test to perform to determine which [`Candidate`] matches a value.
992///
993/// [`Test`] is just the test to perform; it does not include the value
994/// to be tested.
e9174d1e 995#[derive(Debug)]
923072b8 996pub(crate) struct Test<'tcx> {
b039eaaf
SL
997 span: Span,
998 kind: TestKind<'tcx>,
e9174d1e
SL
999}
1000
5869c6ff 1001/// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether
9fa01778
XL
1002/// a match arm has a guard expression attached to it.
1003#[derive(Copy, Clone, Debug)]
923072b8 1004pub(crate) struct ArmHasGuard(pub(crate) bool);
9fa01778 1005
e9174d1e
SL
1006///////////////////////////////////////////////////////////////////////////
1007// Main matching algorithm
1008
dc9dc135 1009impl<'a, 'tcx> Builder<'a, 'tcx> {
92a42be0
SL
1010 /// The main match algorithm. It begins with a set of candidates
1011 /// `candidates` and has the job of generating code to determine
1012 /// which of these candidates, if any, is the correct one. The
9cc50fc6
SL
1013 /// candidates are sorted such that the first item in the list
1014 /// has the highest priority. When a candidate is found to match
74b04a01 1015 /// the value, we will set and generate a branch to the appropriate
5869c6ff 1016 /// pre-binding block.
92a42be0 1017 ///
dc9dc135 1018 /// If we find that *NONE* of the candidates apply, we branch to the
74b04a01
XL
1019 /// `otherwise_block`, setting it to `Some` if required. In principle, this
1020 /// means that the input list was not exhaustive, though at present we
1021 /// sometimes are not smart enough to recognize all exhaustive inputs.
92a42be0 1022 ///
5869c6ff 1023 /// It might be surprising that the input can be non-exhaustive.
92a42be0
SL
1024 /// Indeed, initially, it is not, because all matches are
1025 /// exhaustive in Rust. But during processing we sometimes divide
1026 /// up the list of candidates and recurse with a non-exhaustive
1027 /// list. This is important to keep the size of the generated code
5869c6ff 1028 /// under control. See [`Builder::test_candidates`] for more details.
0bf4aa26 1029 ///
5869c6ff 1030 /// If `fake_borrows` is `Some`, then places which need fake borrows
0bf4aa26 1031 /// will be added to it.
74b04a01
XL
1032 ///
1033 /// For an example of a case where we set `otherwise_block`, even for an
5869c6ff 1034 /// exhaustive match, consider:
74b04a01 1035 ///
5869c6ff 1036 /// ```
04454e1e 1037 /// # fn foo(x: (bool, bool)) {
74b04a01
XL
1038 /// match x {
1039 /// (true, true) => (),
1040 /// (_, false) => (),
1041 /// (false, true) => (),
1042 /// }
04454e1e 1043 /// # }
fc512014 1044 /// ```
74b04a01
XL
1045 ///
1046 /// For this match, we check if `x.0` matches `true` (for the first
5869c6ff
XL
1047 /// arm). If it doesn't match, we check `x.1`. If `x.1` is `true` we check
1048 /// if `x.0` matches `false` (for the third arm). In the (impossible at
74b04a01
XL
1049 /// runtime) case when `x.0` is now `true`, we branch to
1050 /// `otherwise_block`.
b7449926
XL
1051 fn match_candidates<'pat>(
1052 &mut self,
1053 span: Span,
94222f64 1054 scrutinee_span: Span,
74b04a01
XL
1055 start_block: BasicBlock,
1056 otherwise_block: &mut Option<BasicBlock>,
9fa01778 1057 candidates: &mut [&mut Candidate<'pat, 'tcx>],
04454e1e 1058 fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
dc9dc135 1059 ) {
b7449926 1060 debug!(
dc9dc135 1061 "matched_candidate(span={:?}, candidates={:?}, start_block={:?}, otherwise_block={:?})",
dfeec247 1062 span, candidates, start_block, otherwise_block,
b7449926 1063 );
e9174d1e 1064
9fa01778
XL
1065 // Start by simplifying candidates. Once this process is complete, all
1066 // the match pairs which remain require some form of test, whether it
1067 // be a switch or pattern comparison.
74b04a01 1068 let mut split_or_candidate = false;
9fa01778 1069 for candidate in &mut *candidates {
74b04a01 1070 split_or_candidate |= self.simplify_candidate(candidate);
e9174d1e
SL
1071 }
1072
f9f354fc
XL
1073 ensure_sufficient_stack(|| {
1074 if split_or_candidate {
1075 // At least one of the candidates has been split into subcandidates.
1076 // We need to change the candidate list to include those.
1077 let mut new_candidates = Vec::new();
74b04a01 1078
f9f354fc
XL
1079 for candidate in candidates {
1080 candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
1081 }
1082 self.match_simplified_candidates(
1083 span,
94222f64 1084 scrutinee_span,
f9f354fc
XL
1085 start_block,
1086 otherwise_block,
1087 &mut *new_candidates,
1088 fake_borrows,
1089 );
1090 } else {
1091 self.match_simplified_candidates(
1092 span,
94222f64 1093 scrutinee_span,
f9f354fc
XL
1094 start_block,
1095 otherwise_block,
1096 candidates,
1097 fake_borrows,
1098 );
74b04a01 1099 }
f9f354fc 1100 });
74b04a01
XL
1101 }
1102
1103 fn match_simplified_candidates(
1104 &mut self,
1105 span: Span,
94222f64 1106 scrutinee_span: Span,
74b04a01
XL
1107 start_block: BasicBlock,
1108 otherwise_block: &mut Option<BasicBlock>,
1109 candidates: &mut [&mut Candidate<'_, 'tcx>],
04454e1e 1110 fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
74b04a01 1111 ) {
9fa01778
XL
1112 // The candidates are sorted by priority. Check to see whether the
1113 // higher priority candidates (and hence at the front of the slice)
1114 // have satisfied all their match pairs.
dfeec247
XL
1115 let fully_matched = candidates.iter().take_while(|c| c.match_pairs.is_empty()).count();
1116 debug!("match_candidates: {:?} candidates fully matched", fully_matched);
9fa01778 1117 let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched);
abe05a73 1118
74b04a01 1119 let block = if !matched_candidates.is_empty() {
dfeec247
XL
1120 let otherwise_block =
1121 self.select_matched_candidates(matched_candidates, start_block, fake_borrows);
dc9dc135
XL
1122
1123 if let Some(last_otherwise_block) = otherwise_block {
dfeec247 1124 last_otherwise_block
b039eaaf 1125 } else {
9fa01778 1126 // Any remaining candidates are unreachable.
abe05a73 1127 if unmatched_candidates.is_empty() {
dc9dc135 1128 return;
abe05a73 1129 }
dfeec247
XL
1130 self.cfg.start_new_block()
1131 }
dc9dc135 1132 } else {
74b04a01 1133 start_block
dfeec247 1134 };
e9174d1e 1135
9fa01778
XL
1136 // If there are no candidates that still need testing, we're
1137 // done. Since all matches are exhaustive, execution should
1138 // never reach this point.
9cc50fc6 1139 if unmatched_candidates.is_empty() {
dc9dc135 1140 let source_info = self.source_info(span);
74b04a01
XL
1141 if let Some(otherwise) = *otherwise_block {
1142 self.cfg.goto(block, source_info, otherwise);
1143 } else {
1144 *otherwise_block = Some(block);
dc9dc135
XL
1145 }
1146 return;
92a42be0
SL
1147 }
1148
dc9dc135 1149 // Test for the remaining candidates.
74b04a01
XL
1150 self.test_candidates_with_or(
1151 span,
94222f64 1152 scrutinee_span,
74b04a01
XL
1153 unmatched_candidates,
1154 block,
1155 otherwise_block,
1156 fake_borrows,
1157 );
9fa01778
XL
1158 }
1159
5869c6ff
XL
1160 /// Link up matched candidates.
1161 ///
1162 /// For example, if we have something like this:
9fa01778 1163 ///
04454e1e 1164 /// ```ignore (illustrative)
9fa01778 1165 /// ...
5869c6ff 1166 /// Some(x) if cond1 => ...
9fa01778 1167 /// Some(x) => ...
5869c6ff 1168 /// Some(x) if cond2 => ...
9fa01778 1169 /// ...
fc512014 1170 /// ```
9fa01778
XL
1171 ///
1172 /// We generate real edges from:
9fa01778 1173 ///
5869c6ff
XL
1174 /// * `start_block` to the [pre-binding block] of the first pattern,
1175 /// * the [otherwise block] of the first pattern to the second pattern,
1176 /// * the [otherwise block] of the third pattern to a block with an
1177 /// [`Unreachable` terminator](TerminatorKind::Unreachable).
1178 ///
1179 /// In addition, we add fake edges from the otherwise blocks to the
1180 /// pre-binding block of the next candidate in the original set of
9fa01778 1181 /// candidates.
5869c6ff
XL
1182 ///
1183 /// [pre-binding block]: Candidate::pre_binding_block
1184 /// [otherwise block]: Candidate::otherwise_block
9fa01778
XL
1185 fn select_matched_candidates(
1186 &mut self,
1187 matched_candidates: &mut [&mut Candidate<'_, 'tcx>],
74b04a01 1188 start_block: BasicBlock,
04454e1e 1189 fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
9fa01778
XL
1190 ) -> Option<BasicBlock> {
1191 debug_assert!(
1192 !matched_candidates.is_empty(),
1193 "select_matched_candidates called with no candidates",
1194 );
74b04a01
XL
1195 debug_assert!(
1196 matched_candidates.iter().all(|c| c.subcandidates.is_empty()),
1197 "subcandidates should be empty in select_matched_candidates",
1198 );
9fa01778
XL
1199
1200 // Insert a borrows of prefixes of places that are bound and are
1201 // behind a dereference projection.
1202 //
1203 // These borrows are taken to avoid situations like the following:
1204 //
1205 // match x[10] {
1206 // _ if { x = &[0]; false } => (),
1207 // y => (), // Out of bounds array access!
1208 // }
1209 //
1210 // match *x {
1211 // // y is bound by reference in the guard and then by copy in the
1212 // // arm, so y is 2 in the arm!
1213 // y if { y == 1 && (x = &2) == () } => y,
1214 // _ => 3,
1215 // }
1216 if let Some(fake_borrows) = fake_borrows {
dfeec247
XL
1217 for Binding { source, .. } in
1218 matched_candidates.iter().flat_map(|candidate| &candidate.bindings)
9fa01778 1219 {
e1599b0c 1220 if let Some(i) =
f9f354fc 1221 source.projection.iter().rposition(|elem| elem == ProjectionElem::Deref)
e1599b0c
XL
1222 {
1223 let proj_base = &source.projection[..i];
1224
1225 fake_borrows.insert(Place {
dfeec247 1226 local: source.local,
6a06907d 1227 projection: self.tcx.intern_place_elems(proj_base),
e1599b0c 1228 });
9fa01778
XL
1229 }
1230 }
1231 }
1232
1233 let fully_matched_with_guard = matched_candidates
1234 .iter()
74b04a01 1235 .position(|c| !c.has_guard)
9fa01778
XL
1236 .unwrap_or(matched_candidates.len() - 1);
1237
dfeec247
XL
1238 let (reachable_candidates, unreachable_candidates) =
1239 matched_candidates.split_at_mut(fully_matched_with_guard + 1);
9fa01778 1240
74b04a01 1241 let mut next_prebinding = start_block;
9fa01778 1242
74b04a01
XL
1243 for candidate in reachable_candidates.iter_mut() {
1244 assert!(candidate.otherwise_block.is_none());
1245 assert!(candidate.pre_binding_block.is_none());
1246 candidate.pre_binding_block = Some(next_prebinding);
1247 if candidate.has_guard {
1248 // Create the otherwise block for this candidate, which is the
1249 // pre-binding block for the next candidate.
1250 next_prebinding = self.cfg.start_new_block();
1251 candidate.otherwise_block = Some(next_prebinding);
1252 }
dc9dc135 1253 }
9fa01778 1254
74b04a01
XL
1255 debug!(
1256 "match_candidates: add pre_binding_blocks for unreachable {:?}",
1257 unreachable_candidates,
1258 );
1259 for candidate in unreachable_candidates {
1260 assert!(candidate.pre_binding_block.is_none());
1261 candidate.pre_binding_block = Some(self.cfg.start_new_block());
1262 }
1263
1264 reachable_candidates.last_mut().unwrap().otherwise_block
1265 }
1266
1267 /// Tests a candidate where there are only or-patterns left to test, or
1268 /// forwards to [Builder::test_candidates].
1269 ///
1270 /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
5869c6ff 1271 /// so:
74b04a01
XL
1272 ///
1273 /// ```text
1274 /// [ start ]
1275 /// |
1276 /// [ match P, Q ]
1277 /// |
1278 /// +----------------------------------------+------------------------------------+
1279 /// | | |
1280 /// V V V
1281 /// [ P matches ] [ Q matches ] [ otherwise ]
1282 /// | | |
1283 /// V V |
1284 /// [ match R, S ] [ match R, S ] |
1285 /// | | |
1286 /// +--------------+------------+ +--------------+------------+ |
1287 /// | | | | | | |
1288 /// V V V V V V |
1289 /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ] |
1290 /// | | | | | | |
1291 /// +--------------+------------|------------+--------------+ | |
1292 /// | | | |
1293 /// | +----------------------------------------+--------+
1294 /// | |
1295 /// V V
1296 /// [ Success ] [ Failure ]
1297 /// ```
1298 ///
1299 /// In practice there are some complications:
1300 ///
1301 /// * If there's a guard, then the otherwise branch of the first match on
1302 /// `R | S` goes to a test for whether `Q` matches, and the control flow
1303 /// doesn't merge into a single success block until after the guard is
1304 /// tested.
1305 /// * If neither `P` or `Q` has any bindings or type ascriptions and there
1306 /// isn't a match guard, then we create a smaller CFG like:
1307 ///
1308 /// ```text
1309 /// ...
1310 /// +---------------+------------+
1311 /// | | |
1312 /// [ P matches ] [ Q matches ] [ otherwise ]
1313 /// | | |
1314 /// +---------------+ |
1315 /// | ...
1316 /// [ match R, S ]
1317 /// |
1318 /// ...
1319 /// ```
1320 fn test_candidates_with_or(
1321 &mut self,
1322 span: Span,
94222f64 1323 scrutinee_span: Span,
74b04a01
XL
1324 candidates: &mut [&mut Candidate<'_, 'tcx>],
1325 block: BasicBlock,
1326 otherwise_block: &mut Option<BasicBlock>,
04454e1e 1327 fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
74b04a01
XL
1328 ) {
1329 let (first_candidate, remaining_candidates) = candidates.split_first_mut().unwrap();
1330
1331 // All of the or-patterns have been sorted to the end, so if the first
1332 // pattern is an or-pattern we only have or-patterns.
1333 match *first_candidate.match_pairs[0].pattern.kind {
1334 PatKind::Or { .. } => (),
1335 _ => {
94222f64
XL
1336 self.test_candidates(
1337 span,
1338 scrutinee_span,
1339 candidates,
1340 block,
1341 otherwise_block,
1342 fake_borrows,
1343 );
74b04a01 1344 return;
9fa01778
XL
1345 }
1346 }
1347
74b04a01
XL
1348 let match_pairs = mem::take(&mut first_candidate.match_pairs);
1349 first_candidate.pre_binding_block = Some(block);
1350
1351 let mut otherwise = None;
1352 for match_pair in match_pairs {
5099ac24 1353 let PatKind::Or { ref pats } = &*match_pair.pattern.kind else {
74b04a01 1354 bug!("Or-patterns should have been sorted to the end");
5099ac24
FG
1355 };
1356 let or_span = match_pair.pattern.span;
1357 let place = match_pair.place;
1358
1359 first_candidate.visit_leaves(|leaf_candidate| {
1360 self.test_or_pattern(
1361 leaf_candidate,
1362 &mut otherwise,
1363 pats,
1364 or_span,
1365 place.clone(),
1366 fake_borrows,
1367 );
1368 });
9fa01778
XL
1369 }
1370
74b04a01
XL
1371 let remainder_start = otherwise.unwrap_or_else(|| self.cfg.start_new_block());
1372
1373 self.match_candidates(
1374 span,
94222f64 1375 scrutinee_span,
74b04a01
XL
1376 remainder_start,
1377 otherwise_block,
1378 remaining_candidates,
1379 fake_borrows,
1380 )
1381 }
1382
1383 fn test_or_pattern<'pat>(
1384 &mut self,
1385 candidate: &mut Candidate<'pat, 'tcx>,
1386 otherwise: &mut Option<BasicBlock>,
1387 pats: &'pat [Pat<'tcx>],
1388 or_span: Span,
6a06907d 1389 place: PlaceBuilder<'tcx>,
04454e1e 1390 fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
74b04a01
XL
1391 ) {
1392 debug!("test_or_pattern:\ncandidate={:#?}\npats={:#?}", candidate, pats);
6a06907d
XL
1393 let mut or_candidates: Vec<_> = pats
1394 .iter()
1395 .map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard))
1396 .collect();
74b04a01
XL
1397 let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect();
1398 let otherwise = if candidate.otherwise_block.is_some() {
1399 &mut candidate.otherwise_block
9fa01778 1400 } else {
74b04a01
XL
1401 otherwise
1402 };
1403 self.match_candidates(
94222f64 1404 or_span,
74b04a01
XL
1405 or_span,
1406 candidate.pre_binding_block.unwrap(),
1407 otherwise,
1408 &mut or_candidate_refs,
1409 fake_borrows,
1410 );
1411 candidate.subcandidates = or_candidates;
1412 self.merge_trivial_subcandidates(candidate, self.source_info(or_span));
1413 }
1414
1415 /// Try to merge all of the subcandidates of the given candidate into one.
1416 /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`.
1417 fn merge_trivial_subcandidates(
1418 &mut self,
1419 candidate: &mut Candidate<'_, 'tcx>,
1420 source_info: SourceInfo,
1421 ) {
1422 if candidate.subcandidates.is_empty() || candidate.has_guard {
1423 // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
1424 return;
1425 }
1426
1427 let mut can_merge = true;
1428
1429 // Not `Iterator::all` because we don't want to short-circuit.
1430 for subcandidate in &mut candidate.subcandidates {
1431 self.merge_trivial_subcandidates(subcandidate, source_info);
1432
1433 // FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
1434 can_merge &= subcandidate.subcandidates.is_empty()
1435 && subcandidate.bindings.is_empty()
1436 && subcandidate.ascriptions.is_empty();
1437 }
1438
1439 if can_merge {
1440 let any_matches = self.cfg.start_new_block();
1441 for subcandidate in mem::take(&mut candidate.subcandidates) {
1442 let or_block = subcandidate.pre_binding_block.unwrap();
1443 self.cfg.goto(or_block, source_info, any_matches);
1444 }
1445 candidate.pre_binding_block = Some(any_matches);
9fa01778 1446 }
92a42be0
SL
1447 }
1448
9fa01778 1449 /// This is the most subtle part of the matching algorithm. At
92a42be0
SL
1450 /// this point, the input candidates have been fully simplified,
1451 /// and so we know that all remaining match-pairs require some
5869c6ff
XL
1452 /// sort of test. To decide what test to perform, we take the highest
1453 /// priority candidate (the first one in the list, as of January 2021)
1454 /// and extract the first match-pair from the list. From this we decide
1455 /// what kind of test is needed using [`Builder::test`], defined in the
1456 /// [`test` module](mod@test).
92a42be0
SL
1457 ///
1458 /// *Note:* taking the first match pair is somewhat arbitrary, and
1459 /// we might do better here by choosing more carefully what to
1460 /// test.
1461 ///
1462 /// For example, consider the following possible match-pairs:
1463 ///
5869c6ff
XL
1464 /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has
1465 /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has
1466 /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in
92a42be0
SL
1467 /// 4. etc.
1468 ///
5869c6ff
XL
1469 /// [`Switch`]: TestKind::Switch
1470 /// [`SwitchInt`]: TestKind::SwitchInt
1471 /// [`Range`]: TestKind::Range
1472 ///
92a42be0 1473 /// Once we know what sort of test we are going to perform, this
5869c6ff 1474 /// test may also help us winnow down our candidates. So we walk over
92a42be0
SL
1475 /// the candidates (from high to low priority) and check. This
1476 /// gives us, for each outcome of the test, a transformed list of
5869c6ff
XL
1477 /// candidates. For example, if we are testing `x.0`'s variant,
1478 /// and we have a candidate `(x.0 @ Some(v), x.1 @ 22)`,
1479 /// then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)`.
1480 /// Note that the first match-pair is now simpler (and, in fact, irrefutable).
92a42be0
SL
1481 ///
1482 /// But there may also be candidates that the test just doesn't
3157f602 1483 /// apply to. The classical example involves wildcards:
92a42be0 1484 ///
041b39d2
XL
1485 /// ```
1486 /// # let (x, y, z) = (true, true, true);
3157f602 1487 /// match (x, y, z) {
04454e1e
FG
1488 /// (true , _ , true ) => true, // (0)
1489 /// (_ , true , _ ) => true, // (1)
1490 /// (false, false, _ ) => false, // (2)
1491 /// (true , _ , false) => false, // (3)
3157f602 1492 /// }
04454e1e 1493 /// # ;
3157f602
XL
1494 /// ```
1495 ///
1496 /// In that case, after we test on `x`, there are 2 overlapping candidate
1497 /// sets:
1498 ///
1499 /// - If the outcome is that `x` is true, candidates 0, 1, and 3
1500 /// - If the outcome is that `x` is false, candidates 1 and 2
1501 ///
1502 /// Here, the traditional "decision tree" method would generate 2
1503 /// separate code-paths for the 2 separate cases.
1504 ///
1505 /// In some cases, this duplication can create an exponential amount of
1506 /// code. This is most easily seen by noticing that this method terminates
1507 /// with precisely the reachable arms being reachable - but that problem
1508 /// is trivially NP-complete:
1509 ///
04454e1e
FG
1510 /// ```ignore (illustrative)
1511 /// match (var0, var1, var2, var3, ...) {
1512 /// (true , _ , _ , false, true, ...) => false,
1513 /// (_ , true, true , false, _ , ...) => false,
1514 /// (false, _ , false, false, _ , ...) => false,
1515 /// ...
1516 /// _ => true
1517 /// }
3157f602
XL
1518 /// ```
1519 ///
1520 /// Here the last arm is reachable only if there is an assignment to
1521 /// the variables that does not match any of the literals. Therefore,
1522 /// compilation would take an exponential amount of time in some cases.
1523 ///
1524 /// That kind of exponential worst-case might not occur in practice, but
1525 /// our simplistic treatment of constants and guards would make it occur
5869c6ff 1526 /// in very common situations - for example [#29740]:
3157f602 1527 ///
04454e1e 1528 /// ```ignore (illustrative)
92a42be0 1529 /// match x {
3157f602
XL
1530 /// "foo" if foo_guard => ...,
1531 /// "bar" if bar_guard => ...,
1532 /// "baz" if baz_guard => ...,
1533 /// ...
92a42be0
SL
1534 /// }
1535 /// ```
1536 ///
5869c6ff
XL
1537 /// [#29740]: https://github.com/rust-lang/rust/issues/29740
1538 ///
1539 /// Here we first test the match-pair `x @ "foo"`, which is an [`Eq` test].
1540 ///
1541 /// [`Eq` test]: TestKind::Eq
3157f602
XL
1542 ///
1543 /// It might seem that we would end up with 2 disjoint candidate
5869c6ff
XL
1544 /// sets, consisting of the first candidate or the other two, but our
1545 /// algorithm doesn't reason about `"foo"` being distinct from the other
3157f602 1546 /// constants; it considers the latter arms to potentially match after
5869c6ff 1547 /// both outcomes, which obviously leads to an exponential number
3157f602 1548 /// of tests.
92a42be0 1549 ///
3157f602
XL
1550 /// To avoid these kinds of problems, our algorithm tries to ensure
1551 /// the amount of generated tests is linear. When we do a k-way test,
1552 /// we return an additional "unmatched" set alongside the obvious `k`
1553 /// sets. When we encounter a candidate that would be present in more
1554 /// than one of the sets, we put it and all candidates below it into the
1555 /// "unmatched" set. This ensures these `k+1` sets are disjoint.
92a42be0 1556 ///
3157f602
XL
1557 /// After we perform our test, we branch into the appropriate candidate
1558 /// set and recurse with `match_candidates`. These sub-matches are
5869c6ff 1559 /// obviously non-exhaustive - as we discarded our otherwise set - so
3157f602 1560 /// we set their continuation to do `match_candidates` on the
5869c6ff 1561 /// "unmatched" set (which is again non-exhaustive).
92a42be0
SL
1562 ///
1563 /// If you apply this to the above test, you basically wind up
1564 /// with an if-else-if chain, testing each candidate in turn,
1565 /// which is precisely what we want.
3157f602
XL
1566 ///
1567 /// In addition to avoiding exponential-time blowups, this algorithm
5869c6ff 1568 /// also has the nice property that each guard and arm is only generated
3157f602 1569 /// once.
9fa01778 1570 fn test_candidates<'pat, 'b, 'c>(
b7449926
XL
1571 &mut self,
1572 span: Span,
94222f64 1573 scrutinee_span: Span,
9fa01778 1574 mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>],
b7449926 1575 block: BasicBlock,
74b04a01 1576 otherwise_block: &mut Option<BasicBlock>,
04454e1e 1577 fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
dc9dc135 1578 ) {
92a42be0 1579 // extract the match-pair from the highest priority candidate
9cc50fc6 1580 let match_pair = &candidates.first().unwrap().match_pairs[0];
92a42be0 1581 let mut test = self.test(match_pair);
6a06907d 1582 let match_place = match_pair.place.clone();
92a42be0
SL
1583
1584 // most of the time, the test to perform is simply a function
1585 // of the main candidate; but for a test like SwitchInt, we
1586 // may want to add cases based on the candidates that are
1587 // available
1588 match test.kind {
3dfed10e 1589 TestKind::SwitchInt { switch_ty, ref mut options } => {
9cc50fc6 1590 for candidate in candidates.iter() {
fc512014 1591 if !self.add_cases_to_switch(&match_place, candidate, switch_ty, options) {
92a42be0
SL
1592 break;
1593 }
1594 }
1595 }
dfeec247 1596 TestKind::Switch { adt_def: _, ref mut variants } => {
3157f602 1597 for candidate in candidates.iter() {
9fa01778 1598 if !self.add_variants_to_switch(&match_place, candidate, variants) {
3157f602
XL
1599 break;
1600 }
1601 }
1602 }
b7449926 1603 _ => {}
92a42be0
SL
1604 }
1605
0bf4aa26 1606 // Insert a Shallow borrow of any places that is switched on.
5e7ed085
FG
1607 if let Some(fb) = fake_borrows && let Ok(match_place_resolved) =
1608 match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
1609 {
1610 let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results);
1611 fb.insert(resolved_place);
f9f354fc 1612 }
0bf4aa26 1613
92a42be0
SL
1614 // perform the test, branching to one of N blocks. For each of
1615 // those N possible outcomes, create a (initially empty)
1616 // vector of candidates. Those are the candidates that still
1617 // apply if the test has that particular outcome.
dfeec247 1618 debug!("match_candidates: test={:?} match_pair={:?}", test, match_pair);
9fa01778 1619 let mut target_candidates: Vec<Vec<&mut Candidate<'pat, 'tcx>>> = vec![];
dc9dc135 1620 target_candidates.resize_with(test.targets(), Default::default);
9fa01778
XL
1621
1622 let total_candidate_count = candidates.len();
92a42be0
SL
1623
1624 // Sort the candidates into the appropriate vector in
1625 // `target_candidates`. Note that at some point we may
1626 // encounter a candidate where the test is not relevant; at
1627 // that point, we stop sorting.
9fa01778 1628 while let Some(candidate) = candidates.first_mut() {
3c0e092e 1629 let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else {
9fa01778 1630 break;
3c0e092e
XL
1631 };
1632 let (candidate, rest) = candidates.split_first_mut().unwrap();
1633 target_candidates[idx].push(candidate);
1634 candidates = rest;
9fa01778
XL
1635 }
1636 // at least the first candidate ought to be tested
1637 assert!(total_candidate_count > candidates.len());
1638 debug!("tested_candidates: {}", total_candidate_count - candidates.len());
1639 debug!("untested_candidates: {}", candidates.len());
92a42be0 1640
dc9dc135
XL
1641 // HACK(matthewjasper) This is a closure so that we can let the test
1642 // create its blocks before the rest of the match. This currently
1643 // improves the speed of llvm when optimizing long string literal
1644 // matches
1645 let make_target_blocks = move |this: &mut Self| -> Vec<BasicBlock> {
74b04a01
XL
1646 // The block that we should branch to if none of the
1647 // `target_candidates` match. This is either the block where we
1648 // start matching the untested candidates if there are any,
1649 // otherwise it's the `otherwise_block`.
1650 let remainder_start = &mut None;
1651 let remainder_start =
1652 if candidates.is_empty() { &mut *otherwise_block } else { remainder_start };
1653
dc9dc135
XL
1654 // For each outcome of test, process the candidates that still
1655 // apply. Collect a list of blocks where control flow will
1656 // branch if one of the `target_candidate` sets is not
1657 // exhaustive.
74b04a01 1658 let target_blocks: Vec<_> = target_candidates
dfeec247
XL
1659 .into_iter()
1660 .map(|mut candidates| {
74b04a01
XL
1661 if !candidates.is_empty() {
1662 let candidate_start = this.cfg.start_new_block();
dfeec247
XL
1663 this.match_candidates(
1664 span,
94222f64 1665 scrutinee_span,
dfeec247 1666 candidate_start,
74b04a01 1667 remainder_start,
dfeec247
XL
1668 &mut *candidates,
1669 fake_borrows,
dc9dc135 1670 );
74b04a01 1671 candidate_start
dfeec247 1672 } else {
74b04a01 1673 *remainder_start.get_or_insert_with(|| this.cfg.start_new_block())
dfeec247
XL
1674 }
1675 })
74b04a01
XL
1676 .collect();
1677
1678 if !candidates.is_empty() {
1679 let remainder_start = remainder_start.unwrap_or_else(|| this.cfg.start_new_block());
1680 this.match_candidates(
1681 span,
94222f64 1682 scrutinee_span,
74b04a01
XL
1683 remainder_start,
1684 otherwise_block,
1685 candidates,
1686 fake_borrows,
1687 );
1688 };
1689
1690 target_blocks
dc9dc135 1691 };
92a42be0 1692
94222f64 1693 self.perform_test(span, scrutinee_span, block, match_place, &test, make_target_blocks);
9fa01778
XL
1694 }
1695
dfeec247
XL
1696 /// Determine the fake borrows that are needed from a set of places that
1697 /// have to be stable across match guards.
1698 ///
1699 /// Returns a list of places that need a fake borrow and the temporary
1700 /// that's used to store the fake borrow.
1701 ///
1702 /// Match exhaustiveness checking is not able to handle the case where the
1703 /// place being matched on is mutated in the guards. We add "fake borrows"
1704 /// to the guards that prevent any mutation of the place being matched.
1705 /// There are a some subtleties:
1706 ///
1707 /// 1. Borrowing `*x` doesn't prevent assigning to `x`. If `x` is a shared
1708 /// reference, the borrow isn't even tracked. As such we have to add fake
1709 /// borrows of any prefixes of a place
1710 /// 2. We don't want `match x { _ => (), }` to conflict with mutable
1711 /// borrows of `x`, so we only add fake borrows for places which are
1712 /// bound or tested by the match.
1713 /// 3. We don't want the fake borrows to conflict with `ref mut` bindings,
1714 /// so we use a special BorrowKind for them.
1715 /// 4. The fake borrows may be of places in inactive variants, so it would
1716 /// be UB to generate code for them. They therefore have to be removed
1717 /// by a MIR pass run after borrow checking.
9fa01778
XL
1718 fn calculate_fake_borrows<'b>(
1719 &mut self,
04454e1e 1720 fake_borrows: &'b FxIndexSet<Place<'tcx>>,
9fa01778 1721 temp_span: Span,
dfeec247 1722 ) -> Vec<(Place<'tcx>, Local)> {
6a06907d 1723 let tcx = self.tcx;
9fa01778
XL
1724
1725 debug!("add_fake_borrows fake_borrows = {:?}", fake_borrows);
1726
1727 let mut all_fake_borrows = Vec::with_capacity(fake_borrows.len());
1728
1729 // Insert a Shallow borrow of the prefixes of any fake borrows.
dfeec247 1730 for place in fake_borrows {
e74abb32 1731 let mut cursor = place.projection.as_ref();
e1599b0c
XL
1732 while let [proj_base @ .., elem] = cursor {
1733 cursor = proj_base;
1734
9fa01778
XL
1735 if let ProjectionElem::Deref = elem {
1736 // Insert a shallow borrow after a deref. For other
1737 // projections the borrow of prefix_cursor will
1738 // conflict with any mutation of base.
74b04a01 1739 all_fake_borrows.push(PlaceRef { local: place.local, projection: proj_base });
9fa01778 1740 }
9fa01778
XL
1741 }
1742
416331ca 1743 all_fake_borrows.push(place.as_ref());
9fa01778
XL
1744 }
1745
04454e1e
FG
1746 // Deduplicate
1747 let mut dedup = FxHashSet::default();
1748 all_fake_borrows.retain(|b| dedup.insert(*b));
9fa01778
XL
1749
1750 debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows);
1751
dfeec247
XL
1752 all_fake_borrows
1753 .into_iter()
1754 .map(|matched_place_ref| {
1755 let matched_place = Place {
74b04a01 1756 local: matched_place_ref.local,
dfeec247
XL
1757 projection: tcx.intern_place_elems(matched_place_ref.projection),
1758 };
1759 let fake_borrow_deref_ty = matched_place.ty(&self.local_decls, tcx).ty;
1760 let fake_borrow_ty = tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
1761 let fake_borrow_temp =
f9f354fc 1762 self.local_decls.push(LocalDecl::new(fake_borrow_ty, temp_span));
9fa01778 1763
dfeec247
XL
1764 (matched_place, fake_borrow_temp)
1765 })
1766 .collect()
e9174d1e 1767 }
9fa01778 1768}
e9174d1e 1769
9fa01778 1770///////////////////////////////////////////////////////////////////////////
e74abb32 1771// Pat binding - used for `let` and function parameters as well.
9fa01778 1772
dc9dc135 1773impl<'a, 'tcx> Builder<'a, 'tcx> {
923072b8 1774 pub(crate) fn lower_let_expr(
94222f64
XL
1775 &mut self,
1776 mut block: BasicBlock,
1777 expr: &Expr<'tcx>,
1778 pat: &Pat<'tcx>,
1779 else_target: region::Scope,
923072b8 1780 source_scope: Option<SourceScope>,
94222f64
XL
1781 span: Span,
1782 ) -> BlockAnd<()> {
1783 let expr_span = expr.span;
1784 let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span));
94222f64 1785 let wildcard = Pat::wildcard_from_ty(pat.ty);
3c0e092e 1786 let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false);
94222f64
XL
1787 let mut otherwise_candidate = Candidate::new(expr_place_builder.clone(), &wildcard, false);
1788 let fake_borrow_temps = self.lower_match_tree(
1789 block,
1790 pat.span,
1791 pat.span,
1792 false,
1793 &mut [&mut guard_candidate, &mut otherwise_candidate],
1794 );
1795 let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None;
1796 let expr_place: Place<'tcx>;
1797 if let Ok(expr_builder) =
1798 expr_place_builder.try_upvars_resolved(self.tcx, self.typeck_results)
1799 {
1800 expr_place = expr_builder.into_place(self.tcx, self.typeck_results);
1801 opt_expr_place = Some((Some(&expr_place), expr_span));
1802 }
1803 let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();
1804 self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span));
1805
923072b8
FG
1806 self.declare_bindings(
1807 source_scope,
1808 pat.span.to(span),
1809 pat,
1810 ArmHasGuard(false),
1811 opt_expr_place,
1812 );
1813
94222f64
XL
1814 let post_guard_block = self.bind_pattern(
1815 self.source_info(pat.span),
1816 guard_candidate,
1817 None,
1818 &fake_borrow_temps,
1819 expr.span,
1820 None,
1821 None,
1822 None,
1823 );
1824
1825 post_guard_block.unit()
1826 }
1827
e9174d1e 1828 /// Initializes each of the bindings from the candidate by
9fa01778
XL
1829 /// moving/copying/ref'ing the source as appropriate. Tests the guard, if
1830 /// any, and then branches to the arm. Returns the block for the case where
fc512014 1831 /// the guard succeeds.
e9174d1e 1832 ///
e1599b0c
XL
1833 /// Note: we do not check earlier that if there is a guard,
1834 /// there cannot be move bindings. We avoid a use-after-move by only
1835 /// moving the binding once the guard has evaluated to true (see below).
b7449926
XL
1836 fn bind_and_guard_matched_candidate<'pat>(
1837 &mut self,
b7449926 1838 candidate: Candidate<'pat, 'tcx>,
74b04a01 1839 parent_bindings: &[(Vec<Binding<'tcx>>, Vec<Ascription<'tcx>>)],
17df50a5 1840 guard: Option<&Guard<'tcx>>,
923072b8 1841 fake_borrows: &[(Place<'tcx>, Local)],
9fa01778 1842 scrutinee_span: Span,
fc512014 1843 arm_span: Option<Span>,
94222f64 1844 match_scope: Option<region::Scope>,
74b04a01 1845 schedule_drops: bool,
dc9dc135 1846 ) -> BasicBlock {
9fa01778 1847 debug!("bind_and_guard_matched_candidate(candidate={:?})", candidate);
e9174d1e
SL
1848
1849 debug_assert!(candidate.match_pairs.is_empty());
1850
abe05a73
XL
1851 let candidate_source_info = self.source_info(candidate.span);
1852
74b04a01 1853 let mut block = candidate.pre_binding_block.unwrap();
dc9dc135 1854
74b04a01 1855 if candidate.next_candidate_pre_binding_block.is_some() {
dc9dc135
XL
1856 let fresh_block = self.cfg.start_new_block();
1857 self.false_edges(
1858 block,
1859 fresh_block,
1860 candidate.next_candidate_pre_binding_block,
1861 candidate_source_info,
1862 );
1863 block = fresh_block;
dc9dc135 1864 }
e9174d1e 1865
74b04a01
XL
1866 self.ascribe_types(
1867 block,
1868 parent_bindings
1869 .iter()
1870 .flat_map(|(_, ascriptions)| ascriptions)
923072b8
FG
1871 .cloned()
1872 .chain(candidate.ascriptions),
74b04a01
XL
1873 );
1874
83c7162d
XL
1875 // rust-lang/rust#27282: The `autoref` business deserves some
1876 // explanation here.
1877 //
1878 // The intent of the `autoref` flag is that when it is true,
1879 // then any pattern bindings of type T will map to a `&T`
1880 // within the context of the guard expression, but will
1881 // continue to map to a `T` in the context of the arm body. To
1882 // avoid surfacing this distinction in the user source code
1883 // (which would be a severe change to the language and require
1884 // far more revision to the compiler), when `autoref` is true,
1885 // then any occurrence of the identifier in the guard
1886 // expression will automatically get a deref op applied to it.
1887 //
1888 // So an input like:
1889 //
1890 // ```
1891 // let place = Foo::new();
1892 // match place { foo if inspect(foo)
1893 // => feed(foo), ... }
1894 // ```
1895 //
1896 // will be treated as if it were really something like:
1897 //
1898 // ```
1899 // let place = Foo::new();
1900 // match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) }
1901 // => { let tmp2 = place; feed(tmp2) }, ... }
1902 //
1903 // And an input like:
1904 //
1905 // ```
1906 // let place = Foo::new();
1907 // match place { ref mut foo if inspect(foo)
1908 // => feed(foo), ... }
1909 // ```
1910 //
1911 // will be treated as if it were really something like:
1912 //
1913 // ```
1914 // let place = Foo::new();
1915 // match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) }
1916 // => { let tmp2 = &mut place; feed(tmp2) }, ... }
1917 // ```
1918 //
1919 // In short, any pattern binding will always look like *some*
1920 // kind of `&T` within the guard at least in terms of how the
1921 // MIR-borrowck views it, and this will ensure that guard
1922 // expressions cannot mutate their the match inputs via such
1923 // bindings. (It also ensures that guard expressions can at
1924 // most *copy* values from such bindings; non-Copy things
1925 // cannot be moved via pattern bindings in guard expressions.)
1926 //
1927 // ----
1928 //
1929 // Implementation notes (under assumption `autoref` is true).
1930 //
1931 // To encode the distinction above, we must inject the
1932 // temporaries `tmp1` and `tmp2`.
1933 //
1934 // There are two cases of interest: binding by-value, and binding by-ref.
1935 //
1936 // 1. Binding by-value: Things are simple.
1937 //
1938 // * Establishing `tmp1` creates a reference into the
1939 // matched place. This code is emitted by
1940 // bind_matched_candidate_for_guard.
1941 //
1942 // * `tmp2` is only initialized "lazily", after we have
1943 // checked the guard. Thus, the code that can trigger
1944 // moves out of the candidate can only fire after the
1945 // guard evaluated to true. This initialization code is
1946 // emitted by bind_matched_candidate_for_arm.
1947 //
1948 // 2. Binding by-reference: Things are tricky.
1949 //
1950 // * Here, the guard expression wants a `&&` or `&&mut`
1951 // into the original input. This means we need to borrow
9fa01778
XL
1952 // the reference that we create for the arm.
1953 // * So we eagerly create the reference for the arm and then take a
1954 // reference to that.
29967ef6 1955 if let Some(guard) = guard {
6a06907d 1956 let tcx = self.tcx;
74b04a01
XL
1957 let bindings = parent_bindings
1958 .iter()
1959 .flat_map(|(bindings, _)| bindings)
1960 .chain(&candidate.bindings);
48663c56 1961
74b04a01 1962 self.bind_matched_candidate_for_guard(block, schedule_drops, bindings.clone());
48663c56 1963 let guard_frame = GuardFrame {
74b04a01 1964 locals: bindings.map(|b| GuardFrameLocal::new(b.var_id, b.binding_mode)).collect(),
48663c56 1965 };
416331ca 1966 debug!("entering guard building context: {:?}", guard_frame);
48663c56 1967 self.guard_context.push(guard_frame);
83c7162d 1968
48663c56 1969 let re_erased = tcx.lifetimes.re_erased;
9fa01778 1970 let scrutinee_source_info = self.source_info(scrutinee_span);
74b04a01
XL
1971 for &(place, temp) in fake_borrows {
1972 let borrow = Rvalue::Ref(re_erased, BorrowKind::Shallow, place);
ba9703b0 1973 self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow);
9fa01778
XL
1974 }
1975
94222f64
XL
1976 let arm_span = arm_span.unwrap();
1977 let match_scope = match_scope.unwrap();
1978 let mut guard_span = rustc_span::DUMMY_SP;
1979
1980 let (post_guard_block, otherwise_post_guard_block) =
1981 self.in_if_then_scope(match_scope, |this| match *guard {
1982 Guard::If(e) => {
1983 let e = &this.thir[e];
1984 guard_span = e.span;
923072b8
FG
1985 this.then_else_break(
1986 block,
1987 e,
1988 None,
1989 match_scope,
1990 this.source_info(arm_span),
1991 )
6a06907d 1992 }
94222f64
XL
1993 Guard::IfLet(ref pat, scrutinee) => {
1994 let s = &this.thir[scrutinee];
1995 guard_span = s.span;
923072b8 1996 this.lower_let_expr(block, s, pat, match_scope, None, arm_span)
94222f64
XL
1997 }
1998 });
1999
fc512014
XL
2000 let source_info = self.source_info(guard_span);
2001 let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
48663c56 2002 let guard_frame = self.guard_context.pop().unwrap();
dfeec247 2003 debug!("Exiting guard building context with locals: {:?}", guard_frame);
abe05a73 2004
9fa01778 2005 for &(_, temp) in fake_borrows {
60c5eb7d
XL
2006 let cause = FakeReadCause::ForMatchGuard;
2007 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp));
9fa01778 2008 }
94b46f34 2009
74b04a01
XL
2010 let otherwise_block = candidate.otherwise_block.unwrap_or_else(|| {
2011 let unreachable = self.cfg.start_new_block();
2012 self.cfg.terminate(unreachable, source_info, TerminatorKind::Unreachable);
2013 unreachable
2014 });
74b04a01 2015 self.false_edges(
94222f64 2016 otherwise_post_guard_block,
74b04a01
XL
2017 otherwise_block,
2018 candidate.next_candidate_pre_binding_block,
2019 source_info,
dc9dc135
XL
2020 );
2021
94b46f34
XL
2022 // We want to ensure that the matched candidates are bound
2023 // after we have confirmed this candidate *and* any
2024 // associated guard; Binding them on `block` is too soon,
2025 // because that would be before we've checked the result
2026 // from the guard.
2027 //
dc9dc135 2028 // But binding them on the arm is *too late*, because
94b46f34
XL
2029 // then all of the candidates for a single arm would be
2030 // bound in the same place, that would cause a case like:
2031 //
2032 // ```rust
2033 // match (30, 2) {
2034 // (mut x, 1) | (2, mut x) if { true } => { ... }
2035 // ... // ^^^^^^^ (this is `arm_block`)
2036 // }
2037 // ```
2038 //
94222f64 2039 // would yield an `arm_block` something like:
94b46f34
XL
2040 //
2041 // ```
2042 // StorageLive(_4); // _4 is `x`
2043 // _4 = &mut (_1.0: i32); // this is handling `(mut x, 1)` case
2044 // _4 = &mut (_1.1: i32); // this is handling `(2, mut x)` case
2045 // ```
2046 //
2047 // and that is clearly not correct.
fc512014
XL
2048 let by_value_bindings = parent_bindings
2049 .iter()
2050 .flat_map(|(bindings, _)| bindings)
2051 .chain(&candidate.bindings)
2052 .filter(|binding| matches!(binding.binding_mode, BindingMode::ByValue));
48663c56
XL
2053 // Read all of the by reference bindings to ensure that the
2054 // place they refer to can't be modified by the guard.
2055 for binding in by_value_bindings.clone() {
2056 let local_id = self.var_local_id(binding.var_id, RefWithinGuard);
60c5eb7d
XL
2057 let cause = FakeReadCause::ForGuardBinding;
2058 self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id));
83c7162d 2059 }
74b04a01
XL
2060 assert!(schedule_drops, "patterns with guards must schedule drops");
2061 self.bind_matched_candidate_for_arm_body(post_guard_block, true, by_value_bindings);
94b46f34 2062
dc9dc135 2063 post_guard_block
e9174d1e 2064 } else {
94b46f34
XL
2065 // (Here, it is not too early to bind the matched
2066 // candidate on `block`, because there is no guard result
2067 // that we have to inspect before we bind them.)
74b04a01
XL
2068 self.bind_matched_candidate_for_arm_body(
2069 block,
2070 schedule_drops,
2071 parent_bindings
2072 .iter()
2073 .flat_map(|(bindings, _)| bindings)
2074 .chain(&candidate.bindings),
2075 );
dc9dc135 2076 block
e9174d1e
SL
2077 }
2078 }
2079
b7449926
XL
2080 /// Append `AscribeUserType` statements onto the end of `block`
2081 /// for each ascription
923072b8 2082 fn ascribe_types(
74b04a01
XL
2083 &mut self,
2084 block: BasicBlock,
923072b8
FG
2085 ascriptions: impl IntoIterator<Item = Ascription<'tcx>>,
2086 ) {
b7449926 2087 for ascription in ascriptions {
923072b8 2088 let source_info = self.source_info(ascription.annotation.span);
0bf4aa26 2089
923072b8 2090 let base = self.canonical_user_type_annotations.push(ascription.annotation);
b7449926
XL
2091 self.cfg.push(
2092 block,
2093 Statement {
2094 source_info,
2095 kind: StatementKind::AscribeUserType(
923072b8
FG
2096 Box::new((
2097 ascription.source,
2098 UserTypeProjection { base, projs: Vec::new() },
2099 )),
0731742a 2100 ascription.variance,
b7449926
XL
2101 ),
2102 },
2103 );
2104 }
2105 }
2106
74b04a01
XL
2107 fn bind_matched_candidate_for_guard<'b>(
2108 &mut self,
2109 block: BasicBlock,
2110 schedule_drops: bool,
2111 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
2112 ) where
2113 'tcx: 'b,
2114 {
2115 debug!("bind_matched_candidate_for_guard(block={:?})", block);
e9174d1e 2116
83c7162d
XL
2117 // Assign each of the bindings. Since we are binding for a
2118 // guard expression, this will never trigger moves out of the
2119 // candidate.
6a06907d 2120 let re_erased = self.tcx.lifetimes.re_erased;
83c7162d 2121 for binding in bindings {
74b04a01 2122 debug!("bind_matched_candidate_for_guard(binding={:?})", binding);
83c7162d 2123 let source_info = self.source_info(binding.span);
94b46f34
XL
2124
2125 // For each pattern ident P of type T, `ref_for_guard` is
2126 // a reference R: &T pointing to the location matched by
2127 // the pattern, and every occurrence of P within a guard
2128 // denotes *R.
74b04a01
XL
2129 let ref_for_guard = self.storage_live_binding(
2130 block,
2131 binding.var_id,
2132 binding.span,
2133 RefWithinGuard,
2134 schedule_drops,
2135 );
83c7162d
XL
2136 match binding.binding_mode {
2137 BindingMode::ByValue => {
dfeec247 2138 let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source);
ba9703b0 2139 self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
83c7162d 2140 }
0731742a 2141 BindingMode::ByRef(borrow_kind) => {
9fa01778 2142 let value_for_arm = self.storage_live_binding(
b7449926
XL
2143 block,
2144 binding.var_id,
2145 binding.span,
9fa01778 2146 OutsideGuard,
74b04a01 2147 schedule_drops,
b7449926 2148 );
94b46f34 2149
dfeec247 2150 let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source);
ba9703b0 2151 self.cfg.push_assign(block, source_info, value_for_arm, rvalue);
9fa01778 2152 let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm);
ba9703b0 2153 self.cfg.push_assign(block, source_info, ref_for_guard, rvalue);
83c7162d
XL
2154 }
2155 }
2156 }
2157 }
2158
9fa01778 2159 fn bind_matched_candidate_for_arm_body<'b>(
b7449926
XL
2160 &mut self,
2161 block: BasicBlock,
74b04a01 2162 schedule_drops: bool,
9fa01778 2163 bindings: impl IntoIterator<Item = &'b Binding<'tcx>>,
dfeec247
XL
2164 ) where
2165 'tcx: 'b,
2166 {
9fa01778 2167 debug!("bind_matched_candidate_for_arm_body(block={:?})", block);
0731742a 2168
6a06907d 2169 let re_erased = self.tcx.lifetimes.re_erased;
e9174d1e
SL
2170 // Assign each of the bindings. This may trigger moves out of the candidate.
2171 for binding in bindings {
8bb4bdeb 2172 let source_info = self.source_info(binding.span);
74b04a01
XL
2173 let local = self.storage_live_binding(
2174 block,
2175 binding.var_id,
2176 binding.span,
2177 OutsideGuard,
2178 schedule_drops,
2179 );
2180 if schedule_drops {
2181 self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard);
2182 }
e9174d1e 2183 let rvalue = match binding.binding_mode {
ba9703b0 2184 BindingMode::ByValue => Rvalue::Use(self.consume_by_copy_or_move(binding.source)),
0731742a 2185 BindingMode::ByRef(borrow_kind) => {
dfeec247 2186 Rvalue::Ref(re_erased, borrow_kind, binding.source)
83c7162d 2187 }
e9174d1e 2188 };
ba9703b0 2189 self.cfg.push_assign(block, source_info, local, rvalue);
e9174d1e
SL
2190 }
2191 }
2192
9fa01778
XL
2193 /// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound
2194 /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The
2195 /// first local is a binding for occurrences of `var` in the guard, which
2196 /// will have type `&T`. The second local is a binding for occurrences of
2197 /// `var` in the arm body, which will have type `T`.
b7449926
XL
2198 fn declare_binding(
2199 &mut self,
2200 source_info: SourceInfo,
2201 visibility_scope: SourceScope,
2202 mutability: Mutability,
f9f354fc 2203 name: Symbol,
b7449926 2204 mode: BindingMode,
923072b8 2205 var_id: LocalVarId,
b7449926 2206 var_ty: Ty<'tcx>,
532ac7d7 2207 user_ty: UserTypeProjections,
b7449926
XL
2208 has_guard: ArmHasGuard,
2209 opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
2210 pat_span: Span,
2211 ) {
2212 debug!(
2213 "declare_binding(var_id={:?}, name={:?}, mode={:?}, var_ty={:?}, \
2214 visibility_scope={:?}, source_info={:?})",
2215 var_id, name, mode, var_ty, visibility_scope, source_info
2216 );
e9174d1e 2217
6a06907d 2218 let tcx = self.tcx;
dfeec247 2219 let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope };
94b46f34 2220 let binding_mode = match mode {
74b04a01
XL
2221 BindingMode::ByValue => ty::BindingMode::BindByValue(mutability),
2222 BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability),
94b46f34 2223 };
0731742a 2224 debug!("declare_binding: user_ty={:?}", user_ty);
94b46f34 2225 let local = LocalDecl::<'tcx> {
3b2f2976 2226 mutability,
b7449926 2227 ty: var_ty,
94222f64 2228 user_ty: if user_ty.is_empty() { None } else { Some(Box::new(user_ty)) },
3b2f2976 2229 source_info,
ea8adc8c 2230 internal: false,
0bf4aa26 2231 is_block_tail: None,
94222f64 2232 local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
3dfed10e
XL
2233 VarBindingForm {
2234 binding_mode,
2235 // hypothetically, `visit_primary_bindings` could try to unzip
2236 // an outermost hir::Ty as we descend, matching up
2237 // idents in pat; but complex w/ unclear UI payoff.
2238 // Instead, just abandon providing diagnostic info.
2239 opt_ty_info: None,
2240 opt_match_place,
2241 pat_span,
2242 },
94222f64 2243 ))))),
94b46f34 2244 };
dc9dc135 2245 let for_arm_body = self.local_decls.push(local);
60c5eb7d
XL
2246 self.var_debug_info.push(VarDebugInfo {
2247 name,
2248 source_info: debug_source_info,
fc512014 2249 value: VarDebugInfoContents::Place(for_arm_body.into()),
60c5eb7d 2250 });
48663c56 2251 let locals = if has_guard.0 {
94b46f34 2252 let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> {
9fa01778
XL
2253 // This variable isn't mutated but has a name, so has to be
2254 // immutable to avoid the unused mut lint.
b7449926 2255 mutability: Mutability::Not,
48663c56 2256 ty: tcx.mk_imm_ref(tcx.lifetimes.re_erased, var_ty),
f9f354fc 2257 user_ty: None,
83c7162d 2258 source_info,
83c7162d 2259 internal: false,
0bf4aa26 2260 is_block_tail: None,
94222f64 2261 local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
3dfed10e 2262 BindingForm::RefForGuard,
94222f64 2263 )))),
60c5eb7d
XL
2264 });
2265 self.var_debug_info.push(VarDebugInfo {
2266 name,
2267 source_info: debug_source_info,
fc512014 2268 value: VarDebugInfoContents::Place(ref_for_guard.into()),
83c7162d 2269 });
dfeec247 2270 LocalsForNode::ForGuard { ref_for_guard, for_arm_body }
83c7162d
XL
2271 } else {
2272 LocalsForNode::One(for_arm_body)
2273 };
2274 debug!("declare_binding: vars={:?}", locals);
2275 self.var_indices.insert(var_id, locals);
e9174d1e
SL
2276 }
2277}