From b87b335e6859ead3baa7fde20e3ea2ac69f63d85 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sat, 25 Apr 2020 15:48:04 +0200 Subject: add support for cfg feature attributes on expression #4063 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_hir_def/src/body/lower.rs | 4 ++++ crates/ra_hir_ty/src/tests.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 0caedd8d8..571603854 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -141,6 +141,10 @@ impl ExprCollector<'_> { fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { let syntax_ptr = AstPtr::new(&expr); + let attrs = self.expander.parse_attrs(&expr); + if !self.expander.is_cfg_enabled(&attrs) { + return self.missing_expr(); + } match expr { ast::Expr::IfExpr(e) => { let then_branch = self.collect_block_opt(e.then_branch()); diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index b6a96bb5c..588d81282 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -390,6 +390,38 @@ fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() { assert_snapshot!(diagnostics, @r###""###); } +#[test] +fn no_such_field_with_feature_flag_diagnostics_on_block_expr() { + let diagnostics = TestDB::with_files( + r#" + //- /lib.rs crate:foo cfg:feature=foo + struct S { + #[cfg(feature = "foo")] + foo: u32, + #[cfg(not(feature = "foo"))] + bar: u32, + } + + impl S { + fn new(bar: u32) -> Self { + #[cfg(feature = "foo")] + { + Self { foo: bar } + } + #[cfg(not(feature = "foo"))] + { + Self { bar } + } + } + } + "#, + ) + .diagnostics() + .0; + + assert_snapshot!(diagnostics, @r###""###); +} + #[test] fn no_such_field_with_feature_flag_diagnostics_on_struct_fields() { let diagnostics = TestDB::with_files( -- cgit v1.2.3