Skip to content

Commit 192a79c

Browse files
committed
Remove hir::Expr::MacroStmts
This hir expression isn't needed and only existed as it was simpler to deal with at first as it gave us a direct mapping for the ast version of the same construct. This PR removes it, properly handling the statements that are introduced by macro call expressions.
1 parent d9e2207 commit 192a79c

File tree

12 files changed

+88
-110
lines changed

12 files changed

+88
-110
lines changed

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

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -550,20 +550,6 @@ impl ExprCollector<'_> {
550550
None => self.alloc_expr(Expr::Missing, syntax_ptr),
551551
}
552552
}
553-
ast::Expr::MacroStmts(e) => {
554-
let statements: Box<[_]> =
555-
e.statements().filter_map(|s| self.collect_stmt(s)).collect();
556-
let tail = e.expr().map(|e| self.collect_expr(e));
557-
558-
if e.syntax().children().next().is_none() {
559-
// HACK: make sure that macros that expand to nothing aren't treated as a `()`
560-
// expression when used in block tail position.
561-
cov_mark::hit!(empty_macro_in_trailing_position_is_removed);
562-
return None;
563-
}
564-
565-
self.alloc_expr(Expr::MacroStmts { tail, statements }, syntax_ptr)
566-
}
567553
ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),
568554
})
569555
}
@@ -640,7 +626,58 @@ impl ExprCollector<'_> {
640626
}
641627
}
642628

643-
fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Statement> {
629+
fn collect_macro_as_stmt(
630+
&mut self,
631+
mac: ast::MacroExpr,
632+
) -> Option<(Vec<Statement>, Option<ExprId>)> {
633+
let mac_call = mac.macro_call()?;
634+
let syntax_ptr = AstPtr::new(&ast::Expr::from(mac));
635+
let macro_ptr = AstPtr::new(&mac_call);
636+
let expansion = self.collect_macro_call(
637+
mac_call,
638+
macro_ptr,
639+
false,
640+
|this, expansion: Option<ast::MacroStmts>| match expansion {
641+
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+
}
653+
expr => Some(this.collect_expr(expr)),
654+
});
655+
Some((statements, tail))
656+
}
657+
None => None,
658+
},
659+
);
660+
let mut stmts = Vec::new();
661+
let expr = match expansion {
662+
Some((statements, tail)) => {
663+
stmts.extend(statements);
664+
// Make the macro-call point to its expanded expression so we can query
665+
// semantics on syntax pointers to the macro
666+
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+
}
674+
}
675+
None => self.alloc_expr(Expr::Missing, syntax_ptr),
676+
};
677+
Some((stmts, Some(expr)))
678+
}
679+
680+
fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> {
644681
match s {
645682
ast::Stmt::LetStmt(stmt) => {
646683
if self.check_cfg(&stmt).is_none() {
@@ -654,7 +691,7 @@ impl ExprCollector<'_> {
654691
.let_else()
655692
.and_then(|let_else| let_else.block_expr())
656693
.map(|block| self.collect_block(block));
657-
Some(Statement::Let { pat, type_ref, initializer, else_branch })
694+
Some(vec![Statement::Let { pat, type_ref, initializer, else_branch }])
658695
}
659696
ast::Stmt::ExprStmt(stmt) => {
660697
let expr = stmt.expr();
@@ -665,47 +702,15 @@ impl ExprCollector<'_> {
665702
}
666703
let has_semi = stmt.semicolon_token().is_some();
667704
// Note that macro could be expanded to multiple statements
668-
if let Some(expr @ ast::Expr::MacroExpr(mac)) = &expr {
669-
let mac_call = mac.macro_call()?;
670-
let syntax_ptr = AstPtr::new(expr);
671-
let macro_ptr = AstPtr::new(&mac_call);
672-
let stmt = self.collect_macro_call(
673-
mac_call,
674-
macro_ptr,
675-
false,
676-
|this, expansion: Option<ast::MacroStmts>| match expansion {
677-
Some(expansion) => {
678-
let statements = expansion
679-
.statements()
680-
.filter_map(|stmt| this.collect_stmt(stmt))
681-
.collect();
682-
let tail = expansion.expr().map(|expr| this.collect_expr(expr));
683-
684-
let mac_stmts = this.alloc_expr(
685-
Expr::MacroStmts { tail, statements },
686-
AstPtr::new(&ast::Expr::MacroStmts(expansion)),
687-
);
688-
689-
Some(mac_stmts)
690-
}
691-
None => None,
692-
},
693-
);
694-
695-
let expr = match stmt {
696-
Some(expr) => {
697-
// Make the macro-call point to its expanded expression so we can query
698-
// semantics on syntax pointers to the macro
699-
let src = self.expander.to_source(syntax_ptr);
700-
self.source_map.expr_map.insert(src, expr);
701-
expr
702-
}
703-
None => self.alloc_expr(Expr::Missing, syntax_ptr),
704-
};
705-
Some(Statement::Expr { expr, has_semi })
705+
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 });
709+
}
710+
Some(statements)
706711
} else {
707712
let expr = self.collect_expr_opt(expr);
708-
Some(Statement::Expr { expr, has_semi })
713+
Some(vec![Statement::Expr { expr, has_semi }])
709714
}
710715
}
711716
ast::Stmt::Item(_item) => None,
@@ -730,8 +735,15 @@ impl ExprCollector<'_> {
730735
let prev_local_module = mem::replace(&mut self.expander.module, module);
731736

