Skip to content

Commit 4ce35fd

Browse files
committed
Add tests for unsized-locals.
1 parent 5eceab0 commit 4ce35fd

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsized_locals)]
12+
13+
pub trait Foo {
14+
fn foo(self) -> String where Self: Sized;
15+
}
16+
17+
struct A;
18+
19+
impl Foo for A {
20+
fn foo(self) -> String {
21+
format!("hello")
22+
}
23+
}
24+
25+
26+
fn main() {
27+
let x = *(Box::new(A) as Box<dyn Foo>);
28+
x.foo();
29+
//~^ERROR the `foo` method cannot be invoked on a trait object
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsized_locals)]
12+
#![feature(unboxed_closures)]
13+
14+
pub trait FnOnce<Args> {
15+
type Output;
16+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
17+
}
18+
19+
struct A;
20+
21+
impl FnOnce<()> for A {
22+
type Output = String;
23+
extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
24+
format!("hello")
25+
}
26+
}
27+
28+
struct B(i32);
29+
30+
impl FnOnce<()> for B {
31+
type Output = String;
32+
extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
33+
format!("{}", self.0)
34+
}
35+
}
36+
37+
struct C(String);
38+
39+
impl FnOnce<()> for C {
40+
type Output = String;
41+
extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
42+
self.0
43+
}
44+
}
45+
46+
struct D(Box<String>);
47+
48+
impl FnOnce<()> for D {
49+
type Output = String;
50+
extern "rust-call" fn call_once(self, (): ()) -> Self::Output {
51+
*self.0
52+
}
53+
}
54+
55+
56+
fn main() {
57+
let x = *(Box::new(A) as Box<dyn FnOnce<(), Output = String>>);
58+
assert_eq!(x.call_once(()), format!("hello"));
59+
let x = *(Box::new(B(42)) as Box<dyn FnOnce<(), Output = String>>);
60+
assert_eq!(x.call_once(()), format!("42"));
61+
let x = *(Box::new(C(format!("jumping fox"))) as Box<dyn FnOnce<(), Output = String>>);
62+
assert_eq!(x.call_once(()), format!("jumping fox"));
63+
let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box<dyn FnOnce<(), Output = String>>);
64+
assert_eq!(x.call_once(()), format!("lazy dog"));
65+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsized_locals)]
12+
#![feature(unboxed_closures)]
13+
14+
pub trait FnOnce<Args> {
15+
type Output;
16+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
17+
}
18+
19+
struct A;
20+
21+
impl FnOnce<(String, Box<str>)> for A {
22+
type Output = String;
23+
extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
24+
assert_eq!(&s1 as &str, "s1");
25+
assert_eq!(&s2 as &str, "s2");
26+
format!("hello")
27+
}
28+
}
29+
30+
struct B(i32);
31+
32+
impl FnOnce<(String, Box<str>)> for B {
33+
type Output = String;
34+
extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
35+
assert_eq!(&s1 as &str, "s1");
36+
assert_eq!(&s2 as &str, "s2");
37+
format!("{}", self.0)
38+
}
39+
}
40+
41+
struct C(String);
42+
43+
impl FnOnce<(String, Box<str>)> for C {
44+
type Output = String;
45+
extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
46+
assert_eq!(&s1 as &str, "s1");
47+
assert_eq!(&s2 as &str, "s2");
48+
self.0
49+
}
50+
}
51+
52+
struct D(Box<String>);
53+
54+
impl FnOnce<(String, Box<str>)> for D {
55+
type Output = String;
56+
extern "rust-call" fn call_once(self, (s1, s2): (String, Box<str>)) -> Self::Output {
57+
assert_eq!(&s1 as &str, "s1");
58+
assert_eq!(&s2 as &str, "s2");
59+
*self.0
60+
}
61+
}
62+
63+
64+
fn main() {
65+
let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
66+
let x = *(Box::new(A) as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
67+
assert_eq!(x.call_once((s1, s2)), format!("hello"));
68+
let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
69+
let x = *(Box::new(B(42)) as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
70+
assert_eq!(x.call_once((s1, s2)), format!("42"));
71+
let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
72+
let x = *(Box::new(C(format!("jumping fox")))
73+
as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
74+
assert_eq!(x.call_once((s1, s2)), format!("jumping fox"));
75+
let (s1, s2) = (format!("s1"), format!("s2").into_boxed_str());
76+
let x = *(Box::new(D(Box::new(format!("lazy dog"))))
77+
as Box<dyn FnOnce<(String, Box<str>), Output = String>>);
78+
assert_eq!(x.call_once((s1, s2)), format!("lazy dog"));
79+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsized_locals)]
12+
13+
pub trait Foo {
14+
fn foo(self) -> String;
15+
}
16+
17+
struct A;
18+
19+
impl Foo for A {
20+
fn foo(self) -> String {
21+
format!("hello")
22+
}
23+
}
24+
25+
struct B(i32);
26+
27+
impl Foo for B {
28+
fn foo(self) -> String {
29+
format!("{}", self.0)
30+
}
31+
}
32+
33+
struct C(String);
34+
35+
impl Foo for C {
36+
fn foo(self) -> String {
37+
self.0
38+
}
39+
}
40+
41+
struct D(Box<String>);
42+
43+
impl Foo for D {
44+
fn foo(self) -> String {
45+
*self.0
46+
}
47+
}
48+
49+
50+
fn main() {
51+
let x = *(Box::new(A) as Box<dyn Foo>);
52+
assert_eq!(x.foo(), format!("hello"));
53+
let x = *(Box::new(B(42)) as Box<dyn Foo>);
54+
assert_eq!(x.foo(), format!("42"));
55+
let x = *(Box::new(C(format!("jumping fox"))) as Box<dyn Foo>);
56+
assert_eq!(x.foo(), format!("jumping fox"));
57+
let x = *(Box::new(D(Box::new(format!("lazy dog")))) as Box<dyn Foo>);
58+
assert_eq!(x.foo(), format!("lazy dog"));
59+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsized_locals)]
12+
13+
pub trait Foo {
14+
fn foo(self) -> String {
15+
format!("hello")
16+
}
17+
}
18+
19+
struct A;
20+
21+
impl Foo for A {}
22+
23+
24+
fn main() {
25+
let x = *(Box::new(A) as Box<dyn Foo>);
26+
assert_eq!(x.foo(), format!("hello"));
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(unsized_locals)]
12+
13+
pub trait Foo {
14+
fn foo(self) -> String;
15+
}
16+
17+
struct A;
18+
19+
impl Foo for A {
20+
fn foo(self) -> String {
21+
format!("hello")
22+
}
23+
}
24+
25+
26+
fn main() {
27+
let x = *(Box::new(A) as Box<dyn Foo>);
28+
assert_eq!(x.foo(), format!("hello"));
29+
}

0 commit comments

Comments
 (0)