Skip to content

Commit

Permalink
Fix test fallout, and add some rather comprehensive tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Ben-Yehuda authored and arielb1 committed May 19, 2015
1 parent 83acebc commit 9ee2335
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 35 deletions.
15 changes: 15 additions & 0 deletions src/test/compile-fail/cast-ptr-to-int-const.rs
@@ -0,0 +1,15 @@
// 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 main() {
const X: u32 = main as u32; //~ ERROR E0018
const Y: u32 = 0;
const Z: u32 = &Y as *const u32 as u32; //~ ERROR E0018
}
70 changes: 70 additions & 0 deletions src/test/compile-fail/cast-rfc0401.rs
@@ -0,0 +1,70 @@
// 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 vtable kinds
}

fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
{
u as *const str //~ ERROR vtable kinds
}

trait Foo { fn foo(&self) {} }
impl<T> Foo 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 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 through a usize first
let _ = main as f64; //~ ERROR through a usize first
let _ = &v as usize; //~ ERROR through a raw pointer first
let _ = f as *const u8; //~ ERROR through a usize first
let _ = 3 as bool; //~ ERROR compare with zero
let _ = E::A as bool; //~ ERROR compare with zero
let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast

let _ = false as f32; //~ ERROR through an integer first
let _ = E::A as f32; //~ ERROR through an integer first
let _ = 'a' as f32; //~ ERROR through an integer first

let _ = false as *const u8; //~ ERROR through a usize first
let _ = E::A as *const u8; //~ ERROR through a usize first
let _ = 'a' as *const u8; //~ ERROR through a usize first

let _ = 42usize as *const [u8]; //~ ERROR illegal cast
let _ = v as *const [u8]; //~ ERROR illegal cast
let _ = fat_v as *const Foo; //~ ERROR illegal cast
let _ = foo as *const str; //~ ERROR illegal cast
let _ = foo as *mut str; //~ ERROR illegal cast
let _ = main as *mut str; //~ ERROR illegal cast
let _ = &f as *mut f32; //~ ERROR illegal cast
let _ = &f as *const f64; //~ ERROR illegal cast
let _ = fat_v as usize; //~ ERROR through a raw pointer first

