Skip to content

Commit cc32777

Browse files
committed
internal: Replace Vec with Box in hir Expr
1 parent 14dff25 commit cc32777

File tree

5 files changed

+34
-37
lines changed

5 files changed

+34
-37
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ impl ExprCollector<'_> {
130130
self.body.params.push(param_pat);
131131
}
132132

133-
for param in param_list.params() {
134-
let pat = match param.pat() {
135-
None => continue,
136-
Some(pat) => pat,
137-
};
133+
for pat in param_list.params().filter_map(|param| param.pat()) {
138134
let param_pat = self.collect_pat(pat);
139135
self.body.params.push(param_pat);
140136
}
@@ -160,7 +156,7 @@ impl ExprCollector<'_> {
160156
self.make_expr(expr, Err(SyntheticSyntax))
161157
}
162158
fn unit(&mut self) -> ExprId {
163-
self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
159+
self.alloc_expr_desugared(Expr::Tuple { exprs: Box::default() })
164160
}
165161
fn missing_expr(&mut self) -> ExprId {
166162
self.alloc_expr_desugared(Expr::Missing)
@@ -235,7 +231,8 @@ impl ExprCollector<'_> {
235231
expr: else_branch.unwrap_or_else(|| self.unit()),
236232
guard: None,
237233
},
238-
];
234+
]
235+
.into();
239236
return Some(
240237
self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr),
241238
);
@@ -300,7 +297,8 @@ impl ExprCollector<'_> {
300297
let arms = vec![
301298
MatchArm { pat, expr: body, guard: None },
302299
MatchArm { pat: placeholder_pat, expr: break_, guard: None },
303-
];
300+
]
301+
.into();
304302
let match_expr =
305303
self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
306304
return Some(
@@ -324,7 +322,7 @@ impl ExprCollector<'_> {
324322
let args = if let Some(arg_list) = e.arg_list() {
325323
arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
326324
} else {
327-
Vec::new()
325+
Box::default()
328326
};
329327
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
330328
}
@@ -333,7 +331,7 @@ impl ExprCollector<'_> {
333331
let args = if let Some(arg_list) = e.arg_list() {
334332
arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
335333
} else {
336-
Vec::new()
334+
Box::default()
337335
};
338336
let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
339337
let generic_args = e
@@ -367,7 +365,7 @@ impl ExprCollector<'_> {
367365
})
368366
.collect()
369367
} else {
370-
Vec::new()
368+
Box::default()
371369
};
372370
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
373371
}
@@ -429,7 +427,7 @@ impl ExprCollector<'_> {
429427
let spread = nfl.spread().map(|s| self.collect_expr(s));
430428
Expr::RecordLit { path, fields, spread }
431429
} else {
432-
Expr::RecordLit { path, fields: Vec::new(), spread: None }
430+
Expr::RecordLit { path, fields: Box::default(), spread: None }
433431
};
434432

