From bed115d6e1a634d79e175e543a7936cd03200ebc Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sun, 3 May 2020 17:56:45 +0200 Subject: add support of cfg attributes on enum variants #4279 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_hir_def/src/adt.rs | 8 +++++++- crates/ra_hir_ty/src/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index 8eef51828..d0912ddaa 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -117,7 +117,13 @@ fn lower_enum( ast: &InFile, module_id: ModuleId, ) { - for var in ast.value.variant_list().into_iter().flat_map(|it| it.variants()) { + let expander = CfgExpander::new(db, ast.file_id, module_id.krate); + let variants = + ast.value.variant_list().into_iter().flat_map(|it| it.variants()).filter(|var| { + let attrs = expander.parse_attrs(var); + expander.is_cfg_enabled(&attrs) + }); + for var in variants { trace.alloc( || var.clone(), || EnumVariantData { diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs index 588d81282..d60732e19 100644 --- a/crates/ra_hir_ty/src/tests.rs +++ b/crates/ra_hir_ty/src/tests.rs @@ -360,6 +360,33 @@ fn no_such_field_with_feature_flag_diagnostics() { assert_snapshot!(diagnostics, @r###""###); } +#[test] +fn no_such_field_enum_with_feature_flag_diagnostics() { + let diagnostics = TestDB::with_files( + r#" + //- /lib.rs crate:foo cfg:feature=foo + enum Foo { + #[cfg(not(feature = "foo"))] + Buz, + #[cfg(feature = "foo")] + Bar, + Baz + } + + fn test_fn(f: Foo) { + match f { + Foo::Bar => {}, + Foo::Baz => {}, + } + } + "#, + ) + .diagnostics() + .0; + + assert_snapshot!(diagnostics, @r###""###); +} + #[test] fn no_such_field_with_feature_flag_diagnostics_on_struct_lit() { let diagnostics = TestDB::with_files( -- cgit v1.2.3