Skip to content

Commit 8b084cf

Browse files
committed
Fix blocks not considering stmt without semi as tails
1 parent 58d5c69 commit 8b084cf

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,26 @@ impl ExprCollector<'_> {
690690
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
691691
let prev_local_module = mem::replace(&mut self.expander.module, module);
692692

693-
let statements = block.statements().filter_map(|s| self.collect_stmt(s)).collect();
693+
let mut statements: Vec<_> =
694+
block.statements().filter_map(|s| self.collect_stmt(s)).collect();
694695
let tail = block.tail_expr().and_then(|e| self.maybe_collect_expr(e));
696+
let tail = tail.or_else(|| {
697+
let stmt = statements.pop()?;
698+
if let Statement::Expr { expr, has_semi: false } = stmt {
699+
return Some(expr);
700+
}
701+
statements.push(stmt);
702+
None
703+
});
695704

696705
let syntax_node_ptr = AstPtr::new(&block.into());
697706
let expr_id = self.alloc_expr(
698-
Expr::Block { id: block_id, statements, tail, label: None },
707+
Expr::Block {
708+
id: block_id,
709+
statements: statements.into_boxed_slice(),
710+
tail,
711+
label: None,
712+
},
699713
syntax_node_ptr,
700714
);
701715

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use expect_test::expect;
22
use test_utils::{bench, bench_fixture, skip_slow_tests};
33

4+
use crate::tests::check_infer_with_mismatches;
5+
46
use super::{check_infer, check_types};
57

68
#[test]
@@ -1247,3 +1249,27 @@ fn infinitely_recursive_macro_type() {
12471249
"#]],
12481250
);
12491251
}
1252+
#[test]
1253+
fn cfg_tails() {
1254+
check_infer_with_mismatches(
1255+
r#"
1256+
//- /lib.rs crate:foo cfg:feature=foo
1257+
struct S {}
1258+
1259+
impl S {
1260+
fn new2(bar: u32) -> Self {
1261+
#[cfg(feature = "foo")]
1262+
{ Self { } }
1263+
#[cfg(not(feature = "foo"))]
1264+
{ Self { } }
1265+
}
1266+
}
1267+
"#,
1268+
expect![[r#"
1269+
34..37 'bar': u32
1270+
52..170 '{ ... }': S
1271+
62..106 '#[cfg(... { } }': S
1272+
96..104 'Self { }': S
1273+
"#]],
1274+
);
1275+
}

0 commit comments

Comments
 (0)