diff --git a/src/test/compile-fail/dynsized/no-manual-impl.rs b/src/test/compile-fail/dynsized/no-manual-impl.rs new file mode 100644 index 0000000000000..b591bc49246c1 --- /dev/null +++ b/src/test/compile-fail/dynsized/no-manual-impl.rs @@ -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 or the MIT license +// , 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() { } diff --git a/src/test/compile-fail/dynsized/size_of_val.rs b/src/test/compile-fail/dynsized/size_of_val.rs new file mode 100644 index 0000000000000..756372978360e --- /dev/null +++ b/src/test/compile-fail/dynsized/size_of_val.rs @@ -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 or the MIT license +// , 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 +} diff --git a/src/test/compile-fail/dynsized/struct-last-field.rs b/src/test/compile-fail/dynsized/struct-last-field.rs new file mode 100644 index 0000000000000..de27126748855 --- /dev/null +++ b/src/test/compile-fail/dynsized/struct-last-field.rs @@ -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 or the MIT license +// , 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 { + x: usize, + y: T, //~ERROR the trait bound `T: std::marker::DynSized` is not satisfied +} + +fn main() { } diff --git a/src/test/compile-fail/dynsized/supertait-unbound.rs b/src/test/compile-fail/dynsized/supertait-unbound.rs new file mode 100644 index 0000000000000..e4d6ae3cbf213 --- /dev/null +++ b/src/test/compile-fail/dynsized/supertait-unbound.rs @@ -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 or the MIT license +// , 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() { } + +trait Tr: ?DynSized { + fn foo() { + assert_dynsized::(); + //~^ ERROR the trait bound `Self: std::marker::DynSized` is not satisfied + } +} + +fn main() { } + diff --git a/src/test/compile-fail/dynsized/tuple-last-field.rs b/src/test/compile-fail/dynsized/tuple-last-field.rs new file mode 100644 index 0000000000000..df86a100f90b4 --- /dev/null +++ b/src/test/compile-fail/dynsized/tuple-last-field.rs @@ -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 or the MIT license +// , 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() { + 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() { } diff --git a/src/test/run-pass/dynsized/sized-implies-dynsized.rs b/src/test/run-pass/dynsized/sized-implies-dynsized.rs new file mode 100644 index 0000000000000..9aa61b4e6b595 --- /dev/null +++ b/src/test/run-pass/dynsized/sized-implies-dynsized.rs @@ -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 or the MIT license +// , 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() { +} + +fn bar() { + foo::(); +} + +fn baz() { + bar::(); +} + +fn main() { } diff --git a/src/test/run-pass/dynsized/supertrait-default.rs b/src/test/run-pass/dynsized/supertrait-default.rs new file mode 100644 index 0000000000000..dabc76112fe07 --- /dev/null +++ b/src/test/run-pass/dynsized/supertrait-default.rs @@ -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 or the MIT license +// , 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() { } + +trait Tr { + fn foo() { + assert_dynsized::(); + } +} + +fn main() { } +