Skip to content

Commit 13009d3

Browse files
author
Jonas Schievink
committed
Fix lowering of empty macro expressions in trailing position
1 parent 5076f50 commit 13009d3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,13 @@ impl ExprCollector<'_> {
551551
}
552552
}
553553
ast::Expr::MacroStmts(e) => {
554-
let statements = e.statements().filter_map(|s| self.collect_stmt(s)).collect();
554+
let statements: Box<[_]> =
555+
e.statements().filter_map(|s| self.collect_stmt(s)).collect();
555556
let tail = e.expr().map(|e| self.collect_expr(e));
557+
if statements.is_empty() && tail.is_none() {
558+
cov_mark::hit!(empty_macro_in_trailing_position_is_removed);
559+
return None;
560+
}
556561

557562
self.alloc_expr(Expr::MacroStmts { tail, statements }, syntax_ptr)
558563
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,3 +1648,20 @@ fn main() {
16481648
"#]],
16491649
);
16501650
}
1651+
1652+
#[test]
1653+
fn trailing_empty_macro() {
1654+
cov_mark::check!(empty_macro_in_trailing_position_is_removed);
1655+
check_no_mismatches(
1656+
r#"
1657+
macro_rules! m2 {
1658+
($($t:tt)*) => {$($t)*};
1659+
}
1660+
1661+
fn macrostmts() -> u8 {
1662+
m2! { 0 }
1663+
m2! {}
1664+
}
1665+
"#,
1666+
);
1667+
}

0 commit comments

Comments
 (0)