Skip to content

Commit ee02a47

Browse files
committed
Remove unnecessary allocations
1 parent 192a79c commit ee02a47

File tree

4 files changed

+25
-53
lines changed

4 files changed

+25
-53
lines changed

crates/hir-def/src/body/lower.rs

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,9 @@ impl ExprCollector<'_> {
628628

629629
fn collect_macro_as_stmt(
630630
&mut self,
631+
statements: &mut Vec<Statement>,
631632
mac: ast::MacroExpr,
632-
) -> Option<(Vec<Statement>, Option<ExprId>)> {
633+
) -> Option<ExprId> {
633634
let mac_call = mac.macro_call()?;
634635
let syntax_ptr = AstPtr::new(&ast::Expr::from(mac));
635636
let macro_ptr = AstPtr::new(&mac_call);
@@ -639,49 +640,32 @@ impl ExprCollector<'_> {
639640
false,
640641
|this, expansion: Option<ast::MacroStmts>| match expansion {
641642
Some(expansion) => {
642-
let mut statements: Vec<_> = expansion
643-
.statements()
644-
.filter_map(|stmt| this.collect_stmt(stmt))
645-
.flatten()
646-
.collect();
647-
let tail = expansion.expr().and_then(|expr| match expr {
648-
ast::Expr::MacroExpr(mac) => {
649-
let (stmts, tail) = this.collect_macro_as_stmt(mac)?;
650-
statements.extend(stmts);
651-
tail
652-
}
643+
expansion.statements().for_each(|stmt| this.collect_stmt(statements, stmt));
644+
expansion.expr().and_then(|expr| match expr {
645+
ast::Expr::MacroExpr(mac) => this.collect_macro_as_stmt(statements, mac),
653646
expr => Some(this.collect_expr(expr)),
654-
});
655-
Some((statements, tail))
647+
})
656648
}
657649
None => None,
658650
},
659651
);
660-
let mut stmts = Vec::new();
661-
let expr = match expansion {
662-
Some((statements, tail)) => {
663-
stmts.extend(statements);
652+
match expansion {
653+
Some(tail) => {
664654
// Make the macro-call point to its expanded expression so we can query
665655
// semantics on syntax pointers to the macro
666656
let src = self.expander.to_source(syntax_ptr);
667-
match tail {
668-
Some(tail) => {
669-
self.source_map.expr_map.insert(src, tail);
670-
tail
671-
}
672-
None => self.make_expr(Expr::Missing, Ok(src.clone())),
673-
}
657+
self.source_map.expr_map.insert(src, tail);
658+
Some(tail)
674659
}
675-
None => self.alloc_expr(Expr::Missing, syntax_ptr),
676-
};
677-
Some((stmts, Some(expr)))
660+
None => None,
661+
}
678662
}
679663

680-
fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> {
664+
fn collect_stmt(&mut self, statements: &mut Vec<Statement>, s: ast::Stmt) {
681665
match s {
682666
ast::Stmt::LetStmt(stmt) => {
683667
if self.check_cfg(&stmt).is_none() {
684-
return None;
668+
return;
685669
}
686670
let pat = self.collect_pat_opt(stmt.pat());
687671
let type_ref =
@@ -691,29 +675,26 @@ impl ExprCollector<'_> {
691675
.let_else()
692676
.and_then(|let_else| let_else.block_expr())
693677
.map(|block| self.collect_block(block));
694-
Some(vec![Statement::Let { pat, type_ref, initializer, else_branch }])
678+
statements.push(Statement::Let { pat, type_ref, initializer, else_branch });
695679
}
696680
ast::Stmt::ExprStmt(stmt) => {
697681
let expr = stmt.expr();
698-
if let Some(expr) = &expr {
699-
if self.check_cfg(expr).is_none() {
700-
return None;
701-
}
682+
match &expr {
683+
Some(expr) if self.check_cfg(expr).is_none() => return,
684+
_ => (),
702685
}
703686
let has_semi = stmt.semicolon_token().is_some();
704687
// Note that macro could be expanded to multiple statements
705688
if let Some(ast::Expr::MacroExpr(mac)) = expr {
706-
let (mut statements, tail) = self.collect_macro_as_stmt(mac)?;
707-
if let Some(expr) = tail {
708-
statements.push(Statement::Expr { expr, has_semi });
689+
if let Some(expr) = self.collect_macro_as_stmt(statements, mac) {
690+
statements.push(Statement::Expr { expr, has_semi })
709691
}
710-
Some(statements)
711692
} else {
712693
let expr = self.collect_expr_opt(expr);
713-
Some(vec![Statement::Expr { expr, has_semi }])
694+
statements.push(Statement::Expr { expr, has_semi });
714695
}
715696
}
716-
ast::Stmt::Item(_item) => None,
697+
ast::Stmt::Item(_item) => (),
717698
}
718699
}
719700

@@ -734,14 +715,10 @@ impl ExprCollector<'_> {
734715
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
735716
let prev_local_module = mem::replace(&mut self.expander.module, module);
736717

737-
let mut statements: Vec<_> =
738-
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
718+
let mut statements = Vec::new();
719+
block.statements().for_each(|s| self.collect_stmt(&mut statements, s));
739720
let tail = block.tail_expr().and_then(|e| match e {
740-
ast::Expr::MacroExpr(mac) => {
741-
let (stmts, tail) = self.collect_macro_as_stmt(mac)?;
742-
statements.extend(stmts);
743-
tail
744-
}
721+
ast::Expr::MacroExpr(mac) => self.collect_macro_as_stmt(&mut statements, mac),
745722
expr => self.maybe_collect_expr(expr),
746723
});
747724
let tail = tail.or_else(|| {

crates/hir-ty/src/tests/macros.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ fn expr_macro_expanded_in_stmts() {
311311
!3..4 'a': ()
312312
!5..7 '()': ()
313313
57..84 '{ ...); } }': ()
314-
63..82 'id! { ... (); }': ()
315314
"#]],
316315
);
317316
}
@@ -336,7 +335,6 @@ fn recursive_macro_expanded_in_stmts() {
336335
}
337336
"#,
338337
expect![[r#"
339-
!0..13 'ng!{[leta=3]}': {unknown}
340338
!3..4 'a': i32
341339
!5..6 '3': i32
342340
196..237 '{ ...= a; }': ()
@@ -361,7 +359,6 @@ fn recursive_inner_item_macro_rules() {
361359
"#,
362360
expect![[r#"
363361
!0..1 '1': i32
364-
!0..7 'mac!($)': {unknown}
365362
107..143 '{ ...!(); }': ()
366363
129..130 'a': i32
367364
"#]],

crates/hir-ty/src/tests/regression.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ fn issue_6811() {
578578
!11..13 '_b': i32
579579
!14..15 '1': i32
580580
103..131 '{ ...!(); }': ()
581-
109..128 'profil...ion!()': {unknown}
582581
"#]],
583582
);
584583
}

crates/hir-ty/src/tests/simple.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2549,7 +2549,6 @@ impl B for Astruct {}
25492549
expect![[r#"
25502550
569..573 'self': Box<[T], A>
25512551
602..634 '{ ... }': Vec<T, A>
2552-
612..628 'unimpl...ted!()': Vec<T, A>
25532552
648..761 '{ ...t]); }': ()
25542553
658..661 'vec': Vec<i32, Global>
25552554
664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>

0 commit comments

Comments
 (0)