]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/dst-bad-coercions.rs
rollup merge of #17355 : gamazeps/issue17210
[rust.git] / src / test / compile-fail / dst-bad-coercions.rs
1 // Copyright 2014 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 implicit coercions involving DSTs and raw pointers.
12
13 struct S;
14 trait T {}
15 impl T for S {}
16
17 struct Foo<Sized? T> {
18     f: T
19 }
20
21 pub fn main() {
22     // Test that we cannot convert from *-ptr to &-ptr
23     let x: *const S = &S;
24     let y: &S = x; //~ ERROR mismatched types
25     let y: &T = x; //~ ERROR mismatched types
26
27     // Test that we cannot convert from *-ptr to &-ptr (mut version)
28     let x: *mut S = &mut S;
29     let y: &S = x; //~ ERROR mismatched types
30     let y: &T = x; //~ ERROR mismatched types
31
32     // Test that we cannot convert an immutable ptr to a mutable one using *-ptrs
33     let x: &mut T = &S; //~ ERROR mismatched types
34     let x: *mut T = &S; //~ ERROR mismatched types
35     let x: *mut S = &S; //~ ERROR mismatched types
36
37     // The below four sets of tests test that we cannot implicitly deref a *-ptr
38     // during a coercion.
39     let x: *const S = &S;
40     let y: *const T = x;  //~ ERROR mismatched types
41
42     let x: *mut S = &mut S;
43     let y: *mut T = x;  //~ ERROR mismatched types
44
45     let x: *const Foo<S> = &Foo {f: S};
46     let y: *const Foo<T> = x;  //~ ERROR mismatched types
47
48     let x: *mut Foo<S> = &mut Foo {f: S};
49     let y: *mut Foo<T> = x;  //~ ERROR mismatched types
50 }