Skip to content

Commit ab068f1

Browse files
committed
Auto merge of #13156 - Veykril:macro-stmts, r=Veykril
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.
2 parents 7f38581 + ee02a47 commit ab068f1

File tree

13 files changed

+68
-118
lines changed

13 files changed

+68
-118
lines changed

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

Lines changed: 53 additions & 64 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,11 +626,46 @@ 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+
statements: &mut Vec<Statement>,
632+
mac: ast::MacroExpr,
633+
) -> Option<ExprId> {
634+
let mac_call = mac.macro_call()?;
635+
let syntax_ptr = AstPtr::new(&ast::Expr::from(mac));
636+
let macro_ptr = AstPtr::new(&mac_call);
637+
let expansion = self.collect_macro_call(
638+
mac_call,
639+
macro_ptr,
640+
false,
641+
|this, expansion: Option<ast::MacroStmts>| match expansion {
642+
Some(expansion) => {
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),
646+
expr => Some(this.collect_expr(expr)),
647+
})
648+
}
649+
None => None,
650+
},
651+
);
652+
match expansion {
653+
Some(tail) => {
654+
// Make the macro-call point to its expanded expression so we can query
655+
// semantics on syntax pointers to the macro
656+
let src = self.expander.to_source(syntax_ptr);
657+
self.source_map.expr_map.insert(src, tail);
658+
Some(tail)
659+
}
660+
None => None,
661+
}
662+
}
663+
664+
fn collect_stmt(&mut self, statements: &mut Vec<Statement>, s: ast::Stmt) {
644665
match s {
645666
ast::Stmt::LetStmt(stmt) => {
646667
if self.check_cfg(&stmt).is_none() {
647-
return None;
668+
return;
648669
}
649670
let pat = self.collect_pat_opt(stmt.pat());
650671
let type_ref =
@@ -654,61 +675,26 @@ impl ExprCollector<'_> {
654675
.let_else()
655676
.and_then(|let_else| let_else.block_expr())
656677
.map(|block| self.collect_block(block));
657-
Some(Statement::Let { pat, type_ref, initializer, else_branch })
678+
statements.push(Statement::Let { pat, type_ref, initializer, else_branch });
658679
}
659680
ast::Stmt::ExprStmt(stmt) => {
660681
let expr = stmt.expr();
661-
if let Some(expr) = &expr {
662-
if self.check_cfg(expr).is_none() {
663-
return None;
664-
}
682+
match &expr {
683+
Some(expr) if self.check_cfg(expr).is_none() => return,
684+
_ => (),
665685
}
666686
let has_semi = stmt.semicolon_token().is_some();
667687
// 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 })
688+
if let Some(ast::Expr::MacroExpr(mac)) = expr {
689+
if let Some(expr) = self.collect_macro_as_stmt(statements, mac) {
690+
statements.push(Statement::Expr { expr, has_semi })
691+
}
706692
} else {
707693
let expr = self.collect_expr_opt(expr);
708-
Some(Statement::Expr { expr, has_semi })
694+
statements.push(Statement::Expr { expr, has_semi });
709695
}
710696
}
711-
ast::Stmt::Item(_item) => None,
697+
ast::Stmt::Item(_item) => (),
712698
}
713699
}
714700

@@ -729,9 +715,12 @@ impl ExprCollector<'_> {
729715
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
730716
let prev_local_module = mem::replace(&mut self.expander.module, module);
731717

732-
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));
718+
let mut statements = Vec::new();
719+
block.statements().for_each(|s| self.collect_stmt(&mut statements, s));
720+
let tail = block.tail_expr().and_then(|e| match e {
721+
ast::Expr::MacroExpr(mac) => self.collect_macro_as_stmt(&mut statements, mac),
722+
expr => self.maybe_collect_expr(expr),
723+
});
735724
let tail = tail.or_else(|| {
736725
let stmt = statements.pop()?;
737726
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: 1 addition & 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,7 +308,6 @@ 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 '{ ...); } }': ()
@@ -321,7 +316,7 @@ fn expr_macro_expanded_in_stmts() {
321316
}
322317

323318
#[test]
324-
fn recurisve_macro_expanded_in_stmts() {
319+
fn recursive_macro_expanded_in_stmts() {
325320
check_infer(
326321
r#"
327322
macro_rules! ng {
@@ -340,11 +335,6 @@ fn recurisve_macro_expanded_in_stmts() {
340335
}
341336
"#,
342337
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}': ()
348338
!3..4 'a': i32
349339
!5..6 '3': i32
350340
196..237 '{ ...= a; }': ()
@@ -369,8 +359,6 @@ fn recursive_inner_item_macro_rules() {
369359
"#,
370360
expect![[r#"
371361
!0..1 '1': i32
372-
!0..7 'mac!($)': ()
373-
!0..26 'macro_...>{1};}': ()
374362
107..143 '{ ...!(); }': ()
375363
129..130 'a': i32
376364
"#]],

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ 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
@@ -1679,7 +1678,6 @@ fn main() {
16791678

16801679
#[test]
16811680
fn trailing_empty_macro() {
1682-
cov_mark::check!(empty_macro_in_trailing_position_is_removed);
16831681
check_no_mismatches(
16841682
r#"
16851683
macro_rules! m2 {

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>

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)