579: Fix panic on inferring field access on an enum r=matklad a=flodiebold

Seen while skipping through https://youtu.be/ANKBNiSWyfc ;)

Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
bors[bot] 2019-01-20 17:26:26 +00:00
commit 3508ba9bc2
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::EnumVariant(ev) => (ev.variant_data(db), ev.parent_enum(db).generic_params(db)),
// TODO: unions
Def::Enum(_) => {
// this can happen in (invalid) code, but enums don't have fields themselves
return None;
}
_ => panic!(
"trying to get type for field in non-struct/variant {:?}",
def_id
"trying to get type for field {:?} in non-struct/variant {:?}",
field, def_id
),
};
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 {
let (db, _, file_id) = MockDatabase::with_single_file(content);
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]