From 3c4a4c6ae9f62c960ba118b6e1941ba006d221d7 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 20 Jan 2019 18:08:25 +0100 Subject: Fix panic on inferring field access on an enum --- crates/ra_hir/src/ty.rs | 8 ++++++-- crates/ra_hir/src/ty/tests.rs | 14 ++++++++++++++ .../ra_hir/src/ty/tests/data/no_panic_on_field_of_enum.txt | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 crates/ra_hir/src/ty/tests/data/no_panic_on_field_of_enum.txt (limited to 'crates') 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) 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); 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 { ); } +#[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); 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 @@ +[20; 21) 'x': X +[26; 47) '{ ...eld; }': () +[32; 33) 'x': X +[32; 44) 'x.some_field': [unknown] -- cgit v1.2.3