Skip to content

Commit b87b335

Browse files
committed
add support for cfg feature attributes on expression #4063
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent 0598182 commit b87b335

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

crates/ra_hir_def/src/body/lower.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ impl ExprCollector<'_> {
141141

142142
fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
143143
let syntax_ptr = AstPtr::new(&expr);
144+
let attrs = self.expander.parse_attrs(&expr);
145+
if !self.expander.is_cfg_enabled(&attrs) {
146+
return self.missing_expr();
147+
}
144148
match expr {
145149
ast::Expr::IfExpr(e) => {
146150
let then_branch = self.collect_block_opt(e.then_branch());

crates/ra_hir_ty/src/tests.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,38 @@ fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() {
390390
assert_snapshot!(diagnostics, @r###""###);
391391
}
392392

393+
#[test]
394+
fn no_such_field_with_feature_flag_diagnostics_on_block_expr() {
395+
let diagnostics = TestDB::with_files(
396+
r#"
397+
//- /lib.rs crate:foo cfg:feature=foo
398+
struct S {
399+
#[cfg(feature = "foo")]
400+
foo: u32,
401+
#[cfg(not(feature = "foo"))]
402+
bar: u32,
403+
}
404+
405+
impl S {
406+
fn new(bar: u32) -> Self {
407+
#[cfg(feature = "foo")]
408+
{
409+
Self { foo: bar }
410+
}
411+
#[cfg(not(feature = "foo"))]
412+
{
413+
Self { bar }
414+
}
415+
}
416+
}
417+
"#,
418+
)
419+
.diagnostics()
420+
.0;
421+
422+
assert_snapshot!(diagnostics, @r###""###);
423+
}
424+
393425
#[test]
394426
fn no_such_field_with_feature_flag_diagnostics_on_struct_fields() {
395427
let diagnostics = TestDB::with_files(

0 commit comments

Comments
 (0)