From ebbcf9f458522e76c5a84e6771e0ef434d6d5c5b Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 7 Apr 2021 12:45:17 +0100 Subject: Fix inference with conditionally compiled tails Fixes #8378 --- crates/hir_ty/src/diagnostics/expr.rs | 2 +- crates/hir_ty/src/infer/expr.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 79602c3dd..47709c1e8 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -83,7 +83,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { if let Expr::Block { statements, tail, .. } = body_expr { if let Some(t) = tail { self.validate_results_in_tail_expr(body.body_expr, *t, db); - } else if let Some(Statement::Expr(id)) = statements.last() { + } else if let Some(Statement::Expr { expr: id, .. }) = statements.last() { self.validate_missing_tail_expr(body.body_expr, *id, db); } } diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 50497eecb..9476e6297 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -809,7 +809,7 @@ impl<'a> InferenceContext<'a> { let ty = self.resolve_ty_as_possible(ty); self.infer_pat(*pat, &ty, BindingMode::default()); } - Statement::Expr(expr) => { + Statement::Expr { expr, .. } => { self.infer_expr(*expr, &Expectation::none()); } } -- cgit v1.2.3 From 11c926fd97d5efb61a771455fc233afe33939569 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 11 Apr 2021 10:03:39 +0100 Subject: Add a test for conditionally compiled tails --- crates/hir_ty/src/tests/regression.rs | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index d14f5c9bb..e23bd4da9 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs @@ -1050,3 +1050,52 @@ fn test() { "#]], ); } + +#[test] +fn cfg_tail() { + // https://github.com/rust-analyzer/rust-analyzer/issues/8378 + check_infer( + r#" + fn fake_tail(){ + { "first" } + #[cfg(never)] 9 + } + fn multiple_fake(){ + { "fake" } + { "fake" } + { "second" } + #[cfg(never)] { 11 } + #[cfg(never)] 12; + #[cfg(never)] 13 + } + fn no_normal_tail(){ + { "third" } + #[cfg(never)] 14; + #[cfg(never)] 15; + } + fn no_actual_tail(){ + { "fourth" }; + #[cfg(never)] 14; + #[cfg(never)] 15 + } + "#, + expect![[r#" + 14..53 '{ ...)] 9 }': &str + 20..31 '{ "first" }': &str + 22..29 '"first"': &str + 72..190 '{ ...] 13 }': &str + 78..88 '{ "fake" }': &str + 80..86 '"fake"': &str + 93..103 '{ "fake" }': &str + 95..101 '"fake"': &str + 108..120 '{ "second" }': &str + 110..118 '"second"': &str + 210..273 '{ ... 15; }': &str + 216..227 '{ "third" }': &str + 218..225 '"third"': &str + 293..357 '{ ...] 15 }': () + 299..311 '{ "fourth" }': &str + 301..309 '"fourth"': &str + "#]], + ) +} -- cgit v1.2.3