summaryrefslogtreecommitdiffstats
path: root/src/test/run-pass-valgrind/unsized-locals
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/run-pass-valgrind/unsized-locals
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/run-pass-valgrind/unsized-locals')
-rw-r--r--src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs55
-rw-r--r--src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs69
-rw-r--r--src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs49
-rw-r--r--src/test/run-pass-valgrind/unsized-locals/long-live-the-unsized-temporary.rs52
4 files changed, 225 insertions, 0 deletions
diff --git a/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs
new file mode 100644
index 000000000..7f365ce2b
--- /dev/null
+++ b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs
@@ -0,0 +1,55 @@
+#![feature(unsized_locals)]
+#![feature(unboxed_closures)]
+
+pub trait FnOnce<Args> {
+ type Output;
+ extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
+}
+
+struct A;
+
+impl FnOnce<()> for A {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
+ format!("hello")
+ }
+}
+
+struct B(i32);
+
+impl FnOnce<()> for B {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
+ format!("{}", self.0)
+ }
+}
+
+struct C(String);
+
+impl FnOnce<()> for C {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
+ self.0
+ }
+}
+
+struct D(Box<String>);
+
+impl FnOnce<()> for D {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
+ *self.0
+ }
+}
+
+
+fn main() {
+ let x = *(Box::new(A) as Box<dyn FnOnce<(), Output = String>>);
+ assert_eq!(x.call_once(()), format!("hello"));
+ let x = *(Box::new(B(42)) as Box<dyn FnOnce<(), Output = String>>);
+ assert_eq!(x.call_once(()), format!("42"));
+ let x = *(Box::new(C(format!("jumping fox"))) as Box<dyn FnOnce<(), Output = String>>);
+ assert_eq!(x.call_once(()), format!("jumping fox"));
+ let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box<dyn FnOnce<(), Output = String>>);
+ assert_eq!(x.call_once(()), format!("lazy dog"));
+}
diff --git a/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs
new file mode 100644
index 000000000..a78b897d1
--- /dev/null
+++ b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs
@@ -0,0 +1,69 @@
+#![feature(unsized_locals)]
+#![feature(unboxed_closures)]
+
+pub trait FnOnce<Args> {
+ type Output;
+ extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
+}
+
+struct A;
+
+impl FnOnce<(String, Box<str>)> for A {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
+ assert_eq!(&s1 as &str, "s1");
+ assert_eq!(&s2 as &str, "s2");
+ format!("hello")
+ }
+}
+
+struct B(i32);
+
+impl FnOnce<(String, Box<str>)> for B {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
+ assert_eq!(&s1 as &str, "s1");
+ assert_eq!(&s2 as &str, "s2");
+ format!("{}", self.0)
+ }
+}
+
+struct C(String);
+
+impl FnOnce<(String, Box<str>)> for C {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
+ assert_eq!(&s1 as &str, "s1");
+ assert_eq!(&s2 as &str, "s2");
+ self.0
+ }
+}
+
+struct D(Box<String>);
+
+impl FnOnce<(String, Box<str>)> for D {
+ type Output = String;
+ extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
+ assert_eq!(&s1 as &str, "s1");
+ assert_eq!(&s2 as &str, "s2");
+ *self.0
+ }
+}
+
+
+fn main() {
+ let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
+ let x = *(Box::new(A) as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
+ assert_eq!(x.call_once((s1, s2)), format!("hello"));
+ let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
+ let x = *(Box::new(B(42)) as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
+ assert_eq!(x.call_once((s1, s2)), format!("42"));
+ let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
+ let x = *(Box::new(C(format!("jumping fox")))
+ as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
+ assert_eq!(x.call_once((s1, s2)), format!("jumping fox"));
+ let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
+ let x = *(Box::new(D(Box::new(format!("lazy dog"))))
+ as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
+ assert_eq!(x.call_once((s1, s2)), format!("lazy dog"));
+}
diff --git a/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs
new file mode 100644
index 000000000..3d67101e7
--- /dev/null
+++ b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs
@@ -0,0 +1,49 @@
+#![feature(unsized_locals)]
+
+pub trait Foo {
+ fn foo(self) -> String;
+}
+
+struct A;
+
+impl Foo for A {
+ fn foo(self) -> String {
+ format!("hello")
+ }
+}
+
+struct B(i32);
+
+impl Foo for B {
+ fn foo(self) -> String {
+ format!("{}", self.0)
+ }
+}
+
+struct C(String);
+
+impl Foo for C {
+ fn foo(self) -> String {
+ self.0
+ }
+}
+
+struct D(Box<String>);
+
+impl Foo for D {
+ fn foo(self) -> String {
+ *self.0
+ }
+}
+
+
+fn main() {
+ let x = *(Box::new(A) as Box<dyn Foo>);
+ assert_eq!(x.foo(), format!("hello"));
+ let x = *(Box::new(B(42)) as Box<dyn Foo>);
+ assert_eq!(x.foo(), format!("42"));
+ let x = *(Box::new(C(format!("jumping fox"))) as Box<dyn Foo>);
+ assert_eq!(x.foo(), format!("jumping fox"));
+ let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box<dyn Foo>);
+ assert_eq!(x.foo(), format!("lazy dog"));
+}
diff --git a/src/test/run-pass-valgrind/unsized-locals/long-live-the-unsized-temporary.rs b/src/test/run-pass-valgrind/unsized-locals/long-live-the-unsized-temporary.rs
new file mode 100644
index 000000000..a7b905261
--- /dev/null
+++ b/src/test/run-pass-valgrind/unsized-locals/long-live-the-unsized-temporary.rs
@@ -0,0 +1,52 @@
+#![allow(incomplete_features)]
+#![feature(unsized_locals, unsized_fn_params)]
+
+use std::fmt;
+
+fn gen_foo() -> Box<fmt::Display> {
+ Box::new(Box::new("foo"))
+}
+
+fn foo(x: fmt::Display) {
+ assert_eq!(x.to_string(), "foo");
+}
+
+fn foo_indirect(x: fmt::Display) {
+ foo(x);
+}
+
+fn main() {
+ foo(*gen_foo());
+ foo_indirect(*gen_foo());
+
+ {
+ let x: fmt::Display = *gen_foo();
+ foo(x);
+ }
+
+ {
+ let x: fmt::Display = *gen_foo();
+ let y: fmt::Display = *gen_foo();
+ foo(x);
+ foo(y);
+ }
+
+ {
+ let mut cnt: usize = 3;
+ let x = loop {
+ let x: fmt::Display = *gen_foo();
+ if cnt == 0 {
+ break x;
+ } else {
+ cnt -= 1;
+ }
+ };
+ foo(x);
+ }
+
+ {
+ let x: fmt::Display = *gen_foo();
+ let x = if true { x } else { *gen_foo() };
+ foo(x);
+ }
+}