From 5fe220b9873d587188adae63fa205481a9aae9ce Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Feb 2020 15:31:07 +0100 Subject: Fix a common false-positive type mismatch E.g. for `&{ some_string() }` in a context where a `&str` is expected, we reported a mismatch inside the block. The problem is that we're passing an expectation of `str` down, but the expectation is more of a hint in this case. There's a long comment in rustc about this, which I just copied. Also, fix reported location for type mismatches in macros. --- crates/ra_hir_ty/src/tests/coercion.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 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 60ad6e9be..1e303f5ce 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs @@ -457,6 +457,37 @@ fn test() { ); } +#[test] +fn coerce_autoderef_block() { + assert_snapshot!( + infer_with_mismatches(r#" +struct String {} +#[lang = "deref"] +trait Deref { type Target; } +impl Deref for String { type Target = str; } +fn takes_ref_str(x: &str) {} +fn returns_string() -> String { loop {} } +fn test() { + takes_ref_str(&{ returns_string() }); +} +"#, true), + @r###" + [127; 128) 'x': &str + [136; 138) '{}': () + [169; 180) '{ loop {} }': String + [171; 178) 'loop {}': ! + [176; 178) '{}': () + [191; 236) '{ ... }); }': () + [197; 210) 'takes_ref_str': fn takes_ref_str(&str) -> () + [197; 233) 'takes_...g() })': () + [211; 232) '&{ ret...ng() }': &String + [212; 232) '{ retu...ng() }': String + [214; 228) 'returns_string': fn returns_string() -> String + [214; 230) 'return...ring()': String + "### + ); +} + #[test] fn closure_return_coerce() { assert_snapshot!( -- cgit v1.2.3