Skip to content

Commit e5e5a09

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

File tree

3 files changed

+46
-5
lines changed

3 files changed

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,17 +1013,17 @@ fn cfg_tail() {
10131013
"#,
10141014
expect![[r#"
10151015
14..53 '{ ...)] 9 }': ()
1016-
20..31 '{ "first" }': &str
1016+
20..31 '{ "first" }': ()
10171017
22..29 '"first"': &str
10181018
72..190 '{ ...] 13 }': ()
10191019
78..88 '{ "fake" }': &str
10201020
80..86 '"fake"': &str
10211021
93..103 '{ "fake" }': &str
10221022
95..101 '"fake"': &str
1023-
108..120 '{ "second" }': &str
1023+
108..120 '{ "second" }': ()
10241024
110..118 '"second"': &str
10251025
210..273 '{ ... 15; }': ()
1026-
216..227 '{ "third" }': &str
1026+
216..227 '{ "third" }': ()
10271027
218..225 '"third"': &str
10281028
293..357 '{ ...] 15 }': ()
10291029
299..311 '{ "fourth" }': &str

0 commit comments

Comments
 (0)