Skip to content

Commit d51764d

Browse files
committed
Add DynSized related tests
1 parent 1eefab4 commit d51764d

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 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+
// Test that DynSized cannot be implemented manually.
12+
13+
#![feature(extern_types)]
14+
#![feature(dynsized)]
15+
16+
use std::marker::DynSized;
17+
18+
extern {
19+
type foo;
20+
}
21+
22+
impl DynSized for foo { }
23+
//~^ ERROR explicit impls for the `DynSized` trait are not permitted
24+
25+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 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+
// Ensure `size_of_val` / `align_of_val` can't be used on !DynSized types
12+
13+
#![feature(extern_types)]
14+
15+
use std::mem::{size_of_val, align_of_val};
16+
17+
extern {
18+
type A;
19+
}
20+
21+
fn main() {
22+
let x: &A = unsafe {
23+
&*(1usize as *const A)
24+
};
25+
26+
size_of_val(x); //~ERROR the trait bound `A: std::marker::DynSized` is not satisfied
27+
28+
align_of_val(x); //~ERROR the trait bound `A: std::marker::DynSized` is not satisfied
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 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+
// Ensure !DynSized fields can't be used in structs, even as the last field.
12+
13+
#![feature(extern_types)]
14+
#![feature(dynsized)]
15+
16+
use std::marker::DynSized;
17+
18+
extern {
19+
type foo;
20+
}
21+
22+
struct A {
23+
x: foo, //~ERROR the trait bound `foo: std::marker::DynSized` is not satisfied
24+
}
25+
26+
struct B {
27+
x: usize,
28+
y: foo, //~ERROR the trait bound `foo: std::marker::DynSized` is not satisfied
29+
}
30+
31+
struct C<T: ?DynSized> {
32+
x: usize,
33+
y: T, //~ERROR the trait bound `T: std::marker::DynSized` is not satisfied
34+
}
35+
36+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 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+
// Test that traits can opt-out of being DynSized by default.
12+
// See also run-pass/dynsized/supertrait-default.rs
13+
14+
#![feature(dynsized)]
15+
16+
use std::marker::DynSized;
17+
18+
fn assert_dynsized<T: ?Sized>() { }
19+
20+
trait Tr: ?DynSized {
21+
fn foo() {
22+
assert_dynsized::<Self>();
23+
//~^ ERROR the trait bound `Self: std::marker::DynSized` is not satisfied
24+
}
25+
}
26+
27+
fn main() { }
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 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+
// Ensure !DynSized fields can't be used in tuples, even as the last field.
12+
13+
#![feature(extern_types)]
14+
#![feature(dynsized)]
15+
16+
use std::marker::DynSized;
17+
18+
extern {
19+
type foo;
20+
}
21+
22+
fn baz<T: ?DynSized>() {
23+
let x: &(u8, foo); //~ERROR the trait bound `foo: std::marker::DynSized` is not satisfied
24+
25+
let y: &(u8, T); //~ERROR the trait bound `T: std::marker::DynSized` is not satisfied
26+
}
27+
28+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 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(dynsized)]
12+
13+
use std::marker::DynSized;
14+
15+
fn foo<T: ?DynSized>() {
16+
}
17+
18+
fn bar<T: ?Sized>() {
19+
foo::<T>();
20+
}
21+
22+
fn baz<T>() {
23+
bar::<T>();
24+
}
25+
26+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 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+
// Test that traits have DynSized as a super trait by default.
12+
// See also compile-fail/dynsized/supertrait-unbound.rs
13+
14+
#![feature(dynsized)]
15+
16+
use std::marker::DynSized;
17+
18+
fn assert_dynsized<T: ?Sized>() { }
19+
20+
trait Tr {
21+
fn foo() {
22+
assert_dynsized::<Self>();
23+
}
24+
}
25+
26+
fn main() { }
27+

0 commit comments

Comments
 (0)