4285: add support of cfg attributes on enum variants r=edwin0cheng a=bnjjj

close #4279

Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-05-04 03:18:48 +00:00 committed by GitHub
commit d855280818
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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(