// check no error cascade
let _ = main.f as *const u32; //~ ERROR attempted access of field

}
2 changes: 1 addition & 1 deletion src/test/compile-fail/cast-to-bare-fn.rs
Expand Up @@ -13,7 +13,7 @@ fn foo(_x: isize) { }
fn main() {
let v: u64 = 5;
let x = foo as extern "C" fn() -> isize;
//~^ ERROR mismatched types
//~^ ERROR non-scalar cast
let y = v as extern "Rust" fn(isize) -> (isize, isize);
//~^ ERROR non-scalar cast
y(x());
Expand Down
14 changes: 2 additions & 12 deletions src/test/compile-fail/const-cast-different-types.rs
Expand Up @@ -9,18 +9,8 @@
// except according to those terms.

static a: &'static str = "foo";
static b: *const u8 = a as *const u8;
//~^ ERROR mismatched types
//~| expected *const u8
//~| found &'static str
//~| expected u8
//~| found str
static c: *const u8 = &a as *const u8;
//~^ ERROR mismatched types
//~| expected *const u8
//~| found &&'static str
//~| expected u8
//~| found &-ptr
static b: *const u8 = a as *const u8; //~ ERROR illegal cast
static c: *const u8 = &a as *const u8; //~ ERROR illegal cast

fn main() {
}
3 changes: 0 additions & 3 deletions src/test/compile-fail/fat-ptr-cast.rs
Expand Up @@ -9,9 +9,6 @@
// except according to those terms.

// Make sure casts between thin-pointer <-> fat pointer obey RFC401

pub trait Trait {}

fn main() {
let a: &[i32] = &[1, 2, 3];
let b: Box<[i32]> = Box::new([1, 2, 3]);
Expand Down
14 changes: 2 additions & 12 deletions src/test/compile-fail/issue-14845.rs
Expand Up @@ -15,18 +15,8 @@ struct X {

fn main() {
let x = X { a: [0] };
let _f = &x.a as *mut u8;
//~^ ERROR mismatched types
//~| expected `*mut u8`
//~| found `&[u8; 1]`
//~| expected u8
//~| found array of 1 elements
let _f = &x.a as *mut u8; //~ ERROR illegal cast

let local: [u8; 1] = [0];
let _v = &local as *mut u8;
//~^ ERROR mismatched types
//~| expected `*mut u8`
//~| found `&[u8; 1]`
//~| expected u8,
//~| found array of 1 elements
let _v = &local as *mut u8; //~ ERROR illegal cast
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-17444.rs
Expand Up @@ -14,5 +14,5 @@ enum Test {

fn main() {
let _x = Test::Foo as *const isize;
//~^ ERROR illegal cast; cast through an integer first: `Test` as `*const isize`
//~^ ERROR illegal cast; cast through a usize first: `Test` as `*const isize`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-21554.rs
Expand Up @@ -11,5 +11,5 @@
struct Inches(i32);

fn main() {
Inches as f32; //~ ERROR illegal cast; cast through an integer first
Inches as f32; //~ ERROR illegal cast; cast through a usize first
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/typeck-cast-pointer-to-float.rs
Expand Up @@ -11,5 +11,5 @@
fn main() {
let x : i16 = 22;
((&x) as *const i16) as f32;
//~^ ERROR illegal cast; cast through an integer first: `*const i16` as `f32`
//~^ ERROR illegal cast; cast through a usize first: `*const i16` as `f32`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/vector-cast-weirdness.rs
Expand Up @@ -28,7 +28,7 @@ fn main() {
let mut x1 = X { y: [0, 0] };

// This is still an error since we don't allow casts from &mut [T; n] to *mut T.
let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR mismatched types
let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR illegal cast
let t1: *mut [u8; 2] = &mut x1.y as *mut _;
let h1: *mut [u8; 2] = &mut x1.y as *mut [u8; 2];
}
44 changes: 44 additions & 0 deletions src/test/run-pass/cast-enum-with-dtor.rs
@@ -0,0 +1,44 @@
// 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.

#![allow(dead_code)]

// check dtor calling order when casting enums.

use std::sync::atomic;
use std::sync::atomic::Ordering;
use std::mem;

enum E {
A = 0,
B = 1,
C = 2
}

static FLAG: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;

impl Drop for E {
fn drop(&mut self) {
// avoid dtor loop
unsafe { mem::forget(mem::replace(self, E::B)) };

FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst);
}
}

fn main() {
assert_eq!(FLAG.load(Ordering::SeqCst), 0);
{
let e = E::C;
assert_eq!(e as u32, 2);
assert_eq!(FLAG.load(Ordering::SeqCst), 0);
}
assert_eq!(FLAG.load(Ordering::SeqCst), 1);
}
52 changes: 52 additions & 0 deletions src/test/run-pass/cast-rfc0401-vtable-kinds.rs
@@ -0,0 +1,52 @@
// 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.

trait Foo<T> {
fn foo(&self, _: T) -> u32 { 42 }
}

trait Bar {
fn bar(&self) { println!("Bar!"); }
}

impl<T> Foo<T> for () {}
impl Foo<u32> for u32 { fn foo(&self, _: u32) -> u32 { self+43 } }
impl Bar for () {}

unsafe fn fool<'a>(t: *const (Foo<u32>+'a)) -> u32 {
let bar : *const Bar = t as *const Bar;
let foo_e : *const Foo<u16> = t as *const _;
let r_1 = foo_e as *mut Foo<u32>;

(&*r_1).foo(0)*(&*(bar as *const Foo<u32>)).foo(0)
}

#[repr(C)]
struct FooS<T:?Sized>(T);
#[repr(C)]
struct BarS<T:?Sized>(T);

fn foo_to_bar<T:?Sized>(u: *const FooS<T>) -> *const BarS<T> {
u as *const BarS<T>
}

fn main() {
let x = 4u32;
let y : &Foo<u32> = &x;
let fl = unsafe { fool(y as *const Foo<u32>) };
assert_eq!(fl, (43+4)*(43+4));

let s = FooS([0,1,2]);
let u: &FooS<[u32]> = &s;
let u: *const FooS<[u32]> = u;
let bar_ref : *const BarS<[u32]> = foo_to_bar(u);
let z : &BarS<[u32]> = unsafe{&*bar_ref};
assert_eq!(&z.0, &[0,1,2]);
}

0 comments on commit 9ee2335

Please sign in to comment.