diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index d56046136c7a9..f9d3669e444b4 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -11,6 +11,7 @@ use std::cmp::Ordering; use std::fmt; use std::ops::Index; +use std::sync::Arc; use rustc_abi::{FieldIdx, Integer, Size, VariantIdx}; use rustc_ast::{AsmMacro, InlineAsmOptions, InlineAsmTemplatePiece}; @@ -628,7 +629,7 @@ pub enum InlineAsmOperand<'tcx> { #[derive(Clone, Debug, HashStable, TypeVisitable)] pub struct FieldPat<'tcx> { pub field: FieldIdx, - pub pattern: Box>, + pub pattern: Pat<'tcx>, } #[derive(Clone, Debug, HashStable, TypeVisitable)] @@ -689,7 +690,7 @@ impl<'tcx> Pat<'tcx> { Or { pats } => pats.iter().for_each(|p| p.walk_(it)), Array { box ref prefix, ref slice, box ref suffix } | Slice { box ref prefix, ref slice, box ref suffix } => { - prefix.iter().chain(slice.iter()).chain(suffix.iter()).for_each(|p| p.walk_(it)) + prefix.iter().chain(slice.as_deref()).chain(suffix.iter()).for_each(|p| p.walk_(it)) } } } @@ -846,28 +847,28 @@ pub enum PatKind<'tcx> { subpattern: Box>, }, - Range(Box>), + Range(Arc>), /// Matches against a slice, checking the length and extracting elements. /// irrefutable when there is a slice pattern and both `prefix` and `suffix` are empty. /// e.g., `&[ref xs @ ..]`. Slice { - prefix: Box<[Box>]>, + prefix: Box<[Pat<'tcx>]>, slice: Option>>, - suffix: Box<[Box>]>, + suffix: Box<[Pat<'tcx>]>, }, /// Fixed match against an array; irrefutable. Array { - prefix: Box<[Box>]>, + prefix: Box<[Pat<'tcx>]>, slice: Option>>, - suffix: Box<[Box>]>, + suffix: Box<[Pat<'tcx>]>, }, /// An or-pattern, e.g. `p | q`. /// Invariant: `pats.len() >= 2`. Or { - pats: Box<[Box>]>, + pats: Box<[Pat<'tcx>]>, }, /// A never pattern `!`. diff --git a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs index 9d59ffc88ba23..bca57817d6617 100644 --- a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs +++ b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use rustc_middle::mir::*; use rustc_middle::thir::{self, *}; use rustc_middle::ty::{self, Ty, TypeVisitableExt}; @@ -12,11 +14,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// [`PatKind::Leaf`]. /// /// Used internally by [`MatchPairTree::for_pattern`]. - fn field_match_pairs<'pat>( + fn field_match_pairs( &mut self, place: PlaceBuilder<'tcx>, - subpatterns: &'pat [FieldPat<'tcx>], - ) -> Vec> { + subpatterns: &[FieldPat<'tcx>], + ) -> Vec> { subpatterns .iter() .map(|fieldpat| { @@ -31,13 +33,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// array pattern or slice pattern, and adds those trees to `match_pairs`. /// /// Used internally by [`MatchPairTree::for_pattern`]. - fn prefix_slice_suffix<'pat>( + fn prefix_slice_suffix( &mut self, - match_pairs: &mut Vec>, + match_pairs: &mut Vec>, place: &PlaceBuilder<'tcx>, - prefix: &'pat [Box>], - opt_slice: &'pat Option>>, - suffix: &'pat [Box>], + prefix: &[Pat<'tcx>], + opt_slice: &Option>>, + suffix: &[Pat<'tcx>], ) { let tcx = self.tcx; let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) { @@ -83,14 +85,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } -impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> { +impl<'tcx> MatchPairTree<'tcx> { /// Recursively builds a match pair tree for the given pattern and its /// subpatterns. pub(in crate::builder) fn for_pattern( mut place_builder: PlaceBuilder<'tcx>, - pattern: &'pat Pat<'tcx>, + pattern: &Pat<'tcx>, cx: &mut Builder<'_, 'tcx>, - ) -> MatchPairTree<'pat, 'tcx> { + ) -> MatchPairTree<'tcx> { // Force the place type to the pattern's type. // FIXME(oli-obk): can we use this to simplify slice/array pattern hacks? if let Some(resolved) = place_builder.resolve_upvar(cx) { @@ -125,7 +127,7 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> { if range.is_full_range(cx.tcx) == Some(true) { default_irrefutable() } else { - TestCase::Range(range) + TestCase::Range(Arc::clone(range)) } } @@ -255,6 +257,12 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> { PatKind::Never => TestCase::Never, }; - MatchPairTree { place, test_case, subpairs, pattern } + MatchPairTree { + place, + test_case, + subpairs, + pattern_ty: pattern.ty, + pattern_span: pattern.span, + } } } diff --git a/compiler/rustc_mir_build/src/builder/matches/mod.rs b/compiler/rustc_mir_build/src/builder/matches/mod.rs index b21ec8f3083b3..a2f92a93ec544 100644 --- a/compiler/rustc_mir_build/src/builder/matches/mod.rs +++ b/compiler/rustc_mir_build/src/builder/matches/mod.rs @@ -33,6 +33,7 @@ mod util; use std::assert_matches::assert_matches; use std::borrow::Borrow; use std::mem; +use std::sync::Arc; /// Arguments to [`Builder::then_else_break_inner`] that are usually forwarded /// to recursive invocations. @@ -361,11 +362,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let scrutinee_place = unpack!(block = self.lower_scrutinee(block, scrutinee_id, scrutinee_span)); - let arms = arms.iter().map(|arm| &self.thir[*arm]); let match_start_span = span.shrink_to_lo().to(scrutinee_span); let patterns = arms - .clone() - .map(|arm| { + .iter() + .map(|&arm| { + let arm = &self.thir[arm]; let has_match_guard = if arm.guard.is_some() { HasMatchGuard::Yes } else { HasMatchGuard::No }; (&*arm.pattern, has_match_guard) @@ -412,20 +413,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// (by [Builder::lower_match_tree]). /// /// `outer_source_info` is the SourceInfo for the whole match. - fn lower_match_arms<'pat>( + fn lower_match_arms( &mut self, destination: Place<'tcx>, scrutinee_place_builder: PlaceBuilder<'tcx>, scrutinee_span: Span, - arms: impl IntoIterator>, + arms: &[ArmId], built_match_tree: BuiltMatchTree<'tcx>, outer_source_info: SourceInfo, - ) -> BlockAnd<()> - where - 'tcx: 'pat, - { + ) -> BlockAnd<()> { let arm_end_blocks: Vec = arms - .into_iter() + .iter() + .map(|&arm| &self.thir[arm]) .zip(built_match_tree.branches) .map(|(arm, branch)| { debug!("lowering arm {:?}\ncorresponding branch = {:?}", arm, branch); @@ -604,19 +603,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Optimize the case of `let x: T = ...` to write directly // into `x` and then require that `T == typeof(x)`. PatKind::AscribeUserType { - subpattern: - box Pat { - kind: - PatKind::Binding { - mode: BindingMode(ByRef::No, _), - var, - subpattern: None, - .. - }, - .. - }, + ref subpattern, ascription: thir::Ascription { ref annotation, variance: _ }, - } => { + } if let PatKind::Binding { + mode: BindingMode(ByRef::No, _), + var, + subpattern: None, + .. + } = subpattern.kind => + { let place = self.storage_live_binding( block, var, @@ -989,23 +984,19 @@ impl<'tcx> PatternExtraData<'tcx> { /// /// Will typically be incorporated into a [`Candidate`]. #[derive(Debug, Clone)] -struct FlatPat<'pat, 'tcx> { +struct FlatPat<'tcx> { /// To match the pattern, all of these must be satisfied... // Invariant: all the match pairs are recursively simplified. // Invariant: or-patterns must be sorted to the end. - match_pairs: Vec>, + match_pairs: Vec>, extra_data: PatternExtraData<'tcx>, } -impl<'tcx, 'pat> FlatPat<'pat, 'tcx> { +impl<'tcx> FlatPat<'tcx> { /// Creates a `FlatPat` containing a simplified [`MatchPairTree`] list/forest /// for the given pattern. - fn new( - place: PlaceBuilder<'tcx>, - pattern: &'pat Pat<'tcx>, - cx: &mut Builder<'_, 'tcx>, - ) -> Self { + fn new(place: PlaceBuilder<'tcx>, pattern: &Pat<'tcx>, cx: &mut Builder<'_, 'tcx>) -> Self { // First, recursively build a tree of match pairs for the given pattern. let mut match_pairs = vec![MatchPairTree::for_pattern(place, pattern, cx)]; let mut extra_data = PatternExtraData { @@ -1033,7 +1024,7 @@ impl<'tcx, 'pat> FlatPat<'pat, 'tcx> { /// of candidates, where each "leaf" candidate represents one of the ways for /// the arm pattern to successfully match. #[derive(Debug)] -struct Candidate<'pat, 'tcx> { +struct Candidate<'tcx> { /// For the candidate to match, all of these must be satisfied... /// /// --- @@ -1055,7 +1046,7 @@ struct Candidate<'pat, 'tcx> { /// Invariants: /// - All [`TestCase::Irrefutable`] patterns have been removed by simplification. /// - All or-patterns ([`TestCase::Or`]) have been sorted to the end. - match_pairs: Vec>, + match_pairs: Vec>, /// ...and if this is non-empty, one of these subcandidates also has to match... /// @@ -1072,7 +1063,7 @@ struct Candidate<'pat, 'tcx> { /// Invariant: at the end of match tree lowering, this must not contain an /// `is_never` candidate, because that would break binding consistency. /// - See [`Builder::remove_never_subcandidates`]. - subcandidates: Vec>, + subcandidates: Vec>, /// ...and if there is a guard it must be evaluated; if it's `false` then branch to `otherwise_block`. /// @@ -1107,10 +1098,10 @@ struct Candidate<'pat, 'tcx> { false_edge_start_block: Option, } -impl<'tcx, 'pat> Candidate<'pat, 'tcx> { +impl<'tcx> Candidate<'tcx> { fn new( place: PlaceBuilder<'tcx>, - pattern: &'pat Pat<'tcx>, + pattern: &Pat<'tcx>, has_guard: HasMatchGuard, cx: &mut Builder<'_, 'tcx>, ) -> Self { @@ -1123,7 +1114,7 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> { } /// Incorporates an already-simplified [`FlatPat`] into a new candidate. - fn from_flat_pat(flat_pat: FlatPat<'pat, 'tcx>, has_guard: bool) -> Self { + fn from_flat_pat(flat_pat: FlatPat<'tcx>, has_guard: bool) -> Self { Candidate { match_pairs: flat_pat.match_pairs, extra_data: flat_pat.extra_data, @@ -1172,7 +1163,7 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> { /// reference or by value, and to allow a mutable "context" to be shared by the /// traversal callbacks. Most traversals can use the simpler /// [`Candidate::visit_leaves`] wrapper instead. -fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>( +fn traverse_candidate<'tcx, C, T, I>( candidate: C, context: &mut T, // Called when visiting a "leaf" candidate (with no subcandidates). @@ -1184,7 +1175,7 @@ fn traverse_candidate<'pat, 'tcx: 'pat, C, T, I>( // Called after visiting a "node" candidate's children. complete_children: impl Copy + Fn(&mut T), ) where - C: Borrow>, // Typically `Candidate` or `&mut Candidate` + C: Borrow>, // Typically `Candidate` or `&mut Candidate` I: Iterator, { if candidate.borrow().subcandidates.is_empty() { @@ -1234,20 +1225,20 @@ struct Ascription<'tcx> { /// participate in or-pattern expansion, where they are transformed into subcandidates. /// - See [`Builder::expand_and_match_or_candidates`]. #[derive(Debug, Clone)] -enum TestCase<'pat, 'tcx> { +enum TestCase<'tcx> { Irrefutable { binding: Option>, ascription: Option> }, Variant { adt_def: ty::AdtDef<'tcx>, variant_index: VariantIdx }, Constant { value: mir::Const<'tcx> }, - Range(&'pat PatRange<'tcx>), + Range(Arc>), Slice { len: usize, variable_length: bool }, Deref { temp: Place<'tcx>, mutability: Mutability }, Never, - Or { pats: Box<[FlatPat<'pat, 'tcx>]> }, + Or { pats: Box<[FlatPat<'tcx>]> }, } -impl<'pat, 'tcx> TestCase<'pat, 'tcx> { - fn as_range(&self) -> Option<&'pat PatRange<'tcx>> { - if let Self::Range(v) = self { Some(*v) } else { None } +impl<'tcx> TestCase<'tcx> { + fn as_range(&self) -> Option<&PatRange<'tcx>> { + if let Self::Range(v) = self { Some(v.as_ref()) } else { None } } } @@ -1257,7 +1248,7 @@ impl<'pat, 'tcx> TestCase<'pat, 'tcx> { /// Each node also has a list of subpairs (possibly empty) that must also match, /// and a reference to the THIR pattern it represents. #[derive(Debug, Clone)] -pub(crate) struct MatchPairTree<'pat, 'tcx> { +pub(crate) struct MatchPairTree<'tcx> { /// This place... /// /// --- @@ -1272,7 +1263,7 @@ pub(crate) struct MatchPairTree<'pat, 'tcx> { /// --- /// Invariant: after creation and simplification in [`FlatPat::new`], /// this must not be [`TestCase::Irrefutable`]. - test_case: TestCase<'pat, 'tcx>, + test_case: TestCase<'tcx>, /// ... and these subpairs must match. /// @@ -1283,8 +1274,10 @@ pub(crate) struct MatchPairTree<'pat, 'tcx> { /// that tests its field for the value `3`. subpairs: Vec, - /// The pattern this was created from. - pattern: &'pat Pat<'tcx>, + /// Type field of the pattern this node was created from. + pattern_ty: Ty<'tcx>, + /// Span field of the pattern this node was created from. + pattern_span: Span, } /// See [`Test`] for more. @@ -1320,7 +1313,7 @@ enum TestKind<'tcx> { }, /// Test whether the value falls within an inclusive or exclusive range. - Range(Box>), + Range(Arc>), /// Test that the length of the slice is `== len` or `>= len`. Len { len: u64, op: BinOp }, @@ -1423,7 +1416,7 @@ struct BuiltMatchTree<'tcx> { impl<'tcx> MatchTreeSubBranch<'tcx> { fn from_sub_candidate( - candidate: Candidate<'_, 'tcx>, + candidate: Candidate<'tcx>, parent_data: &Vec>, ) -> Self { debug_assert!(candidate.match_pairs.is_empty()); @@ -1449,12 +1442,12 @@ impl<'tcx> MatchTreeSubBranch<'tcx> { } impl<'tcx> MatchTreeBranch<'tcx> { - fn from_candidate(candidate: Candidate<'_, 'tcx>) -> Self { + fn from_candidate(candidate: Candidate<'tcx>) -> Self { let mut sub_branches = Vec::new(); traverse_candidate( candidate, &mut Vec::new(), - &mut |candidate: Candidate<'_, '_>, parent_data: &mut Vec>| { + &mut |candidate: Candidate<'_>, parent_data: &mut Vec>| { sub_branches.push(MatchTreeSubBranch::from_sub_candidate(candidate, parent_data)); }, |inner_candidate, parent_data| { @@ -1485,23 +1478,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// `refutable` indicates whether the candidate list is refutable (for `if let` and `let else`) /// or not (for `let` and `match`). In the refutable case we return the block to which we branch /// on failure. - fn lower_match_tree<'pat>( + fn lower_match_tree( &mut self, block: BasicBlock, scrutinee_span: Span, scrutinee_place_builder: &PlaceBuilder<'tcx>, match_start_span: Span, - patterns: Vec<(&'pat Pat<'tcx>, HasMatchGuard)>, + patterns: Vec<(&Pat<'tcx>, HasMatchGuard)>, refutable: bool, - ) -> BuiltMatchTree<'tcx> - where - 'tcx: 'pat, - { + ) -> BuiltMatchTree<'tcx> { // Assemble the initial list of candidates. These top-level candidates are 1:1 with the // input patterns, but other parts of match lowering also introduce subcandidates (for // sub-or-patterns). So inside the algorithm, the candidates list may not correspond to // match arms directly. - let mut candidates: Vec> = patterns + let mut candidates: Vec> = patterns .into_iter() .map(|(pat, has_guard)| { Candidate::new(scrutinee_place_builder.clone(), pat, has_guard, self) @@ -1664,7 +1654,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { span: Span, scrutinee_span: Span, start_block: BasicBlock, - candidates: &mut [&mut Candidate<'_, 'tcx>], + candidates: &mut [&mut Candidate<'tcx>], ) -> BasicBlock { ensure_sufficient_stack(|| { self.match_candidates_inner(span, scrutinee_span, start_block, candidates) @@ -1678,7 +1668,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { span: Span, scrutinee_span: Span, mut start_block: BasicBlock, - candidates: &mut [&mut Candidate<'_, 'tcx>], + candidates: &mut [&mut Candidate<'tcx>], ) -> BasicBlock { if let [first, ..] = candidates { if first.false_edge_start_block.is_none() { @@ -1747,7 +1737,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// [otherwise block]: Candidate::otherwise_block fn select_matched_candidate( &mut self, - candidate: &mut Candidate<'_, 'tcx>, + candidate: &mut Candidate<'tcx>, start_block: BasicBlock, ) -> BasicBlock { assert!(candidate.otherwise_block.is_none()); @@ -1765,13 +1755,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// Takes a list of candidates such that some of the candidates' first match pairs are /// or-patterns. This expands as many or-patterns as possible and processes the resulting /// candidates. Returns the unprocessed candidates if any. - fn expand_and_match_or_candidates<'pat, 'b, 'c>( + fn expand_and_match_or_candidates<'b, 'c>( &mut self, span: Span, scrutinee_span: Span, start_block: BasicBlock, - candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>], - ) -> BlockAnd<&'b mut [&'c mut Candidate<'pat, 'tcx>]> { + candidates: &'b mut [&'c mut Candidate<'tcx>], + ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> { // We can't expand or-patterns freely. The rule is: // - If a candidate doesn't start with an or-pattern, we include it in // the expansion list as-is (i.e. it "expands" to itself). @@ -1865,14 +1855,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new /// subcandidate. Any candidate that has been expanded this way should also be postprocessed /// at the end of [`Self::expand_and_match_or_candidates`]. - fn create_or_subcandidates<'pat>( + fn create_or_subcandidates( &mut self, - candidate: &mut Candidate<'pat, 'tcx>, - match_pair: MatchPairTree<'pat, 'tcx>, + candidate: &mut Candidate<'tcx>, + match_pair: MatchPairTree<'tcx>, ) { let TestCase::Or { pats } = match_pair.test_case else { bug!() }; debug!("expanding or-pattern: candidate={:#?}\npats={:#?}", candidate, pats); - candidate.or_span = Some(match_pair.pattern.span); + candidate.or_span = Some(match_pair.pattern_span); candidate.subcandidates = pats .into_vec() .into_iter() @@ -1938,7 +1928,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// /// Note that this takes place _after_ the subcandidates have participated /// in match tree lowering. - fn merge_trivial_subcandidates(&mut self, candidate: &mut Candidate<'_, 'tcx>) { + fn merge_trivial_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) { assert!(!candidate.subcandidates.is_empty()); if candidate.has_guard { // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard. @@ -1981,7 +1971,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// Never subcandidates may have a set of bindings inconsistent with their siblings, /// which would break later code. So we filter them out. Note that we can't filter out /// top-level candidates this way. - fn remove_never_subcandidates(&mut self, candidate: &mut Candidate<'_, 'tcx>) { + fn remove_never_subcandidates(&mut self, candidate: &mut Candidate<'tcx>) { if candidate.subcandidates.is_empty() { return; } @@ -2020,7 +2010,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { &mut self, span: Span, scrutinee_span: Span, - candidate: &mut Candidate<'_, 'tcx>, + candidate: &mut Candidate<'tcx>, ) { if candidate.match_pairs.is_empty() { return; @@ -2086,7 +2076,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// [`Switch`]: TestKind::Switch /// [`SwitchInt`]: TestKind::SwitchInt /// [`Range`]: TestKind::Range - fn pick_test(&mut self, candidates: &[&mut Candidate<'_, 'tcx>]) -> (Place<'tcx>, Test<'tcx>) { + fn pick_test(&mut self, candidates: &[&mut Candidate<'tcx>]) -> (Place<'tcx>, Test<'tcx>) { // Extract the match-pair from the highest priority candidate let match_pair = &candidates[0].match_pairs[0]; let test = self.pick_test_for_match_pair(match_pair); @@ -2137,18 +2127,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// The sorted candidates are mutated to remove entailed match pairs: /// - candidate 0 becomes `[z @ true]` since we know that `x` was `true`; /// - candidate 1 becomes `[y @ false]` since we know that `x` was `false`. - fn sort_candidates<'b, 'c, 'pat>( + fn sort_candidates<'b, 'c>( &mut self, match_place: Place<'tcx>, test: &Test<'tcx>, - mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>], + mut candidates: &'b mut [&'c mut Candidate<'tcx>], ) -> ( - &'b mut [&'c mut Candidate<'pat, 'tcx>], - FxIndexMap, Vec<&'b mut Candidate<'pat, 'tcx>>>, + &'b mut [&'c mut Candidate<'tcx>], + FxIndexMap, Vec<&'b mut Candidate<'tcx>>>, ) { // For each of the possible outcomes, collect vector of candidates that apply if the test // has that particular outcome. - let mut target_candidates: FxIndexMap<_, Vec<&mut Candidate<'_, '_>>> = Default::default(); + let mut target_candidates: FxIndexMap<_, Vec<&mut Candidate<'_>>> = Default::default(); let total_candidate_count = candidates.len(); @@ -2274,13 +2264,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// ``` /// /// We return the unprocessed candidates. - fn test_candidates<'pat, 'b, 'c>( + fn test_candidates<'b, 'c>( &mut self, span: Span, scrutinee_span: Span, - candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>], + candidates: &'b mut [&'c mut Candidate<'tcx>], start_block: BasicBlock, - ) -> BlockAnd<&'b mut [&'c mut Candidate<'pat, 'tcx>]> { + ) -> BlockAnd<&'b mut [&'c mut Candidate<'tcx>]> { // Choose a match pair from the first candidate, and use it to determine a // test to perform that will confirm or refute that match pair. let (match_place, test) = self.pick_test(candidates); diff --git a/compiler/rustc_mir_build/src/builder/matches/simplify.rs b/compiler/rustc_mir_build/src/builder/matches/simplify.rs index ebaed1e431bbc..15c860151dc9e 100644 --- a/compiler/rustc_mir_build/src/builder/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/builder/matches/simplify.rs @@ -23,9 +23,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// Simplify a list of match pairs so they all require a test. Stores relevant bindings and /// ascriptions in `extra_data`. #[instrument(skip(self), level = "debug")] - pub(super) fn simplify_match_pairs<'pat>( + pub(super) fn simplify_match_pairs( &mut self, - match_pairs: &mut Vec>, + match_pairs: &mut Vec>, extra_data: &mut PatternExtraData<'tcx>, ) { // In order to please the borrow checker, in a pattern like `x @ pat` we must lower the diff --git a/compiler/rustc_mir_build/src/builder/matches/test.rs b/compiler/rustc_mir_build/src/builder/matches/test.rs index afe6b4475be3c..834f8be0d007a 100644 --- a/compiler/rustc_mir_build/src/builder/matches/test.rs +++ b/compiler/rustc_mir_build/src/builder/matches/test.rs @@ -6,6 +6,7 @@ // the candidates based on the result. use std::cmp::Ordering; +use std::sync::Arc; use rustc_data_structures::fx::FxIndexMap; use rustc_hir::{LangItem, RangeEnd}; @@ -26,20 +27,20 @@ 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 pick_test_for_match_pair<'pat>( + pub(super) fn pick_test_for_match_pair( &mut self, - match_pair: &MatchPairTree<'pat, 'tcx>, + match_pair: &MatchPairTree<'tcx>, ) -> Test<'tcx> { let kind = match match_pair.test_case { TestCase::Variant { adt_def, variant_index: _ } => TestKind::Switch { adt_def }, - TestCase::Constant { .. } if match_pair.pattern.ty.is_bool() => TestKind::If, - TestCase::Constant { .. } if is_switch_ty(match_pair.pattern.ty) => TestKind::SwitchInt, - TestCase::Constant { value } => TestKind::Eq { value, ty: match_pair.pattern.ty }, + TestCase::Constant { .. } if match_pair.pattern_ty.is_bool() => TestKind::If, + TestCase::Constant { .. } if is_switch_ty(match_pair.pattern_ty) => TestKind::SwitchInt, + TestCase::Constant { value } => TestKind::Eq { value, ty: match_pair.pattern_ty }, - TestCase::Range(range) => { - assert_eq!(range.ty, match_pair.pattern.ty); - TestKind::Range(Box::new(range.clone())) + TestCase::Range(ref range) => { + assert_eq!(range.ty, match_pair.pattern_ty); + TestKind::Range(Arc::clone(range)) } TestCase::Slice { len, variable_length } => { @@ -56,13 +57,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { TestCase::Or { .. } => bug!("or-patterns should have already been handled"), TestCase::Irrefutable { .. } => span_bug!( - match_pair.pattern.span, + match_pair.pattern_span, "simplifiable pattern found: {:?}", - match_pair.pattern + match_pair.pattern_span ), }; - Test { span: match_pair.pattern.span, kind } + Test { span: match_pair.pattern_span, kind } } #[instrument(skip(self, target_blocks, place), level = "debug")] @@ -521,8 +522,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { &mut self, test_place: Place<'tcx>, test: &Test<'tcx>, - candidate: &mut Candidate<'_, 'tcx>, - sorted_candidates: &FxIndexMap, Vec<&mut Candidate<'_, 'tcx>>>, + candidate: &mut Candidate<'tcx>, + sorted_candidates: &FxIndexMap, Vec<&mut Candidate<'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 @@ -558,14 +559,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // FIXME(#29623) we could use PatKind::Range to rule // things out here, in some cases. (TestKind::SwitchInt, &TestCase::Constant { value }) - if is_switch_ty(match_pair.pattern.ty) => + if is_switch_ty(match_pair.pattern_ty) => { // An important invariant of candidate sorting is that a candidate // must not match in multiple branches. For `SwitchInt` tests, adding // a new value might invalidate that property for range patterns that // have already been sorted into the failure arm, so we must take care // not to add such values here. - let is_covering_range = |test_case: &TestCase<'_, 'tcx>| { + let is_covering_range = |test_case: &TestCase<'tcx>| { test_case.as_range().is_some_and(|range| { matches!( range.contains(value, self.tcx, self.typing_env()), @@ -573,7 +574,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ) }) }; - let is_conflicting_candidate = |candidate: &&mut Candidate<'_, 'tcx>| { + let is_conflicting_candidate = |candidate: &&mut Candidate<'tcx>| { candidate .match_pairs .iter() @@ -685,8 +686,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } - (TestKind::Range(test), &TestCase::Range(pat)) => { - if test.as_ref() == pat { + (TestKind::Range(test), TestCase::Range(pat)) => { + if test == pat { fully_matched = true; Some(TestBranch::Success) } else { diff --git a/compiler/rustc_mir_build/src/builder/matches/util.rs b/compiler/rustc_mir_build/src/builder/matches/util.rs index 1bd399e511b39..83e79572b2a8e 100644 --- a/compiler/rustc_mir_build/src/builder/matches/util.rs +++ b/compiler/rustc_mir_build/src/builder/matches/util.rs @@ -67,7 +67,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// a MIR pass run after borrow checking. pub(super) fn collect_fake_borrows<'tcx>( cx: &mut Builder<'_, 'tcx>, - candidates: &[Candidate<'_, 'tcx>], + candidates: &[Candidate<'tcx>], temp_span: Span, scrutinee_base: PlaceBase, ) -> Vec<(Place<'tcx>, Local, FakeBorrowKind)> { @@ -135,7 +135,7 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> { } } - fn visit_candidate(&mut self, candidate: &Candidate<'_, 'tcx>) { + fn visit_candidate(&mut self, candidate: &Candidate<'tcx>) { for binding in &candidate.extra_data.bindings { self.visit_binding(binding); } @@ -144,7 +144,7 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> { } } - fn visit_flat_pat(&mut self, flat_pat: &FlatPat<'_, 'tcx>) { + fn visit_flat_pat(&mut self, flat_pat: &FlatPat<'tcx>) { for binding in &flat_pat.extra_data.bindings { self.visit_binding(binding); } @@ -153,7 +153,7 @@ impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> { } } - fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'_, 'tcx>) { + fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'tcx>) { if let TestCase::Or { pats, .. } = &match_pair.test_case { for flat_pat in pats.iter() { self.visit_flat_pat(flat_pat) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 8247a6c6a32d5..697cb7cf37a3c 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -676,12 +676,14 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { let mut interpreted_as_const = None; let mut interpreted_as_const_sugg = None; - if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } - | PatKind::AscribeUserType { - subpattern: - box Pat { kind: PatKind::ExpandedConstant { def_id, is_inline: false, .. }, .. }, - .. - } = pat.kind + // These next few matches want to peek through `AscribeUserType` to see + // the underlying pattern. + let mut unpeeled_pat = pat; + while let PatKind::AscribeUserType { ref subpattern, .. } = unpeeled_pat.kind { + unpeeled_pat = subpattern; + } + + if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = unpeeled_pat.kind && let DefKind::Const = self.tcx.def_kind(def_id) && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span) // We filter out paths with multiple path::segments. @@ -692,11 +694,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { // When we encounter a constant as the binding name, point at the `const` definition. interpreted_as_const = Some(span); interpreted_as_const_sugg = Some(InterpretedAsConst { span: pat.span, variable }); - } else if let PatKind::Constant { .. } - | PatKind::AscribeUserType { - subpattern: box Pat { kind: PatKind::Constant { .. }, .. }, - .. - } = pat.kind + } else if let PatKind::Constant { .. } = unpeeled_pat.kind && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span) { // If the pattern to match is an integer literal: diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index cc6d69710e4c2..551ec5cf4e9c6 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -208,7 +208,7 @@ impl<'tcx> ConstToPat<'tcx> { let field = FieldIdx::new(idx); // Patterns can only use monomorphic types. let ty = self.tcx.normalize_erasing_regions(self.typing_env, ty); - FieldPat { field, pattern: self.valtree_to_pat(val, ty) } + FieldPat { field, pattern: *self.valtree_to_pat(val, ty) } }) .collect() } @@ -277,7 +277,7 @@ impl<'tcx> ConstToPat<'tcx> { prefix: cv .unwrap_branch() .iter() - .map(|val| self.valtree_to_pat(*val, *elem_ty)) + .map(|val| *self.valtree_to_pat(*val, *elem_ty)) .collect(), slice: None, suffix: Box::new([]), @@ -286,7 +286,7 @@ impl<'tcx> ConstToPat<'tcx> { prefix: cv .unwrap_branch() .iter() - .map(|val| self.valtree_to_pat(*val, *elem_ty)) + .map(|val| *self.valtree_to_pat(*val, *elem_ty)) .collect(), slice: None, suffix: Box::new([]), diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 20a728d6d5b2c..3566cabdb5352 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -4,6 +4,7 @@ mod check_match; mod const_to_pat; use std::cmp::Ordering; +use std::sync::Arc; use rustc_abi::{FieldIdx, Integer}; use rustc_errors::MultiSpan; @@ -260,7 +261,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { let hi = hi.unwrap_or(PatRangeBoundary::PosInfinity); let cmp = lo.compare_with(hi, ty, self.tcx, self.typing_env); - let mut kind = PatKind::Range(Box::new(PatRange { lo, hi, end, ty })); + let mut kind = PatKind::Range(Arc::new(PatRange { lo, hi, end, ty })); match (end, cmp) { // `x..y` where `x < y`. (RangeEnd::Excluded, Some(Ordering::Less)) => {} @@ -416,7 +417,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { .iter() .map(|field| FieldPat { field: self.typeck_results.field_index(field.hir_id), - pattern: self.lower_pattern(field.pat), + pattern: *self.lower_pattern(field.pat), }) .collect(); @@ -444,13 +445,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> { .enumerate_and_adjust(expected_len, gap_pos) .map(|(i, subpattern)| FieldPat { field: FieldIdx::new(i), - pattern: self.lower_pattern(subpattern), + pattern: *self.lower_pattern(subpattern), }) .collect() } - fn lower_patterns(&mut self, pats: &'tcx [hir::Pat<'tcx>]) -> Box<[Box>]> { - pats.iter().map(|p| self.lower_pattern(p)).collect() + fn lower_patterns(&mut self, pats: &'tcx [hir::Pat<'tcx>]) -> Box<[Pat<'tcx>]> { + pats.iter().map(|p| *self.lower_pattern(p)).collect() } fn lower_opt_pattern(&mut self, pat: Option<&'tcx hir::Pat<'tcx>>) -> Option>> { diff --git a/compiler/rustc_mir_build/src/thir/print.rs b/compiler/rustc_mir_build/src/thir/print.rs index 729c8f784ba9f..9ab87dd99ffc0 100644 --- a/compiler/rustc_mir_build/src/thir/print.rs +++ b/compiler/rustc_mir_build/src/thir/print.rs @@ -643,8 +643,8 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> { print_indented!(self, "}", depth_lvl); } - fn print_pat(&mut self, pat: &Box>, depth_lvl: usize) { - let Pat { ty, span, kind } = &**pat; + fn print_pat(&mut self, pat: &Pat<'tcx>, depth_lvl: usize) { + let &Pat { ty, span, ref kind } = pat; print_indented!(self, "Pat: {", depth_lvl); print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 1); diff --git a/compiler/rustc_ty_utils/src/consts.rs b/compiler/rustc_ty_utils/src/consts.rs index 4038a1d68faec..cbe49d000b7d7 100644 --- a/compiler/rustc_ty_utils/src/consts.rs +++ b/compiler/rustc_ty_utils/src/consts.rs @@ -373,7 +373,8 @@ impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> { match pat.kind { thir::PatKind::Constant { value } => value.has_non_region_param(), - thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => { + thir::PatKind::Range(ref range) => { + let &thir::PatRange { lo, hi, .. } = range.as_ref(); lo.has_non_region_param() || hi.has_non_region_param() } _ => false,