]> git.lizzy.rs Git - rust.git/commitdiff
Add tests for autoderef on block tail
authorbogon-right <bogon-right@bytedance.com>
Sun, 16 Oct 2022 20:03:23 +0000 (04:03 +0800)
committerbogon-right <bogon-right@bytedance.com>
Sun, 16 Oct 2022 20:03:23 +0000 (04:03 +0800)
src/test/ui/coercion/coerce-block-tail-26978.rs [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-26978.stderr [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-57749.rs [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-57749.stderr [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-83783.rs [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-83783.stderr [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-83850.rs [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail-83850.stderr [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail.rs [new file with mode: 0644]
src/test/ui/coercion/coerce-block-tail.stderr [new file with mode: 0644]

diff --git a/src/test/ui/coercion/coerce-block-tail-26978.rs b/src/test/ui/coercion/coerce-block-tail-26978.rs
new file mode 100644 (file)
index 0000000..01c8ab5
--- /dev/null
@@ -0,0 +1,11 @@
+// check-fail
+fn f(_: &i32) {}
+
+fn main() {
+    let x = Box::new(1i32);
+
+    f(&x);
+    f(&(x));
+    f(&{x});
+    //~^ ERROR mismatched types
+}
diff --git a/src/test/ui/coercion/coerce-block-tail-26978.stderr b/src/test/ui/coercion/coerce-block-tail-26978.stderr
new file mode 100644 (file)
index 0000000..384debd
--- /dev/null
@@ -0,0 +1,16 @@
+error[E0308]: mismatched types
+  --> $DIR/coerce-block-tail-26978.rs:9:9
+   |
+LL |     f(&{x});
+   |         ^ expected `i32`, found struct `Box`
+   |
+   = note: expected type `i32`
+            found struct `Box<i32>`
+help: consider unboxing the value
+   |
+LL |     f(&{*x});
+   |         +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/coercion/coerce-block-tail-57749.rs b/src/test/ui/coercion/coerce-block-tail-57749.rs
new file mode 100644 (file)
index 0000000..79b5b33
--- /dev/null
@@ -0,0 +1,35 @@
+// check-fail
+use std::ops::Deref;
+
+fn main() {
+    fn save(who: &str) {
+        println!("I'll save you, {}!", who);
+    }
+
+    struct Madoka;
+
+    impl Deref for Madoka {
+        type Target = str;
+        fn deref(&self) -> &Self::Target {
+            "Madoka"
+        }
+    }
+
+    save(&{ Madoka });
+
+    fn reset(how: &u32) {
+        println!("Reset {} times", how);
+    }
+
+    struct Homura;
+
+    impl Deref for Homura {
+        type Target = u32;
+        fn deref(&self) -> &Self::Target {
+            &42
+        }
+    }
+
+    reset(&{ Homura });
+    //~^ ERROR mismatched types
+}
diff --git a/src/test/ui/coercion/coerce-block-tail-57749.stderr b/src/test/ui/coercion/coerce-block-tail-57749.stderr
new file mode 100644 (file)
index 0000000..d5660c8
--- /dev/null
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+  --> $DIR/coerce-block-tail-57749.rs:33:14
+   |
+LL |     reset(&{ Homura });
+   |              ^^^^^^ expected `u32`, found struct `Homura`
+   |
+help: consider dereferencing the type
+   |
+LL |     reset(&{ *Homura });
+   |              +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/coercion/coerce-block-tail-83783.rs b/src/test/ui/coercion/coerce-block-tail-83783.rs
new file mode 100644 (file)
index 0000000..18c8ae3
--- /dev/null
@@ -0,0 +1,13 @@
+// check-fail
+// edition:2018
+fn _consume_reference<T: ?Sized>(_: &T) {}
+
+async fn _foo() {
+    _consume_reference::<i32>(&Box::new(7_i32));
+    _consume_reference::<i32>(&async { Box::new(7_i32) }.await);
+    //~^ ERROR mismatched types
+    _consume_reference::<[i32]>(&vec![7_i32]);
+    _consume_reference::<[i32]>(&async { vec![7_i32] }.await);
+}
+
+fn main() { }
diff --git a/src/test/ui/coercion/coerce-block-tail-83783.stderr b/src/test/ui/coercion/coerce-block-tail-83783.stderr
new file mode 100644 (file)
index 0000000..5f53606
--- /dev/null
@@ -0,0 +1,12 @@
+error[E0308]: mismatched types
+  --> $DIR/coerce-block-tail-83783.rs:7:32
+   |
+LL |     _consume_reference::<i32>(&async { Box::new(7_i32) }.await);
+   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found struct `Box`
+   |
+   = note: expected type `i32`
+            found struct `Box<i32>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/coercion/coerce-block-tail-83850.rs b/src/test/ui/coercion/coerce-block-tail-83850.rs
new file mode 100644 (file)
index 0000000..77fdf99
--- /dev/null
@@ -0,0 +1,7 @@
+// check-fail
+fn f(_: &[i32]) {}
+
+fn main() {
+    f(&Box::new([1, 2]));
+    //~^ ERROR mismatched types
+}
diff --git a/src/test/ui/coercion/coerce-block-tail-83850.stderr b/src/test/ui/coercion/coerce-block-tail-83850.stderr
new file mode 100644 (file)
index 0000000..bbf6075
--- /dev/null
@@ -0,0 +1,19 @@
+error[E0308]: mismatched types
+  --> $DIR/coerce-block-tail-83850.rs:5:7
+   |
+LL |     f(&Box::new([1, 2]));
+   |     - ^^^^^^^^^^^^^^^^^ expected slice `[i32]`, found struct `Box`
+   |     |
+   |     arguments to this function are incorrect
+   |
+   = note: expected reference `&[i32]`
+              found reference `&Box<[{integer}; 2]>`
+note: function defined here
+  --> $DIR/coerce-block-tail-83850.rs:2:4
+   |
+LL | fn f(_: &[i32]) {}
+   |    ^ ---------
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/coercion/coerce-block-tail.rs b/src/test/ui/coercion/coerce-block-tail.rs
new file mode 100644 (file)
index 0000000..dcbcd37
--- /dev/null
@@ -0,0 +1,6 @@
+// check-fail
+fn main() {
+    let _: &str = & { String::from("hahah")};
+    let _: &i32 = & { Box::new(1i32) };
+    //~^ ERROR mismatched types
+}
diff --git a/src/test/ui/coercion/coerce-block-tail.stderr b/src/test/ui/coercion/coerce-block-tail.stderr
new file mode 100644 (file)
index 0000000..318cf75
--- /dev/null
@@ -0,0 +1,16 @@
+error[E0308]: mismatched types
+  --> $DIR/coerce-block-tail.rs:4:23
+   |
+LL |     let _: &i32 = & { Box::new(1i32) };
+   |                       ^^^^^^^^^^^^^^ expected `i32`, found struct `Box`
+   |
+   = note: expected type `i32`
+            found struct `Box<i32>`
+help: consider unboxing the value
+   |
+LL |     let _: &i32 = & { *Box::new(1i32) };
+   |                       +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.