Fix panic on inferring field access on an enum

This commit is contained in:
Florian Diebold 2019-01-20 18:08:25 +01:00
parent 1c296d54e3
commit 3c4a4c6ae9
3 changed files with 24 additions and 2 deletions

View File

@ -688,9 +688,13 @@ pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name)
Def::Struct(s) => (s.variant_data(db), s.generic_params(db)), Def::Struct(s) => (s.variant_data(db), s.generic_params(db)),
Def::EnumVariant(ev) => (ev.variant_data(db), ev.parent_enum(db).generic_params(db)), Def::EnumVariant(ev) => (ev.variant_data(db), ev.parent_enum(db).generic_params(db)),
// TODO: unions // TODO: unions
Def::Enum(_) => {
// this can happen in (invalid) code, but enums don't have fields themselves
return None;
}
_ => panic!( _ => panic!(
"trying to get type for field in non-struct/variant {:?}", "trying to get type for field {:?} in non-struct/variant {:?}",
def_id field, def_id
), ),
}; };
let module = def_id.module(db); let module = def_id.module(db);

View File

@ -507,6 +507,20 @@ fn test() -> i128 {
); );
} }
#[test]
fn no_panic_on_field_of_enum() {
check_inference(
r#"
enum X {}
fn test(x: X) {
x.some_field;
}
"#,
"no_panic_on_field_of_enum.txt",
);
}
fn infer(content: &str) -> String { fn infer(content: &str) -> String {
let (db, _, file_id) = MockDatabase::with_single_file(content); let (db, _, file_id) = MockDatabase::with_single_file(content);
let source_file = db.source_file(file_id); let source_file = db.source_file(file_id);

View File

@ -0,0 +1,4 @@
[20; 21) 'x': X
[26; 47) '{ ...eld; }': ()
[32; 33) 'x': X
[32; 44) 'x.some_field': [unknown]