Skip to content

Commit 3903243

Browse files
committed
Auto merge of #13027 - jonas-schievink:fix-mismatch-with-trailing-empty-macro, r=jonas-schievink
fix: Fix incorrect type mismatch with `cfg_if!` and other macros in expression position Fixes #12940 This is a bit of a hack, ideally `MacroStmts` would not exist at all after HIR lowering, but that requires changing how the lowering code works.
2 parents 3561433 + 8c60813 commit 3903243

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

+9-1
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

+17
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)