about summary refs log tree commit diff
path: root/src/test/ui/box
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/ui/box')
-rw-r--r--src/test/ui/box/thin_align.rs26
-rw-r--r--src/test/ui/box/thin_drop.rs37
-rw-r--r--src/test/ui/box/thin_new.rs30
-rw-r--r--src/test/ui/box/thin_zst.rs34
4 files changed, 127 insertions, 0 deletions
diff --git a/src/test/ui/box/thin_align.rs b/src/test/ui/box/thin_align.rs
new file mode 100644
index 00000000000..3c61d0090e4
--- /dev/null
+++ b/src/test/ui/box/thin_align.rs
@@ -0,0 +1,26 @@
+#![feature(thin_box)]
+// run-pass
+use std::boxed::ThinBox;
+use std::error::Error;
+use std::ops::Deref;
+use std::fmt;
+
+fn main() {
+    let expected = "Foo error!";
+    let a: ThinBox<dyn Error> = ThinBox::new_unsize(Foo(expected));
+    let a = a.deref();
+    let msg = a.to_string();
+    assert_eq!(expected, msg);
+}
+
+#[derive(Debug)]
+#[repr(align(1024))]
+struct Foo(&'static str);
+
+impl fmt::Display for Foo {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+impl Error for Foo {}
diff --git a/src/test/ui/box/thin_drop.rs b/src/test/ui/box/thin_drop.rs
new file mode 100644
index 00000000000..965613c114e
--- /dev/null
+++ b/src/test/ui/box/thin_drop.rs
@@ -0,0 +1,37 @@
+#![feature(thin_box)]
+// run-pass
+use std::boxed::ThinBox;
+use std::error::Error;
+use std::ops::Deref;
+use std::fmt;
+
+fn main() {
+    let expected = "Foo error!";
+    let mut dropped = false;
+    {
+        let foo = Foo(expected, &mut dropped);
+        let a: ThinBox<dyn Error> = ThinBox::new_unsize(foo);
+        let a = a.deref();
+        let msg = a.to_string();
+        assert_eq!(expected, msg);
+    }
+    assert!(dropped);
+}
+
+#[derive(Debug)]
+#[repr(align(1024))]
+struct Foo<'a>(&'static str, &'a mut bool);
+
+impl Drop for Foo<'_> {
+    fn drop(&mut self) {
+        *self.1 = true;
+    }
+}
+
+impl fmt::Display for Foo<'_> {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", self.0)
+    }
+}
+
+impl Error for Foo<'_> {}
diff --git a/src/test/ui/box/thin_new.rs b/src/test/ui/box/thin_new.rs
new file mode 100644
index 00000000000..53f46478be4
--- /dev/null
+++ b/src/test/ui/box/thin_new.rs
@@ -0,0 +1,30 @@
+#![feature(thin_box)]
+// run-pass
+use std::boxed::ThinBox;
+use std::error::Error;
+use std::{fmt, mem};
+
+fn main() {
+    let thin_error: ThinBox<dyn Error> = ThinBox::new_unsize(Foo);
+    assert_eq!(mem::size_of::<*const i32>(), mem::size_of_val(&thin_error));
+    println!("{:?}", thin_error);
+
+    let thin = ThinBox::new(42i32);
+    assert_eq!(mem::size_of::<*const i32>(), mem::size_of_val(&thin));
+    println!("{:?}", thin);
+
+    let thin_slice = ThinBox::<[i32]>::new_unsize([1, 2, 3, 4]);
+    assert_eq!(mem::size_of::<*const i32>(), mem::size_of_val(&thin_slice));
+    println!("{:?}", thin_slice);
+}
+
+#[derive(Debug)]
+struct Foo;
+
+impl fmt::Display for Foo {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "boooo!")
+    }
+}
+
+impl Error for Foo {}
diff --git a/src/test/ui/box/thin_zst.rs b/src/test/ui/box/thin_zst.rs
new file mode 100644
index 00000000000..77c400d17bb
--- /dev/null
+++ b/src/test/ui/box/thin_zst.rs
@@ -0,0 +1,34 @@
+#![feature(thin_box)]
+// run-pass
+use std::boxed::ThinBox;
+use std::error::Error;
+use std::{fmt, mem};
+use std::ops::DerefMut;
+
+const EXPECTED: &str = "boooo!";
+
+fn main() {
+    let thin_error: ThinBox<dyn Error> = ThinBox::new_unsize(Foo);
+    assert_eq!(mem::size_of::<*const i32>(), mem::size_of_val(&thin_error));
+    let msg = thin_error.to_string();
+    assert_eq!(EXPECTED, msg);
+
+    let mut thin_concrete_error: ThinBox<Foo> = ThinBox::new(Foo);
+    assert_eq!(mem::size_of::<*const i32>(), mem::size_of_val(&thin_concrete_error));
+    let msg = thin_concrete_error.to_string();
+    assert_eq!(EXPECTED, msg);
+    let inner = thin_concrete_error.deref_mut();
+    let msg = inner.to_string();
+    assert_eq!(EXPECTED, msg);
+}
+
+#[derive(Debug)]
+struct Foo;
+
+impl fmt::Display for Foo {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", EXPECTED)
+    }
+}
+
+impl Error for Foo {}