]>
Commit | Line | Data |
---|---|---|
62682a34 SL |
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 | { | |
c1a9b12d SL |
13 | u as *const V |
14 | //~^ ERROR casting | |
15 | //~^^ NOTE vtable kinds | |
62682a34 SL |
16 | } |
17 | ||
18 | fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str | |
19 | { | |
c1a9b12d SL |
20 | u as *const str |
21 | //~^ ERROR casting | |
22 | //~^^ NOTE vtable kinds | |
62682a34 SL |
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 foo: &Foo = &f; | |
41 | ||
42 | let _ = v as &u8; //~ ERROR non-scalar | |
43 | let _ = v as E; //~ ERROR non-scalar | |
44 | let _ = v as fn(); //~ ERROR non-scalar | |
45 | let _ = v as (u32,); //~ ERROR non-scalar | |
46 | let _ = Some(&v) as *const u8; //~ ERROR non-scalar | |
47 | ||
c1a9b12d SL |
48 | let _ = v as f32; |
49 | //~^ ERROR casting | |
50 | //~^^ HELP through a usize first | |
51 | let _ = main as f64; | |
52 | //~^ ERROR casting | |
53 | //~^^ HELP through a usize first | |
54 | let _ = &v as usize; | |
55 | //~^ ERROR casting | |
56 | //~^^ HELP through a raw pointer first | |
57 | let _ = f as *const u8; | |
58 | //~^ ERROR casting | |
59 | //~^^ HELP through a usize first | |
60 | let _ = 3 as bool; | |
61 | //~^ ERROR cannot cast as `bool` | |
62 | //~^^ HELP compare with zero | |
63 | let _ = E::A as bool; | |
64 | //~^ ERROR cannot cast as `bool` | |
65 | //~^^ HELP compare with zero | |
62682a34 SL |
66 | let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast |
67 | ||
c1a9b12d SL |
68 | let _ = false as f32; |
69 | //~^ ERROR casting | |
70 | //~^^ HELP through an integer first | |
71 | let _ = E::A as f32; | |
72 | //~^ ERROR casting | |
73 | //~^^ HELP through an integer first | |
74 | let _ = 'a' as f32; | |
75 | //~^ ERROR casting | |
76 | //~^^ HELP through an integer first | |
62682a34 | 77 | |
c1a9b12d SL |
78 | let _ = false as *const u8; |
79 | //~^ ERROR casting | |
80 | //~^^ HELP through a usize first | |
81 | let _ = E::A as *const u8; | |
82 | //~^ ERROR casting | |
83 | //~^^ HELP through a usize first | |
84 | let _ = 'a' as *const u8; | |
85 | //~^ ERROR casting | |
86 | //~^^ HELP through a usize first | |
62682a34 | 87 | |
c1a9b12d SL |
88 | let _ = 42usize as *const [u8]; //~ ERROR casting |
89 | let _ = v as *const [u8]; //~ ERROR casting | |
62682a34 SL |
90 | let _ = fat_v as *const Foo; |
91 | //~^ ERROR `core::marker::Sized` is not implemented for the type `[u8]` | |
c1a9b12d SL |
92 | let _ = foo as *const str; //~ ERROR casting |
93 | let _ = foo as *mut str; //~ ERROR casting | |
94 | let _ = main as *mut str; //~ ERROR casting | |
95 | let _ = &f as *mut f32; //~ ERROR casting | |
96 | let _ = &f as *const f64; //~ ERROR casting | |
97 | let _ = fat_v as usize; | |
98 | //~^ ERROR casting | |
99 | //~^^ HELP through a raw pointer first | |
62682a34 SL |
100 | |
101 | let a : *const str = "hello"; | |
102 | let _ = a as *const Foo; | |
103 | //~^ ERROR `core::marker::Sized` is not implemented for the type `str` | |
104 | ||
105 | // check no error cascade | |
106 | let _ = main.f as *const u32; //~ ERROR attempted access of field | |
107 | ||
108 | let cf: *const Foo = &0; | |
c1a9b12d SL |
109 | let _ = cf as *const [u8]; |
110 | //~^ ERROR casting | |
111 | //~^^ NOTE vtable kinds | |
112 | let _ = cf as *const Bar; | |
113 | //~^ ERROR casting | |
114 | //~^^ NOTE vtable kinds | |
62682a34 | 115 | } |