From de39d221a15c0a146ed8adbdb1616692180948bb Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 21 Feb 2020 18:24:18 +0100 Subject: Implement unsize coercion using proper trait solving --- crates/ra_hir_ty/src/tests/coercion.rs | 82 ++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index 42330b269..aa2dfb5f0 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs @@ -548,3 +548,85 @@ impl S { "### ); } + +#[test] +fn coerce_unsize_array() { + assert_snapshot!( + infer_with_mismatches(r#" +#[lang = "unsize"] +pub trait Unsize {} +#[lang = "coerce_unsized"] +pub trait CoerceUnsized {} + +impl, U> CoerceUnsized<&U> for &T {} + +fn test() { + let f: &[usize] = &[1, 2, 3]; +} +"#, true), + @r###" + [162; 199) '{ ... 3]; }': () + [172; 173) 'f': &[usize] + [186; 196) '&[1, 2, 3]': &[usize; _] + [187; 196) '[1, 2, 3]': [usize; _] + [188; 189) '1': usize + [191; 192) '2': usize + [194; 195) '3': usize + "### + ); +} + +#[ignore] +#[test] +fn coerce_unsize_trait_object() { + assert_snapshot!( + infer_with_mismatches(r#" +#[lang = "unsize"] +pub trait Unsize {} +#[lang = "coerce_unsized"] +pub trait CoerceUnsized {} + +impl, U> CoerceUnsized<&U> for &T {} + +trait Foo {} +trait Bar: Foo {} +struct S; +impl Foo for S {} +impl Bar for S {} + +fn test() { + let obj: &dyn Bar = &S; + let obj: &dyn Foo = obj; +} +"#, true), + @r###" + "### + ); +} + +#[ignore] +#[test] +fn coerce_unsize_generic() { + // FIXME: Implement this + // https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions + assert_snapshot!( + infer_with_mismatches(r#" +#[lang = "unsize"] +pub trait Unsize {} +#[lang = "coerce_unsized"] +pub trait CoerceUnsized {} + +impl, U> CoerceUnsized<&U> for &T {} + +struct Foo { t: T }; +struct Bar(Foo); + +fn test() { + let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; + let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); +} +"#, true), + @r###" + "### + ); +} -- cgit v1.2.3