From f86fe3d891ab295e9e394a1338da86524a6205d3 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 5 Dec 2019 23:02:31 +0100 Subject: Don't unify within a reference If we are expecting a `&Foo` and get a `&something`, when checking the `something`, we are *expecting* a `Foo`, but we shouldn't try to unify whatever we get with that expectation, because it could actually be a `&Foo`, and `&&Foo` coerces to `&Foo`. So this fixes quite a few false type mismatches. --- crates/ra_hir_ty/src/tests/coercion.rs | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'crates/ra_hir_ty/src/tests/coercion.rs') diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index 1530fcc63..58b22396f 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs @@ -1,3 +1,4 @@ +use super::infer_with_mismatches; use insta::assert_snapshot; use test_utils::covers; @@ -367,3 +368,38 @@ fn test() { "### ); } + +#[test] +fn coerce_autoderef() { + assert_snapshot!( + infer_with_mismatches(r#" +struct Foo; +fn takes_ref_foo(x: &Foo) {} +fn test() { + takes_ref_foo(&Foo); + takes_ref_foo(&&Foo); + takes_ref_foo(&&&Foo); +} +"#, true), + @r###" + [30; 31) 'x': &Foo + [39; 41) '{}': () + [52; 133) '{ ...oo); }': () + [58; 71) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> () + [58; 77) 'takes_...(&Foo)': () + [72; 76) '&Foo': &Foo + [73; 76) 'Foo': Foo + [83; 96) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> () + [83; 103) 'takes_...&&Foo)': () + [97; 102) '&&Foo': &&Foo + [98; 102) '&Foo': &Foo + [99; 102) 'Foo': Foo + [109; 122) 'takes_ref_foo': fn takes_ref_foo(&Foo) -> () + [109; 130) 'takes_...&&Foo)': () + [123; 129) '&&&Foo': &&&Foo + [124; 129) '&&Foo': &&Foo + [125; 129) '&Foo': &Foo + [126; 129) 'Foo': Foo + "### + ); +} -- cgit v1.2.3