diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/no_panic_on_field_of_enum.txt | 4 |
3 files changed, 24 insertions, 2 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 9b7182485..c602450e1 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -688,9 +688,13 @@ pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) | |||
688 | Def::Struct(s) => (s.variant_data(db), s.generic_params(db)), | 688 | Def::Struct(s) => (s.variant_data(db), s.generic_params(db)), |
689 | Def::EnumVariant(ev) => (ev.variant_data(db), ev.parent_enum(db).generic_params(db)), | 689 | Def::EnumVariant(ev) => (ev.variant_data(db), ev.parent_enum(db).generic_params(db)), |
690 | // TODO: unions | 690 | // TODO: unions |
691 | Def::Enum(_) => { | ||
692 | // this can happen in (invalid) code, but enums don't have fields themselves | ||
693 | return None; | ||
694 | } | ||
691 | _ => panic!( | 695 | _ => panic!( |
692 | "trying to get type for field in non-struct/variant {:?}", | 696 | "trying to get type for field {:?} in non-struct/variant {:?}", |
693 | def_id | 697 | field, def_id |
694 | ), | 698 | ), |
695 | }; | 699 | }; |
696 | let module = def_id.module(db); | 700 | let module = def_id.module(db); |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 06e32df59..5aa24a29b 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -507,6 +507,20 @@ fn test() -> i128 { | |||
507 | ); | 507 | ); |
508 | } | 508 | } |
509 | 509 | ||
510 | #[test] | ||
511 | fn no_panic_on_field_of_enum() { | ||
512 | check_inference( | ||
513 | r#" | ||
514 | enum X {} | ||
515 | |||
516 | fn test(x: X) { | ||
517 | x.some_field; | ||
518 | } | ||
519 | "#, | ||
520 | "no_panic_on_field_of_enum.txt", | ||
521 | ); | ||
522 | } | ||
523 | |||
510 | fn infer(content: &str) -> String { | 524 | fn infer(content: &str) -> String { |
511 | let (db, _, file_id) = MockDatabase::with_single_file(content); | 525 | let (db, _, file_id) = MockDatabase::with_single_file(content); |
512 | let source_file = db.source_file(file_id); | 526 | let source_file = db.source_file(file_id); |
diff --git a/crates/ra_hir/src/ty/tests/data/no_panic_on_field_of_enum.txt b/crates/ra_hir/src/ty/tests/data/no_panic_on_field_of_enum.txt new file mode 100644 index 000000000..4dca0b7f4 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/no_panic_on_field_of_enum.txt | |||
@@ -0,0 +1,4 @@ | |||
1 | [20; 21) 'x': X | ||
2 | [26; 47) '{ ...eld; }': () | ||
3 | [32; 33) 'x': X | ||
4 | [32; 44) 'x.some_field': [unknown] | ||