about summary refs log tree commit diff
path: root/tests/ui/box
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/box
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/box')
-rw-r--r--tests/ui/box/alloc-unstable-fail.rs6
-rw-r--r--tests/ui/box/alloc-unstable-fail.stderr12
-rw-r--r--tests/ui/box/alloc-unstable.rs8
-rw-r--r--tests/ui/box/into-boxed-slice-fail.rs14
-rw-r--r--tests/ui/box/into-boxed-slice-fail.stderr45
-rw-r--r--tests/ui/box/into-boxed-slice.rs11
-rw-r--r--tests/ui/box/issue-82446.rs15
-rw-r--r--tests/ui/box/issue-82446.stderr12
-rw-r--r--tests/ui/box/issue-95036.rs22
-rw-r--r--tests/ui/box/large-allocator-ice.rs29
-rw-r--r--tests/ui/box/leak-alloc.rs29
-rw-r--r--tests/ui/box/leak-alloc.stderr15
-rw-r--r--tests/ui/box/new-box-syntax.rs27
-rw-r--r--tests/ui/box/new-box.rs30
-rw-r--r--tests/ui/box/new.rs6
-rw-r--r--tests/ui/box/thin_align.rs26
-rw-r--r--tests/ui/box/thin_drop.rs37
-rw-r--r--tests/ui/box/thin_new.rs30
-rw-r--r--tests/ui/box/thin_zst.rs34
19 files changed, 408 insertions, 0 deletions
diff --git a/tests/ui/box/alloc-unstable-fail.rs b/tests/ui/box/alloc-unstable-fail.rs
new file mode 100644
index 00000000000..9427571648c
--- /dev/null
+++ b/tests/ui/box/alloc-unstable-fail.rs
@@ -0,0 +1,6 @@
+use std::boxed::Box;
+
+fn main() {
+    let _boxed: Box<u32, _> = Box::new(10);
+    //~^ ERROR use of unstable library feature 'allocator_api'
+}
diff --git a/tests/ui/box/alloc-unstable-fail.stderr b/tests/ui/box/alloc-unstable-fail.stderr
new file mode 100644
index 00000000000..03ae36e8890
--- /dev/null
+++ b/tests/ui/box/alloc-unstable-fail.stderr
@@ -0,0 +1,12 @@
+error[E0658]: use of unstable library feature 'allocator_api'
+  --> $DIR/alloc-unstable-fail.rs:4:26
+   |
+LL |     let _boxed: Box<u32, _> = Box::new(10);
+   |                          ^
+   |
+   = note: see issue #32838 <https://github.com/rust-lang/rust/issues/32838> for more information
+   = help: add `#![feature(allocator_api)]` to the crate attributes to enable
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/box/alloc-unstable.rs b/tests/ui/box/alloc-unstable.rs
new file mode 100644
index 00000000000..66388d0d512
--- /dev/null
+++ b/tests/ui/box/alloc-unstable.rs
@@ -0,0 +1,8 @@
+// run-pass
+#![feature(allocator_api)]
+
+use std::boxed::Box;
+
+fn main() {
+    let _boxed: Box<u32, _> = Box::new(10);
+}
diff --git a/tests/ui/box/into-boxed-slice-fail.rs b/tests/ui/box/into-boxed-slice-fail.rs
new file mode 100644
index 00000000000..49dbb170f8e
--- /dev/null
+++ b/tests/ui/box/into-boxed-slice-fail.rs
@@ -0,0 +1,14 @@
+#![feature(box_into_boxed_slice)]
+
+use std::boxed::Box;
+use std::fmt::Debug;
+fn main() {
+    let boxed_slice = Box::new([1,2,3]) as Box<[u8]>;
+    let _ = Box::into_boxed_slice(boxed_slice);
+    //~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
+    //~^^ ERROR the size for values of type `[u8]` cannot be known at compilation time
+    let boxed_trait: Box<dyn Debug> = Box::new(5u8);
+    let _ = Box::into_boxed_slice(boxed_trait);
+    //~^ ERROR the size for values of type `dyn Debug` cannot be known at compilation time
+    //~^^ ERROR the size for values of type `dyn Debug` cannot be known at compilation time
+}
diff --git a/tests/ui/box/into-boxed-slice-fail.stderr b/tests/ui/box/into-boxed-slice-fail.stderr
new file mode 100644
index 00000000000..f102f666dc2
--- /dev/null
+++ b/tests/ui/box/into-boxed-slice-fail.stderr
@@ -0,0 +1,45 @@
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/into-boxed-slice-fail.rs:7:35
+   |
+LL |     let _ = Box::into_boxed_slice(boxed_slice);
+   |             --------------------- ^^^^^^^^^^^ doesn't have a size known at compile-time
+   |             |
+   |             required by a bound introduced by this call
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: required by a bound in `Box::<T, A>::into_boxed_slice`
+  --> $SRC_DIR/alloc/src/boxed.rs:LL:COL
+
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/into-boxed-slice-fail.rs:7:13
+   |
+LL |     let _ = Box::into_boxed_slice(boxed_slice);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+   = note: slice and array elements must have `Sized` type
+
+error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
+  --> $DIR/into-boxed-slice-fail.rs:11:35
+   |
+LL |     let _ = Box::into_boxed_slice(boxed_trait);
+   |             --------------------- ^^^^^^^^^^^ doesn't have a size known at compile-time
+   |             |
+   |             required by a bound introduced by this call
+   |
+   = help: the trait `Sized` is not implemented for `dyn Debug`
+note: required by a bound in `Box::<T, A>::into_boxed_slice`
+  --> $SRC_DIR/alloc/src/boxed.rs:LL:COL
+
+error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
+  --> $DIR/into-boxed-slice-fail.rs:11:13
+   |
+LL |     let _ = Box::into_boxed_slice(boxed_trait);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `dyn Debug`
+   = note: slice and array elements must have `Sized` type
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/tests/ui/box/into-boxed-slice.rs b/tests/ui/box/into-boxed-slice.rs
new file mode 100644
index 00000000000..61b3d915253
--- /dev/null
+++ b/tests/ui/box/into-boxed-slice.rs
@@ -0,0 +1,11 @@
+// run-pass
+#![feature(box_into_boxed_slice)]
+
+use std::boxed::Box;
+fn main() {
+    assert_eq!(Box::into_boxed_slice(Box::new(5u8)), Box::new([5u8]) as Box<[u8]>);
+    assert_eq!(Box::into_boxed_slice(Box::new([25u8])), Box::new([[25u8]]) as Box<[[u8; 1]]>);
+    let a: Box<[Box<[u8; 1]>]> = Box::into_boxed_slice(Box::new(Box::new([5u8])));
+    let b: Box<[Box<[u8; 1]>]> = Box::new([Box::new([5u8])]);
+    assert_eq!(a, b);
+}
diff --git a/tests/ui/box/issue-82446.rs b/tests/ui/box/issue-82446.rs
new file mode 100644
index 00000000000..2960f7fbc21
--- /dev/null
+++ b/tests/ui/box/issue-82446.rs
@@ -0,0 +1,15 @@
+// https://github.com/rust-lang/rust/issues/82446
+// Spurious 'help: store this in the heap' regression test
+trait MyTrait {}
+
+struct Foo {
+    val: Box<dyn MyTrait>
+}
+
+fn make_it(val: &Box<dyn MyTrait>) {
+    Foo {
+        val //~ ERROR [E0308]
+    };
+}
+
+fn main() {}
diff --git a/tests/ui/box/issue-82446.stderr b/tests/ui/box/issue-82446.stderr
new file mode 100644
index 00000000000..0374737957e
--- /dev/null
+++ b/tests/ui/box/issue-82446.stderr
@@ -0,0 +1,12 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-82446.rs:11:9
+   |
+LL |         val
+   |         ^^^ expected struct `Box`, found reference
+   |
+   = note: expected struct `Box<(dyn MyTrait + 'static)>`
+           found reference `&Box<(dyn MyTrait + 'static)>`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/box/issue-95036.rs b/tests/ui/box/issue-95036.rs
new file mode 100644
index 00000000000..0611fabc15c
--- /dev/null
+++ b/tests/ui/box/issue-95036.rs
@@ -0,0 +1,22 @@
+// compile-flags: -O
+// build-pass
+
+#![feature(allocator_api)]
+
+#[inline(never)]
+pub fn by_ref(node: &mut Box<[u8; 1], &std::alloc::Global>) {
+    node[0] = 9u8;
+}
+
+pub fn main() {
+    let mut node = Box::new_in([5u8], &std::alloc::Global);
+    node[0] = 7u8;
+
+    std::hint::black_box(node);
+
+    let mut node = Box::new_in([5u8], &std::alloc::Global);
+
+    by_ref(&mut node);
+
+    std::hint::black_box(node);
+}
diff --git a/tests/ui/box/large-allocator-ice.rs b/tests/ui/box/large-allocator-ice.rs
new file mode 100644
index 00000000000..b3a882ff089
--- /dev/null
+++ b/tests/ui/box/large-allocator-ice.rs
@@ -0,0 +1,29 @@
+// build-pass
+#![feature(allocator_api)]
+#![allow(unused_must_use)]
+
+use std::alloc::Allocator;
+
+struct BigAllocator([usize; 2]);
+
+unsafe impl Allocator for BigAllocator {
+    fn allocate(
+        &self,
+        _: std::alloc::Layout,
+    ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
+        todo!()
+    }
+    unsafe fn deallocate(&self, _: std::ptr::NonNull<u8>, _: std::alloc::Layout) {
+        todo!()
+    }
+}
+
+fn main() {
+    Box::new_in((), &std::alloc::Global);
+    Box::new_in((), BigAllocator([0; 2]));
+    generic_function(0);
+}
+
+fn generic_function<T>(val: T) {
+    *Box::new_in(val, &std::alloc::Global);
+}
diff --git a/tests/ui/box/leak-alloc.rs b/tests/ui/box/leak-alloc.rs
new file mode 100644
index 00000000000..3f0f39f448b
--- /dev/null
+++ b/tests/ui/box/leak-alloc.rs
@@ -0,0 +1,29 @@
+#![feature(allocator_api)]
+
+use std::alloc::{AllocError, Allocator, Layout, System};
+use std::ptr::NonNull;
+
+use std::boxed::Box;
+
+struct Alloc {}
+
+unsafe impl Allocator for Alloc {
+    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
+        System.allocate(layout)
+    }
+
+    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
+        System.deallocate(ptr, layout)
+    }
+}
+
+fn use_value(_: u32) {}
+
+fn main() {
+    let alloc = Alloc {};
+    let boxed = Box::new_in(10, alloc.by_ref());
+    let theref = Box::leak(boxed);
+    drop(alloc);
+    //~^ ERROR cannot move out of `alloc` because it is borrowed
+    use_value(*theref)
+}
diff --git a/tests/ui/box/leak-alloc.stderr b/tests/ui/box/leak-alloc.stderr
new file mode 100644
index 00000000000..e8a6ad0995a
--- /dev/null
+++ b/tests/ui/box/leak-alloc.stderr
@@ -0,0 +1,15 @@
+error[E0505]: cannot move out of `alloc` because it is borrowed
+  --> $DIR/leak-alloc.rs:26:10
+   |
+LL |     let boxed = Box::new_in(10, alloc.by_ref());
+   |                                 -------------- borrow of `alloc` occurs here
+LL |     let theref = Box::leak(boxed);
+LL |     drop(alloc);
+   |          ^^^^^ move out of `alloc` occurs here
+LL |
+LL |     use_value(*theref)
+   |               ------- borrow later used here
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0505`.
diff --git a/tests/ui/box/new-box-syntax.rs b/tests/ui/box/new-box-syntax.rs
new file mode 100644
index 00000000000..c56e1dd4625
--- /dev/null
+++ b/tests/ui/box/new-box-syntax.rs
@@ -0,0 +1,27 @@
+// run-pass
+// pretty-expanded FIXME #23616
+
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+#![allow(dead_code, unused_variables)]
+
+// Tests that the new `box` syntax works with unique pointers.
+
+use std::boxed::Box;
+
+struct Structure {
+    x: isize,
+    y: isize,
+}
+
+pub fn main() {
+    let y: Box<isize> = Box::new(2);
+    let b: Box<isize> = Box::new(1 + 2);
+    let c = Box::new(3 + 4);
+
+    let s: Box<Structure> = Box::new(Structure {
+        x: 3,
+        y: 4,
+    });
+}
diff --git a/tests/ui/box/new-box.rs b/tests/ui/box/new-box.rs
new file mode 100644
index 00000000000..96a3b197f46
--- /dev/null
+++ b/tests/ui/box/new-box.rs
@@ -0,0 +1,30 @@
+// run-pass
+
+fn f(x: Box<isize>) {
+    let y: &isize = &*x;
+    println!("{}", *x);
+    println!("{}", *y);
+}
+
+trait Trait {
+    fn printme(&self);
+}
+
+struct Struct;
+
+impl Trait for Struct {
+    fn printme(&self) {
+        println!("hello world!");
+    }
+}
+
+fn g(x: Box<dyn Trait>) {
+    x.printme();
+    let y: &dyn Trait = &*x;
+    y.printme();
+}
+
+fn main() {
+    f(Box::new(1234));
+    g(Box::new(Struct) as Box<dyn Trait>);
+}
diff --git a/tests/ui/box/new.rs b/tests/ui/box/new.rs
new file mode 100644
index 00000000000..be1a40cf779
--- /dev/null
+++ b/tests/ui/box/new.rs
@@ -0,0 +1,6 @@
+// run-pass
+// pretty-expanded FIXME #23616
+
+fn main() {
+    let _a = Box::new(1);
+}
diff --git a/tests/ui/box/thin_align.rs b/tests/ui/box/thin_align.rs
new file mode 100644
index 00000000000..3c61d0090e4
--- /dev/null
+++ b/tests/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/tests/ui/box/thin_drop.rs b/tests/ui/box/thin_drop.rs
new file mode 100644
index 00000000000..965613c114e
--- /dev/null
+++ b/tests/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/tests/ui/box/thin_new.rs b/tests/ui/box/thin_new.rs
new file mode 100644
index 00000000000..53f46478be4
--- /dev/null
+++ b/tests/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/tests/ui/box/thin_zst.rs b/tests/ui/box/thin_zst.rs
new file mode 100644
index 00000000000..77c400d17bb
--- /dev/null
+++ b/tests/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 {}