mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 14:03:35 +00:00
Remove unnecessary allocations
This commit is contained in:
parent
192a79c235
commit
ee02a4721b
4 changed files with 25 additions and 53 deletions
|
@ -628,8 +628,9 @@ impl ExprCollector<'_> {
|
||||||
|
|
||||||
fn collect_macro_as_stmt(
|
fn collect_macro_as_stmt(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
statements: &mut Vec<Statement>,
|
||||||
mac: ast::MacroExpr,
|
mac: ast::MacroExpr,
|
||||||
) -> Option<(Vec<Statement>, Option<ExprId>)> {
|
) -> Option<ExprId> {
|
||||||
let mac_call = mac.macro_call()?;
|
let mac_call = mac.macro_call()?;
|
||||||
let syntax_ptr = AstPtr::new(&ast::Expr::from(mac));
|
let syntax_ptr = AstPtr::new(&ast::Expr::from(mac));
|
||||||
let macro_ptr = AstPtr::new(&mac_call);
|
let macro_ptr = AstPtr::new(&mac_call);
|
||||||
|
@ -639,49 +640,32 @@ impl ExprCollector<'_> {
|
||||||
false,
|
false,
|
||||||
|this, expansion: Option<ast::MacroStmts>| match expansion {
|
|this, expansion: Option<ast::MacroStmts>| match expansion {
|
||||||
Some(expansion) => {
|
Some(expansion) => {
|
||||||
let mut statements: Vec<_> = expansion
|
expansion.statements().for_each(|stmt| this.collect_stmt(statements, stmt));
|
||||||
.statements()
|
expansion.expr().and_then(|expr| match expr {
|
||||||
.filter_map(|stmt| this.collect_stmt(stmt))
|
ast::Expr::MacroExpr(mac) => this.collect_macro_as_stmt(statements, mac),
|
||||||
.flatten()
|
|
||||||
.collect();
|
|
||||||
let tail = expansion.expr().and_then(|expr| match expr {
|
|
||||||
ast::Expr::MacroExpr(mac) => {
|
|
||||||
let (stmts, tail) = this.collect_macro_as_stmt(mac)?;
|
|
||||||
statements.extend(stmts);
|
|
||||||
tail
|
|
||||||
}
|
|
||||||
expr => Some(this.collect_expr(expr)),
|
expr => Some(this.collect_expr(expr)),
|
||||||
});
|
})
|
||||||
Some((statements, tail))
|
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
let mut stmts = Vec::new();
|
match expansion {
|
||||||
let expr = match expansion {
|
Some(tail) => {
|
||||||
Some((statements, tail)) => {
|
|
||||||
stmts.extend(statements);
|
|
||||||
// Make the macro-call point to its expanded expression so we can query
|
// Make the macro-call point to its expanded expression so we can query
|
||||||
// semantics on syntax pointers to the macro
|
// semantics on syntax pointers to the macro
|
||||||
let src = self.expander.to_source(syntax_ptr);
|
let src = self.expander.to_source(syntax_ptr);
|
||||||
match tail {
|
self.source_map.expr_map.insert(src, tail);
|
||||||
Some(tail) => {
|
Some(tail)
|
||||||
self.source_map.expr_map.insert(src, tail);
|
|
||||||
tail
|
|
||||||
}
|
|
||||||
None => self.make_expr(Expr::Missing, Ok(src.clone())),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => self.alloc_expr(Expr::Missing, syntax_ptr),
|
None => None,
|
||||||
};
|
}
|
||||||
Some((stmts, Some(expr)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> {
|
fn collect_stmt(&mut self, statements: &mut Vec<Statement>, s: ast::Stmt) {
|
||||||
match s {
|
match s {
|
||||||
ast::Stmt::LetStmt(stmt) => {
|
ast::Stmt::LetStmt(stmt) => {
|
||||||
if self.check_cfg(&stmt).is_none() {
|
if self.check_cfg(&stmt).is_none() {
|
||||||
return None;
|
return;
|
||||||
}
|
}
|
||||||
let pat = self.collect_pat_opt(stmt.pat());
|
let pat = self.collect_pat_opt(stmt.pat());
|
||||||
let type_ref =
|
let type_ref =
|
||||||
|
@ -691,29 +675,26 @@ impl ExprCollector<'_> {
|
||||||
.let_else()
|
.let_else()
|
||||||
.and_then(|let_else| let_else.block_expr())
|
.and_then(|let_else| let_else.block_expr())
|
||||||
.map(|block| self.collect_block(block));
|
.map(|block| self.collect_block(block));
|
||||||
Some(vec![Statement::Let { pat, type_ref, initializer, else_branch }])
|
statements.push(Statement::Let { pat, type_ref, initializer, else_branch });
|
||||||
}
|
}
|
||||||
ast::Stmt::ExprStmt(stmt) => {
|
ast::Stmt::ExprStmt(stmt) => {
|
||||||
let expr = stmt.expr();
|
let expr = stmt.expr();
|
||||||
if let Some(expr) = &expr {
|
match &expr {
|
||||||
if self.check_cfg(expr).is_none() {
|
Some(expr) if self.check_cfg(expr).is_none() => return,
|
||||||
return None;
|
_ => (),
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let has_semi = stmt.semicolon_token().is_some();
|
let has_semi = stmt.semicolon_token().is_some();
|
||||||
// Note that macro could be expanded to multiple statements
|
// Note that macro could be expanded to multiple statements
|
||||||
if let Some(ast::Expr::MacroExpr(mac)) = expr {
|
if let Some(ast::Expr::MacroExpr(mac)) = expr {
|
||||||
let (mut statements, tail) = self.collect_macro_as_stmt(mac)?;
|
if let Some(expr) = self.collect_macro_as_stmt(statements, mac) {
|
||||||
if let Some(expr) = tail {
|
statements.push(Statement::Expr { expr, has_semi })
|
||||||
statements.push(Statement::Expr { expr, has_semi });
|
|
||||||
}
|
}
|
||||||
Some(statements)
|
|
||||||
} else {
|
} else {
|
||||||
let expr = self.collect_expr_opt(expr);
|
let expr = self.collect_expr_opt(expr);
|
||||||
Some(vec![Statement::Expr { expr, has_semi }])
|
statements.push(Statement::Expr { expr, has_semi });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::Stmt::Item(_item) => None,
|
ast::Stmt::Item(_item) => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -734,14 +715,10 @@ impl ExprCollector<'_> {
|
||||||
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
|
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
|
||||||
let prev_local_module = mem::replace(&mut self.expander.module, module);
|
let prev_local_module = mem::replace(&mut self.expander.module, module);
|
||||||
|
|
||||||
let mut statements: Vec<_> =
|
let mut statements = Vec::new();
|
||||||
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
|
block.statements().for_each(|s| self.collect_stmt(&mut statements, s));
|
||||||
let tail = block.tail_expr().and_then(|e| match e {
|
let tail = block.tail_expr().and_then(|e| match e {
|
||||||
ast::Expr::MacroExpr(mac) => {
|
ast::Expr::MacroExpr(mac) => self.collect_macro_as_stmt(&mut statements, mac),
|
||||||
let (stmts, tail) = self.collect_macro_as_stmt(mac)?;
|
|
||||||
statements.extend(stmts);
|
|
||||||
tail
|
|
||||||
}
|
|
||||||
expr => self.maybe_collect_expr(expr),
|
expr => self.maybe_collect_expr(expr),
|
||||||
});
|
});
|
||||||
let tail = tail.or_else(|| {
|
let tail = tail.or_else(|| {
|
||||||
|
|
|
@ -311,7 +311,6 @@ fn expr_macro_expanded_in_stmts() {
|
||||||
!3..4 'a': ()
|
!3..4 'a': ()
|
||||||
!5..7 '()': ()
|
!5..7 '()': ()
|
||||||
57..84 '{ ...); } }': ()
|
57..84 '{ ...); } }': ()
|
||||||
63..82 'id! { ... (); }': ()
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -336,7 +335,6 @@ fn recursive_macro_expanded_in_stmts() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
!0..13 'ng!{[leta=3]}': {unknown}
|
|
||||||
!3..4 'a': i32
|
!3..4 'a': i32
|
||||||
!5..6 '3': i32
|
!5..6 '3': i32
|
||||||
196..237 '{ ...= a; }': ()
|
196..237 '{ ...= a; }': ()
|
||||||
|
@ -361,7 +359,6 @@ fn recursive_inner_item_macro_rules() {
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
!0..1 '1': i32
|
!0..1 '1': i32
|
||||||
!0..7 'mac!($)': {unknown}
|
|
||||||
107..143 '{ ...!(); }': ()
|
107..143 '{ ...!(); }': ()
|
||||||
129..130 'a': i32
|
129..130 'a': i32
|
||||||
"#]],
|
"#]],
|
||||||
|
|
|
@ -578,7 +578,6 @@ fn issue_6811() {
|
||||||
!11..13 '_b': i32
|
!11..13 '_b': i32
|
||||||
!14..15 '1': i32
|
!14..15 '1': i32
|
||||||
103..131 '{ ...!(); }': ()
|
103..131 '{ ...!(); }': ()
|
||||||
109..128 'profil...ion!()': {unknown}
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2549,7 +2549,6 @@ impl B for Astruct {}
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
569..573 'self': Box<[T], A>
|
569..573 'self': Box<[T], A>
|
||||||
602..634 '{ ... }': Vec<T, A>
|
602..634 '{ ... }': Vec<T, A>
|
||||||
612..628 'unimpl...ted!()': Vec<T, A>
|
|
||||||
648..761 '{ ...t]); }': ()
|
648..761 '{ ...t]); }': ()
|
||||||
658..661 'vec': Vec<i32, Global>
|
658..661 'vec': Vec<i32, Global>
|
||||||
664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
|
664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
|
||||||
|
|
Loading…
Reference in a new issue