-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/test/compile-fail/unsized-locals/by-value-trait-object-safety.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<dyn Foo>); | ||
x.foo(); | ||
//~^ERROR the `foo` method cannot be invoked on a trait object | ||
} |
65 changes: 65 additions & 0 deletions
65
src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<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")); | ||
} |
79 changes: 79 additions & 0 deletions
79
src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects-rust-call2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<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")); | ||
} |
59 changes: 59 additions & 0 deletions
59
src/test/run-pass-valgrind/unsized-locals/by-value-trait-objects.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<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")); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/run-pass/unsized-locals/by-value-trait-object-safety-withdefault.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<dyn Foo>); | ||
assert_eq!(x.foo(), format!("hello")); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/run-pass/unsized-locals/by-value-trait-object-safety.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<dyn Foo>); | ||
assert_eq!(x.foo(), format!("hello")); | ||
} |