From 4ce35fdd349927c3cf1143421c71e4e75acb34e9 Mon Sep 17 00:00:00 2001 From: Masaki Hara Date: Wed, 12 Sep 2018 00:19:09 +0900 Subject: [PATCH] Add tests for unsized-locals. --- .../by-value-trait-object-safety.rs | 30 +++++++ .../by-value-trait-objects-rust-call.rs | 65 +++++++++++++++ .../by-value-trait-objects-rust-call2.rs | 79 +++++++++++++++++++ .../unsized-locals/by-value-trait-objects.rs | 59 ++++++++++++++ ...y-value-trait-object-safety-withdefault.rs | 27 +++++++ .../by-value-trait-object-safety.rs | 29 +++++++ 6 files changed, 289 insertions(+) create mode 100644 src/test/compile-fail/unsized-locals/by-value-trait-object-safety.rs create mode 100644 src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs create mode 100644 src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs create mode 100644 src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs create mode 100644 src/test/run-pass/unsized-locals/by-value-trait-object-safety-withdefault.rs create mode 100644 src/test/run-pass/unsized-locals/by-value-trait-object-safety.rs diff --git a/src/test/compile-fail/unsized-locals/by-value-trait-object-safety.rs b/src/test/compile-fail/unsized-locals/by-value-trait-object-safety.rs new file mode 100644 index 0000000000000..9ac67da24431e --- /dev/null +++ b/src/test/compile-fail/unsized-locals/by-value-trait-object-safety.rs @@ -0,0 +1,30 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unsized_locals)] + +pub trait Foo { + fn foo(self) -> String where Self: Sized; +} + +struct A; + +impl Foo for A { + fn foo(self) -> String { + format!("hello") + } +} + + +fn main() { + let x = *(Box::new(A) as Box); + x.foo(); + //~^ERROR the `foo` method cannot be invoked on a trait object +} 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 0000000000000..23b1c72a5bf95 --- /dev/null +++ b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs @@ -0,0 +1,65 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unsized_locals)] +#![feature(unboxed_closures)] + +pub trait FnOnce { + 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); + +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>); + assert_eq!(x.call_once(()), format!("hello")); + let x = *(Box::new(B(42)) as Box>); + assert_eq!(x.call_once(()), format!("42")); + let x = *(Box::new(C(format!("jumping fox"))) as Box>); + assert_eq!(x.call_once(()), format!("jumping fox")); + let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box>); + 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 0000000000000..f5d1c7e84d212 --- /dev/null +++ b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs @@ -0,0 +1,79 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unsized_locals)] +#![feature(unboxed_closures)] + +pub trait FnOnce { + type Output; + extern "rust-call" fn call_once(self, args: Args) -> Self::Output; +} + +struct A; + +impl FnOnce<(String, Box)> for A { + type Output = String; + extern "rust-call" fn call_once(self, (s1, s2): (String, Box)) -> Self::Output { + assert_eq!(&s1 as &str, "s1"); + assert_eq!(&s2 as &str, "s2"); + format!("hello") + } +} + +struct B(i32); + +impl FnOnce<(String, Box)> for B { + type Output = String; + extern "rust-call" fn call_once(self, (s1, s2): (String, Box)) -> Self::Output { + assert_eq!(&s1 as &str, "s1"); + assert_eq!(&s2 as &str, "s2"); + format!("{}", self.0) + } +} + +struct C(String); + +impl FnOnce<(String, Box)> for C { + type Output = String; + extern "rust-call" fn call_once(self, (s1, s2): (String, Box)) -> Self::Output { + assert_eq!(&s1 as &str, "s1"); + assert_eq!(&s2 as &str, "s2"); + self.0 + } +} + +struct D(Box); + +impl FnOnce<(String, Box)> for D { + type Output = String; + extern "rust-call" fn call_once(self, (s1, s2): (String, Box)) -> 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), 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), 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), 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), 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 0000000000000..688dcaed81590 --- /dev/null +++ b/src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs @@ -0,0 +1,59 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![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); + +impl Foo for D { + fn foo(self) -> String { + *self.0 + } +} + + +fn main() { + let x = *(Box::new(A) as Box); + assert_eq!(x.foo(), format!("hello")); + let x = *(Box::new(B(42)) as Box); + assert_eq!(x.foo(), format!("42")); + let x = *(Box::new(C(format!("jumping fox"))) as Box); + assert_eq!(x.foo(), format!("jumping fox")); + let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box); + assert_eq!(x.foo(), format!("lazy dog")); +} diff --git a/src/test/run-pass/unsized-locals/by-value-trait-object-safety-withdefault.rs b/src/test/run-pass/unsized-locals/by-value-trait-object-safety-withdefault.rs new file mode 100644 index 0000000000000..b2d228b4da301 --- /dev/null +++ b/src/test/run-pass/unsized-locals/by-value-trait-object-safety-withdefault.rs @@ -0,0 +1,27 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unsized_locals)] + +pub trait Foo { + fn foo(self) -> String { + format!("hello") + } +} + +struct A; + +impl Foo for A {} + + +fn main() { + let x = *(Box::new(A) as Box); + assert_eq!(x.foo(), format!("hello")); +} diff --git a/src/test/run-pass/unsized-locals/by-value-trait-object-safety.rs b/src/test/run-pass/unsized-locals/by-value-trait-object-safety.rs new file mode 100644 index 0000000000000..2912df7ce075e --- /dev/null +++ b/src/test/run-pass/unsized-locals/by-value-trait-object-safety.rs @@ -0,0 +1,29 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unsized_locals)] + +pub trait Foo { + fn foo(self) -> String; +} + +struct A; + +impl Foo for A { + fn foo(self) -> String { + format!("hello") + } +} + + +fn main() { + let x = *(Box::new(A) as Box); + assert_eq!(x.foo(), format!("hello")); +}