From 20487a1b4a7c2fdffdb1de61c7837ee6f673f21a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 3 Jun 2021 23:12:35 +0200 Subject: Fix coercion in match with expected type Plus add infrastructure to test type mismatches without expect. --- crates/hir_ty/src/tests/coercion.rs | 43 ++++++++++++++++++++-------- crates/hir_ty/src/tests/patterns.rs | 57 +++++++++++-------------------------- 2 files changed, 48 insertions(+), 52 deletions(-) (limited to 'crates/hir_ty/src/tests') diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index 6dac7e103..71047703d 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -1,6 +1,6 @@ use expect_test::expect; -use super::{check_infer, check_infer_with_mismatches, check_types}; +use super::{check_infer, check_infer_with_mismatches, check_no_mismatches, check_types}; #[test] fn infer_block_expr_type_mismatch() { @@ -963,7 +963,7 @@ fn test() -> i32 { #[test] fn panic_macro() { - check_infer_with_mismatches( + check_no_mismatches( r#" mod panic { #[macro_export] @@ -991,15 +991,34 @@ fn main() { panic!() } "#, - expect![[r#" - 174..185 '{ loop {} }': ! - 176..183 'loop {}': ! - 181..183 '{}': () - !0..24 '$crate...:panic': fn panic() -> ! - !0..26 '$crate...anic()': ! - !0..26 '$crate...anic()': ! - !0..28 '$crate...015!()': ! - 454..470 '{ ...c!() }': () - "#]], + ); +} + +#[test] +fn coerce_unsize_expected_type() { + check_no_mismatches( + r#" +#[lang = "sized"] +pub trait Sized {} +#[lang = "unsize"] +pub trait Unsize {} +#[lang = "coerce_unsized"] +pub trait CoerceUnsized {} + +impl, U> CoerceUnsized<&U> for &T {} + +fn main() { + let foo: &[u32] = &[1, 2]; + let foo: &[u32] = match true { + true => &[1, 2], + false => &[1, 2, 3], + }; + let foo: &[u32] = if true { + &[1, 2] + } else { + &[1, 2, 3] + }; +} + "#, ); } diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs index 7d00cee9b..aa513c56d 100644 --- a/crates/hir_ty/src/tests/patterns.rs +++ b/crates/hir_ty/src/tests/patterns.rs @@ -1,6 +1,6 @@ use expect_test::expect; -use super::{check_infer, check_infer_with_mismatches, check_types}; +use super::{check_infer, check_infer_with_mismatches, check_mismatches, check_types}; #[test] fn infer_pattern() { @@ -518,47 +518,24 @@ fn infer_generics_in_patterns() { #[test] fn infer_const_pattern() { - check_infer_with_mismatches( + check_mismatches( r#" - enum Option { None } - use Option::None; - struct Foo; - const Bar: usize = 1; - - fn test() { - let a: Option = None; - let b: Option = match a { - None => None, - }; - let _: () = match () { Foo => Foo }; // Expected mismatch - let _: () = match () { Bar => Bar }; // Expected mismatch - } +enum Option { None } +use Option::None; +struct Foo; +const Bar: usize = 1; + +fn test() { + let a: Option = None; + let b: Option = match a { + None => None, + }; + let _: () = match () { Foo => () }; + // ^^^ expected (), got Foo + let _: () = match () { Bar => () }; + // ^^^ expected (), got usize +} "#, - expect![[r#" - 73..74 '1': usize - 87..309 '{ ...atch }': () - 97..98 'a': Option - 114..118 'None': Option - 128..129 'b': Option - 145..182 'match ... }': Option - 151..152 'a': Option - 163..167 'None': Option - 171..175 'None': Option - 192..193 '_': () - 200..223 'match ... Foo }': Foo - 206..208 '()': () - 211..214 'Foo': Foo - 218..221 'Foo': Foo - 254..255 '_': () - 262..285 'match ... Bar }': usize - 268..270 '()': () - 273..276 'Bar': usize - 280..283 'Bar': usize - 200..223: expected (), got Foo - 211..214: expected (), got Foo - 262..285: expected (), got usize - 273..276: expected (), got usize - "#]], ); } -- cgit v1.2.3