Skip to content

Commit

Permalink
Add tests for unsized-locals.
Browse files Browse the repository at this point in the history
  • Loading branch information
qnighy committed Oct 24, 2018
1 parent 5eceab0 commit 4ce35fd
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 0 deletions.
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
}
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"));
}
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"));
}
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"));
}
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 src/test/run-pass/unsized-locals/by-value-trait-object-safety.rs
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"));
}

0 comments on commit 4ce35fd

Please sign in to comment.