]> git.lizzy.rs Git - rust.git/commitdiff
Fix coercion from &Foo to an inference variable in a reference
authorFlorian Diebold <flodiebold@gmail.com>
Sun, 8 Dec 2019 10:23:05 +0000 (11:23 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Sun, 8 Dec 2019 12:06:59 +0000 (13:06 +0100)
We didn't try to unify within the reference, but we should.

crates/ra_hir_ty/src/infer/coerce.rs
crates/ra_hir_ty/src/tests/coercion.rs

index 9daa77cfa92d9756a551f8d77676e1bebe5d34cc..0f4dac45edf2b454117bc9ed96af9e133815205e 100644 (file)
@@ -332,7 +332,11 @@ fn unify_autoderef_behind_ref(&mut self, from_ty: &Ty, to_ty: &Ty) -> bool {
                     // It will not recurse to `coerce`.
                     return self.table.unify_substs(st1, st2, 0);
                 }
-                _ => {}
+                _ => {
+                    if self.table.unify_inner_trivial(&derefed_ty, &to_ty) {
+                        return true;
+                    }
+                }
             }
         }
 
index 58b22396fc9cf978189c4280334b498d1c0e0ac2..ac9e3872a5bdcc672e9481b800dca2c06093cf7c 100644 (file)
@@ -403,3 +403,40 @@ fn test() {
     "###
     );
 }
+
+#[test]
+fn coerce_autoderef_generic() {
+    assert_snapshot!(
+        infer_with_mismatches(r#"
+struct Foo;
+fn takes_ref<T>(x: &T) -> T { *x }
+fn test() {
+    takes_ref(&Foo);
+    takes_ref(&&Foo);
+    takes_ref(&&&Foo);
+}
+"#, true),
+        @r###"
+    [29; 30) 'x': &T
+    [41; 47) '{ *x }': T
+    [43; 45) '*x': T
+    [44; 45) 'x': &T
+    [58; 127) '{     ...oo); }': ()
+    [64; 73) 'takes_ref': fn takes_ref<Foo>(&T) -> T
+    [64; 79) 'takes_ref(&Foo)': Foo
+    [74; 78) '&Foo': &Foo
+    [75; 78) 'Foo': Foo
+    [85; 94) 'takes_ref': fn takes_ref<&Foo>(&T) -> T
+    [85; 101) 'takes_...&&Foo)': &Foo
+    [95; 100) '&&Foo': &&Foo
+    [96; 100) '&Foo': &Foo
+    [97; 100) 'Foo': Foo
+    [107; 116) 'takes_ref': fn takes_ref<&&Foo>(&T) -> T
+    [107; 124) 'takes_...&&Foo)': &&Foo
+    [117; 123) '&&&Foo': &&&Foo
+    [118; 123) '&&Foo': &&Foo
+    [119; 123) '&Foo': &Foo
+    [120; 123) 'Foo': Foo
+    "###
+    );
+}