Skip to content

Commit

Permalink
Add DynSized related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Sep 29, 2017
1 parent 1eefab4 commit d51764d
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/test/compile-fail/dynsized/no-manual-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 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.

// Test that DynSized cannot be implemented manually.

#![feature(extern_types)]
#![feature(dynsized)]

use std::marker::DynSized;

extern {
type foo;
}

impl DynSized for foo { }
//~^ ERROR explicit impls for the `DynSized` trait are not permitted

fn main() { }
29 changes: 29 additions & 0 deletions src/test/compile-fail/dynsized/size_of_val.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017 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.

// Ensure `size_of_val` / `align_of_val` can't be used on !DynSized types

#![feature(extern_types)]

use std::mem::{size_of_val, align_of_val};

extern {
type A;
}

fn main() {
let x: &A = unsafe {
&*(1usize as *const A)
};

size_of_val(x); //~ERROR the trait bound `A: std::marker::DynSized` is not satisfied

align_of_val(x); //~ERROR the trait bound `A: std::marker::DynSized` is not satisfied
}
36 changes: 36 additions & 0 deletions src/test/compile-fail/dynsized/struct-last-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2017 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.

// Ensure !DynSized fields can't be used in structs, even as the last field.

#![feature(extern_types)]
#![feature(dynsized)]

use std::marker::DynSized;

extern {
type foo;
}

struct A {
x: foo, //~ERROR the trait bound `foo: std::marker::DynSized` is not satisfied
}

struct B {
x: usize,
y: foo, //~ERROR the trait bound `foo: std::marker::DynSized` is not satisfied
}

struct C<T: ?DynSized> {
x: usize,
y: T, //~ERROR the trait bound `T: std::marker::DynSized` is not satisfied
}

fn main() { }
28 changes: 28 additions & 0 deletions src/test/compile-fail/dynsized/supertait-unbound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017 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.

// Test that traits can opt-out of being DynSized by default.
// See also run-pass/dynsized/supertrait-default.rs

#![feature(dynsized)]

use std::marker::DynSized;

fn assert_dynsized<T: ?Sized>() { }

trait Tr: ?DynSized {
fn foo() {
assert_dynsized::<Self>();
//~^ ERROR the trait bound `Self: std::marker::DynSized` is not satisfied
}
}

fn main() { }

28 changes: 28 additions & 0 deletions src/test/compile-fail/dynsized/tuple-last-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017 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.

// Ensure !DynSized fields can't be used in tuples, even as the last field.

#![feature(extern_types)]
#![feature(dynsized)]

use std::marker::DynSized;

extern {
type foo;
}

fn baz<T: ?DynSized>() {
let x: &(u8, foo); //~ERROR the trait bound `foo: std::marker::DynSized` is not satisfied

let y: &(u8, T); //~ERROR the trait bound `T: std::marker::DynSized` is not satisfied
}

fn main() { }
26 changes: 26 additions & 0 deletions src/test/run-pass/dynsized/sized-implies-dynsized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2017 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(dynsized)]

use std::marker::DynSized;

fn foo<T: ?DynSized>() {
}

fn bar<T: ?Sized>() {
foo::<T>();
}

fn baz<T>() {
bar::<T>();
}

fn main() { }
27 changes: 27 additions & 0 deletions src/test/run-pass/dynsized/supertrait-default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017 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.

// Test that traits have DynSized as a super trait by default.
// See also compile-fail/dynsized/supertrait-unbound.rs

#![feature(dynsized)]

use std::marker::DynSized;

fn assert_dynsized<T: ?Sized>() { }

trait Tr {
fn foo() {
assert_dynsized::<Self>();
}
}

fn main() { }

0 comments on commit d51764d

Please sign in to comment.