435433
self.alloc_expr(record_lit, syntax_ptr)
@@ -496,7 +494,10 @@ impl ExprCollector<'_> {
496494
.and_then(|r| r.ty())
497495
.map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
498496
let body = self.collect_expr_opt(e.body());
499-
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
497+
self.alloc_expr(
498+
Expr::Lambda { args: args.into(), arg_types: arg_types.into(), ret_type, body },
499+
syntax_ptr,
500+
)
500501
}
501502
ast::Expr::BinExpr(e) => {
502503
let lhs = self.collect_expr_opt(e.lhs());
@@ -718,7 +719,7 @@ impl ExprCollector<'_> {
718719
self.statements_in_scope.pop();
719720
}
720721
let tail = tail;
721-
let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements);
722+
let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements).into();
722723
let syntax_node_ptr = AstPtr::new(&block.into());
723724
let expr_id = self.alloc_expr(
724725
Expr::Block { id: block_id, statements, tail, label: None },

crates/hir_def/src/body/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
204204
}
205205
Expr::Match { expr, arms } => {
206206
compute_expr_scopes(*expr, body, scopes, scope);
207-
for arm in arms {
207+
for arm in arms.iter() {
208208
let mut scope = scopes.new_scope(scope);
209209
scopes.add_bindings(body, scope, arm.pat);
210210
match arm.guard {

crates/hir_def/src/expr.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub enum Expr {
6161
},
6262
Block {
6363
id: BlockId,
64-
statements: Vec<Statement>,
64+
statements: Box<[Statement]>,
6565
tail: Option<ExprId>,
6666
label: Option<LabelId>,
6767
},
@@ -82,17 +82,17 @@ pub enum Expr {
8282
},
8383
Call {
8484
callee: ExprId,
85-
args: Vec<ExprId>,
85+
args: Box<[ExprId]>,
8686
},
8787
MethodCall {
8888
receiver: ExprId,
8989
method_name: Name,
90-
args: Vec<ExprId>,
90+
args: Box<[ExprId]>,
9191
generic_args: Option<Box<GenericArgs>>,
9292
},
9393
Match {
9494
expr: ExprId,
95-
arms: Vec<MatchArm>,
95+
arms: Box<[MatchArm]>,
9696
},
9797
Continue {
9898
label: Option<Name>,
@@ -109,7 +109,7 @@ pub enum Expr {
109109
},
110110
RecordLit {
111111
path: Option<Box<Path>>,
112-
fields: Vec<RecordLitField>,
112+
fields: Box<[RecordLitField]>,
113113
spread: Option<ExprId>,
114114
},
115115
Field {
@@ -162,13 +162,13 @@ pub enum Expr {
162162
index: ExprId,
163163
},
164164
Lambda {
165-
args: Vec<PatId>,
166-
arg_types: Vec<Option<Interned<TypeRef>>>,
165+
args: Box<[PatId]>,
166+
arg_types: Box<[Option<Interned<TypeRef>>]>,
167167
ret_type: Option<Interned<TypeRef>>,
168168
body: ExprId,
169169
},
170170
Tuple {
171-
exprs: Vec<ExprId>,
171+
exprs: Box<[ExprId]>,
172172
},
173173
Unsafe {
174174
body: ExprId,
@@ -233,7 +233,7 @@ impl Expr {
233233
}
234234
}
235235
Expr::Block { statements, tail, .. } => {
236-
for stmt in statements {
236+
for stmt in statements.iter() {
237237
match stmt {
238238
Statement::Let { initializer, .. } => {
239239
if let Some(expr) = initializer {
@@ -262,19 +262,19 @@ impl Expr {
262262
}
263263
Expr::Call { callee, args } => {
264264
f(*callee);
265-
for arg in args {
265+
for arg in args.iter() {
266266
f(*arg);
267267
}
268268
}
269269
Expr::MethodCall { receiver, args, .. } => {
270270
f(*receiver);
271-
for arg in args {
271+
for arg in args.iter() {
272272
f(*arg);
273273
}
274274
}
275275
Expr::Match { expr, arms } => {
276276
f(*expr);
277-
for arm in arms {
277+
for arm in arms.iter() {
278278
f(arm.expr);
279279
}
280280
}
@@ -285,7 +285,7 @@ impl Expr {
285285
}
286286
}
287287
Expr::RecordLit { fields, spread, .. } => {
288-
for field in fields {
288+
for field in fields.iter() {
289289
f(field.expr);
290290
}
291291
if let Some(expr) = spread {
@@ -321,7 +321,7 @@ impl Expr {
321321
f(*expr);
322322
}
323323
Expr::Tuple { exprs } => {
324-
for expr in exprs {
324+
for expr in exprs.iter() {
325325
f(*expr);
326326
}
327327
}

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,16 @@ impl ExprValidator {
202202
}
203203

204204
let is_method_call = matches!(expr, Expr::MethodCall { .. });
205-
let (sig, args) = match expr {
205+
let (sig, mut arg_count) = match expr {
206206
Expr::Call { callee, args } => {
207207
let callee = &self.infer.type_of_expr[*callee];
208208
let sig = match callee.callable_sig(db) {
209209
Some(sig) => sig,
210210
None => return,
211211
};
212-
(sig, args.clone())
212+
(sig, args.len())
213213
}
214214
Expr::MethodCall { receiver, args, .. } => {
215-
let mut args = args.clone();
216-
args.insert(0, *receiver);
217-
218215
let receiver = &self.infer.type_of_expr[*receiver];
219216
if receiver.strip_references().is_unknown() {
220217
// if the receiver is of unknown type, it's very likely we
@@ -229,7 +226,7 @@ impl ExprValidator {
229226
};
230227
let sig = db.callable_item_signature(callee.into()).substitute(&Interner, &subst);
231228

232-
(sig, args)
229+
(sig, args.len() + 1)
233230
}
234231
_ => return,
235232
};
@@ -241,7 +238,6 @@ impl ExprValidator {
241238
let params = sig.params();
242239

243240
let mut param_count = params.len();
244-
let mut arg_count = args.len();
245241

246242
if arg_count != param_count {
247243
if is_method_call {

crates/hir_ty/src/infer/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl<'a> InferenceContext<'a> {
375375
let matchee_diverges = self.diverges;
376376
let mut all_arms_diverge = Diverges::Always;
377377

378-
for arm in arms {
378+
for arm in arms.iter() {
379379
self.diverges = Diverges::Maybe;
380380
let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
381381
match arm.guard {

0 commit comments

Comments
 (0)