forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast-rfc0401.rs
124 lines (110 loc) · 3.8 KB
/
cast-rfc0401.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn illegal_cast<U:?Sized,V:?Sized>(u: *const U) -> *const V
{
u as *const V
//~^ ERROR casting
//~^^ NOTE vtable kinds
}
fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
{
u as *const str
//~^ ERROR casting
//~^^ NOTE vtable kinds
}
trait Foo { fn foo(&self) {} }
impl<T> Foo for T {}
trait Bar { fn foo(&self) {} }
impl<T> Bar for T {}
enum E {
A, B
}
fn main()
{
let f: f32 = 1.2;
let v = 0 as *const u8;
let fat_v : *const [u8] = unsafe { &*(0 as *const [u8; 1])};
let fat_sv : *const [i8] = unsafe { &*(0 as *const [i8; 1])};
let foo: &Foo = &f;
let _ = v as &u8; //~ ERROR non-scalar
let _ = v as E; //~ ERROR non-scalar
let _ = v as fn(); //~ ERROR non-scalar
let _ = v as (u32,); //~ ERROR non-scalar
let _ = Some(&v) as *const u8; //~ ERROR non-scalar
let _ = v as f32;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = main as f64;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = &v as usize;
//~^ ERROR casting
//~^^ HELP through a raw pointer first
let _ = f as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = 3_i32 as bool;
//~^ ERROR cannot cast as `bool` [E0054]
//~| unsupported cast
//~| HELP compare with zero
let _ = E::A as bool;
//~^ ERROR cannot cast as `bool` [E0054]
//~| unsupported cast
//~| HELP compare with zero
let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast
let _ = false as f32;
//~^ ERROR casting
//~^^ HELP through an integer first
let _ = E::A as f32;
//~^ ERROR casting
//~^^ HELP through an integer first
let _ = 'a' as f32;
//~^ ERROR casting
//~^^ HELP through an integer first
let _ = false as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = E::A as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = 'a' as *const u8;
//~^ ERROR casting
//~^^ HELP through a usize first
let _ = 42usize as *const [u8]; //~ ERROR casting
let _ = v as *const [u8]; //~ ERROR cannot cast
let _ = fat_v as *const Foo;
//~^ ERROR the trait bound `[u8]: std::marker::Sized` is not satisfied
//~| NOTE trait `[u8]: std::marker::Sized` not satisfied
//~| NOTE `[u8]` does not have a constant size known at compile-time
//~| NOTE required for the cast to the object type `Foo`
let _ = foo as *const str; //~ ERROR casting
let _ = foo as *mut str; //~ ERROR casting
let _ = main as *mut str; //~ ERROR casting
let _ = &f as *mut f32; //~ ERROR casting
let _ = &f as *const f64; //~ ERROR casting
let _ = fat_sv as usize;
//~^ ERROR casting
//~^^ HELP through a thin pointer first
let a : *const str = "hello";
let _ = a as *const Foo;
//~^ ERROR the trait bound `str: std::marker::Sized` is not satisfied
//~| NOTE trait `str: std::marker::Sized` not satisfied
//~| NOTE `str` does not have a constant size known at compile-time
//~| NOTE required for the cast to the object type `Foo`
// check no error cascade
let _ = main.f as *const u32; //~ no field `f` on type `fn() {main}`
let cf: *const Foo = &0;
let _ = cf as *const [u16];
//~^ ERROR casting
//~^^ NOTE vtable kinds
let _ = cf as *const Bar;
//~^ ERROR casting
//~^^ NOTE vtable kinds
}