summaryrefslogtreecommitdiffstats
path: root/src/test/ui/consts/const-blocks
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/consts/const-blocks
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/consts/const-blocks')
-rw-r--r--src/test/ui/consts/const-blocks/const-repeat.rs27
-rw-r--r--src/test/ui/consts/const-blocks/fn-call-in-const.rs23
-rw-r--r--src/test/ui/consts/const-blocks/fn-call-in-non-const.rs16
-rw-r--r--src/test/ui/consts/const-blocks/fn-call-in-non-const.stderr18
-rw-r--r--src/test/ui/consts/const-blocks/migrate-fail.rs22
-rw-r--r--src/test/ui/consts/const-blocks/migrate-fail.stderr29
-rw-r--r--src/test/ui/consts/const-blocks/migrate-pass.rs125
-rw-r--r--src/test/ui/consts/const-blocks/nll-fail.rs22
-rw-r--r--src/test/ui/consts/const-blocks/nll-fail.stderr29
-rw-r--r--src/test/ui/consts/const-blocks/nll-pass.rs125
-rw-r--r--src/test/ui/consts/const-blocks/run-pass.rs11
-rw-r--r--src/test/ui/consts/const-blocks/trait-error.rs7
-rw-r--r--src/test/ui/consts/const-blocks/trait-error.stderr19
13 files changed, 473 insertions, 0 deletions
diff --git a/src/test/ui/consts/const-blocks/const-repeat.rs b/src/test/ui/consts/const-blocks/const-repeat.rs
new file mode 100644
index 000000000..65d02317d
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/const-repeat.rs
@@ -0,0 +1,27 @@
+// run-pass
+
+// Repeating a *constant* of non-Copy type (not just a constant expression) is already stable.
+
+const EMPTY: Vec<i32> = Vec::new();
+
+pub fn bar() -> [Vec<i32>; 2] {
+ [EMPTY; 2]
+}
+
+struct Bomb;
+
+impl Drop for Bomb {
+ fn drop(&mut self) {
+ panic!("BOOM!");
+ }
+}
+
+const BOOM: Bomb = Bomb;
+
+fn main() {
+ let _x = bar();
+
+ // Make sure the destructor does not get called for empty arrays. `[CONST; N]` should
+ // instantiate (and then later drop) the const exactly `N` times.
+ let _x = [BOOM; 0];
+}
diff --git a/src/test/ui/consts/const-blocks/fn-call-in-const.rs b/src/test/ui/consts/const-blocks/fn-call-in-const.rs
new file mode 100644
index 000000000..20496f627
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/fn-call-in-const.rs
@@ -0,0 +1,23 @@
+// run-pass
+
+#![feature(inline_const)]
+#![allow(unused)]
+
+// Some type that is not copyable.
+struct Bar;
+
+const fn type_no_copy() -> Option<Bar> {
+ None
+}
+
+const fn type_copy() -> u32 {
+ 3
+}
+
+const _: [u32; 2] = [type_copy(); 2];
+
+// This is allowed because all promotion contexts use the explicit rules for promotability when
+// inside an explicit const context.
+const _: [Option<Bar>; 2] = [const { type_no_copy() }; 2];
+
+fn main() {}
diff --git a/src/test/ui/consts/const-blocks/fn-call-in-non-const.rs b/src/test/ui/consts/const-blocks/fn-call-in-non-const.rs
new file mode 100644
index 000000000..18b4dc714
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/fn-call-in-non-const.rs
@@ -0,0 +1,16 @@
+// Some type that is not copyable.
+struct Bar;
+
+const fn no_copy() -> Option<Bar> {
+ None
+}
+
+const fn copy() -> u32 {
+ 3
+}
+
+fn main() {
+ let _: [u32; 2] = [copy(); 2];
+ let _: [Option<Bar>; 2] = [no_copy(); 2];
+ //~^ ERROR the trait bound `Bar: Copy` is not satisfied
+}
diff --git a/src/test/ui/consts/const-blocks/fn-call-in-non-const.stderr b/src/test/ui/consts/const-blocks/fn-call-in-non-const.stderr
new file mode 100644
index 000000000..5306fed22
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/fn-call-in-non-const.stderr
@@ -0,0 +1,18 @@
+error[E0277]: the trait bound `Bar: Copy` is not satisfied
+ --> $DIR/fn-call-in-non-const.rs:14:32
+ |
+LL | let _: [Option<Bar>; 2] = [no_copy(); 2];
+ | ^^^^^^^^^ the trait `Copy` is not implemented for `Bar`
+ |
+ = note: required because of the requirements on the impl of `Copy` for `Option<Bar>`
+ = note: the `Copy` trait is required because this value will be copied for each element of the array
+ = help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position, like `const VAL: Type = const_fn();` and `let x = [VAL; 42];`
+ = help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
+help: consider annotating `Bar` with `#[derive(Copy)]`
+ |
+LL | #[derive(Copy)]
+ |
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/consts/const-blocks/migrate-fail.rs b/src/test/ui/consts/const-blocks/migrate-fail.rs
new file mode 100644
index 000000000..fddbfbb9d
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/migrate-fail.rs
@@ -0,0 +1,22 @@
+#![allow(warnings)]
+
+// Some type that is not copyable.
+struct Bar;
+
+mod non_constants {
+ use Bar;
+
+ fn no_impl_copy_empty_value_multiple_elements() {
+ let x = None;
+ let arr: [Option<Bar>; 2] = [x; 2];
+ //~^ ERROR the trait bound `Bar: Copy` is not satisfied [E0277]
+ }
+
+ fn no_impl_copy_value_multiple_elements() {
+ let x = Some(Bar);
+ let arr: [Option<Bar>; 2] = [x; 2];
+ //~^ ERROR the trait bound `Bar: Copy` is not satisfied [E0277]
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/consts/const-blocks/migrate-fail.stderr b/src/test/ui/consts/const-blocks/migrate-fail.stderr
new file mode 100644
index 000000000..803281c07
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/migrate-fail.stderr
@@ -0,0 +1,29 @@
+error[E0277]: the trait bound `Bar: Copy` is not satisfied
+ --> $DIR/migrate-fail.rs:11:38
+ |
+LL | let arr: [Option<Bar>; 2] = [x; 2];
+ | ^ the trait `Copy` is not implemented for `Bar`
+ |
+ = note: required because of the requirements on the impl of `Copy` for `Option<Bar>`
+ = note: the `Copy` trait is required because this value will be copied for each element of the array
+help: consider annotating `Bar` with `#[derive(Copy)]`
+ |
+LL | #[derive(Copy)]
+ |
+
+error[E0277]: the trait bound `Bar: Copy` is not satisfied
+ --> $DIR/migrate-fail.rs:17:38
+ |
+LL | let arr: [Option<Bar>; 2] = [x; 2];
+ | ^ the trait `Copy` is not implemented for `Bar`
+ |
+ = note: required because of the requirements on the impl of `Copy` for `Option<Bar>`
+ = note: the `Copy` trait is required because this value will be copied for each element of the array
+help: consider annotating `Bar` with `#[derive(Copy)]`
+ |
+LL | #[derive(Copy)]
+ |
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/consts/const-blocks/migrate-pass.rs b/src/test/ui/consts/const-blocks/migrate-pass.rs
new file mode 100644
index 000000000..fd66f5aa6
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/migrate-pass.rs
@@ -0,0 +1,125 @@
+// check-pass
+#![allow(warnings)]
+
+// Some type that is not copyable.
+struct Bar;
+
+mod constants {
+ use Bar;
+
+ fn no_impl_copy_empty_value_no_elements() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 0] = [FOO; 0];
+ }
+
+ fn no_impl_copy_empty_value_single_element() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 1] = [FOO; 1];
+ }
+
+ fn no_impl_copy_empty_value_multiple_elements() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 2] = [FOO; 2];
+ }
+
+ fn no_impl_copy_value_no_elements() {
+ const FOO: Option<Bar> = Some(Bar);
+ const ARR: [Option<Bar>; 0] = [FOO; 0];
+ }
+
+ fn no_impl_copy_value_single_element() {
+ const FOO: Option<Bar> = Some(Bar);
+ const ARR: [Option<Bar>; 1] = [FOO; 1];
+ }
+
+ fn no_impl_copy_value_multiple_elements() {
+ const FOO: Option<Bar> = Some(Bar);
+ const ARR: [Option<Bar>; 2] = [FOO; 2];
+ }
+
+ fn impl_copy_empty_value_no_elements() {
+ const FOO: Option<u32> = None;
+ const ARR: [Option<u32>; 0] = [FOO; 0];
+ }
+
+ fn impl_copy_empty_value_one_element() {
+ const FOO: Option<u32> = None;
+ const ARR: [Option<u32>; 1] = [FOO; 1];
+ }
+
+ fn impl_copy_empty_value_multiple_elements() {
+ const FOO: Option<u32> = None;
+ const ARR: [Option<u32>; 2] = [FOO; 2];
+ }
+
+ fn impl_copy_value_no_elements() {
+ const FOO: Option<u32> = Some(4);
+ const ARR: [Option<u32>; 0] = [FOO; 0];
+ }
+
+ fn impl_copy_value_one_element() {
+ const FOO: Option<u32> = Some(4);
+ const ARR: [Option<u32>; 1] = [FOO; 1];
+ }
+
+ fn impl_copy_value_multiple_elements() {
+ const FOO: Option<u32> = Some(4);
+ const ARR: [Option<u32>; 2] = [FOO; 2];
+ }
+}
+
+mod non_constants {
+ use Bar;
+
+ fn no_impl_copy_empty_value_no_elements() {
+ let x = None;
+ let arr: [Option<Bar>; 0] = [x; 0];
+ }
+
+ fn no_impl_copy_empty_value_single_element() {
+ let x = None;
+ let arr: [Option<Bar>; 1] = [x; 1];
+ }
+
+ fn no_impl_copy_value_no_elements() {
+ let x = Some(Bar);
+ let arr: [Option<Bar>; 0] = [x; 0];
+ }
+
+ fn no_impl_copy_value_single_element() {
+ let x = Some(Bar);
+ let arr: [Option<Bar>; 1] = [x; 1];
+ }
+
+ fn impl_copy_empty_value_no_elements() {
+ let x: Option<u32> = None;
+ let arr: [Option<u32>; 0] = [x; 0];
+ }
+
+ fn impl_copy_empty_value_one_element() {
+ let x: Option<u32> = None;
+ let arr: [Option<u32>; 1] = [x; 1];
+ }
+
+ fn impl_copy_empty_value_multiple_elements() {
+ let x: Option<u32> = None;
+ let arr: [Option<u32>; 2] = [x; 2];
+ }
+
+ fn impl_copy_value_no_elements() {
+ let x: Option<u32> = Some(4);
+ let arr: [Option<u32>; 0] = [x; 0];
+ }
+
+ fn impl_copy_value_one_element() {
+ let x: Option<u32> = Some(4);
+ let arr: [Option<u32>; 1] = [x; 1];
+ }
+
+ fn impl_copy_value_multiple_elements() {
+ let x: Option<u32> = Some(4);
+ let arr: [Option<u32>; 2] = [x; 2];
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/consts/const-blocks/nll-fail.rs b/src/test/ui/consts/const-blocks/nll-fail.rs
new file mode 100644
index 000000000..fddbfbb9d
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/nll-fail.rs
@@ -0,0 +1,22 @@
+#![allow(warnings)]
+
+// Some type that is not copyable.
+struct Bar;
+
+mod non_constants {
+ use Bar;
+
+ fn no_impl_copy_empty_value_multiple_elements() {
+ let x = None;
+ let arr: [Option<Bar>; 2] = [x; 2];
+ //~^ ERROR the trait bound `Bar: Copy` is not satisfied [E0277]
+ }
+
+ fn no_impl_copy_value_multiple_elements() {
+ let x = Some(Bar);
+ let arr: [Option<Bar>; 2] = [x; 2];
+ //~^ ERROR the trait bound `Bar: Copy` is not satisfied [E0277]
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/consts/const-blocks/nll-fail.stderr b/src/test/ui/consts/const-blocks/nll-fail.stderr
new file mode 100644
index 000000000..8841af15d
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/nll-fail.stderr
@@ -0,0 +1,29 @@
+error[E0277]: the trait bound `Bar: Copy` is not satisfied
+ --> $DIR/nll-fail.rs:11:38
+ |
+LL | let arr: [Option<Bar>; 2] = [x; 2];
+ | ^ the trait `Copy` is not implemented for `Bar`
+ |
+ = note: required because of the requirements on the impl of `Copy` for `Option<Bar>`
+ = note: the `Copy` trait is required because this value will be copied for each element of the array
+help: consider annotating `Bar` with `#[derive(Copy)]`
+ |
+LL | #[derive(Copy)]
+ |
+
+error[E0277]: the trait bound `Bar: Copy` is not satisfied
+ --> $DIR/nll-fail.rs:17:38
+ |
+LL | let arr: [Option<Bar>; 2] = [x; 2];
+ | ^ the trait `Copy` is not implemented for `Bar`
+ |
+ = note: required because of the requirements on the impl of `Copy` for `Option<Bar>`
+ = note: the `Copy` trait is required because this value will be copied for each element of the array
+help: consider annotating `Bar` with `#[derive(Copy)]`
+ |
+LL | #[derive(Copy)]
+ |
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/consts/const-blocks/nll-pass.rs b/src/test/ui/consts/const-blocks/nll-pass.rs
new file mode 100644
index 000000000..fd66f5aa6
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/nll-pass.rs
@@ -0,0 +1,125 @@
+// check-pass
+#![allow(warnings)]
+
+// Some type that is not copyable.
+struct Bar;
+
+mod constants {
+ use Bar;
+
+ fn no_impl_copy_empty_value_no_elements() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 0] = [FOO; 0];
+ }
+
+ fn no_impl_copy_empty_value_single_element() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 1] = [FOO; 1];
+ }
+
+ fn no_impl_copy_empty_value_multiple_elements() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 2] = [FOO; 2];
+ }
+
+ fn no_impl_copy_value_no_elements() {
+ const FOO: Option<Bar> = Some(Bar);
+ const ARR: [Option<Bar>; 0] = [FOO; 0];
+ }
+
+ fn no_impl_copy_value_single_element() {
+ const FOO: Option<Bar> = Some(Bar);
+ const ARR: [Option<Bar>; 1] = [FOO; 1];
+ }
+
+ fn no_impl_copy_value_multiple_elements() {
+ const FOO: Option<Bar> = Some(Bar);
+ const ARR: [Option<Bar>; 2] = [FOO; 2];
+ }
+
+ fn impl_copy_empty_value_no_elements() {
+ const FOO: Option<u32> = None;
+ const ARR: [Option<u32>; 0] = [FOO; 0];
+ }
+
+ fn impl_copy_empty_value_one_element() {
+ const FOO: Option<u32> = None;
+ const ARR: [Option<u32>; 1] = [FOO; 1];
+ }
+
+ fn impl_copy_empty_value_multiple_elements() {
+ const FOO: Option<u32> = None;
+ const ARR: [Option<u32>; 2] = [FOO; 2];
+ }
+
+ fn impl_copy_value_no_elements() {
+ const FOO: Option<u32> = Some(4);
+ const ARR: [Option<u32>; 0] = [FOO; 0];
+ }
+
+ fn impl_copy_value_one_element() {
+ const FOO: Option<u32> = Some(4);
+ const ARR: [Option<u32>; 1] = [FOO; 1];
+ }
+
+ fn impl_copy_value_multiple_elements() {
+ const FOO: Option<u32> = Some(4);
+ const ARR: [Option<u32>; 2] = [FOO; 2];
+ }
+}
+
+mod non_constants {
+ use Bar;
+
+ fn no_impl_copy_empty_value_no_elements() {
+ let x = None;
+ let arr: [Option<Bar>; 0] = [x; 0];
+ }
+
+ fn no_impl_copy_empty_value_single_element() {
+ let x = None;
+ let arr: [Option<Bar>; 1] = [x; 1];
+ }
+
+ fn no_impl_copy_value_no_elements() {
+ let x = Some(Bar);
+ let arr: [Option<Bar>; 0] = [x; 0];
+ }
+
+ fn no_impl_copy_value_single_element() {
+ let x = Some(Bar);
+ let arr: [Option<Bar>; 1] = [x; 1];
+ }
+
+ fn impl_copy_empty_value_no_elements() {
+ let x: Option<u32> = None;
+ let arr: [Option<u32>; 0] = [x; 0];
+ }
+
+ fn impl_copy_empty_value_one_element() {
+ let x: Option<u32> = None;
+ let arr: [Option<u32>; 1] = [x; 1];
+ }
+
+ fn impl_copy_empty_value_multiple_elements() {
+ let x: Option<u32> = None;
+ let arr: [Option<u32>; 2] = [x; 2];
+ }
+
+ fn impl_copy_value_no_elements() {
+ let x: Option<u32> = Some(4);
+ let arr: [Option<u32>; 0] = [x; 0];
+ }
+
+ fn impl_copy_value_one_element() {
+ let x: Option<u32> = Some(4);
+ let arr: [Option<u32>; 1] = [x; 1];
+ }
+
+ fn impl_copy_value_multiple_elements() {
+ let x: Option<u32> = Some(4);
+ let arr: [Option<u32>; 2] = [x; 2];
+ }
+}
+
+fn main() {}
diff --git a/src/test/ui/consts/const-blocks/run-pass.rs b/src/test/ui/consts/const-blocks/run-pass.rs
new file mode 100644
index 000000000..e11f69bab
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/run-pass.rs
@@ -0,0 +1,11 @@
+// run-pass
+
+#[derive(Debug, Eq, PartialEq)]
+struct Bar;
+
+fn main() {
+ const FOO: Option<Bar> = None;
+ const ARR: [Option<Bar>; 2] = [FOO; 2];
+
+ assert_eq!(ARR, [None::<Bar>, None::<Bar>]);
+}
diff --git a/src/test/ui/consts/const-blocks/trait-error.rs b/src/test/ui/consts/const-blocks/trait-error.rs
new file mode 100644
index 000000000..49d1e9b94
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/trait-error.rs
@@ -0,0 +1,7 @@
+#[derive(Copy, Clone)]
+struct Foo<T>(T);
+
+fn main() {
+ [Foo(String::new()); 4];
+ //~^ ERROR the trait bound `String: Copy` is not satisfied [E0277]
+}
diff --git a/src/test/ui/consts/const-blocks/trait-error.stderr b/src/test/ui/consts/const-blocks/trait-error.stderr
new file mode 100644
index 000000000..ece200ad1
--- /dev/null
+++ b/src/test/ui/consts/const-blocks/trait-error.stderr
@@ -0,0 +1,19 @@
+error[E0277]: the trait bound `String: Copy` is not satisfied
+ --> $DIR/trait-error.rs:5:6
+ |
+LL | [Foo(String::new()); 4];
+ | ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
+ |
+note: required because of the requirements on the impl of `Copy` for `Foo<String>`
+ --> $DIR/trait-error.rs:1:10
+ |
+LL | #[derive(Copy, Clone)]
+ | ^^^^
+ = note: the `Copy` trait is required because this value will be copied for each element of the array
+ = help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position, like `const VAL: Type = const_fn();` and `let x = [VAL; 42];`
+ = help: create an inline `const` block, see RFC #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information
+ = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.