|
| 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 //~ ERROR vtable kinds |
| 14 | +} |
| 15 | + |
| 16 | +fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str |
| 17 | +{ |
| 18 | + u as *const str //~ ERROR vtable kinds |
| 19 | +} |
| 20 | + |
| 21 | +trait Foo { fn foo(&self) {} } |
| 22 | +impl<T> Foo for T {} |
| 23 | + |
| 24 | +enum E { |
| 25 | + A, B |
| 26 | +} |
| 27 | + |
| 28 | +fn main() |
| 29 | +{ |
| 30 | + let f: f32 = 1.2; |
| 31 | + let v = 0 as *const u8; |
| 32 | + let fat_v : *const [u8] = unsafe { &*(0 as *const [u8; 1])}; |
| 33 | + let foo: &Foo = &f; |
| 34 | + |
| 35 | + let _ = v as &u8; //~ ERROR non-scalar |
| 36 | + let _ = v as E; //~ ERROR non-scalar |
| 37 | + let _ = v as fn(); //~ ERROR non-scalar |
| 38 | + let _ = v as (u32,); //~ ERROR non-scalar |
| 39 | + let _ = Some(&v) as *const u8; //~ ERROR non-scalar |
| 40 | + |
| 41 | + let _ = v as f32; //~ ERROR through a usize first |
| 42 | + let _ = main as f64; //~ ERROR through a usize first |
| 43 | + let _ = &v as usize; //~ ERROR through a raw pointer first |
| 44 | + let _ = f as *const u8; //~ ERROR through a usize first |
| 45 | + let _ = 3 as bool; //~ ERROR compare with zero |
| 46 | + let _ = E::A as bool; //~ ERROR compare with zero |
| 47 | + let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast |
| 48 | + |
| 49 | + let _ = false as f32; //~ ERROR through an integer first |
| 50 | + let _ = E::A as f32; //~ ERROR through an integer first |
| 51 | + let _ = 'a' as f32; //~ ERROR through an integer first |
| 52 | + |
| 53 | + let _ = false as *const u8; //~ ERROR through a usize first |
| 54 | + let _ = E::A as *const u8; //~ ERROR through a usize first |
| 55 | + let _ = 'a' as *const u8; //~ ERROR through a usize first |
| 56 | + |
| 57 | + let _ = 42usize as *const [u8]; //~ ERROR illegal cast |
| 58 | + let _ = v as *const [u8]; //~ ERROR illegal cast |
| 59 | + let _ = fat_v as *const Foo; //~ ERROR illegal cast |
| 60 | + let _ = foo as *const str; //~ ERROR illegal cast |
| 61 | + let _ = foo as *mut str; //~ ERROR illegal cast |
| 62 | + let _ = main as *mut str; //~ ERROR illegal cast |
| 63 | + let _ = &f as *mut f32; //~ ERROR illegal cast |
| 64 | + let _ = &f as *const f64; //~ ERROR illegal cast |
| 65 | + let _ = fat_v as usize; //~ ERROR through a raw pointer first |
| 66 | + |
| 67 | + // check no error cascade |
| 68 | + let _ = main.f as *const u32; //~ ERROR attempted access of field |
| 69 | + |
| 70 | +} |
0 commit comments