add support of cfg attributes on enum variants #4279

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
Benjamin Coenen 2020-05-03 17:56:45 +02:00
parent 682c079043
commit bed115d6e1
2 changed files with 34 additions and 1 deletions

View file

@ -117,7 +117,13 @@ fn lower_enum(
ast: &InFile<ast::EnumDef>,
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 {

View file

@ -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(