]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/cast-rfc0401.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / compile-fail / cast-rfc0401.rs
1 // Copyright 2015 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 fn illegal_cast<U:?Sized,V:?Sized>(u: *const U) -> *const V
12 {
13 u as *const V
14 //~^ ERROR casting
15 //~^^ NOTE vtable kinds
16 }
17
18 fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
19 {
20 u as *const str
21 //~^ ERROR casting
22 //~^^ NOTE vtable kinds
23 }
24
25 trait Foo { fn foo(&self) {} }
26 impl<T> Foo for T {}
27
28 trait Bar { fn foo(&self) {} }
29 impl<T> Bar for T {}
30
31 enum E {
32 A, B
33 }
34
35 fn main()
36 {
37 let f: f32 = 1.2;
38 let v = 0 as *const u8;
39 let fat_v : *const [u8] = unsafe { &*(0 as *const [u8; 1])};
40 let fat_sv : *const [i8] = unsafe { &*(0 as *const [i8; 1])};
41 let foo: &Foo = &f;
42
43 let _ = v as &u8; //~ ERROR non-scalar
44 let _ = v as E; //~ ERROR non-scalar
45 let _ = v as fn(); //~ ERROR non-scalar
46 let _ = v as (u32,); //~ ERROR non-scalar
47 let _ = Some(&v) as *const u8; //~ ERROR non-scalar
48
49 let _ = v as f32;
50 //~^ ERROR casting
51 //~^^ HELP through a usize first
52 let _ = main as f64;
53 //~^ ERROR casting
54 //~^^ HELP through a usize first
55 let _ = &v as usize;
56 //~^ ERROR casting
57 //~^^ HELP through a raw pointer first
58 let _ = f as *const u8;
59 //~^ ERROR casting
60 //~^^ HELP through a usize first
61 let _ = 3 as bool;
62 //~^ ERROR cannot cast as `bool`
63 //~^^ HELP compare with zero
64 //~^^^ HELP run `rustc --explain E0054` to see a detailed explanation
65 let _ = E::A as bool;
66 //~^ ERROR cannot cast as `bool`
67 //~^^ HELP compare with zero
68 //~^^^ HELP run `rustc --explain E0054` to see a detailed explanation
69 let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast
70
71 let _ = false as f32;
72 //~^ ERROR casting
73 //~^^ HELP through an integer first
74 let _ = E::A as f32;
75 //~^ ERROR casting
76 //~^^ HELP through an integer first
77 let _ = 'a' as f32;
78 //~^ ERROR casting
79 //~^^ HELP through an integer first
80
81 let _ = false as *const u8;
82 //~^ ERROR casting
83 //~^^ HELP through a usize first
84 let _ = E::A as *const u8;
85 //~^ ERROR casting
86 //~^^ HELP through a usize first
87 let _ = 'a' as *const u8;
88 //~^ ERROR casting
89 //~^^ HELP through a usize first
90
91 let _ = 42usize as *const [u8]; //~ ERROR casting
92 let _ = v as *const [u8]; //~ ERROR cannot cast
93 let _ = fat_v as *const Foo;
94 //~^ ERROR the trait bound `[u8]: std::marker::Sized` is not satisfied
95 //~^^ HELP run `rustc --explain E0277` to see a detailed explanation
96 //~^^^ NOTE `[u8]` does not have a constant size known at compile-time
97 //~^^^^ NOTE required for the cast to the object type `Foo`
98 let _ = foo as *const str; //~ ERROR casting
99 let _ = foo as *mut str; //~ ERROR casting
100 let _ = main as *mut str; //~ ERROR casting
101 let _ = &f as *mut f32; //~ ERROR casting
102 let _ = &f as *const f64; //~ ERROR casting
103 let _ = fat_sv as usize;
104 //~^ ERROR casting
105 //~^^ HELP through a thin pointer first
106
107 let a : *const str = "hello";
108 let _ = a as *const Foo;
109 //~^ ERROR the trait bound `str: std::marker::Sized` is not satisfied
110 //~^^ HELP run `rustc --explain E0277` to see a detailed explanation
111 //~^^^ NOTE `str` does not have a constant size known at compile-time
112 //~^^^^ NOTE required for the cast to the object type `Foo`
113
114 // check no error cascade
115 let _ = main.f as *const u32; //~ ERROR attempted access of field
116
117 let cf: *const Foo = &0;
118 let _ = cf as *const [u16];
119 //~^ ERROR casting
120 //~^^ NOTE vtable kinds
121 let _ = cf as *const Bar;
122 //~^ ERROR casting
123 //~^^ NOTE vtable kinds
124 }