mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 20:23:59 +00:00
Fix panic on inferring field access on an enum
This commit is contained in:
parent
1c296d54e3
commit
3c4a4c6ae9
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -0,0 +1,4 @@
|
||||
[20; 21) 'x': X
|
||||
[26; 47) '{ ...eld; }': ()
|
||||
[32; 33) 'x': X
|
||||
[32; 44) 'x.some_field': [unknown]
|
Loading…
Reference in New Issue
Block a user