Skip to content

Commit

Permalink
Auto merge of #13027 - jonas-schievink:fix-mismatch-with-trailing-emp…
Browse files Browse the repository at this point in the history
…ty-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.
  • Loading branch information
bors committed Aug 15, 2022
2 parents 3561433 + 8c60813 commit 3903243
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,17 @@ impl ExprCollector<'_> {
}
}
ast::Expr::MacroStmts(e) => {
let statements = e.statements().filter_map(|s| self.collect_stmt(s)).collect();
let statements: Box<[_]> =
e.statements().filter_map(|s| self.collect_stmt(s)).collect();
let tail = e.expr().map(|e| self.collect_expr(e));

if e.syntax().children().next().is_none() {
// HACK: make sure that macros that expand to nothing aren't treated as a `()`
// expression when used in block tail position.
cov_mark::hit!(empty_macro_in_trailing_position_is_removed);
return None;
}

self.alloc_expr(Expr::MacroStmts { tail, statements }, syntax_ptr)
}
ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),
Expand Down
17 changes: 17 additions & 0 deletions crates/hir-ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,3 +1648,20 @@ fn main() {
"#]],
);
}

#[test]
fn trailing_empty_macro() {
cov_mark::check!(empty_macro_in_trailing_position_is_removed);
check_no_mismatches(
r#"
macro_rules! m2 {
($($t:tt)*) => {$($t)*};
}
fn macrostmts() -> u8 {
m2! { 0 }
m2! {}
}
"#,
);
}

0 comments on commit 3903243

Please sign in to comment.