2015-05-05 11:39:27 -05:00
|
|
|
// 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
|
|
|
|
{
|
2015-07-26 19:31:38 -05:00
|
|
|
u as *const V
|
|
|
|
//~^ ERROR casting
|
|
|
|
//~^^ NOTE vtable kinds
|
2015-05-05 11:39:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
|
|
|
|
{
|
2015-07-26 19:31:38 -05:00
|
|
|
u as *const str
|
|
|
|
//~^ ERROR casting
|
|
|
|
//~^^ NOTE vtable kinds
|
2015-05-05 11:39:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Foo { fn foo(&self) {} }
|
|
|
|
impl<T> Foo for T {}
|
|
|
|
|
2015-06-18 07:51:06 -05:00
|
|
|
trait Bar { fn foo(&self) {} }
|
|
|
|
impl<T> Bar for T {}
|
|
|
|
|
2015-05-05 11:39:27 -05:00
|
|
|
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])};
|
2015-10-21 11:01:58 -05:00
|
|
|
let fat_sv : *const [i8] = unsafe { &*(0 as *const [i8; 1])};
|
2015-05-05 11:39:27 -05:00
|
|
|
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
|
|
|
|
|
2015-07-26 19:31:38 -05:00
|
|
|
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
|
2016-03-15 03:49:10 -05:00
|
|
|
let _ = 3_i32 as bool;
|
2016-08-22 00:51:21 -05:00
|
|
|
//~^ ERROR cannot cast as `bool` [E0054]
|
|
|
|
//~| unsupported cast
|
2016-04-15 20:23:50 -05:00
|
|
|
//~| HELP compare with zero
|
2015-07-26 19:31:38 -05:00
|
|
|
let _ = E::A as bool;
|
2016-08-22 00:51:21 -05:00
|
|
|
//~^ ERROR cannot cast as `bool` [E0054]
|
|
|
|
//~| unsupported cast
|
2016-04-15 20:23:50 -05:00
|
|
|
//~| HELP compare with zero
|
2015-05-05 11:39:27 -05:00
|
|
|
let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast
|
|
|
|
|
2015-07-26 19:31:38 -05:00
|
|
|
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
|
2015-05-05 11:39:27 -05:00
|
|
|
|
2015-07-26 19:31:38 -05:00
|
|
|
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
|
2015-05-05 11:39:27 -05:00
|
|
|
|
2015-07-26 19:31:38 -05:00
|
|
|
let _ = 42usize as *const [u8]; //~ ERROR casting
|
2016-03-13 14:35:04 -05:00
|
|
|
let _ = v as *const [u8]; //~ ERROR cannot cast
|
2015-05-13 13:58:26 -05:00
|
|
|
let _ = fat_v as *const Foo;
|
2016-04-05 14:56:23 -05:00
|
|
|
//~^ ERROR the trait bound `[u8]: std::marker::Sized` is not satisfied
|
2016-04-15 20:23:50 -05:00
|
|
|
//~| NOTE `[u8]` does not have a constant size known at compile-time
|
|
|
|
//~| NOTE required for the cast to the object type `Foo`
|
2015-07-26 19:31:38 -05:00
|
|
|
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
|
2015-10-21 11:01:58 -05:00
|
|
|
let _ = fat_sv as usize;
|
2015-07-26 19:31:38 -05:00
|
|
|
//~^ ERROR casting
|
2015-08-07 21:08:28 -05:00
|
|
|
//~^^ HELP through a thin pointer first
|
2015-05-05 11:39:27 -05:00
|
|
|
|
2015-05-14 07:04:49 -05:00
|
|
|
let a : *const str = "hello";
|
|
|
|
let _ = a as *const Foo;
|
2016-04-05 14:56:23 -05:00
|
|
|
//~^ ERROR the trait bound `str: std::marker::Sized` is not satisfied
|
2016-04-15 20:23:50 -05:00
|
|
|
//~| NOTE `str` does not have a constant size known at compile-time
|
|
|
|
//~| NOTE required for the cast to the object type `Foo`
|
2015-05-14 07:04:49 -05:00
|
|
|
|
2015-05-05 11:39:27 -05:00
|
|
|
// check no error cascade
|
|
|
|
let _ = main.f as *const u32; //~ ERROR attempted access of field
|
|
|
|
|
2015-06-18 07:51:06 -05:00
|
|
|
let cf: *const Foo = &0;
|
2015-10-21 11:01:58 -05:00
|
|
|
let _ = cf as *const [u16];
|
2015-07-26 19:31:38 -05:00
|
|
|
//~^ ERROR casting
|
|
|
|
//~^^ NOTE vtable kinds
|
|
|
|
let _ = cf as *const Bar;
|
|
|
|
//~^ ERROR casting
|
|
|
|
//~^^ NOTE vtable kinds
|
2015-05-05 11:39:27 -05:00
|
|
|
}
|