732737
let mut statements: Vec<_> =
733-
block.statements().filter_map(|s| self.collect_stmt(s)).collect();
734-
let tail = block.tail_expr().and_then(|e| self.maybe_collect_expr(e));
738+
block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
739+
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+
}
745+
expr => self.maybe_collect_expr(expr),
746+
});
735747
let tail = tail.or_else(|| {
736748
let stmt = statements.pop()?;
737749
if let Statement::Expr { expr, has_semi: false } = stmt {

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,6 @@ impl<'a> Printer<'a> {
422422
}
423423
w!(self, "}}");
424424
}
425-
Expr::MacroStmts { statements, tail } => {
426-
w!(self, "{{ // macro statements");
427-
self.indented(|p| {
428-
for stmt in statements.iter() {
429-
p.print_stmt(stmt);
430-
}
431-
if let Some(tail) = tail {
432-
p.print_expr(*tail);
433-
}
434-
});
435-
self.newline();
436-
w!(self, "}}");
437-
}
438425
}
439426
}
440427

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
176176

177177
scopes.set_scope(expr, *scope);
178178
match &body[expr] {
179-
Expr::MacroStmts { statements, tail } => {
180-
compute_block_scopes(statements, *tail, body, scopes, scope);
181-
}
182179
Expr::Block { statements, tail, id, label } => {
183180
let mut scope = scopes.new_block_scope(*scope, *id, make_label(label));
184181
// Overwrite the old scope for the block expr, so that every block scope can be found

crates/hir-def/src/expr.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ pub enum Expr {
206206
Unsafe {
207207
body: ExprId,
208208
},
209-
MacroStmts {
210-
statements: Box<[Statement]>,
211-
tail: Option<ExprId>,
212-
},
213209
Array(Array),
214210
Literal(Literal),
215211
Underscore,
@@ -263,7 +259,7 @@ impl Expr {
263259
Expr::Let { expr, .. } => {
264260
f(*expr);
265261
}
266-
Expr::MacroStmts { tail, statements } | Expr::Block { statements, tail, .. } => {
262+
Expr::Block { statements, tail, .. } => {
267263
for stmt in statements.iter() {
268264
match stmt {
269265
Statement::Let { initializer, .. } => {

crates/hir-expand/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ impl ExpandTo {
969969
if parent.kind() == MACRO_EXPR
970970
&& parent
971971
.parent()
972-
.map_or(true, |p| matches!(p.kind(), EXPR_STMT | STMT_LIST | MACRO_STMTS))
972+
.map_or(false, |p| matches!(p.kind(), EXPR_STMT | STMT_LIST | MACRO_STMTS))
973973
{
974974
return ExpandTo::Statements;
975975
}

crates/hir-ty/src/infer/expr.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,6 @@ impl<'a> InferenceContext<'a> {
794794
None => self.table.new_float_var(),
795795
},
796796
},
797-
Expr::MacroStmts { tail, statements } => {
798-
self.infer_block(tgt_expr, statements, *tail, expected)
799-
}
800797
Expr::Underscore => {
801798
// Underscore expressions may only appear in assignee expressions,
802799
// which are handled by `infer_assignee_expr()`, so any underscore

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ fn expr_macro_def_expanded_in_various_places() {
193193
!0..6 '1isize': isize
194194
!0..6 '1isize': isize
195195
!0..6 '1isize': isize
196-
!0..6 '1isize': isize
197-
!0..6 '1isize': isize
198196
39..442 '{ ...!(); }': ()
199197
73..94 'spam!(...am!())': {unknown}
200198
100..119 'for _ ...!() {}': ()
@@ -276,8 +274,6 @@ fn expr_macro_rules_expanded_in_various_places() {
276274
!0..6 '1isize': isize
277275
!0..6 '1isize': isize
278276
!0..6 '1isize': isize
279-
!0..6 '1isize': isize
280-
!0..6 '1isize': isize
281277
53..456 '{ ...!(); }': ()
282278
87..108 'spam!(...am!())': {unknown}
283279
114..133 'for _ ...!() {}': ()
@@ -312,16 +308,16 @@ fn expr_macro_expanded_in_stmts() {
312308
}
313309
"#,
314310
expect![[r#"
315-
!0..8 'leta=();': ()
316311
!3..4 'a': ()
317312
!5..7 '()': ()
318313
57..84 '{ ...); } }': ()
314+
63..82 'id! { ... (); }': ()
319315
"#]],
320316
);
321317
}
322318

323319
#[test]
324-
fn recurisve_macro_expanded_in_stmts() {
320+
fn recursive_macro_expanded_in_stmts() {
325321
check_infer(
326322
r#"
327323
macro_rules! ng {
@@ -340,11 +336,7 @@ fn recurisve_macro_expanded_in_stmts() {
340336
}
341337
"#,
342338
expect![[r#"
343-
!0..7 'leta=3;': ()
344-
!0..13 'ng!{[leta=3]}': ()
345-
!0..13 'ng!{[leta=]3}': ()
346-
!0..13 'ng!{[leta]=3}': ()
347-
!0..13 'ng!{[let]a=3}': ()
339+
!0..13 'ng!{[leta=3]}': {unknown}
348340
!3..4 'a': i32
349341
!5..6 '3': i32
350342
196..237 '{ ...= a; }': ()
@@ -369,8 +361,7 @@ fn recursive_inner_item_macro_rules() {
369361
"#,
370362
expect![[r#"
371363
!0..1 '1': i32
372-
!0..7 'mac!($)': ()
373-
!0..26 'macro_...>{1};}': ()
364+
!0..7 'mac!($)': {unknown}
374365
107..143 '{ ...!(); }': ()
375366
129..130 'a': i32
376367
"#]],

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,12 @@ fn issue_6811() {
573573
}
574574
"#,
575575
expect![[r#"
576-
!0..16 'let_a=...t_b=1;': ()
577576
!3..5 '_a': i32
578577
!6..7 '1': i32
579578
!11..13 '_b': i32
580579
!14..15 '1': i32
581580
103..131 '{ ...!(); }': ()
581+
109..128 'profil...ion!()': {unknown}
582582
"#]],
583583
);
584584
}
@@ -1679,7 +1679,6 @@ fn main() {
16791679

16801680
#[test]
16811681
fn trailing_empty_macro() {
1682-
cov_mark::check!(empty_macro_in_trailing_position_is_removed);
16831682
check_no_mismatches(
16841683
r#"
16851684
macro_rules! m2 {

crates/hir/src/source_analyzer.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,19 @@ impl SourceAnalyzer {
140140
) -> Option<InFile<ast::Expr>> {
141141
let macro_file = self.body_source_map()?.node_macro_file(expr.as_ref())?;
142142
let expanded = db.parse_or_expand(macro_file)?;
143-
144-
let res = match ast::MacroCall::cast(expanded.clone()) {
145-
Some(call) => self.expand_expr(db, InFile::new(macro_file, call))?,
146-
_ => InFile::new(macro_file, ast::Expr::cast(expanded)?),
143+
let res = if let Some(stmts) = ast::MacroStmts::cast(expanded.clone()) {
144+
match stmts.expr()? {
145+
ast::Expr::MacroExpr(mac) => {
146+
self.expand_expr(db, InFile::new(macro_file, mac.macro_call()?))?
147+
}
148+
expr => InFile::new(macro_file, expr),
149+
}
150+
} else if let Some(call) = ast::MacroCall::cast(expanded.clone()) {
151+
self.expand_expr(db, InFile::new(macro_file, call))?
152+
} else {
153+
InFile::new(macro_file, ast::Expr::cast(expanded)?)
147154
};
155+
148156
Some(res)
149157
}
150158

crates/ide-db/src/syntax_helpers/node_ext.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
315315
| ast::Expr::IndexExpr(_)
316316
| ast::Expr::Literal(_)
317317
| ast::Expr::MacroExpr(_)
318-
| ast::Expr::MacroStmts(_)
319318
| ast::Expr::MethodCallExpr(_)
320319
| ast::Expr::ParenExpr(_)
321320
| ast::Expr::PathExpr(_)

crates/syntax/rust.ungram

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ Expr =
343343
| Literal
344344
| LoopExpr
345345
| MacroExpr
346-
| MacroStmts
347346
| MatchExpr
348347
| MethodCallExpr
349348
| ParenExpr

crates/syntax/src/ast/generated/nodes.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,6 @@ pub enum Expr {
15261526
Literal(Literal),
15271527
LoopExpr(LoopExpr),
15281528
MacroExpr(MacroExpr),
1529-
MacroStmts(MacroStmts),
15301529
MatchExpr(MatchExpr),
15311530
MethodCallExpr(MethodCallExpr),
15321531
ParenExpr(ParenExpr),
@@ -3342,9 +3341,6 @@ impl From<LoopExpr> for Expr {
33423341
impl From<MacroExpr> for Expr {
33433342
fn from(node: MacroExpr) -> Expr { Expr::MacroExpr(node) }
33443343
}
3345-
impl From<MacroStmts> for Expr {
3346-
fn from(node: MacroStmts) -> Expr { Expr::MacroStmts(node) }
3347-
}
33483344
impl From<MatchExpr> for Expr {
33493345
fn from(node: MatchExpr) -> Expr { Expr::MatchExpr(node) }
33503346
}
@@ -3411,7 +3407,6 @@ impl AstNode for Expr {
34113407
| LITERAL
34123408
| LOOP_EXPR
34133409
| MACRO_EXPR
3414-
| MACRO_STMTS
34153410
| MATCH_EXPR
34163411
| METHOD_CALL_EXPR
34173412
| PAREN_EXPR
@@ -3448,7 +3443,6 @@ impl AstNode for Expr {
34483443
LITERAL => Expr::Literal(Literal { syntax }),
34493444
LOOP_EXPR => Expr::LoopExpr(LoopExpr { syntax }),
34503445
MACRO_EXPR => Expr::MacroExpr(MacroExpr { syntax }),
3451-
MACRO_STMTS => Expr::MacroStmts(MacroStmts { syntax }),
34523446
MATCH_EXPR => Expr::MatchExpr(MatchExpr { syntax }),
34533447
METHOD_CALL_EXPR => Expr::MethodCallExpr(MethodCallExpr { syntax }),
34543448
PAREN_EXPR => Expr::ParenExpr(ParenExpr { syntax }),
@@ -3487,7 +3481,6 @@ impl AstNode for Expr {
34873481
Expr::Literal(it) => &it.syntax,
34883482
Expr::LoopExpr(it) => &it.syntax,
34893483
Expr::MacroExpr(it) => &it.syntax,
3490-
Expr::MacroStmts(it) => &it.syntax,
34913484
Expr::MatchExpr(it) => &it.syntax,
34923485
Expr::MethodCallExpr(it) => &it.syntax,
34933486
Expr::ParenExpr(it) => &it.syntax,

0 commit comments

Comments
 (0)