Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/tests/coercion.rs')
-rw-r--r--crates/hir-ty/src/tests/coercion.rs111
1 files changed, 105 insertions, 6 deletions
diff --git a/crates/hir-ty/src/tests/coercion.rs b/crates/hir-ty/src/tests/coercion.rs
index b524922b6c..16e5ef85d0 100644
--- a/crates/hir-ty/src/tests/coercion.rs
+++ b/crates/hir-ty/src/tests/coercion.rs
@@ -258,7 +258,6 @@ fn test() {
#[test]
fn coerce_autoderef_block() {
- // FIXME: We should know mutability in overloaded deref
check_no_mismatches(
r#"
//- minicore: deref
@@ -268,7 +267,7 @@ fn takes_ref_str(x: &str) {}
fn returns_string() -> String { loop {} }
fn test() {
takes_ref_str(&{ returns_string() });
- // ^^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(None), Deref(Some(OverloadedDeref(None))), Borrow(Ref(Not))
+ // ^^^^^^^^^^^^^^^^^^^^^ adjustments: Deref(None), Deref(Some(OverloadedDeref(Some(Not)))), Borrow(Ref(Not))
}
"#,
);
@@ -397,9 +396,39 @@ fn test() {
}
#[test]
+fn coerce_fn_item_to_fn_ptr_in_array() {
+ check_no_mismatches(
+ r"
+fn foo(x: u32) -> isize { 1 }
+fn bar(x: u32) -> isize { 1 }
+fn test() {
+ let f = [foo, bar];
+ // ^^^ adjustments: Pointer(ReifyFnPointer)
+}",
+ );
+}
+
+#[test]
fn coerce_fn_items_in_match_arms() {
cov_mark::check!(coerce_fn_reification);
+ check_no_mismatches(
+ r"
+fn foo1(x: u32) -> isize { 1 }
+fn foo2(x: u32) -> isize { 2 }
+fn foo3(x: u32) -> isize { 3 }
+fn test() {
+ let x = match 1 {
+ 1 => foo1,
+ // ^^^^ adjustments: Pointer(ReifyFnPointer)
+ 2 => foo2,
+ // ^^^^ adjustments: Pointer(ReifyFnPointer)
+ _ => foo3,
+ // ^^^^ adjustments: Pointer(ReifyFnPointer)
+ };
+ x;
+}",
+ );
check_types(
r"
fn foo1(x: u32) -> isize { 1 }
@@ -507,7 +536,6 @@ fn test() {
#[test]
fn coerce_unsize_generic() {
- // FIXME: fix the type mismatches here
check(
r#"
//- minicore: coerce_unsized
@@ -516,9 +544,9 @@ struct Bar<T>(Foo<T>);
fn test() {
let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
- //^^^^^^^^^ expected [usize], got [usize; 3]
+ //^^^^^^^^^^^^^^^^^^^^^ expected &Foo<[usize]>, got &Foo<[i32; 3]>
let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
- //^^^^^^^^^ expected [usize], got [usize; 3]
+ //^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &Bar<[usize]>, got &Bar<[i32; 3]>
}
"#,
);
@@ -547,7 +575,7 @@ fn two_closures_lub() {
fn foo(c: i32) {
let add = |a: i32, b: i32| a + b;
let sub = |a, b| a - b;
- //^^^^^^^^^^^^ |i32, i32| -> i32
+ //^^^^^^^^^^^^ impl Fn(i32, i32) -> i32
if c > 42 { add } else { sub };
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fn(i32, i32) -> i32
}
@@ -842,3 +870,74 @@ fn test() {
}",
);
}
+
+#[test]
+fn adjust_index() {
+ check_no_mismatches(
+ r"
+//- minicore: index, slice, coerce_unsized
+fn test() {
+ let x = [1, 2, 3];
+ x[2] = 6;
+ // ^ adjustments: Borrow(Ref(Mut))
+}
+ ",
+ );
+ check_no_mismatches(
+ r"
+//- minicore: index
+struct Struct;
+impl core::ops::Index<usize> for Struct {
+ type Output = ();
+
+ fn index(&self, index: usize) -> &Self::Output { &() }
+}
+struct StructMut;
+
+impl core::ops::Index<usize> for StructMut {
+ type Output = ();
+
+ fn index(&self, index: usize) -> &Self::Output { &() }
+}
+impl core::ops::IndexMut for StructMut {
+ fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut () }
+}
+fn test() {
+ Struct[0];
+ // ^^^^^^ adjustments: Borrow(Ref(Not))
+ StructMut[0];
+ // ^^^^^^^^^ adjustments: Borrow(Ref(Not))
+ &mut StructMut[0];
+ // ^^^^^^^^^ adjustments: Borrow(Ref(Mut))
+}",
+ );
+}
+
+#[test]
+fn regression_14443_dyn_coercion_block_impls() {
+ check_no_mismatches(
+ r#"
+//- minicore: coerce_unsized
+trait T {}
+
+fn dyn_t(d: &dyn T) {}
+
+fn main() {
+ struct A;
+ impl T for A {}
+
+ let a = A;
+
+ let b = {
+ struct B;
+ impl T for B {}
+
+ B
+ };
+
+ dyn_t(&a);
+ dyn_t(&b);
+}
+"#,
+ )
+}