mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-16 01:38:13 +00:00
Merge #579
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:
commit
3508ba9bc2
3 changed files with 24 additions and 2 deletions
|
@ -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 a new issue