From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_mir_build/src/build/matches/mod.rs | 2354 ++++++++++++++++++++ .../rustc_mir_build/src/build/matches/simplify.rs | 318 +++ compiler/rustc_mir_build/src/build/matches/test.rs | 837 +++++++ compiler/rustc_mir_build/src/build/matches/util.rs | 109 + 4 files changed, 3618 insertions(+) create mode 100644 compiler/rustc_mir_build/src/build/matches/mod.rs create mode 100644 compiler/rustc_mir_build/src/build/matches/simplify.rs create mode 100644 compiler/rustc_mir_build/src/build/matches/test.rs create mode 100644 compiler/rustc_mir_build/src/build/matches/util.rs (limited to 'compiler/rustc_mir_build/src/build/matches') diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs new file mode 100644 index 000000000..58b1564cc --- /dev/null +++ b/compiler/rustc_mir_build/src/build/matches/mod.rs @@ -0,0 +1,2354 @@ +//! Code related to match expressions. These are sufficiently complex to +//! warrant their own module and submodules. :) This main module includes the +//! high-level algorithm, the submodules contain the details. +//! +//! This also includes code for pattern bindings in `let` statements and +//! function parameters. + +use crate::build::expr::as_place::PlaceBuilder; +use crate::build::scope::DropKind; +use crate::build::ForGuard::{self, OutsideGuard, RefWithinGuard}; +use crate::build::{BlockAnd, BlockAndExtension, Builder}; +use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode}; +use rustc_data_structures::{ + fx::{FxHashSet, FxIndexMap, FxIndexSet}, + stack::ensure_sufficient_stack, +}; +use rustc_index::bit_set::BitSet; +use rustc_middle::middle::region; +use rustc_middle::mir::*; +use rustc_middle::thir::{self, *}; +use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty}; +use rustc_span::symbol::Symbol; +use rustc_span::{BytePos, Pos, Span}; +use rustc_target::abi::VariantIdx; +use smallvec::{smallvec, SmallVec}; + +// helper functions, broken out by category: +mod simplify; +mod test; +mod util; + +use std::borrow::Borrow; +use std::convert::TryFrom; +use std::mem; + +impl<'a, 'tcx> Builder<'a, 'tcx> { + pub(crate) fn then_else_break( + &mut self, + mut block: BasicBlock, + expr: &Expr<'tcx>, + temp_scope_override: Option, + break_scope: region::Scope, + variable_source_info: SourceInfo, + ) -> BlockAnd<()> { + let this = self; + let expr_span = expr.span; + + match expr.kind { + ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => { + let lhs_then_block = unpack!(this.then_else_break( + block, + &this.thir[lhs], + temp_scope_override, + break_scope, + variable_source_info, + )); + + let rhs_then_block = unpack!(this.then_else_break( + lhs_then_block, + &this.thir[rhs], + temp_scope_override, + break_scope, + variable_source_info, + )); + + rhs_then_block.unit() + } + ExprKind::Scope { region_scope, lint_level, value } => { + let region_scope = (region_scope, this.source_info(expr_span)); + this.in_scope(region_scope, lint_level, |this| { + this.then_else_break( + block, + &this.thir[value], + temp_scope_override, + break_scope, + variable_source_info, + ) + }) + } + ExprKind::Let { expr, ref pat } => this.lower_let_expr( + block, + &this.thir[expr], + pat, + break_scope, + Some(variable_source_info.scope), + variable_source_info.span, + ), + _ => { + let temp_scope = temp_scope_override.unwrap_or_else(|| this.local_scope()); + let mutability = Mutability::Mut; + let place = + unpack!(block = this.as_temp(block, Some(temp_scope), expr, mutability)); + let operand = Operand::Move(Place::from(place)); + + let then_block = this.cfg.start_new_block(); + let else_block = this.cfg.start_new_block(); + let term = TerminatorKind::if_(this.tcx, operand, then_block, else_block); + + let source_info = this.source_info(expr_span); + this.cfg.terminate(block, source_info, term); + this.break_for_else(else_block, break_scope, source_info); + + then_block.unit() + } + } + } + + /// Generates MIR for a `match` expression. + /// + /// The MIR that we generate for a match looks like this. + /// + /// ```text + /// [ 0. Pre-match ] + /// | + /// [ 1. Evaluate Scrutinee (expression being matched on) ] + /// [ (fake read of scrutinee) ] + /// | + /// [ 2. Decision tree -- check discriminants ] <--------+ + /// | | + /// | (once a specific arm is chosen) | + /// | | + /// [pre_binding_block] [otherwise_block] + /// | | + /// [ 3. Create "guard bindings" for arm ] | + /// [ (create fake borrows) ] | + /// | | + /// [ 4. Execute guard code ] | + /// [ (read fake borrows) ] --(guard is false)-----------+ + /// | + /// | (guard results in true) + /// | + /// [ 5. Create real bindings and execute arm ] + /// | + /// [ Exit match ] + /// ``` + /// + /// All of the different arms have been stacked on top of each other to + /// simplify the diagram. For an arm with no guard the blocks marked 3 and + /// 4 and the fake borrows are omitted. + /// + /// We generate MIR in the following steps: + /// + /// 1. Evaluate the scrutinee and add the fake read of it ([Builder::lower_scrutinee]). + /// 2. Create the decision tree ([Builder::lower_match_tree]). + /// 3. Determine the fake borrows that are needed from the places that were + /// matched against and create the required temporaries for them + /// ([Builder::calculate_fake_borrows]). + /// 4. Create everything else: the guards and the arms ([Builder::lower_match_arms]). + /// + /// ## False edges + /// + /// We don't want to have the exact structure of the decision tree be + /// visible through borrow checking. False edges ensure that the CFG as + /// seen by borrow checking doesn't encode this. False edges are added: + /// + /// * From each pre-binding block to the next pre-binding block. + /// * From each otherwise block to the next pre-binding block. + #[tracing::instrument(level = "debug", skip(self, arms))] + pub(crate) fn match_expr( + &mut self, + destination: Place<'tcx>, + span: Span, + mut block: BasicBlock, + scrutinee: &Expr<'tcx>, + arms: &[ArmId], + ) -> BlockAnd<()> { + let scrutinee_span = scrutinee.span; + let scrutinee_place = + unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,)); + + let mut arm_candidates = self.create_match_candidates(scrutinee_place.clone(), &arms); + + let match_has_guard = arms.iter().copied().any(|arm| self.thir[arm].guard.is_some()); + let mut candidates = + arm_candidates.iter_mut().map(|(_, candidate)| candidate).collect::>(); + + let match_start_span = span.shrink_to_lo().to(scrutinee.span); + + let fake_borrow_temps = self.lower_match_tree( + block, + scrutinee_span, + match_start_span, + match_has_guard, + &mut candidates, + ); + + self.lower_match_arms( + destination, + scrutinee_place, + scrutinee_span, + arm_candidates, + self.source_info(span), + fake_borrow_temps, + ) + } + + /// Evaluate the scrutinee and add the fake read of it. + fn lower_scrutinee( + &mut self, + mut block: BasicBlock, + scrutinee: &Expr<'tcx>, + scrutinee_span: Span, + ) -> BlockAnd> { + let scrutinee_place_builder = unpack!(block = self.as_place_builder(block, scrutinee)); + // Matching on a `scrutinee_place` with an uninhabited type doesn't + // generate any memory reads by itself, and so if the place "expression" + // contains unsafe operations like raw pointer dereferences or union + // field projections, we wouldn't know to require an `unsafe` block + // around a `match` equivalent to `std::intrinsics::unreachable()`. + // See issue #47412 for this hole being discovered in the wild. + // + // HACK(eddyb) Work around the above issue by adding a dummy inspection + // of `scrutinee_place`, specifically by applying `ReadForMatch`. + // + // NOTE: ReadForMatch also checks that the scrutinee is initialized. + // This is currently needed to not allow matching on an uninitialized, + // uninhabited value. If we get never patterns, those will check that + // the place is initialized, and so this read would only be used to + // check safety. + let cause_matched_place = FakeReadCause::ForMatchedPlace(None); + let source_info = self.source_info(scrutinee_span); + + if let Ok(scrutinee_builder) = + scrutinee_place_builder.clone().try_upvars_resolved(self.tcx, self.typeck_results) + { + let scrutinee_place = scrutinee_builder.into_place(self.tcx, self.typeck_results); + self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place); + } + + block.and(scrutinee_place_builder) + } + + /// Create the initial `Candidate`s for a `match` expression. + fn create_match_candidates<'pat>( + &mut self, + scrutinee: PlaceBuilder<'tcx>, + arms: &'pat [ArmId], + ) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)> + where + 'a: 'pat, + { + // Assemble a list of candidates: there is one candidate per pattern, + // which means there may be more than one candidate *per arm*. + arms.iter() + .copied() + .map(|arm| { + let arm = &self.thir[arm]; + let arm_has_guard = arm.guard.is_some(); + let arm_candidate = Candidate::new(scrutinee.clone(), &arm.pattern, arm_has_guard); + (arm, arm_candidate) + }) + .collect() + } + + /// Create the decision tree for the match expression, starting from `block`. + /// + /// Modifies `candidates` to store the bindings and type ascriptions for + /// that candidate. + /// + /// Returns the places that need fake borrows because we bind or test them. + fn lower_match_tree<'pat>( + &mut self, + block: BasicBlock, + scrutinee_span: Span, + match_start_span: Span, + match_has_guard: bool, + candidates: &mut [&mut Candidate<'pat, 'tcx>], + ) -> Vec<(Place<'tcx>, Local)> { + // The set of places that we are creating fake borrows of. If there are + // no match guards then we don't need any fake borrows, so don't track + // them. + let mut fake_borrows = match_has_guard.then(FxIndexSet::default); + + let mut otherwise = None; + + // This will generate code to test scrutinee_place and + // branch to the appropriate arm block + self.match_candidates( + match_start_span, + scrutinee_span, + block, + &mut otherwise, + candidates, + &mut fake_borrows, + ); + + if let Some(otherwise_block) = otherwise { + // See the doc comment on `match_candidates` for why we may have an + // otherwise block. Match checking will ensure this is actually + // unreachable. + let source_info = self.source_info(scrutinee_span); + self.cfg.terminate(otherwise_block, source_info, TerminatorKind::Unreachable); + } + + // Link each leaf candidate to the `pre_binding_block` of the next one. + let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None; + + for candidate in candidates { + candidate.visit_leaves(|leaf_candidate| { + if let Some(ref mut prev) = previous_candidate { + prev.next_candidate_pre_binding_block = leaf_candidate.pre_binding_block; + } + previous_candidate = Some(leaf_candidate); + }); + } + + if let Some(ref borrows) = fake_borrows { + self.calculate_fake_borrows(borrows, scrutinee_span) + } else { + Vec::new() + } + } + + /// Lower the bindings, guards and arm bodies of a `match` expression. + /// + /// The decision tree should have already been created + /// (by [Builder::lower_match_tree]). + /// + /// `outer_source_info` is the SourceInfo for the whole match. + fn lower_match_arms( + &mut self, + destination: Place<'tcx>, + scrutinee_place_builder: PlaceBuilder<'tcx>, + scrutinee_span: Span, + arm_candidates: Vec<(&'_ Arm<'tcx>, Candidate<'_, 'tcx>)>, + outer_source_info: SourceInfo, + fake_borrow_temps: Vec<(Place<'tcx>, Local)>, + ) -> BlockAnd<()> { + let arm_end_blocks: Vec<_> = arm_candidates + .into_iter() + .map(|(arm, candidate)| { + debug!("lowering arm {:?}\ncandidate = {:?}", arm, candidate); + + let arm_source_info = self.source_info(arm.span); + let arm_scope = (arm.scope, arm_source_info); + let match_scope = self.local_scope(); + self.in_scope(arm_scope, arm.lint_level, |this| { + // `try_upvars_resolved` may fail if it is unable to resolve the given + // `PlaceBuilder` inside a closure. In this case, we don't want to include + // a scrutinee place. `scrutinee_place_builder` will fail to be resolved + // if the only match arm is a wildcard (`_`). + // Example: + // ``` + // let foo = (0, 1); + // let c = || { + // match foo { _ => () }; + // }; + // ``` + let mut opt_scrutinee_place: Option<(Option<&Place<'tcx>>, Span)> = None; + let scrutinee_place: Place<'tcx>; + if let Ok(scrutinee_builder) = scrutinee_place_builder + .clone() + .try_upvars_resolved(this.tcx, this.typeck_results) + { + scrutinee_place = + scrutinee_builder.into_place(this.tcx, this.typeck_results); + opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span)); + } + let scope = this.declare_bindings( + None, + arm.span, + &arm.pattern, + ArmHasGuard(arm.guard.is_some()), + opt_scrutinee_place, + ); + + let arm_block = this.bind_pattern( + outer_source_info, + candidate, + arm.guard.as_ref(), + &fake_borrow_temps, + scrutinee_span, + Some(arm.span), + Some(arm.scope), + Some(match_scope), + ); + + if let Some(source_scope) = scope { + this.source_scope = source_scope; + } + + this.expr_into_dest(destination, arm_block, &&this.thir[arm.body]) + }) + }) + .collect(); + + // all the arm blocks will rejoin here + let end_block = self.cfg.start_new_block(); + + let end_brace = self.source_info( + outer_source_info.span.with_lo(outer_source_info.span.hi() - BytePos::from_usize(1)), + ); + for arm_block in arm_end_blocks { + let block = &self.cfg.basic_blocks[arm_block.0]; + let last_location = block.statements.last().map(|s| s.source_info); + + self.cfg.goto(unpack!(arm_block), last_location.unwrap_or(end_brace), end_block); + } + + self.source_scope = outer_source_info.scope; + + end_block.unit() + } + + /// Binds the variables and ascribes types for a given `match` arm or + /// `let` binding. + /// + /// Also check if the guard matches, if it's provided. + /// `arm_scope` should be `Some` if and only if this is called for a + /// `match` arm. + fn bind_pattern( + &mut self, + outer_source_info: SourceInfo, + candidate: Candidate<'_, 'tcx>, + guard: Option<&Guard<'tcx>>, + fake_borrow_temps: &[(Place<'tcx>, Local)], + scrutinee_span: Span, + arm_span: Option, + arm_scope: Option, + match_scope: Option, + ) -> BasicBlock { + if candidate.subcandidates.is_empty() { + // Avoid generating another `BasicBlock` when we only have one + // candidate. + self.bind_and_guard_matched_candidate( + candidate, + &[], + guard, + fake_borrow_temps, + scrutinee_span, + arm_span, + match_scope, + true, + ) + } else { + // It's helpful to avoid scheduling drops multiple times to save + // drop elaboration from having to clean up the extra drops. + // + // If we are in a `let` then we only schedule drops for the first + // candidate. + // + // If we're in a `match` arm then we could have a case like so: + // + // Ok(x) | Err(x) if return => { /* ... */ } + // + // In this case we don't want a drop of `x` scheduled when we + // return: it isn't bound by move until right before enter the arm. + // To handle this we instead unschedule it's drop after each time + // we lower the guard. + let target_block = self.cfg.start_new_block(); + let mut schedule_drops = true; + // We keep a stack of all of the bindings and type ascriptions + // from the parent candidates that we visit, that also need to + // be bound for each candidate. + traverse_candidate( + candidate, + &mut Vec::new(), + &mut |leaf_candidate, parent_bindings| { + if let Some(arm_scope) = arm_scope { + self.clear_top_scope(arm_scope); + } + let binding_end = self.bind_and_guard_matched_candidate( + leaf_candidate, + parent_bindings, + guard, + &fake_borrow_temps, + scrutinee_span, + arm_span, + match_scope, + schedule_drops, + ); + if arm_scope.is_none() { + schedule_drops = false; + } + self.cfg.goto(binding_end, outer_source_info, target_block); + }, + |inner_candidate, parent_bindings| { + parent_bindings.push((inner_candidate.bindings, inner_candidate.ascriptions)); + inner_candidate.subcandidates.into_iter() + }, + |parent_bindings| { + parent_bindings.pop(); + }, + ); + + target_block + } + } + + pub(super) fn expr_into_pattern( + &mut self, + mut block: BasicBlock, + irrefutable_pat: Pat<'tcx>, + initializer: &Expr<'tcx>, + ) -> BlockAnd<()> { + match *irrefutable_pat.kind { + // Optimize the case of `let x = ...` to write directly into `x` + PatKind::Binding { mode: BindingMode::ByValue, var, subpattern: None, .. } => { + let place = + self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true); + unpack!(block = self.expr_into_dest(place, block, initializer)); + + // Inject a fake read, see comments on `FakeReadCause::ForLet`. + let source_info = self.source_info(irrefutable_pat.span); + self.cfg.push_fake_read(block, source_info, FakeReadCause::ForLet(None), place); + + self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard); + block.unit() + } + + // Optimize the case of `let x: T = ...` to write directly + // into `x` and then require that `T == typeof(x)`. + // + // Weirdly, this is needed to prevent the + // `intrinsic-move-val.rs` test case from crashing. That + // test works with uninitialized values in a rather + // dubious way, so it may be that the test is kind of + // broken. + PatKind::AscribeUserType { + subpattern: + Pat { + kind: + box PatKind::Binding { + mode: BindingMode::ByValue, + var, + subpattern: None, + .. + }, + .. + }, + ascription: thir::Ascription { annotation, variance: _ }, + } => { + let place = + self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true); + unpack!(block = self.expr_into_dest(place, block, initializer)); + + // Inject a fake read, see comments on `FakeReadCause::ForLet`. + let pattern_source_info = self.source_info(irrefutable_pat.span); + let cause_let = FakeReadCause::ForLet(None); + self.cfg.push_fake_read(block, pattern_source_info, cause_let, place); + + let ty_source_info = self.source_info(annotation.span); + + let base = self.canonical_user_type_annotations.push(annotation); + self.cfg.push( + block, + Statement { + source_info: ty_source_info, + kind: StatementKind::AscribeUserType( + Box::new((place, UserTypeProjection { base, projs: Vec::new() })), + // We always use invariant as the variance here. This is because the + // variance field from the ascription refers to the variance to use + // when applying the type to the value being matched, but this + // ascription applies rather to the type of the binding. e.g., in this + // example: + // + // ``` + // let x: T = + // ``` + // + // We are creating an ascription that defines the type of `x` to be + // exactly `T` (i.e., with invariance). The variance field, in + // contrast, is intended to be used to relate `T` to the type of + // ``. + ty::Variance::Invariant, + ), + }, + ); + + self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard); + block.unit() + } + + _ => { + let place_builder = unpack!(block = self.as_place_builder(block, initializer)); + self.place_into_pattern(block, irrefutable_pat, place_builder, true) + } + } + } + + pub(crate) fn place_into_pattern( + &mut self, + block: BasicBlock, + irrefutable_pat: Pat<'tcx>, + initializer: PlaceBuilder<'tcx>, + set_match_place: bool, + ) -> BlockAnd<()> { + let mut candidate = Candidate::new(initializer.clone(), &irrefutable_pat, false); + let fake_borrow_temps = self.lower_match_tree( + block, + irrefutable_pat.span, + irrefutable_pat.span, + false, + &mut [&mut candidate], + ); + // For matches and function arguments, the place that is being matched + // can be set when creating the variables. But the place for + // let PATTERN = ... might not even exist until we do the assignment. + // so we set it here instead. + if set_match_place { + let mut candidate_ref = &candidate; + while let Some(next) = { + for binding in &candidate_ref.bindings { + let local = self.var_local_id(binding.var_id, OutsideGuard); + + let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( + VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. }, + )))) = self.local_decls[local].local_info else { + bug!("Let binding to non-user variable.") + }; + // `try_upvars_resolved` may fail if it is unable to resolve the given + // `PlaceBuilder` inside a closure. In this case, we don't want to include + // a scrutinee place. `scrutinee_place_builder` will fail for destructured + // assignments. This is because a closure only captures the precise places + // that it will read and as a result a closure may not capture the entire + // tuple/struct and rather have individual places that will be read in the + // final MIR. + // Example: + // ``` + // let foo = (0, 1); + // let c = || { + // let (v1, v2) = foo; + // }; + // ``` + if let Ok(match_pair_resolved) = + initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results) + { + let place = match_pair_resolved.into_place(self.tcx, self.typeck_results); + *match_place = Some(place); + } + } + // All of the subcandidates should bind the same locals, so we + // only visit the first one. + candidate_ref.subcandidates.get(0) + } { + candidate_ref = next; + } + } + + self.bind_pattern( + self.source_info(irrefutable_pat.span), + candidate, + None, + &fake_borrow_temps, + irrefutable_pat.span, + None, + None, + None, + ) + .unit() + } + + /// Declares the bindings of the given patterns and returns the visibility + /// scope for the bindings in these patterns, if such a scope had to be + /// created. NOTE: Declaring the bindings should always be done in their + /// drop scope. + pub(crate) fn declare_bindings( + &mut self, + mut visibility_scope: Option, + scope_span: Span, + pattern: &Pat<'tcx>, + has_guard: ArmHasGuard, + opt_match_place: Option<(Option<&Place<'tcx>>, Span)>, + ) -> Option { + debug!("declare_bindings: pattern={:?}", pattern); + self.visit_primary_bindings( + &pattern, + UserTypeProjections::none(), + &mut |this, mutability, name, mode, var, span, ty, user_ty| { + if visibility_scope.is_none() { + visibility_scope = + Some(this.new_source_scope(scope_span, LintLevel::Inherited, None)); + } + let source_info = SourceInfo { span, scope: this.source_scope }; + let visibility_scope = visibility_scope.unwrap(); + this.declare_binding( + source_info, + visibility_scope, + mutability, + name, + mode, + var, + ty, + user_ty, + has_guard, + opt_match_place.map(|(x, y)| (x.cloned(), y)), + pattern.span, + ); + }, + ); + visibility_scope + } + + pub(crate) fn storage_live_binding( + &mut self, + block: BasicBlock, + var: LocalVarId, + span: Span, + for_guard: ForGuard, + schedule_drop: bool, + ) -> Place<'tcx> { + let local_id = self.var_local_id(var, for_guard); + let source_info = self.source_info(span); + self.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(local_id) }); + // Altough there is almost always scope for given variable in corner cases + // like #92893 we might get variable with no scope. + if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) && schedule_drop{ + self.schedule_drop(span, region_scope, local_id, DropKind::Storage); + } + Place::from(local_id) + } + + pub(crate) fn schedule_drop_for_binding( + &mut self, + var: LocalVarId, + span: Span, + for_guard: ForGuard, + ) { + let local_id = self.var_local_id(var, for_guard); + if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) { + self.schedule_drop(span, region_scope, local_id, DropKind::Value); + } + } + + /// Visit all of the primary bindings in a patterns, that is, visit the + /// leftmost occurrence of each variable bound in a pattern. A variable + /// will occur more than once in an or-pattern. + pub(super) fn visit_primary_bindings( + &mut self, + pattern: &Pat<'tcx>, + pattern_user_ty: UserTypeProjections, + f: &mut impl FnMut( + &mut Self, + Mutability, + Symbol, + BindingMode, + LocalVarId, + Span, + Ty<'tcx>, + UserTypeProjections, + ), + ) { + debug!( + "visit_primary_bindings: pattern={:?} pattern_user_ty={:?}", + pattern, pattern_user_ty + ); + match *pattern.kind { + PatKind::Binding { + mutability, + name, + mode, + var, + ty, + ref subpattern, + is_primary, + .. + } => { + if is_primary { + f(self, mutability, name, mode, var, pattern.span, ty, pattern_user_ty.clone()); + } + if let Some(subpattern) = subpattern.as_ref() { + self.visit_primary_bindings(subpattern, pattern_user_ty, 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 { + self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f); + } + for subpattern in slice { + self.visit_primary_bindings( + subpattern, + pattern_user_ty.clone().subslice(from, to), + f, + ); + } + for subpattern in suffix { + self.visit_primary_bindings(subpattern, pattern_user_ty.clone().index(), f); + } + } + + PatKind::Constant { .. } | PatKind::Range { .. } | PatKind::Wild => {} + + PatKind::Deref { ref subpattern } => { + self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f); + } + + PatKind::AscribeUserType { + ref subpattern, + ascription: thir::Ascription { ref annotation, variance: _ }, + } => { + // This corresponds to something like + // + // ``` + // let A::<'a>(_): A<'static> = ...; + // ``` + // + // Note that the variance doesn't apply here, as we are tracking the effect + // of `user_ty` on any bindings contained with subpattern. + + let projection = UserTypeProjection { + base: self.canonical_user_type_annotations.push(annotation.clone()), + projs: Vec::new(), + }; + let subpattern_user_ty = + pattern_user_ty.push_projection(&projection, annotation.span); + self.visit_primary_bindings(subpattern, subpattern_user_ty, f) + } + + PatKind::Leaf { ref subpatterns } => { + for subpattern in subpatterns { + let subpattern_user_ty = pattern_user_ty.clone().leaf(subpattern.field); + debug!("visit_primary_bindings: subpattern_user_ty={:?}", subpattern_user_ty); + self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f); + } + } + + PatKind::Variant { adt_def, substs: _, variant_index, ref subpatterns } => { + for subpattern in subpatterns { + let subpattern_user_ty = + pattern_user_ty.clone().variant(adt_def, variant_index, subpattern.field); + self.visit_primary_bindings(&subpattern.pattern, subpattern_user_ty, f); + } + } + PatKind::Or { ref pats } => { + // In cases where we recover from errors the primary bindings + // may not all be in the leftmost subpattern. For example in + // `let (x | y) = ...`, the primary binding of `y` occurs in + // the right subpattern + for subpattern in pats { + self.visit_primary_bindings(subpattern, pattern_user_ty.clone(), f); + } + } + } + } +} + +#[derive(Debug)] +struct Candidate<'pat, 'tcx> { + /// [`Span`] of the original pattern that gave rise to this candidate. + span: Span, + + /// Whether this `Candidate` has a guard. + has_guard: bool, + + /// All of these must be satisfied... + match_pairs: SmallVec<[MatchPair<'pat, 'tcx>; 1]>, + + /// ...these bindings established... + bindings: Vec>, + + /// ...and these types asserted... + ascriptions: Vec>, + + /// ...and if this is non-empty, one of these subcandidates also has to match... + subcandidates: Vec>, + + /// ...and the guard must be evaluated; if it's `false` then branch to `otherwise_block`. + otherwise_block: Option, + + /// The block before the `bindings` have been established. + pre_binding_block: Option, + /// The pre-binding block of the next candidate. + next_candidate_pre_binding_block: Option, +} + +impl<'tcx, 'pat> Candidate<'pat, 'tcx> { + fn new(place: PlaceBuilder<'tcx>, pattern: &'pat Pat<'tcx>, has_guard: bool) -> Self { + Candidate { + span: pattern.span, + has_guard, + match_pairs: smallvec![MatchPair { place, pattern }], + bindings: Vec::new(), + ascriptions: Vec::new(), + subcandidates: Vec::new(), + otherwise_block: None, + pre_binding_block: None, + next_candidate_pre_binding_block: None, + } + } + + /// Visit the leaf candidates (those with no subcandidates) contained in + /// this candidate. + fn visit_leaves<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) { + traverse_candidate( + self, + &mut (), + &mut move |c, _| visit_leaf(c), + move |c, _| c.subcandidates.iter_mut(), + |_| {}, + ); + } +} + +/// A depth-first traversal of the `Candidate` and all of its recursive +/// subcandidates. +fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>( + candidate: C, + context: &mut T, + visit_leaf: &mut impl FnMut(C, &mut T), + get_children: impl Copy + Fn(C, &mut T) -> I, + complete_children: impl Copy + Fn(&mut T), +) where + C: Borrow>, + I: Iterator, +{ + if candidate.borrow().subcandidates.is_empty() { + visit_leaf(candidate, context) + } else { + for child in get_children(candidate, context) { + traverse_candidate(child, context, visit_leaf, get_children, complete_children); + } + complete_children(context) + } +} + +#[derive(Clone, Debug)] +struct Binding<'tcx> { + span: Span, + source: Place<'tcx>, + var_id: LocalVarId, + binding_mode: BindingMode, +} + +/// Indicates that the type of `source` must be a subtype of the +/// user-given type `user_ty`; this is basically a no-op but can +/// influence region inference. +#[derive(Clone, Debug)] +struct Ascription<'tcx> { + source: Place<'tcx>, + annotation: CanonicalUserTypeAnnotation<'tcx>, + variance: ty::Variance, +} + +#[derive(Clone, Debug)] +pub(crate) struct MatchPair<'pat, 'tcx> { + // this place... + place: PlaceBuilder<'tcx>, + + // ... must match this pattern. + pattern: &'pat Pat<'tcx>, +} + +/// See [`Test`] for more. +#[derive(Clone, Debug, PartialEq)] +enum TestKind<'tcx> { + /// Test what enum variant a value is. + Switch { + /// The enum type being tested. + adt_def: ty::AdtDef<'tcx>, + /// The set of variants that we should create a branch for. We also + /// create an additional "otherwise" case. + variants: BitSet, + }, + + /// Test what value an integer, `bool`, or `char` has. + SwitchInt { + /// The type of the value that we're testing. + switch_ty: Ty<'tcx>, + /// The (ordered) set of values that we test for. + /// + /// For integers and `char`s we create a branch to each of the values in + /// `options`, as well as an "otherwise" branch for all other values, even + /// in the (rare) case that `options` is exhaustive. + /// + /// For `bool` we always generate two edges, one for `true` and one for + /// `false`. + options: FxIndexMap, u128>, + }, + + /// Test for equality with value, possibly after an unsizing coercion to + /// `ty`, + Eq { + value: ConstantKind<'tcx>, + // Integer types are handled by `SwitchInt`, and constants with ADT + // types are converted back into patterns, so this can only be `&str`, + // `&[T]`, `f32` or `f64`. + ty: Ty<'tcx>, + }, + + /// Test whether the value falls within an inclusive or exclusive range + Range(PatRange<'tcx>), + + /// Test that the length of the slice is equal to `len`. + Len { len: u64, op: BinOp }, +} + +/// A test to perform to determine which [`Candidate`] matches a value. +/// +/// [`Test`] is just the test to perform; it does not include the value +/// to be tested. +#[derive(Debug)] +pub(crate) struct Test<'tcx> { + span: Span, + kind: TestKind<'tcx>, +} + +/// `ArmHasGuard` is a wrapper around a boolean flag. It indicates whether +/// a match arm has a guard expression attached to it. +#[derive(Copy, Clone, Debug)] +pub(crate) struct ArmHasGuard(pub(crate) bool); + +/////////////////////////////////////////////////////////////////////////// +// Main matching algorithm + +impl<'a, 'tcx> Builder<'a, 'tcx> { + /// The main match algorithm. It begins with a set of candidates + /// `candidates` and has the job of generating code to determine + /// which of these candidates, if any, is the correct one. The + /// candidates are sorted such that the first item in the list + /// has the highest priority. When a candidate is found to match + /// the value, we will set and generate a branch to the appropriate + /// pre-binding block. + /// + /// If we find that *NONE* of the candidates apply, we branch to the + /// `otherwise_block`, setting it to `Some` if required. In principle, this + /// means that the input list was not exhaustive, though at present we + /// sometimes are not smart enough to recognize all exhaustive inputs. + /// + /// It might be surprising that the input can be non-exhaustive. + /// Indeed, initially, it is not, because all matches are + /// exhaustive in Rust. But during processing we sometimes divide + /// up the list of candidates and recurse with a non-exhaustive + /// list. This is important to keep the size of the generated code + /// under control. See [`Builder::test_candidates`] for more details. + /// + /// If `fake_borrows` is `Some`, then places which need fake borrows + /// will be added to it. + /// + /// For an example of a case where we set `otherwise_block`, even for an + /// exhaustive match, consider: + /// + /// ``` + /// # fn foo(x: (bool, bool)) { + /// match x { + /// (true, true) => (), + /// (_, false) => (), + /// (false, true) => (), + /// } + /// # } + /// ``` + /// + /// For this match, we check if `x.0` matches `true` (for the first + /// arm). If it doesn't match, we check `x.1`. If `x.1` is `true` we check + /// if `x.0` matches `false` (for the third arm). In the (impossible at + /// runtime) case when `x.0` is now `true`, we branch to + /// `otherwise_block`. + fn match_candidates<'pat>( + &mut self, + span: Span, + scrutinee_span: Span, + start_block: BasicBlock, + otherwise_block: &mut Option, + candidates: &mut [&mut Candidate<'pat, 'tcx>], + fake_borrows: &mut Option>>, + ) { + debug!( + "matched_candidate(span={:?}, candidates={:?}, start_block={:?}, otherwise_block={:?})", + span, candidates, start_block, otherwise_block, + ); + + // Start by simplifying candidates. Once this process is complete, all + // the match pairs which remain require some form of test, whether it + // be a switch or pattern comparison. + let mut split_or_candidate = false; + for candidate in &mut *candidates { + split_or_candidate |= self.simplify_candidate(candidate); + } + + ensure_sufficient_stack(|| { + if split_or_candidate { + // At least one of the candidates has been split into subcandidates. + // We need to change the candidate list to include those. + let mut new_candidates = Vec::new(); + + for candidate in candidates { + candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate)); + } + self.match_simplified_candidates( + span, + scrutinee_span, + start_block, + otherwise_block, + &mut *new_candidates, + fake_borrows, + ); + } else { + self.match_simplified_candidates( + span, + scrutinee_span, + start_block, + otherwise_block, + candidates, + fake_borrows, + ); + } + }); + } + + fn match_simplified_candidates( + &mut self, + span: Span, + scrutinee_span: Span, + start_block: BasicBlock, + otherwise_block: &mut Option, + candidates: &mut [&mut Candidate<'_, 'tcx>], + fake_borrows: &mut Option>>, + ) { + // The candidates are sorted by priority. Check to see whether the + // higher priority candidates (and hence at the front of the slice) + // have satisfied all their match pairs. + let fully_matched = candidates.iter().take_while(|c| c.match_pairs.is_empty()).count(); + debug!("match_candidates: {:?} candidates fully matched", fully_matched); + let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched); + + let block = if !matched_candidates.is_empty() { + let otherwise_block = + self.select_matched_candidates(matched_candidates, start_block, fake_borrows); + + if let Some(last_otherwise_block) = otherwise_block { + last_otherwise_block + } else { + // Any remaining candidates are unreachable. + if unmatched_candidates.is_empty() { + return; + } + self.cfg.start_new_block() + } + } else { + start_block + }; + + // If there are no candidates that still need testing, we're + // done. Since all matches are exhaustive, execution should + // never reach this point. + if unmatched_candidates.is_empty() { + let source_info = self.source_info(span); + if let Some(otherwise) = *otherwise_block { + self.cfg.goto(block, source_info, otherwise); + } else { + *otherwise_block = Some(block); + } + return; + } + + // Test for the remaining candidates. + self.test_candidates_with_or( + span, + scrutinee_span, + unmatched_candidates, + block, + otherwise_block, + fake_borrows, + ); + } + + /// Link up matched candidates. + /// + /// For example, if we have something like this: + /// + /// ```ignore (illustrative) + /// ... + /// Some(x) if cond1 => ... + /// Some(x) => ... + /// Some(x) if cond2 => ... + /// ... + /// ``` + /// + /// We generate real edges from: + /// + /// * `start_block` to the [pre-binding block] of the first pattern, + /// * the [otherwise block] of the first pattern to the second pattern, + /// * the [otherwise block] of the third pattern to a block with an + /// [`Unreachable` terminator](TerminatorKind::Unreachable). + /// + /// In addition, we add fake edges from the otherwise blocks to the + /// pre-binding block of the next candidate in the original set of + /// candidates. + /// + /// [pre-binding block]: Candidate::pre_binding_block + /// [otherwise block]: Candidate::otherwise_block + fn select_matched_candidates( + &mut self, + matched_candidates: &mut [&mut Candidate<'_, 'tcx>], + start_block: BasicBlock, + fake_borrows: &mut Option>>, + ) -> Option { + debug_assert!( + !matched_candidates.is_empty(), + "select_matched_candidates called with no candidates", + ); + debug_assert!( + matched_candidates.iter().all(|c| c.subcandidates.is_empty()), + "subcandidates should be empty in select_matched_candidates", + ); + + // Insert a borrows of prefixes of places that are bound and are + // behind a dereference projection. + // + // These borrows are taken to avoid situations like the following: + // + // match x[10] { + // _ if { x = &[0]; false } => (), + // y => (), // Out of bounds array access! + // } + // + // match *x { + // // y is bound by reference in the guard and then by copy in the + // // arm, so y is 2 in the arm! + // y if { y == 1 && (x = &2) == () } => y, + // _ => 3, + // } + if let Some(fake_borrows) = fake_borrows { + for Binding { source, .. } in + matched_candidates.iter().flat_map(|candidate| &candidate.bindings) + { + if let Some(i) = + source.projection.iter().rposition(|elem| elem == ProjectionElem::Deref) + { + let proj_base = &source.projection[..i]; + + fake_borrows.insert(Place { + local: source.local, + projection: self.tcx.intern_place_elems(proj_base), + }); + } + } + } + + let fully_matched_with_guard = matched_candidates + .iter() + .position(|c| !c.has_guard) + .unwrap_or(matched_candidates.len() - 1); + + let (reachable_candidates, unreachable_candidates) = + matched_candidates.split_at_mut(fully_matched_with_guard + 1); + + let mut next_prebinding = start_block; + + for candidate in reachable_candidates.iter_mut() { + assert!(candidate.otherwise_block.is_none()); + assert!(candidate.pre_binding_block.is_none()); + candidate.pre_binding_block = Some(next_prebinding); + if candidate.has_guard { + // Create the otherwise block for this candidate, which is the + // pre-binding block for the next candidate. + next_prebinding = self.cfg.start_new_block(); + candidate.otherwise_block = Some(next_prebinding); + } + } + + debug!( + "match_candidates: add pre_binding_blocks for unreachable {:?}", + unreachable_candidates, + ); + for candidate in unreachable_candidates { + assert!(candidate.pre_binding_block.is_none()); + candidate.pre_binding_block = Some(self.cfg.start_new_block()); + } + + reachable_candidates.last_mut().unwrap().otherwise_block + } + + /// Tests a candidate where there are only or-patterns left to test, or + /// forwards to [Builder::test_candidates]. + /// + /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like + /// so: + /// + /// ```text + /// [ start ] + /// | + /// [ match P, Q ] + /// | + /// +----------------------------------------+------------------------------------+ + /// | | | + /// V V V + /// [ P matches ] [ Q matches ] [ otherwise ] + /// | | | + /// V V | + /// [ match R, S ] [ match R, S ] | + /// | | | + /// +--------------+------------+ +--------------+------------+ | + /// | | | | | | | + /// V V V V V V | + /// [ R matches ] [ S matches ] [otherwise ] [ R matches ] [ S matches ] [otherwise ] | + /// | | | | | | | + /// +--------------+------------|------------+--------------+ | | + /// | | | | + /// | +----------------------------------------+--------+ + /// | | + /// V V + /// [ Success ] [ Failure ] + /// ``` + /// + /// In practice there are some complications: + /// + /// * If there's a guard, then the otherwise branch of the first match on + /// `R | S` goes to a test for whether `Q` matches, and the control flow + /// doesn't merge into a single success block until after the guard is + /// tested. + /// * If neither `P` or `Q` has any bindings or type ascriptions and there + /// isn't a match guard, then we create a smaller CFG like: + /// + /// ```text + /// ... + /// +---------------+------------+ + /// | | | + /// [ P matches ] [ Q matches ] [ otherwise ] + /// | | | + /// +---------------+ | + /// | ... + /// [ match R, S ] + /// | + /// ... + /// ``` + fn test_candidates_with_or( + &mut self, + span: Span, + scrutinee_span: Span, + candidates: &mut [&mut Candidate<'_, 'tcx>], + block: BasicBlock, + otherwise_block: &mut Option, + fake_borrows: &mut Option>>, + ) { + let (first_candidate, remaining_candidates) = candidates.split_first_mut().unwrap(); + + // All of the or-patterns have been sorted to the end, so if the first + // pattern is an or-pattern we only have or-patterns. + match *first_candidate.match_pairs[0].pattern.kind { + PatKind::Or { .. } => (), + _ => { + self.test_candidates( + span, + scrutinee_span, + candidates, + block, + otherwise_block, + fake_borrows, + ); + return; + } + } + + let match_pairs = mem::take(&mut first_candidate.match_pairs); + first_candidate.pre_binding_block = Some(block); + + let mut otherwise = None; + for match_pair in match_pairs { + let PatKind::Or { ref pats } = &*match_pair.pattern.kind else { + bug!("Or-patterns should have been sorted to the end"); + }; + let or_span = match_pair.pattern.span; + let place = match_pair.place; + + first_candidate.visit_leaves(|leaf_candidate| { + self.test_or_pattern( + leaf_candidate, + &mut otherwise, + pats, + or_span, + place.clone(), + fake_borrows, + ); + }); + } + + let remainder_start = otherwise.unwrap_or_else(|| self.cfg.start_new_block()); + + self.match_candidates( + span, + scrutinee_span, + remainder_start, + otherwise_block, + remaining_candidates, + fake_borrows, + ) + } + + fn test_or_pattern<'pat>( + &mut self, + candidate: &mut Candidate<'pat, 'tcx>, + otherwise: &mut Option, + pats: &'pat [Pat<'tcx>], + or_span: Span, + place: PlaceBuilder<'tcx>, + fake_borrows: &mut Option>>, + ) { + debug!("test_or_pattern:\ncandidate={:#?}\npats={:#?}", candidate, pats); + let mut or_candidates: Vec<_> = pats + .iter() + .map(|pat| Candidate::new(place.clone(), pat, candidate.has_guard)) + .collect(); + let mut or_candidate_refs: Vec<_> = or_candidates.iter_mut().collect(); + let otherwise = if candidate.otherwise_block.is_some() { + &mut candidate.otherwise_block + } else { + otherwise + }; + self.match_candidates( + or_span, + or_span, + candidate.pre_binding_block.unwrap(), + otherwise, + &mut or_candidate_refs, + fake_borrows, + ); + candidate.subcandidates = or_candidates; + self.merge_trivial_subcandidates(candidate, self.source_info(or_span)); + } + + /// Try to merge all of the subcandidates of the given candidate into one. + /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. + fn merge_trivial_subcandidates( + &mut self, + candidate: &mut Candidate<'_, 'tcx>, + source_info: SourceInfo, + ) { + if candidate.subcandidates.is_empty() || candidate.has_guard { + // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard. + return; + } + + let mut can_merge = true; + + // Not `Iterator::all` because we don't want to short-circuit. + for subcandidate in &mut candidate.subcandidates { + self.merge_trivial_subcandidates(subcandidate, source_info); + + // FIXME(or_patterns; matthewjasper) Try to be more aggressive here. + can_merge &= subcandidate.subcandidates.is_empty() + && subcandidate.bindings.is_empty() + && subcandidate.ascriptions.is_empty(); + } + + if can_merge { + let any_matches = self.cfg.start_new_block(); + for subcandidate in mem::take(&mut candidate.subcandidates) { + let or_block = subcandidate.pre_binding_block.unwrap(); + self.cfg.goto(or_block, source_info, any_matches); + } + candidate.pre_binding_block = Some(any_matches); + } + } + + /// This is the most subtle part of the matching algorithm. At + /// this point, the input candidates have been fully simplified, + /// and so we know that all remaining match-pairs require some + /// sort of test. To decide what test to perform, we take the highest + /// priority candidate (the first one in the list, as of January 2021) + /// and extract the first match-pair from the list. From this we decide + /// what kind of test is needed using [`Builder::test`], defined in the + /// [`test` module](mod@test). + /// + /// *Note:* taking the first match pair is somewhat arbitrary, and + /// we might do better here by choosing more carefully what to + /// test. + /// + /// For example, consider the following possible match-pairs: + /// + /// 1. `x @ Some(P)` -- we will do a [`Switch`] to decide what variant `x` has + /// 2. `x @ 22` -- we will do a [`SwitchInt`] to decide what value `x` has + /// 3. `x @ 3..5` -- we will do a [`Range`] test to decide what range `x` falls in + /// 4. etc. + /// + /// [`Switch`]: TestKind::Switch + /// [`SwitchInt`]: TestKind::SwitchInt + /// [`Range`]: TestKind::Range + /// + /// Once we know what sort of test we are going to perform, this + /// test may also help us winnow down our candidates. So we walk over + /// the candidates (from high to low priority) and check. This + /// gives us, for each outcome of the test, a transformed list of + /// candidates. For example, if we are testing `x.0`'s variant, + /// and we have a candidate `(x.0 @ Some(v), x.1 @ 22)`, + /// then we would have a resulting candidate of `((x.0 as Some).0 @ v, x.1 @ 22)`. + /// Note that the first match-pair is now simpler (and, in fact, irrefutable). + /// + /// But there may also be candidates that the test just doesn't + /// apply to. The classical example involves wildcards: + /// + /// ``` + /// # let (x, y, z) = (true, true, true); + /// match (x, y, z) { + /// (true , _ , true ) => true, // (0) + /// (_ , true , _ ) => true, // (1) + /// (false, false, _ ) => false, // (2) + /// (true , _ , false) => false, // (3) + /// } + /// # ; + /// ``` + /// + /// In that case, after we test on `x`, there are 2 overlapping candidate + /// sets: + /// + /// - If the outcome is that `x` is true, candidates 0, 1, and 3 + /// - If the outcome is that `x` is false, candidates 1 and 2 + /// + /// Here, the traditional "decision tree" method would generate 2 + /// separate code-paths for the 2 separate cases. + /// + /// In some cases, this duplication can create an exponential amount of + /// code. This is most easily seen by noticing that this method terminates + /// with precisely the reachable arms being reachable - but that problem + /// is trivially NP-complete: + /// + /// ```ignore (illustrative) + /// match (var0, var1, var2, var3, ...) { + /// (true , _ , _ , false, true, ...) => false, + /// (_ , true, true , false, _ , ...) => false, + /// (false, _ , false, false, _ , ...) => false, + /// ... + /// _ => true + /// } + /// ``` + /// + /// Here the last arm is reachable only if there is an assignment to + /// the variables that does not match any of the literals. Therefore, + /// compilation would take an exponential amount of time in some cases. + /// + /// That kind of exponential worst-case might not occur in practice, but + /// our simplistic treatment of constants and guards would make it occur + /// in very common situations - for example [#29740]: + /// + /// ```ignore (illustrative) + /// match x { + /// "foo" if foo_guard => ..., + /// "bar" if bar_guard => ..., + /// "baz" if baz_guard => ..., + /// ... + /// } + /// ``` + /// + /// [#29740]: https://github.com/rust-lang/rust/issues/29740 + /// + /// Here we first test the match-pair `x @ "foo"`, which is an [`Eq` test]. + /// + /// [`Eq` test]: TestKind::Eq + /// + /// It might seem that we would end up with 2 disjoint candidate + /// sets, consisting of the first candidate or the other two, but our + /// algorithm doesn't reason about `"foo"` being distinct from the other + /// constants; it considers the latter arms to potentially match after + /// both outcomes, which obviously leads to an exponential number + /// of tests. + /// + /// To avoid these kinds of problems, our algorithm tries to ensure + /// the amount of generated tests is linear. When we do a k-way test, + /// we return an additional "unmatched" set alongside the obvious `k` + /// sets. When we encounter a candidate that would be present in more + /// than one of the sets, we put it and all candidates below it into the + /// "unmatched" set. This ensures these `k+1` sets are disjoint. + /// + /// After we perform our test, we branch into the appropriate candidate + /// set and recurse with `match_candidates`. These sub-matches are + /// obviously non-exhaustive - as we discarded our otherwise set - so + /// we set their continuation to do `match_candidates` on the + /// "unmatched" set (which is again non-exhaustive). + /// + /// If you apply this to the above test, you basically wind up + /// with an if-else-if chain, testing each candidate in turn, + /// which is precisely what we want. + /// + /// In addition to avoiding exponential-time blowups, this algorithm + /// also has the nice property that each guard and arm is only generated + /// once. + fn test_candidates<'pat, 'b, 'c>( + &mut self, + span: Span, + scrutinee_span: Span, + mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>], + block: BasicBlock, + otherwise_block: &mut Option, + fake_borrows: &mut Option>>, + ) { + // extract the match-pair from the highest priority candidate + let match_pair = &candidates.first().unwrap().match_pairs[0]; + let mut test = self.test(match_pair); + let match_place = match_pair.place.clone(); + + // most of the time, the test to perform is simply a function + // of the main candidate; but for a test like SwitchInt, we + // may want to add cases based on the candidates that are + // available + match test.kind { + TestKind::SwitchInt { switch_ty, ref mut options } => { + for candidate in candidates.iter() { + if !self.add_cases_to_switch(&match_place, candidate, switch_ty, options) { + break; + } + } + } + TestKind::Switch { adt_def: _, ref mut variants } => { + for candidate in candidates.iter() { + if !self.add_variants_to_switch(&match_place, candidate, variants) { + break; + } + } + } + _ => {} + } + + // Insert a Shallow borrow of any places that is switched on. + if let Some(fb) = fake_borrows && let Ok(match_place_resolved) = + match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results) + { + let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results); + fb.insert(resolved_place); + } + + // perform the test, branching to one of N blocks. For each of + // those N possible outcomes, create a (initially empty) + // vector of candidates. Those are the candidates that still + // apply if the test has that particular outcome. + debug!("test_candidates: test={:?} match_pair={:?}", test, match_pair); + let mut target_candidates: Vec>> = vec![]; + target_candidates.resize_with(test.targets(), Default::default); + + let total_candidate_count = candidates.len(); + + // Sort the candidates into the appropriate vector in + // `target_candidates`. Note that at some point we may + // encounter a candidate where the test is not relevant; at + // that point, we stop sorting. + while let Some(candidate) = candidates.first_mut() { + let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else { + break; + }; + let (candidate, rest) = candidates.split_first_mut().unwrap(); + target_candidates[idx].push(candidate); + candidates = rest; + } + // at least the first candidate ought to be tested + assert!(total_candidate_count > candidates.len()); + debug!("test_candidates: tested_candidates: {}", total_candidate_count - candidates.len()); + debug!("test_candidates: untested_candidates: {}", candidates.len()); + + // HACK(matthewjasper) This is a closure so that we can let the test + // create its blocks before the rest of the match. This currently + // improves the speed of llvm when optimizing long string literal + // matches + let make_target_blocks = move |this: &mut Self| -> Vec { + // The block that we should branch to if none of the + // `target_candidates` match. This is either the block where we + // start matching the untested candidates if there are any, + // otherwise it's the `otherwise_block`. + let remainder_start = &mut None; + let remainder_start = + if candidates.is_empty() { &mut *otherwise_block } else { remainder_start }; + + // For each outcome of test, process the candidates that still + // apply. Collect a list of blocks where control flow will + // branch if one of the `target_candidate` sets is not + // exhaustive. + let target_blocks: Vec<_> = target_candidates + .into_iter() + .map(|mut candidates| { + if !candidates.is_empty() { + let candidate_start = this.cfg.start_new_block(); + this.match_candidates( + span, + scrutinee_span, + candidate_start, + remainder_start, + &mut *candidates, + fake_borrows, + ); + candidate_start + } else { + *remainder_start.get_or_insert_with(|| this.cfg.start_new_block()) + } + }) + .collect(); + + if !candidates.is_empty() { + let remainder_start = remainder_start.unwrap_or_else(|| this.cfg.start_new_block()); + this.match_candidates( + span, + scrutinee_span, + remainder_start, + otherwise_block, + candidates, + fake_borrows, + ); + }; + + target_blocks + }; + + self.perform_test(span, scrutinee_span, block, match_place, &test, make_target_blocks); + } + + /// Determine the fake borrows that are needed from a set of places that + /// have to be stable across match guards. + /// + /// Returns a list of places that need a fake borrow and the temporary + /// that's used to store the fake borrow. + /// + /// Match exhaustiveness checking is not able to handle the case where the + /// place being matched on is mutated in the guards. We add "fake borrows" + /// to the guards that prevent any mutation of the place being matched. + /// There are a some subtleties: + /// + /// 1. Borrowing `*x` doesn't prevent assigning to `x`. If `x` is a shared + /// reference, the borrow isn't even tracked. As such we have to add fake + /// borrows of any prefixes of a place + /// 2. We don't want `match x { _ => (), }` to conflict with mutable + /// borrows of `x`, so we only add fake borrows for places which are + /// bound or tested by the match. + /// 3. We don't want the fake borrows to conflict with `ref mut` bindings, + /// so we use a special BorrowKind for them. + /// 4. The fake borrows may be of places in inactive variants, so it would + /// be UB to generate code for them. They therefore have to be removed + /// by a MIR pass run after borrow checking. + fn calculate_fake_borrows<'b>( + &mut self, + fake_borrows: &'b FxIndexSet>, + temp_span: Span, + ) -> Vec<(Place<'tcx>, Local)> { + let tcx = self.tcx; + + debug!("add_fake_borrows fake_borrows = {:?}", fake_borrows); + + let mut all_fake_borrows = Vec::with_capacity(fake_borrows.len()); + + // Insert a Shallow borrow of the prefixes of any fake borrows. + for place in fake_borrows { + let mut cursor = place.projection.as_ref(); + while let [proj_base @ .., elem] = cursor { + cursor = proj_base; + + if let ProjectionElem::Deref = elem { + // Insert a shallow borrow after a deref. For other + // projections the borrow of prefix_cursor will + // conflict with any mutation of base. + all_fake_borrows.push(PlaceRef { local: place.local, projection: proj_base }); + } + } + + all_fake_borrows.push(place.as_ref()); + } + + // Deduplicate + let mut dedup = FxHashSet::default(); + all_fake_borrows.retain(|b| dedup.insert(*b)); + + debug!("add_fake_borrows all_fake_borrows = {:?}", all_fake_borrows); + + all_fake_borrows + .into_iter() + .map(|matched_place_ref| { + let matched_place = Place { + local: matched_place_ref.local, + projection: tcx.intern_place_elems(matched_place_ref.projection), + }; + let fake_borrow_deref_ty = matched_place.ty(&self.local_decls, tcx).ty; + let fake_borrow_ty = tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty); + let fake_borrow_temp = + self.local_decls.push(LocalDecl::new(fake_borrow_ty, temp_span)); + + (matched_place, fake_borrow_temp) + }) + .collect() + } +} + +/////////////////////////////////////////////////////////////////////////// +// Pat binding - used for `let` and function parameters as well. + +impl<'a, 'tcx> Builder<'a, 'tcx> { + pub(crate) fn lower_let_expr( + &mut self, + mut block: BasicBlock, + expr: &Expr<'tcx>, + pat: &Pat<'tcx>, + else_target: region::Scope, + source_scope: Option, + span: Span, + ) -> BlockAnd<()> { + let expr_span = expr.span; + let expr_place_builder = unpack!(block = self.lower_scrutinee(block, expr, expr_span)); + let wildcard = Pat::wildcard_from_ty(pat.ty); + let mut guard_candidate = Candidate::new(expr_place_builder.clone(), &pat, false); + let mut otherwise_candidate = Candidate::new(expr_place_builder.clone(), &wildcard, false); + let fake_borrow_temps = self.lower_match_tree( + block, + pat.span, + pat.span, + false, + &mut [&mut guard_candidate, &mut otherwise_candidate], + ); + let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None; + let expr_place: Place<'tcx>; + if let Ok(expr_builder) = + expr_place_builder.try_upvars_resolved(self.tcx, self.typeck_results) + { + expr_place = expr_builder.into_place(self.tcx, self.typeck_results); + opt_expr_place = Some((Some(&expr_place), expr_span)); + } + let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap(); + self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span)); + + self.declare_bindings( + source_scope, + pat.span.to(span), + pat, + ArmHasGuard(false), + opt_expr_place, + ); + + let post_guard_block = self.bind_pattern( + self.source_info(pat.span), + guard_candidate, + None, + &fake_borrow_temps, + expr.span, + None, + None, + None, + ); + + post_guard_block.unit() + } + + /// Initializes each of the bindings from the candidate by + /// moving/copying/ref'ing the source as appropriate. Tests the guard, if + /// any, and then branches to the arm. Returns the block for the case where + /// the guard succeeds. + /// + /// Note: we do not check earlier that if there is a guard, + /// there cannot be move bindings. We avoid a use-after-move by only + /// moving the binding once the guard has evaluated to true (see below). + fn bind_and_guard_matched_candidate<'pat>( + &mut self, + candidate: Candidate<'pat, 'tcx>, + parent_bindings: &[(Vec>, Vec>)], + guard: Option<&Guard<'tcx>>, + fake_borrows: &[(Place<'tcx>, Local)], + scrutinee_span: Span, + arm_span: Option, + match_scope: Option, + schedule_drops: bool, + ) -> BasicBlock { + debug!("bind_and_guard_matched_candidate(candidate={:?})", candidate); + + debug_assert!(candidate.match_pairs.is_empty()); + + let candidate_source_info = self.source_info(candidate.span); + + let mut block = candidate.pre_binding_block.unwrap(); + + if candidate.next_candidate_pre_binding_block.is_some() { + let fresh_block = self.cfg.start_new_block(); + self.false_edges( + block, + fresh_block, + candidate.next_candidate_pre_binding_block, + candidate_source_info, + ); + block = fresh_block; + } + + self.ascribe_types( + block, + parent_bindings + .iter() + .flat_map(|(_, ascriptions)| ascriptions) + .cloned() + .chain(candidate.ascriptions), + ); + + // rust-lang/rust#27282: The `autoref` business deserves some + // explanation here. + // + // The intent of the `autoref` flag is that when it is true, + // then any pattern bindings of type T will map to a `&T` + // within the context of the guard expression, but will + // continue to map to a `T` in the context of the arm body. To + // avoid surfacing this distinction in the user source code + // (which would be a severe change to the language and require + // far more revision to the compiler), when `autoref` is true, + // then any occurrence of the identifier in the guard + // expression will automatically get a deref op applied to it. + // + // So an input like: + // + // ``` + // let place = Foo::new(); + // match place { foo if inspect(foo) + // => feed(foo), ... } + // ``` + // + // will be treated as if it were really something like: + // + // ``` + // let place = Foo::new(); + // match place { Foo { .. } if { let tmp1 = &place; inspect(*tmp1) } + // => { let tmp2 = place; feed(tmp2) }, ... } + // + // And an input like: + // + // ``` + // let place = Foo::new(); + // match place { ref mut foo if inspect(foo) + // => feed(foo), ... } + // ``` + // + // will be treated as if it were really something like: + // + // ``` + // let place = Foo::new(); + // match place { Foo { .. } if { let tmp1 = & &mut place; inspect(*tmp1) } + // => { let tmp2 = &mut place; feed(tmp2) }, ... } + // ``` + // + // In short, any pattern binding will always look like *some* + // kind of `&T` within the guard at least in terms of how the + // MIR-borrowck views it, and this will ensure that guard + // expressions cannot mutate their the match inputs via such + // bindings. (It also ensures that guard expressions can at + // most *copy* values from such bindings; non-Copy things + // cannot be moved via pattern bindings in guard expressions.) + // + // ---- + // + // Implementation notes (under assumption `autoref` is true). + // + // To encode the distinction above, we must inject the + // temporaries `tmp1` and `tmp2`. + // + // There are two cases of interest: binding by-value, and binding by-ref. + // + // 1. Binding by-value: Things are simple. + // + // * Establishing `tmp1` creates a reference into the + // matched place. This code is emitted by + // bind_matched_candidate_for_guard. + // + // * `tmp2` is only initialized "lazily", after we have + // checked the guard. Thus, the code that can trigger + // moves out of the candidate can only fire after the + // guard evaluated to true. This initialization code is + // emitted by bind_matched_candidate_for_arm. + // + // 2. Binding by-reference: Things are tricky. + // + // * Here, the guard expression wants a `&&` or `&&mut` + // into the original input. This means we need to borrow + // the reference that we create for the arm. + // * So we eagerly create the reference for the arm and then take a + // reference to that. + if let Some(guard) = guard { + let tcx = self.tcx; + let bindings = parent_bindings + .iter() + .flat_map(|(bindings, _)| bindings) + .chain(&candidate.bindings); + + self.bind_matched_candidate_for_guard(block, schedule_drops, bindings.clone()); + let guard_frame = GuardFrame { + locals: bindings.map(|b| GuardFrameLocal::new(b.var_id, b.binding_mode)).collect(), + }; + debug!("entering guard building context: {:?}", guard_frame); + self.guard_context.push(guard_frame); + + let re_erased = tcx.lifetimes.re_erased; + let scrutinee_source_info = self.source_info(scrutinee_span); + for &(place, temp) in fake_borrows { + let borrow = Rvalue::Ref(re_erased, BorrowKind::Shallow, place); + self.cfg.push_assign(block, scrutinee_source_info, Place::from(temp), borrow); + } + + let arm_span = arm_span.unwrap(); + let match_scope = match_scope.unwrap(); + let mut guard_span = rustc_span::DUMMY_SP; + + let (post_guard_block, otherwise_post_guard_block) = + self.in_if_then_scope(match_scope, |this| match *guard { + Guard::If(e) => { + let e = &this.thir[e]; + guard_span = e.span; + this.then_else_break( + block, + e, + None, + match_scope, + this.source_info(arm_span), + ) + } + Guard::IfLet(ref pat, scrutinee) => { + let s = &this.thir[scrutinee]; + guard_span = s.span; + this.lower_let_expr(block, s, pat, match_scope, None, arm_span) + } + }); + + let source_info = self.source_info(guard_span); + let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span)); + let guard_frame = self.guard_context.pop().unwrap(); + debug!("Exiting guard building context with locals: {:?}", guard_frame); + + for &(_, temp) in fake_borrows { + let cause = FakeReadCause::ForMatchGuard; + self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(temp)); + } + + let otherwise_block = candidate.otherwise_block.unwrap_or_else(|| { + let unreachable = self.cfg.start_new_block(); + self.cfg.terminate(unreachable, source_info, TerminatorKind::Unreachable); + unreachable + }); + self.false_edges( + otherwise_post_guard_block, + otherwise_block, + candidate.next_candidate_pre_binding_block, + source_info, + ); + + // We want to ensure that the matched candidates are bound + // after we have confirmed this candidate *and* any + // associated guard; Binding them on `block` is too soon, + // because that would be before we've checked the result + // from the guard. + // + // But binding them on the arm is *too late*, because + // then all of the candidates for a single arm would be + // bound in the same place, that would cause a case like: + // + // ```rust + // match (30, 2) { + // (mut x, 1) | (2, mut x) if { true } => { ... } + // ... // ^^^^^^^ (this is `arm_block`) + // } + // ``` + // + // would yield an `arm_block` something like: + // + // ``` + // StorageLive(_4); // _4 is `x` + // _4 = &mut (_1.0: i32); // this is handling `(mut x, 1)` case + // _4 = &mut (_1.1: i32); // this is handling `(2, mut x)` case + // ``` + // + // and that is clearly not correct. + let by_value_bindings = parent_bindings + .iter() + .flat_map(|(bindings, _)| bindings) + .chain(&candidate.bindings) + .filter(|binding| matches!(binding.binding_mode, BindingMode::ByValue)); + // Read all of the by reference bindings to ensure that the + // place they refer to can't be modified by the guard. + for binding in by_value_bindings.clone() { + let local_id = self.var_local_id(binding.var_id, RefWithinGuard); + let cause = FakeReadCause::ForGuardBinding; + self.cfg.push_fake_read(post_guard_block, guard_end, cause, Place::from(local_id)); + } + assert!(schedule_drops, "patterns with guards must schedule drops"); + self.bind_matched_candidate_for_arm_body(post_guard_block, true, by_value_bindings); + + post_guard_block + } else { + // (Here, it is not too early to bind the matched + // candidate on `block`, because there is no guard result + // that we have to inspect before we bind them.) + self.bind_matched_candidate_for_arm_body( + block, + schedule_drops, + parent_bindings + .iter() + .flat_map(|(bindings, _)| bindings) + .chain(&candidate.bindings), + ); + block + } + } + + /// Append `AscribeUserType` statements onto the end of `block` + /// for each ascription + fn ascribe_types( + &mut self, + block: BasicBlock, + ascriptions: impl IntoIterator>, + ) { + for ascription in ascriptions { + let source_info = self.source_info(ascription.annotation.span); + + let base = self.canonical_user_type_annotations.push(ascription.annotation); + self.cfg.push( + block, + Statement { + source_info, + kind: StatementKind::AscribeUserType( + Box::new(( + ascription.source, + UserTypeProjection { base, projs: Vec::new() }, + )), + ascription.variance, + ), + }, + ); + } + } + + fn bind_matched_candidate_for_guard<'b>( + &mut self, + block: BasicBlock, + schedule_drops: bool, + bindings: impl IntoIterator>, + ) where + 'tcx: 'b, + { + debug!("bind_matched_candidate_for_guard(block={:?})", block); + + // Assign each of the bindings. Since we are binding for a + // guard expression, this will never trigger moves out of the + // candidate. + let re_erased = self.tcx.lifetimes.re_erased; + for binding in bindings { + debug!("bind_matched_candidate_for_guard(binding={:?})", binding); + let source_info = self.source_info(binding.span); + + // For each pattern ident P of type T, `ref_for_guard` is + // a reference R: &T pointing to the location matched by + // the pattern, and every occurrence of P within a guard + // denotes *R. + let ref_for_guard = self.storage_live_binding( + block, + binding.var_id, + binding.span, + RefWithinGuard, + schedule_drops, + ); + match binding.binding_mode { + BindingMode::ByValue => { + let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, binding.source); + self.cfg.push_assign(block, source_info, ref_for_guard, rvalue); + } + BindingMode::ByRef(borrow_kind) => { + let value_for_arm = self.storage_live_binding( + block, + binding.var_id, + binding.span, + OutsideGuard, + schedule_drops, + ); + + let rvalue = Rvalue::Ref(re_erased, borrow_kind, binding.source); + self.cfg.push_assign(block, source_info, value_for_arm, rvalue); + let rvalue = Rvalue::Ref(re_erased, BorrowKind::Shared, value_for_arm); + self.cfg.push_assign(block, source_info, ref_for_guard, rvalue); + } + } + } + } + + fn bind_matched_candidate_for_arm_body<'b>( + &mut self, + block: BasicBlock, + schedule_drops: bool, + bindings: impl IntoIterator>, + ) where + 'tcx: 'b, + { + debug!("bind_matched_candidate_for_arm_body(block={:?})", block); + + let re_erased = self.tcx.lifetimes.re_erased; + // Assign each of the bindings. This may trigger moves out of the candidate. + for binding in bindings { + let source_info = self.source_info(binding.span); + let local = self.storage_live_binding( + block, + binding.var_id, + binding.span, + OutsideGuard, + schedule_drops, + ); + if schedule_drops { + self.schedule_drop_for_binding(binding.var_id, binding.span, OutsideGuard); + } + let rvalue = match binding.binding_mode { + BindingMode::ByValue => Rvalue::Use(self.consume_by_copy_or_move(binding.source)), + BindingMode::ByRef(borrow_kind) => { + Rvalue::Ref(re_erased, borrow_kind, binding.source) + } + }; + self.cfg.push_assign(block, source_info, local, rvalue); + } + } + + /// Each binding (`ref mut var`/`ref var`/`mut var`/`var`, where the bound + /// `var` has type `T` in the arm body) in a pattern maps to 2 locals. The + /// first local is a binding for occurrences of `var` in the guard, which + /// will have type `&T`. The second local is a binding for occurrences of + /// `var` in the arm body, which will have type `T`. + fn declare_binding( + &mut self, + source_info: SourceInfo, + visibility_scope: SourceScope, + mutability: Mutability, + name: Symbol, + mode: BindingMode, + var_id: LocalVarId, + var_ty: Ty<'tcx>, + user_ty: UserTypeProjections, + has_guard: ArmHasGuard, + opt_match_place: Option<(Option>, Span)>, + pat_span: Span, + ) { + debug!( + "declare_binding(var_id={:?}, name={:?}, mode={:?}, var_ty={:?}, \ + visibility_scope={:?}, source_info={:?})", + var_id, name, mode, var_ty, visibility_scope, source_info + ); + + let tcx = self.tcx; + let debug_source_info = SourceInfo { span: source_info.span, scope: visibility_scope }; + let binding_mode = match mode { + BindingMode::ByValue => ty::BindingMode::BindByValue(mutability), + BindingMode::ByRef(_) => ty::BindingMode::BindByReference(mutability), + }; + debug!("declare_binding: user_ty={:?}", user_ty); + let local = LocalDecl::<'tcx> { + mutability, + ty: var_ty, + user_ty: if user_ty.is_empty() { None } else { Some(Box::new(user_ty)) }, + source_info, + internal: false, + is_block_tail: None, + local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( + VarBindingForm { + binding_mode, + // hypothetically, `visit_primary_bindings` could try to unzip + // an outermost hir::Ty as we descend, matching up + // idents in pat; but complex w/ unclear UI payoff. + // Instead, just abandon providing diagnostic info. + opt_ty_info: None, + opt_match_place, + pat_span, + }, + ))))), + }; + let for_arm_body = self.local_decls.push(local); + self.var_debug_info.push(VarDebugInfo { + name, + source_info: debug_source_info, + value: VarDebugInfoContents::Place(for_arm_body.into()), + }); + let locals = if has_guard.0 { + let ref_for_guard = self.local_decls.push(LocalDecl::<'tcx> { + // This variable isn't mutated but has a name, so has to be + // immutable to avoid the unused mut lint. + mutability: Mutability::Not, + ty: tcx.mk_imm_ref(tcx.lifetimes.re_erased, var_ty), + user_ty: None, + source_info, + internal: false, + is_block_tail: None, + local_info: Some(Box::new(LocalInfo::User(ClearCrossCrate::Set( + BindingForm::RefForGuard, + )))), + }); + self.var_debug_info.push(VarDebugInfo { + name, + source_info: debug_source_info, + value: VarDebugInfoContents::Place(ref_for_guard.into()), + }); + LocalsForNode::ForGuard { ref_for_guard, for_arm_body } + } else { + LocalsForNode::One(for_arm_body) + }; + debug!("declare_binding: vars={:?}", locals); + self.var_indices.insert(var_id, locals); + } + + pub(crate) fn ast_let_else( + &mut self, + mut block: BasicBlock, + init: &Expr<'tcx>, + initializer_span: Span, + else_block: &Block, + visibility_scope: Option, + remainder_scope: region::Scope, + remainder_span: Span, + pattern: &Pat<'tcx>, + ) -> BlockAnd<()> { + let (matching, failure) = self.in_if_then_scope(remainder_scope, |this| { + let scrutinee = unpack!(block = this.lower_scrutinee(block, init, initializer_span)); + let pat = Pat { ty: init.ty, span: else_block.span, kind: Box::new(PatKind::Wild) }; + let mut wildcard = Candidate::new(scrutinee.clone(), &pat, false); + this.declare_bindings( + visibility_scope, + remainder_span, + pattern, + ArmHasGuard(false), + Some((None, initializer_span)), + ); + let mut candidate = Candidate::new(scrutinee.clone(), pattern, false); + let fake_borrow_temps = this.lower_match_tree( + block, + initializer_span, + pattern.span, + false, + &mut [&mut candidate, &mut wildcard], + ); + // This block is for the matching case + let matching = this.bind_pattern( + this.source_info(pattern.span), + candidate, + None, + &fake_borrow_temps, + initializer_span, + None, + None, + None, + ); + // This block is for the failure case + let failure = this.bind_pattern( + this.source_info(else_block.span), + wildcard, + None, + &fake_borrow_temps, + initializer_span, + None, + None, + None, + ); + this.break_for_else(failure, remainder_scope, this.source_info(initializer_span)); + matching.unit() + }); + + // This place is not really used because this destination place + // should never be used to take values at the end of the failure + // block. + let dummy_place = Place { local: RETURN_PLACE, projection: ty::List::empty() }; + let failure_block; + unpack!( + failure_block = self.ast_block( + dummy_place, + failure, + else_block, + self.source_info(else_block.span), + ) + ); + self.cfg.terminate( + failure_block, + self.source_info(else_block.span), + TerminatorKind::Unreachable, + ); + matching.unit() + } +} diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs new file mode 100644 index 000000000..c62989041 --- /dev/null +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -0,0 +1,318 @@ +//! Simplifying Candidates +//! +//! *Simplifying* a match pair `place @ pattern` means breaking it down +//! into bindings or other, simpler match pairs. For example: +//! +//! - `place @ (P1, P2)` can be simplified to `[place.0 @ P1, place.1 @ P2]` +//! - `place @ x` can be simplified to `[]` by binding `x` to `place` +//! +//! The `simplify_candidate` routine just repeatedly applies these +//! sort of simplifications until there is nothing left to +//! simplify. Match pairs cannot be simplified if they require some +//! sort of test: for example, testing which variant an enum is, or +//! testing a value against a constant. + +use crate::build::expr::as_place::PlaceBuilder; +use crate::build::matches::{Ascription, Binding, Candidate, MatchPair}; +use crate::build::Builder; +use rustc_hir::RangeEnd; +use rustc_middle::thir::{self, *}; +use rustc_middle::ty; +use rustc_middle::ty::layout::IntegerExt; +use rustc_target::abi::{Integer, Size}; + +use std::mem; + +impl<'a, 'tcx> Builder<'a, 'tcx> { + /// Simplify a candidate so that all match pairs require a test. + /// + /// This method will also split a candidate, in which the only + /// match-pair is an or-pattern, into multiple candidates. + /// This is so that + /// + /// match x { + /// 0 | 1 => { ... }, + /// 2 | 3 => { ... }, + /// } + /// + /// only generates a single switch. If this happens this method returns + /// `true`. + pub(super) fn simplify_candidate<'pat>( + &mut self, + candidate: &mut Candidate<'pat, 'tcx>, + ) -> bool { + // repeatedly simplify match pairs until fixed point is reached + debug!(?candidate, "simplify_candidate"); + + // existing_bindings and new_bindings exists to keep the semantics in order. + // Reversing the binding order for bindings after `@` changes the binding order in places + // it shouldn't be changed, for example `let (Some(a), Some(b)) = (x, y)` + // + // To avoid this, the binding occurs in the following manner: + // * the bindings for one iteration of the following loop occurs in order (i.e. left to + // right) + // * the bindings from the previous iteration of the loop is prepended to the bindings from + // the current iteration (in the implementation this is done by mem::swap and extend) + // * after all iterations, these new bindings are then appended to the bindings that were + // preexisting (i.e. `candidate.binding` when the function was called). + // + // example: + // candidate.bindings = [1, 2, 3] + // binding in iter 1: [4, 5] + // binding in iter 2: [6, 7] + // + // final binding: [1, 2, 3, 6, 7, 4, 5] + let mut existing_bindings = mem::take(&mut candidate.bindings); + let mut new_bindings = Vec::new(); + loop { + let match_pairs = mem::take(&mut candidate.match_pairs); + + if let [MatchPair { pattern: Pat { kind: box PatKind::Or { pats }, .. }, place }] = + &*match_pairs + { + existing_bindings.extend_from_slice(&new_bindings); + mem::swap(&mut candidate.bindings, &mut existing_bindings); + candidate.subcandidates = + self.create_or_subcandidates(candidate, place.clone(), pats); + return true; + } + + let mut changed = false; + for match_pair in match_pairs { + match self.simplify_match_pair(match_pair, candidate) { + Ok(()) => { + changed = true; + } + Err(match_pair) => { + candidate.match_pairs.push(match_pair); + } + } + } + // Avoid issue #69971: the binding order should be right to left if there are more + // bindings after `@` to please the borrow checker + // Ex + // struct NonCopyStruct { + // copy_field: u32, + // } + // + // fn foo1(x: NonCopyStruct) { + // let y @ NonCopyStruct { copy_field: z } = x; + // // the above should turn into + // let z = x.copy_field; + // let y = x; + // } + candidate.bindings.extend_from_slice(&new_bindings); + mem::swap(&mut candidate.bindings, &mut new_bindings); + candidate.bindings.clear(); + + if !changed { + existing_bindings.extend_from_slice(&new_bindings); + mem::swap(&mut candidate.bindings, &mut existing_bindings); + // Move or-patterns to the end, because they can result in us + // creating additional candidates, so we want to test them as + // late as possible. + candidate + .match_pairs + .sort_by_key(|pair| matches!(*pair.pattern.kind, PatKind::Or { .. })); + debug!(simplified = ?candidate, "simplify_candidate"); + return false; // if we were not able to simplify any, done. + } + } + } + + /// Given `candidate` that has a single or-pattern for its match-pairs, + /// creates a fresh candidate for each of its input subpatterns passed via + /// `pats`. + fn create_or_subcandidates<'pat>( + &mut self, + candidate: &Candidate<'pat, 'tcx>, + place: PlaceBuilder<'tcx>, + pats: &'pat [Pat<'tcx>], + ) -> Vec> { + pats.iter() + .map(|pat| { + let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard); + self.simplify_candidate(&mut candidate); + candidate + }) + .collect() + } + + /// Tries to simplify `match_pair`, returning `Ok(())` if + /// successful. If successful, new match pairs and bindings will + /// have been pushed into the candidate. If no simplification is + /// possible, `Err` is returned and no changes are made to + /// candidate. + fn simplify_match_pair<'pat>( + &mut self, + match_pair: MatchPair<'pat, 'tcx>, + candidate: &mut Candidate<'pat, 'tcx>, + ) -> Result<(), MatchPair<'pat, 'tcx>> { + let tcx = self.tcx; + match *match_pair.pattern.kind { + PatKind::AscribeUserType { + ref subpattern, + ascription: thir::Ascription { ref annotation, variance }, + } => { + // Apply the type ascription to the value at `match_pair.place`, which is the + if let Ok(place_resolved) = + match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results) + { + candidate.ascriptions.push(Ascription { + annotation: annotation.clone(), + source: place_resolved.into_place(self.tcx, self.typeck_results), + variance, + }); + } + + candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern)); + + Ok(()) + } + + PatKind::Wild => { + // nothing left to do + Ok(()) + } + + PatKind::Binding { + name: _, + mutability: _, + mode, + var, + ty: _, + ref subpattern, + is_primary: _, + } => { + if let Ok(place_resolved) = + match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results) + { + candidate.bindings.push(Binding { + span: match_pair.pattern.span, + source: place_resolved.into_place(self.tcx, self.typeck_results), + var_id: var, + binding_mode: mode, + }); + } + + if let Some(subpattern) = subpattern.as_ref() { + // this is the `x @ P` case; have to keep matching against `P` now + candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern)); + } + + Ok(()) + } + + PatKind::Constant { .. } => { + // FIXME normalize patterns when possible + Err(match_pair) + } + + PatKind::Range(PatRange { lo, hi, end }) => { + let (range, bias) = match *lo.ty().kind() { + ty::Char => { + (Some(('\u{0000}' as u128, '\u{10FFFF}' as u128, Size::from_bits(32))), 0) + } + ty::Int(ity) => { + let size = Integer::from_int_ty(&tcx, ity).size(); + let max = size.truncate(u128::MAX); + let bias = 1u128 << (size.bits() - 1); + (Some((0, max, size)), bias) + } + ty::Uint(uty) => { + let size = Integer::from_uint_ty(&tcx, uty).size(); + let max = size.truncate(u128::MAX); + (Some((0, max, size)), 0) + } + _ => (None, 0), + }; + if let Some((min, max, sz)) = range { + // We want to compare ranges numerically, but the order of the bitwise + // representation of signed integers does not match their numeric order. Thus, + // to correct the ordering, we need to shift the range of signed integers to + // correct the comparison. This is achieved by XORing with a bias (see + // pattern/_match.rs for another pertinent example of this pattern). + // + // Also, for performance, it's important to only do the second `try_to_bits` if + // necessary. + let lo = lo.try_to_bits(sz).unwrap() ^ bias; + if lo <= min { + let hi = hi.try_to_bits(sz).unwrap() ^ bias; + if hi > max || hi == max && end == RangeEnd::Included { + // Irrefutable pattern match. + return Ok(()); + } + } + } + Err(match_pair) + } + + PatKind::Slice { ref prefix, ref slice, ref suffix } => { + if prefix.is_empty() && slice.is_some() && suffix.is_empty() { + // irrefutable + self.prefix_slice_suffix( + &mut candidate.match_pairs, + &match_pair.place, + prefix, + slice.as_ref(), + suffix, + ); + Ok(()) + } else { + Err(match_pair) + } + } + + PatKind::Variant { adt_def, substs, variant_index, ref subpatterns } => { + let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| { + i == variant_index || { + self.tcx.features().exhaustive_patterns + && !v + .uninhabited_from( + self.tcx, + substs, + adt_def.adt_kind(), + self.param_env, + ) + .is_empty() + } + }) && (adt_def.did().is_local() + || !adt_def.is_variant_list_non_exhaustive()); + if irrefutable { + let place_builder = match_pair.place.downcast(adt_def, variant_index); + candidate + .match_pairs + .extend(self.field_match_pairs(place_builder, subpatterns)); + Ok(()) + } else { + Err(match_pair) + } + } + + PatKind::Array { ref prefix, ref slice, ref suffix } => { + self.prefix_slice_suffix( + &mut candidate.match_pairs, + &match_pair.place, + prefix, + slice.as_ref(), + suffix, + ); + Ok(()) + } + + PatKind::Leaf { ref subpatterns } => { + // tuple struct, match subpats (if any) + candidate.match_pairs.extend(self.field_match_pairs(match_pair.place, subpatterns)); + Ok(()) + } + + PatKind::Deref { ref subpattern } => { + let place_builder = match_pair.place.deref(); + candidate.match_pairs.push(MatchPair::new(place_builder, subpattern)); + Ok(()) + } + + PatKind::Or { .. } => Err(match_pair), + } + } +} diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs new file mode 100644 index 000000000..598da80c5 --- /dev/null +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -0,0 +1,837 @@ +// Testing candidates +// +// After candidates have been simplified, the only match pairs that +// remain are those that require some sort of test. The functions here +// identify what tests are needed, perform the tests, and then filter +// the candidates based on the result. + +use crate::build::expr::as_place::PlaceBuilder; +use crate::build::matches::{Candidate, MatchPair, Test, TestKind}; +use crate::build::Builder; +use crate::thir::pattern::compare_const_vals; +use rustc_data_structures::fx::FxIndexMap; +use rustc_hir::{LangItem, RangeEnd}; +use rustc_index::bit_set::BitSet; +use rustc_middle::mir::*; +use rustc_middle::thir::*; +use rustc_middle::ty::subst::{GenericArg, Subst}; +use rustc_middle::ty::util::IntTypeExt; +use rustc_middle::ty::{self, adjustment::PointerCast, Ty, TyCtxt}; +use rustc_span::def_id::DefId; +use rustc_span::symbol::{sym, Symbol}; +use rustc_span::Span; +use rustc_target::abi::VariantIdx; + +use std::cmp::Ordering; + +impl<'a, 'tcx> Builder<'a, 'tcx> { + /// Identifies what test is needed to decide if `match_pair` is applicable. + /// + /// It is a bug to call this with a not-fully-simplified pattern. + pub(super) fn test<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> Test<'tcx> { + match *match_pair.pattern.kind { + PatKind::Variant { adt_def, substs: _, variant_index: _, subpatterns: _ } => Test { + span: match_pair.pattern.span, + kind: TestKind::Switch { + adt_def, + variants: BitSet::new_empty(adt_def.variants().len()), + }, + }, + + PatKind::Constant { .. } if is_switch_ty(match_pair.pattern.ty) => { + // For integers, we use a `SwitchInt` match, which allows + // us to handle more cases. + Test { + span: match_pair.pattern.span, + kind: TestKind::SwitchInt { + switch_ty: match_pair.pattern.ty, + + // these maps are empty to start; cases are + // added below in add_cases_to_switch + options: Default::default(), + }, + } + } + + PatKind::Constant { value } => Test { + span: match_pair.pattern.span, + kind: TestKind::Eq { value, ty: match_pair.pattern.ty }, + }, + + PatKind::Range(range) => { + assert_eq!(range.lo.ty(), match_pair.pattern.ty); + assert_eq!(range.hi.ty(), match_pair.pattern.ty); + Test { span: match_pair.pattern.span, kind: TestKind::Range(range) } + } + + PatKind::Slice { ref prefix, ref slice, ref suffix } => { + let len = prefix.len() + suffix.len(); + let op = if slice.is_some() { BinOp::Ge } else { BinOp::Eq }; + Test { span: match_pair.pattern.span, kind: TestKind::Len { len: len as u64, op } } + } + + PatKind::Or { .. } => bug!("or-patterns should have already been handled"), + + PatKind::AscribeUserType { .. } + | PatKind::Array { .. } + | PatKind::Wild + | PatKind::Binding { .. } + | PatKind::Leaf { .. } + | PatKind::Deref { .. } => self.error_simplifyable(match_pair), + } + } + + pub(super) fn add_cases_to_switch<'pat>( + &mut self, + test_place: &PlaceBuilder<'tcx>, + candidate: &Candidate<'pat, 'tcx>, + switch_ty: Ty<'tcx>, + options: &mut FxIndexMap, u128>, + ) -> bool { + let Some(match_pair) = candidate.match_pairs.iter().find(|mp| mp.place == *test_place) else { + return false; + }; + + match *match_pair.pattern.kind { + PatKind::Constant { value } => { + options + .entry(value) + .or_insert_with(|| value.eval_bits(self.tcx, self.param_env, switch_ty)); + true + } + PatKind::Variant { .. } => { + panic!("you should have called add_variants_to_switch instead!"); + } + PatKind::Range(range) => { + // Check that none of the switch values are in the range. + self.values_not_contained_in_range(range, options).unwrap_or(false) + } + PatKind::Slice { .. } + | PatKind::Array { .. } + | PatKind::Wild + | PatKind::Or { .. } + | PatKind::Binding { .. } + | PatKind::AscribeUserType { .. } + | PatKind::Leaf { .. } + | PatKind::Deref { .. } => { + // don't know how to add these patterns to a switch + false + } + } + } + + pub(super) fn add_variants_to_switch<'pat>( + &mut self, + test_place: &PlaceBuilder<'tcx>, + candidate: &Candidate<'pat, 'tcx>, + variants: &mut BitSet, + ) -> bool { + let Some(match_pair) = candidate.match_pairs.iter().find(|mp| mp.place == *test_place) else { + return false; + }; + + match *match_pair.pattern.kind { + PatKind::Variant { adt_def: _, variant_index, .. } => { + // We have a pattern testing for variant `variant_index` + // set the corresponding index to true + variants.insert(variant_index); + true + } + _ => { + // don't know how to add these patterns to a switch + false + } + } + } + + pub(super) fn perform_test( + &mut self, + match_start_span: Span, + scrutinee_span: Span, + block: BasicBlock, + place_builder: PlaceBuilder<'tcx>, + test: &Test<'tcx>, + make_target_blocks: impl FnOnce(&mut Self) -> Vec, + ) { + let place: Place<'tcx>; + if let Ok(test_place_builder) = + place_builder.try_upvars_resolved(self.tcx, self.typeck_results) + { + place = test_place_builder.into_place(self.tcx, self.typeck_results); + } else { + return; + } + debug!( + "perform_test({:?}, {:?}: {:?}, {:?})", + block, + place, + place.ty(&self.local_decls, self.tcx), + test + ); + + let source_info = self.source_info(test.span); + match test.kind { + TestKind::Switch { adt_def, ref variants } => { + let target_blocks = make_target_blocks(self); + // Variants is a BitVec of indexes into adt_def.variants. + let num_enum_variants = adt_def.variants().len(); + debug_assert_eq!(target_blocks.len(), num_enum_variants + 1); + let otherwise_block = *target_blocks.last().unwrap(); + let tcx = self.tcx; + let switch_targets = SwitchTargets::new( + adt_def.discriminants(tcx).filter_map(|(idx, discr)| { + if variants.contains(idx) { + debug_assert_ne!( + target_blocks[idx.index()], + otherwise_block, + "no canididates for tested discriminant: {:?}", + discr, + ); + Some((discr.val, target_blocks[idx.index()])) + } else { + debug_assert_eq!( + target_blocks[idx.index()], + otherwise_block, + "found canididates for untested discriminant: {:?}", + discr, + ); + None + } + }), + otherwise_block, + ); + debug!("num_enum_variants: {}, variants: {:?}", num_enum_variants, variants); + let discr_ty = adt_def.repr().discr_type().to_ty(tcx); + let discr = self.temp(discr_ty, test.span); + self.cfg.push_assign( + block, + self.source_info(scrutinee_span), + discr, + Rvalue::Discriminant(place), + ); + self.cfg.terminate( + block, + self.source_info(match_start_span), + TerminatorKind::SwitchInt { + discr: Operand::Move(discr), + switch_ty: discr_ty, + targets: switch_targets, + }, + ); + } + + TestKind::SwitchInt { switch_ty, ref options } => { + let target_blocks = make_target_blocks(self); + let terminator = if *switch_ty.kind() == ty::Bool { + assert!(!options.is_empty() && options.len() <= 2); + let [first_bb, second_bb] = *target_blocks else { + bug!("`TestKind::SwitchInt` on `bool` should have two targets") + }; + let (true_bb, false_bb) = match options[0] { + 1 => (first_bb, second_bb), + 0 => (second_bb, first_bb), + v => span_bug!(test.span, "expected boolean value but got {:?}", v), + }; + TerminatorKind::if_(self.tcx, Operand::Copy(place), true_bb, false_bb) + } else { + // The switch may be inexhaustive so we have a catch all block + debug_assert_eq!(options.len() + 1, target_blocks.len()); + let otherwise_block = *target_blocks.last().unwrap(); + let switch_targets = SwitchTargets::new( + options.values().copied().zip(target_blocks), + otherwise_block, + ); + TerminatorKind::SwitchInt { + discr: Operand::Copy(place), + switch_ty, + targets: switch_targets, + } + }; + self.cfg.terminate(block, self.source_info(match_start_span), terminator); + } + + TestKind::Eq { value, ty } => { + if !ty.is_scalar() { + // Use `PartialEq::eq` instead of `BinOp::Eq` + // (the binop can only handle primitives) + self.non_scalar_compare( + block, + make_target_blocks, + source_info, + value, + place, + ty, + ); + } else if let [success, fail] = *make_target_blocks(self) { + assert_eq!(value.ty(), ty); + let expect = self.literal_operand(test.span, value); + let val = Operand::Copy(place); + self.compare(block, success, fail, source_info, BinOp::Eq, expect, val); + } else { + bug!("`TestKind::Eq` should have two target blocks"); + } + } + + TestKind::Range(PatRange { lo, hi, ref end }) => { + let lower_bound_success = self.cfg.start_new_block(); + let target_blocks = make_target_blocks(self); + + // Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons. + let lo = self.literal_operand(test.span, lo); + let hi = self.literal_operand(test.span, hi); + let val = Operand::Copy(place); + + let [success, fail] = *target_blocks else { + bug!("`TestKind::Range` should have two target blocks"); + }; + self.compare( + block, + lower_bound_success, + fail, + source_info, + BinOp::Le, + lo, + val.clone(), + ); + let op = match *end { + RangeEnd::Included => BinOp::Le, + RangeEnd::Excluded => BinOp::Lt, + }; + self.compare(lower_bound_success, success, fail, source_info, op, val, hi); + } + + TestKind::Len { len, op } => { + let target_blocks = make_target_blocks(self); + + let usize_ty = self.tcx.types.usize; + let actual = self.temp(usize_ty, test.span); + + // actual = len(place) + self.cfg.push_assign(block, source_info, actual, Rvalue::Len(place)); + + // expected = + let expected = self.push_usize(block, source_info, len); + + let [true_bb, false_bb] = *target_blocks else { + bug!("`TestKind::Len` should have two target blocks"); + }; + // result = actual == expected OR result = actual < expected + // branch based on result + self.compare( + block, + true_bb, + false_bb, + source_info, + op, + Operand::Move(actual), + Operand::Move(expected), + ); + } + } + } + + /// Compare using the provided built-in comparison operator + fn compare( + &mut self, + block: BasicBlock, + success_block: BasicBlock, + fail_block: BasicBlock, + source_info: SourceInfo, + op: BinOp, + left: Operand<'tcx>, + right: Operand<'tcx>, + ) { + let bool_ty = self.tcx.types.bool; + let result = self.temp(bool_ty, source_info.span); + + // result = op(left, right) + self.cfg.push_assign( + block, + source_info, + result, + Rvalue::BinaryOp(op, Box::new((left, right))), + ); + + // branch based on result + self.cfg.terminate( + block, + source_info, + TerminatorKind::if_(self.tcx, Operand::Move(result), success_block, fail_block), + ); + } + + /// Compare two `&T` values using `::eq` + fn non_scalar_compare( + &mut self, + block: BasicBlock, + make_target_blocks: impl FnOnce(&mut Self) -> Vec, + source_info: SourceInfo, + value: ConstantKind<'tcx>, + place: Place<'tcx>, + mut ty: Ty<'tcx>, + ) { + let mut expect = self.literal_operand(source_info.span, value); + let mut val = Operand::Copy(place); + + // If we're using `b"..."` as a pattern, we need to insert an + // unsizing coercion, as the byte string has the type `&[u8; N]`. + // + // We want to do this even when the scrutinee is a reference to an + // array, so we can call `<[u8]>::eq` rather than having to find an + // `<[u8; N]>::eq`. + let unsize = |ty: Ty<'tcx>| match ty.kind() { + ty::Ref(region, rty, _) => match rty.kind() { + ty::Array(inner_ty, n) => Some((region, inner_ty, n)), + _ => None, + }, + _ => None, + }; + let opt_ref_ty = unsize(ty); + let opt_ref_test_ty = unsize(value.ty()); + match (opt_ref_ty, opt_ref_test_ty) { + // nothing to do, neither is an array + (None, None) => {} + (Some((region, elem_ty, _)), _) | (None, Some((region, elem_ty, _))) => { + let tcx = self.tcx; + // make both a slice + ty = tcx.mk_imm_ref(*region, tcx.mk_slice(*elem_ty)); + if opt_ref_ty.is_some() { + let temp = self.temp(ty, source_info.span); + self.cfg.push_assign( + block, + source_info, + temp, + Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), val, ty), + ); + val = Operand::Move(temp); + } + if opt_ref_test_ty.is_some() { + let slice = self.temp(ty, source_info.span); + self.cfg.push_assign( + block, + source_info, + slice, + Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), expect, ty), + ); + expect = Operand::Move(slice); + } + } + } + + let ty::Ref(_, deref_ty, _) = *ty.kind() else { + bug!("non_scalar_compare called on non-reference type: {}", ty); + }; + + let eq_def_id = self.tcx.require_lang_item(LangItem::PartialEq, None); + let method = trait_method(self.tcx, eq_def_id, sym::eq, deref_ty, &[deref_ty.into()]); + + let bool_ty = self.tcx.types.bool; + let eq_result = self.temp(bool_ty, source_info.span); + let eq_block = self.cfg.start_new_block(); + self.cfg.terminate( + block, + source_info, + TerminatorKind::Call { + func: Operand::Constant(Box::new(Constant { + span: source_info.span, + + // FIXME(#54571): This constant comes from user input (a + // constant in a pattern). Are there forms where users can add + // type annotations here? For example, an associated constant? + // Need to experiment. + user_ty: None, + + literal: method, + })), + args: vec![val, expect], + destination: eq_result, + target: Some(eq_block), + cleanup: None, + from_hir_call: false, + fn_span: source_info.span, + }, + ); + self.diverge_from(block); + + let [success_block, fail_block] = *make_target_blocks(self) else { + bug!("`TestKind::Eq` should have two target blocks") + }; + // check the result + self.cfg.terminate( + eq_block, + source_info, + TerminatorKind::if_(self.tcx, Operand::Move(eq_result), success_block, fail_block), + ); + } + + /// Given that we are performing `test` against `test_place`, this job + /// sorts out what the status of `candidate` will be after the test. See + /// `test_candidates` for the usage of this function. The returned index is + /// the index that this candidate should be placed in the + /// `target_candidates` vec. The candidate may be modified to update its + /// `match_pairs`. + /// + /// So, for example, if this candidate is `x @ Some(P0)` and the `Test` is + /// a variant test, then we would modify the candidate to be `(x as + /// Option).0 @ P0` and return the index corresponding to the variant + /// `Some`. + /// + /// However, in some cases, the test may just not be relevant to candidate. + /// For example, suppose we are testing whether `foo.x == 22`, but in one + /// match arm we have `Foo { x: _, ... }`... in that case, the test for + /// what value `x` has has no particular relevance to this candidate. In + /// such cases, this function just returns None without doing anything. + /// This is used by the overall `match_candidates` algorithm to structure + /// the match as a whole. See `match_candidates` for more details. + /// + /// FIXME(#29623). In some cases, we have some tricky choices to make. for + /// example, if we are testing that `x == 22`, but the candidate is `x @ + /// 13..55`, what should we do? In the event that the test is true, we know + /// that the candidate applies, but in the event of false, we don't know + /// that it *doesn't* apply. For now, we return false, indicate that the + /// test does not apply to this candidate, but it might be we can get + /// tighter match code if we do something a bit different. + pub(super) fn sort_candidate<'pat>( + &mut self, + test_place: &PlaceBuilder<'tcx>, + test: &Test<'tcx>, + candidate: &mut Candidate<'pat, 'tcx>, + ) -> Option { + // Find the match_pair for this place (if any). At present, + // afaik, there can be at most one. (In the future, if we + // adopted a more general `@` operator, there might be more + // than one, but it'd be very unusual to have two sides that + // both require tests; you'd expect one side to be simplified + // away.) + let (match_pair_index, match_pair) = + candidate.match_pairs.iter().enumerate().find(|&(_, mp)| mp.place == *test_place)?; + + match (&test.kind, &*match_pair.pattern.kind) { + // If we are performing a variant switch, then this + // informs variant patterns, but nothing else. + ( + &TestKind::Switch { adt_def: tested_adt_def, .. }, + &PatKind::Variant { adt_def, variant_index, ref subpatterns, .. }, + ) => { + assert_eq!(adt_def, tested_adt_def); + self.candidate_after_variant_switch( + match_pair_index, + adt_def, + variant_index, + subpatterns, + candidate, + ); + Some(variant_index.as_usize()) + } + + (&TestKind::Switch { .. }, _) => None, + + // If we are performing a switch over integers, then this informs integer + // equality, but nothing else. + // + // FIXME(#29623) we could use PatKind::Range to rule + // things out here, in some cases. + ( + &TestKind::SwitchInt { switch_ty: _, ref options }, + &PatKind::Constant { ref value }, + ) if is_switch_ty(match_pair.pattern.ty) => { + let index = options.get_index_of(value).unwrap(); + self.candidate_without_match_pair(match_pair_index, candidate); + Some(index) + } + + (&TestKind::SwitchInt { switch_ty: _, ref options }, &PatKind::Range(range)) => { + let not_contained = + self.values_not_contained_in_range(range, options).unwrap_or(false); + + if not_contained { + // No switch values are contained in the pattern range, + // so the pattern can be matched only if this test fails. + let otherwise = options.len(); + Some(otherwise) + } else { + None + } + } + + (&TestKind::SwitchInt { .. }, _) => None, + + ( + &TestKind::Len { len: test_len, op: BinOp::Eq }, + &PatKind::Slice { ref prefix, ref slice, ref suffix }, + ) => { + let pat_len = (prefix.len() + suffix.len()) as u64; + match (test_len.cmp(&pat_len), slice) { + (Ordering::Equal, &None) => { + // on true, min_len = len = $actual_length, + // on false, len != $actual_length + self.candidate_after_slice_test( + match_pair_index, + candidate, + prefix, + slice.as_ref(), + suffix, + ); + Some(0) + } + (Ordering::Less, _) => { + // test_len < pat_len. If $actual_len = test_len, + // then $actual_len < pat_len and we don't have + // enough elements. + Some(1) + } + (Ordering::Equal | Ordering::Greater, &Some(_)) => { + // This can match both if $actual_len = test_len >= pat_len, + // and if $actual_len > test_len. We can't advance. + None + } + (Ordering::Greater, &None) => { + // test_len != pat_len, so if $actual_len = test_len, then + // $actual_len != pat_len. + Some(1) + } + } + } + + ( + &TestKind::Len { len: test_len, op: BinOp::Ge }, + &PatKind::Slice { ref prefix, ref slice, ref suffix }, + ) => { + // the test is `$actual_len >= test_len` + let pat_len = (prefix.len() + suffix.len()) as u64; + match (test_len.cmp(&pat_len), slice) { + (Ordering::Equal, &Some(_)) => { + // $actual_len >= test_len = pat_len, + // so we can match. + self.candidate_after_slice_test( + match_pair_index, + candidate, + prefix, + slice.as_ref(), + suffix, + ); + Some(0) + } + (Ordering::Less, _) | (Ordering::Equal, &None) => { + // test_len <= pat_len. If $actual_len < test_len, + // then it is also < pat_len, so the test passing is + // necessary (but insufficient). + Some(0) + } + (Ordering::Greater, &None) => { + // test_len > pat_len. If $actual_len >= test_len > pat_len, + // then we know we won't have a match. + Some(1) + } + (Ordering::Greater, &Some(_)) => { + // test_len < pat_len, and is therefore less + // strict. This can still go both ways. + None + } + } + } + + (&TestKind::Range(test), &PatKind::Range(pat)) => { + use std::cmp::Ordering::*; + + if test == pat { + self.candidate_without_match_pair(match_pair_index, candidate); + return Some(0); + } + + // For performance, it's important to only do the second + // `compare_const_vals` if necessary. + let no_overlap = if matches!( + (compare_const_vals(self.tcx, test.hi, pat.lo, self.param_env)?, test.end), + (Less, _) | (Equal, RangeEnd::Excluded) // test < pat + ) || matches!( + (compare_const_vals(self.tcx, test.lo, pat.hi, self.param_env)?, pat.end), + (Greater, _) | (Equal, RangeEnd::Excluded) // test > pat + ) { + Some(1) + } else { + None + }; + + // If the testing range does not overlap with pattern range, + // the pattern can be matched only if this test fails. + no_overlap + } + + (&TestKind::Range(range), &PatKind::Constant { value }) => { + if let Some(false) = self.const_range_contains(range, value) { + // `value` is not contained in the testing range, + // so `value` can be matched only if this test fails. + Some(1) + } else { + None + } + } + + (&TestKind::Range { .. }, _) => None, + + (&TestKind::Eq { .. } | &TestKind::Len { .. }, _) => { + // The call to `self.test(&match_pair)` below is not actually used to generate any + // MIR. Instead, we just want to compare with `test` (the parameter of the method) + // to see if it is the same. + // + // However, at this point we can still encounter or-patterns that were extracted + // from previous calls to `sort_candidate`, so we need to manually address that + // case to avoid panicking in `self.test()`. + if let PatKind::Or { .. } = &*match_pair.pattern.kind { + return None; + } + + // These are all binary tests. + // + // FIXME(#29623) we can be more clever here + let pattern_test = self.test(&match_pair); + if pattern_test.kind == test.kind { + self.candidate_without_match_pair(match_pair_index, candidate); + Some(0) + } else { + None + } + } + } + } + + fn candidate_without_match_pair( + &mut self, + match_pair_index: usize, + candidate: &mut Candidate<'_, 'tcx>, + ) { + candidate.match_pairs.remove(match_pair_index); + } + + fn candidate_after_slice_test<'pat>( + &mut self, + match_pair_index: usize, + candidate: &mut Candidate<'pat, 'tcx>, + prefix: &'pat [Pat<'tcx>], + opt_slice: Option<&'pat Pat<'tcx>>, + suffix: &'pat [Pat<'tcx>], + ) { + let removed_place = candidate.match_pairs.remove(match_pair_index).place; + self.prefix_slice_suffix( + &mut candidate.match_pairs, + &removed_place, + prefix, + opt_slice, + suffix, + ); + } + + fn candidate_after_variant_switch<'pat>( + &mut self, + match_pair_index: usize, + adt_def: ty::AdtDef<'tcx>, + variant_index: VariantIdx, + subpatterns: &'pat [FieldPat<'tcx>], + candidate: &mut Candidate<'pat, 'tcx>, + ) { + let match_pair = candidate.match_pairs.remove(match_pair_index); + + // So, if we have a match-pattern like `x @ Enum::Variant(P1, P2)`, + // we want to create a set of derived match-patterns like + // `(x as Variant).0 @ P1` and `(x as Variant).1 @ P1`. + let elem = + ProjectionElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index); + let downcast_place = match_pair.place.project(elem); // `(x as Variant)` + let consequent_match_pairs = subpatterns.iter().map(|subpattern| { + // e.g., `(x as Variant).0` + let place = downcast_place.clone().field(subpattern.field, subpattern.pattern.ty); + // e.g., `(x as Variant).0 @ P1` + MatchPair::new(place, &subpattern.pattern) + }); + + candidate.match_pairs.extend(consequent_match_pairs); + } + + fn error_simplifyable<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> ! { + span_bug!(match_pair.pattern.span, "simplifyable pattern found: {:?}", match_pair.pattern) + } + + fn const_range_contains( + &self, + range: PatRange<'tcx>, + value: ConstantKind<'tcx>, + ) -> Option { + use std::cmp::Ordering::*; + + // For performance, it's important to only do the second + // `compare_const_vals` if necessary. + Some( + matches!(compare_const_vals(self.tcx, range.lo, value, self.param_env)?, Less | Equal) + && matches!( + (compare_const_vals(self.tcx, value, range.hi, self.param_env)?, range.end), + (Less, _) | (Equal, RangeEnd::Included) + ), + ) + } + + fn values_not_contained_in_range( + &self, + range: PatRange<'tcx>, + options: &FxIndexMap, u128>, + ) -> Option { + for &val in options.keys() { + if self.const_range_contains(range, val)? { + return Some(false); + } + } + + Some(true) + } +} + +impl Test<'_> { + pub(super) fn targets(&self) -> usize { + match self.kind { + TestKind::Eq { .. } | TestKind::Range(_) | TestKind::Len { .. } => 2, + TestKind::Switch { adt_def, .. } => { + // While the switch that we generate doesn't test for all + // variants, we have a target for each variant and the + // otherwise case, and we make sure that all of the cases not + // specified have the same block. + adt_def.variants().len() + 1 + } + TestKind::SwitchInt { switch_ty, ref options, .. } => { + if switch_ty.is_bool() { + // `bool` is special cased in `perform_test` to always + // branch to two blocks. + 2 + } else { + options.len() + 1 + } + } + } + } +} + +fn is_switch_ty(ty: Ty<'_>) -> bool { + ty.is_integral() || ty.is_char() || ty.is_bool() +} + +fn trait_method<'tcx>( + tcx: TyCtxt<'tcx>, + trait_def_id: DefId, + method_name: Symbol, + self_ty: Ty<'tcx>, + params: &[GenericArg<'tcx>], +) -> ConstantKind<'tcx> { + let substs = tcx.mk_substs_trait(self_ty, params); + + // The unhygienic comparison here is acceptable because this is only + // used on known traits. + let item = tcx + .associated_items(trait_def_id) + .filter_by_name_unhygienic(method_name) + .find(|item| item.kind == ty::AssocKind::Fn) + .expect("trait method not found"); + + let method_ty = tcx.bound_type_of(item.def_id); + let method_ty = method_ty.subst(tcx, substs); + + ConstantKind::zero_sized(method_ty) +} diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs new file mode 100644 index 000000000..9a1e98d3b --- /dev/null +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -0,0 +1,109 @@ +use crate::build::expr::as_place::PlaceBuilder; +use crate::build::matches::MatchPair; +use crate::build::Builder; +use rustc_middle::mir::*; +use rustc_middle::thir::*; +use rustc_middle::ty; +use smallvec::SmallVec; +use std::convert::TryInto; + +impl<'a, 'tcx> Builder<'a, 'tcx> { + pub(crate) fn field_match_pairs<'pat>( + &mut self, + place: PlaceBuilder<'tcx>, + subpatterns: &'pat [FieldPat<'tcx>], + ) -> Vec> { + subpatterns + .iter() + .map(|fieldpat| { + let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty); + MatchPair::new(place, &fieldpat.pattern) + }) + .collect() + } + + pub(crate) fn prefix_slice_suffix<'pat>( + &mut self, + match_pairs: &mut SmallVec<[MatchPair<'pat, 'tcx>; 1]>, + place: &PlaceBuilder<'tcx>, + prefix: &'pat [Pat<'tcx>], + opt_slice: Option<&'pat Pat<'tcx>>, + suffix: &'pat [Pat<'tcx>], + ) { + let tcx = self.tcx; + let (min_length, exact_size) = if let Ok(place_resolved) = + place.clone().try_upvars_resolved(tcx, self.typeck_results) + { + match place_resolved + .into_place(tcx, self.typeck_results) + .ty(&self.local_decls, tcx) + .ty + .kind() + { + ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true), + _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false), + } + } else { + ((prefix.len() + suffix.len()).try_into().unwrap(), false) + }; + + match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| { + let elem = + ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false }; + let place = place.clone().project(elem); + MatchPair::new(place, subpattern) + })); + + if let Some(subslice_pat) = opt_slice { + let suffix_len = suffix.len() as u64; + let subslice = place.clone().project(ProjectionElem::Subslice { + from: prefix.len() as u64, + to: if exact_size { min_length - suffix_len } else { suffix_len }, + from_end: !exact_size, + }); + match_pairs.push(MatchPair::new(subslice, subslice_pat)); + } + + match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| { + let end_offset = (idx + 1) as u64; + let elem = ProjectionElem::ConstantIndex { + offset: if exact_size { min_length - end_offset } else { end_offset }, + min_length, + from_end: !exact_size, + }; + let place = place.clone().project(elem); + MatchPair::new(place, subpattern) + })); + } + + /// Creates a false edge to `imaginary_target` and a real edge to + /// real_target. If `imaginary_target` is none, or is the same as the real + /// target, a Goto is generated instead to simplify the generated MIR. + pub(crate) fn false_edges( + &mut self, + from_block: BasicBlock, + real_target: BasicBlock, + imaginary_target: Option, + source_info: SourceInfo, + ) { + match imaginary_target { + Some(target) if target != real_target => { + self.cfg.terminate( + from_block, + source_info, + TerminatorKind::FalseEdge { real_target, imaginary_target: target }, + ); + } + _ => self.cfg.goto(from_block, source_info, real_target), + } + } +} + +impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { + pub(crate) fn new( + place: PlaceBuilder<'tcx>, + pattern: &'pat Pat<'tcx>, + ) -> MatchPair<'pat, 'tcx> { + MatchPair { place, pattern } + } +} -- cgit v1.2.3