1
Fork 0

Rollup merge of #136435 - Zalathar:thir-pat-stuff, r=Nadrieril

Simplify some code for lowering THIR patterns

I've been playing around with some radically different ways of storing THIR patterns, and while those experiments haven't yet produced a clear win, I have noticed various smaller things in the existing code that can be made a bit nicer.

Some of the more significant changes:
- With a little bit of extra effort (and thoughtful use of Arc), we can completely remove an entire layer of `'pat` lifetimes from the intermediate data structures used for match lowering.
- In several places, lists of THIR patterns were being double-boxed for no apparent reason.
This commit is contained in:
Matthias Krüger 2025-02-06 13:10:00 +01:00 committed by GitHub
commit c9635e51b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 148 additions and 148 deletions

View file

@ -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};
@ -618,7 +619,7 @@ pub enum InlineAsmOperand<'tcx> {
#[derive(Debug, HashStable, TypeVisitable)]
pub struct FieldPat<'tcx> {
pub field: FieldIdx,
pub pattern: Box<Pat<'tcx>>,
pub pattern: Pat<'tcx>,
}
#[derive(Debug, HashStable, TypeVisitable)]
@ -679,7 +680,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))
}
}
}
@ -836,28 +837,28 @@ pub enum PatKind<'tcx> {
subpattern: Box<Pat<'tcx>>,
},
Range(Box<PatRange<'tcx>>),
Range(Arc<PatRange<'tcx>>),
/// 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<Pat<'tcx>>]>,
prefix: Box<[Pat<'tcx>]>,
slice: Option<Box<Pat<'tcx>>>,
suffix: Box<[Box<Pat<'tcx>>]>,
suffix: Box<[Pat<'tcx>]>,
},
/// Fixed match against an array; irrefutable.
Array {
prefix: Box<[Box<Pat<'tcx>>]>,
prefix: Box<[Pat<'tcx>]>,
slice: Option<Box<Pat<'tcx>>>,
suffix: Box<[Box<Pat<'tcx>>]>,
suffix: Box<[Pat<'tcx>]>,
},
/// An or-pattern, e.g. `p | q`.
/// Invariant: `pats.len() >= 2`.
Or {
pats: Box<[Box<Pat<'tcx>>]>,
pats: Box<[Pat<'tcx>]>,
},
/// A never pattern `!`.