Skip to content

Commit 8c60813

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

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,17 @@ 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));
556557

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+
557565
self.alloc_expr(Expr::MacroStmts { tail, statements }, syntax_ptr)
558566
}
559567
ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),

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)