internal: Replace Vec with Box in hir Pat

This commit is contained in:
Lukas Wirth 2021-11-20 16:17:16 +01:00
parent cc327774b7
commit ceaec9d866
3 changed files with 33 additions and 47 deletions

View File

@ -813,7 +813,7 @@ impl ExprCollector<'_> {
ast::Pat::RecordPat(p) => {
let path =
p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
let args: Vec<_> = p
let args = p
.record_pat_field_list()
.expect("every struct should have a field list")
.fields()
@ -903,7 +903,7 @@ impl ExprCollector<'_> {
}
}
fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Box<[PatId]>, Option<usize>) {
// Find the location of the `..`, if there is one. Note that we do not
// consider the possibility of there being multiple `..` here.
let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
@ -962,7 +962,7 @@ impl From<ast::LiteralKind> for Literal {
Literal::Float(Default::default(), ty)
}
LiteralKind::ByteString(bs) => {
let text = bs.value().map(Vec::from).unwrap_or_else(Default::default);
let text = bs.value().map(Box::from).unwrap_or_else(Default::default);
Literal::ByteString(text)
}
LiteralKind::String(_) => Literal::String(Default::default()),

View File

@ -41,7 +41,7 @@ pub type LabelId = Idx<Label>;
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Literal {
String(String),
ByteString(Vec<u8>),
ByteString(Box<[u8]>),
Char(char),
Bool(bool),
Int(i128, Option<BuiltinInt>),
@ -182,7 +182,7 @@ pub enum Expr {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Array {
ElementList(Vec<ExprId>),
ElementList(Box<[ExprId]>),
Repeat { initializer: ExprId, repeat: ExprId },
}
@ -228,23 +228,23 @@ impl Expr {
Expr::If { condition, then_branch, else_branch } => {
f(*condition);
f(*then_branch);
if let Some(else_branch) = else_branch {
f(*else_branch);
if let &Some(else_branch) = else_branch {
f(else_branch);
}
}
Expr::Block { statements, tail, .. } => {
for stmt in statements.iter() {
match stmt {
Statement::Let { initializer, .. } => {
if let Some(expr) = initializer {
f(*expr);
if let &Some(expr) = initializer {
f(expr);
}
}
Statement::Expr { expr: expression, .. } => f(*expression),
}
}
if let Some(expr) = tail {
f(*expr);
if let &Some(expr) = tail {
f(expr);
}
}
Expr::TryBlock { body }
@ -262,34 +262,28 @@ impl Expr {
}
Expr::Call { callee, args } => {
f(*callee);
for arg in args.iter() {
f(*arg);
}
args.iter().copied().for_each(f);
}
Expr::MethodCall { receiver, args, .. } => {
f(*receiver);
for arg in args.iter() {
f(*arg);
}
args.iter().copied().for_each(f);
}
Expr::Match { expr, arms } => {
f(*expr);
for arm in arms.iter() {
f(arm.expr);
}
arms.iter().map(|arm| arm.expr).for_each(f);
}
Expr::Continue { .. } => {}
Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
if let Some(expr) = expr {
f(*expr);
if let &Some(expr) = expr {
f(expr);
}
}
Expr::RecordLit { fields, spread, .. } => {
for field in fields.iter() {
f(field.expr);
}
if let Some(expr) = spread {
f(*expr);
if let &Some(expr) = spread {
f(expr);
}
}
Expr::Lambda { body, .. } => {
@ -300,11 +294,11 @@ impl Expr {
f(*rhs);
}
Expr::Range { lhs, rhs, .. } => {
if let Some(lhs) = rhs {
f(*lhs);
if let &Some(lhs) = rhs {
f(lhs);
}
if let Some(rhs) = lhs {
f(*rhs);
if let &Some(rhs) = lhs {
f(rhs);
}
}
Expr::Index { base, index } => {
@ -320,17 +314,9 @@ impl Expr {
| Expr::Box { expr } => {
f(*expr);
}
Expr::Tuple { exprs } => {
for expr in exprs.iter() {
f(*expr);
}
}
Expr::Tuple { exprs } => exprs.iter().copied().for_each(f),
Expr::Array(a) => match a {
Array::ElementList(exprs) => {
for expr in exprs {
f(*expr);
}
}
Array::ElementList(exprs) => exprs.iter().copied().for_each(f),
Array::Repeat { initializer, repeat } => {
f(*initializer);
f(*repeat)
@ -386,15 +372,15 @@ pub struct RecordFieldPat {
pub enum Pat {
Missing,
Wild,
Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
Or(Vec<PatId>),
Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool },
Tuple { args: Box<[PatId]>, ellipsis: Option<usize> },
Or(Box<[PatId]>),
Record { path: Option<Box<Path>>, args: Box<[RecordFieldPat]>, ellipsis: bool },
Range { start: ExprId, end: ExprId },
Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
Slice { prefix: Box<[PatId]>, slice: Option<PatId>, suffix: Box<[PatId]> },
Path(Box<Path>),
Lit(ExprId),
Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> },
TupleStruct { path: Option<Box<Path>>, args: Box<[PatId]>, ellipsis: Option<usize> },
Ref { pat: PatId, mutability: Mutability },
Box { inner: PatId },
ConstBlock(ExprId),

View File

@ -226,8 +226,8 @@ impl<'a> InferenceContext<'a> {
_ => self.err_ty(),
};
for pat_id in prefix.iter().chain(suffix) {
self.infer_pat(*pat_id, &elem_ty, default_bm);
for &pat_id in prefix.iter().chain(suffix.iter()) {
self.infer_pat(pat_id, &elem_ty, default_bm);
}
let pat_ty = match expected.kind(&Interner) {
@ -235,8 +235,8 @@ impl<'a> InferenceContext<'a> {
_ => TyKind::Slice(elem_ty),
}
.intern(&Interner);
if let Some(slice_pat_id) = slice {
self.infer_pat(*slice_pat_id, &pat_ty, default_bm);
if let &Some(slice_pat_id) = slice {
self.infer_pat(slice_pat_id, &pat_ty, default_bm);
}
pat_ty