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