Add tests for unsized-locals.

This commit is contained in:
Masaki Hara 2018-09-12 00:19:09 +09:00
parent 5eceab02ec
commit 4ce35fdd34
6 changed files with 289 additions and 0 deletions

View file

@ -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
}

View file

@ -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"));
}

View file

@ -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"));
}

View file

@ -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"));
}

View file

@ -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"));
}

View file

@ -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"));
}