From e45f476d5691a6a0d994eb6f777bbf291348f93e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 21 Jan 2019 21:42:19 +0100 Subject: Fix panic trying to get substs on unknown type Fixes #585. --- crates/ra_hir/src/ty.rs | 6 +++--- crates/ra_hir/src/ty/tests.rs | 16 ++++++++++++++++ crates/ra_hir/src/ty/tests/data/bug_585.txt | 8 ++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 crates/ra_hir/src/ty/tests/data/bug_585.txt (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index c602450e1..e690ae158 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -1134,7 +1134,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.unify(&ty, expected); - let substs = ty.substs().expect("adt should have substs"); + let substs = ty.substs().unwrap_or_else(Substs::empty); for (i, &subpat) in subpats.iter().enumerate() { let expected_ty = fields @@ -1155,7 +1155,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.unify(&ty, expected); - let substs = ty.substs().expect("adt should have substs"); + let substs = ty.substs().unwrap_or_else(Substs::empty); for subpat in subpats { let matching_field = fields.iter().find(|field| field.name() == &subpat.name); @@ -1403,7 +1403,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { spread, } => { let (ty, def_id) = self.resolve_variant(path.as_ref()); - let substs = ty.substs().expect("adt should have substs"); + let substs = ty.substs().unwrap_or_else(Substs::empty); for field in fields { let field_ty = if let Some(def_id) = def_id { self.db diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 5aa24a29b..dc9b626dc 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -521,6 +521,22 @@ fn test(x: X) { ); } +#[test] +fn bug_585() { + check_inference( + r#" +fn test() { + X {}; + match x { + A::B {} => (), + A::Y() => (), + } +} +"#, + "bug_585.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/bug_585.txt b/crates/ra_hir/src/ty/tests/data/bug_585.txt new file mode 100644 index 000000000..627702a38 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/bug_585.txt @@ -0,0 +1,8 @@ +[11; 89) '{ ... } }': () +[17; 21) 'X {}': [unknown] +[27; 87) 'match ... }': () +[33; 34) 'x': [unknown] +[45; 52) 'A::B {}': [unknown] +[56; 58) '()': () +[68; 74) 'A::Y()': [unknown] +[78; 80) '()': () -- cgit v1.2.3