Skip to content

Commit 360b894

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
Fix test fallout, and add some rather comprehensive tests.
1 parent bad556d commit 360b894

14 files changed

+359
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 main() {
12+
const X: u32 = main as u32; //~ ERROR E0018
13+
const Y: u32 = 0;
14+
const Z: u32 = &Y as *const u32 as u32; //~ ERROR E0018
15+
}

src/test/compile-fail/cast-rfc0401.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

src/test/compile-fail/cast-to-bare-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn foo(_x: isize) { }
1313
fn main() {
1414
let v: u64 = 5;
1515
let x = foo as extern "C" fn() -> isize;
16-
//~^ ERROR mismatched types
16+
//~^ ERROR non-scalar cast
1717
let y = v as extern "Rust" fn(isize) -> (isize, isize);
1818
//~^ ERROR non-scalar cast
1919
y(x());

src/test/compile-fail/const-cast-different-types.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,8 @@
99
// except according to those terms.
1010

1111
static a: &'static str = "foo";
12-
static b: *const u8 = a as *const u8;
13-
//~^ ERROR mismatched types
14-
//~| expected *const u8
15-
//~| found &'static str
16-
//~| expected u8
17-
//~| found str
18-
static c: *const u8 = &a as *const u8;
19-
//~^ ERROR mismatched types
20-
//~| expected *const u8
21-
//~| found &&'static str
22-
//~| expected u8
23-
//~| found &-ptr
12+
static b: *const u8 = a as *const u8; //~ ERROR illegal cast
13+
static c: *const u8 = &a as *const u8; //~ ERROR illegal cast
2414

2515
fn main() {
2616
}

src/test/compile-fail/fat-ptr-cast.rs

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
// except according to those terms.
1010

1111
// Make sure casts between thin-pointer <-> fat pointer obey RFC401
12-
13-
pub trait Trait {}
14-
1512
fn main() {
1613
let a: &[i32] = &[1, 2, 3];
1714
let b: Box<[i32]> = Box::new([1, 2, 3]);

src/test/compile-fail/issue-14845.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@ struct X {
1515

1616
fn main() {
1717
let x = X { a: [0] };
18-
let _f = &x.a as *mut u8;
19-
//~^ ERROR mismatched types
20-
//~| expected `*mut u8`
21-
//~| found `&[u8; 1]`
22-
//~| expected u8
23-
//~| found array of 1 elements
18+
let _f = &x.a as *mut u8; //~ ERROR illegal cast
2419

2520
let local: [u8; 1] = [0];
26-
let _v = &local as *mut u8;
27-
//~^ ERROR mismatched types
28-
//~| expected `*mut u8`
29-
//~| found `&[u8; 1]`
30-
//~| expected u8,
31-
//~| found array of 1 elements
21+
let _v = &local as *mut u8; //~ ERROR illegal cast
3222
}

src/test/compile-fail/issue-17444.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ enum Test {
1414

1515
fn main() {
1616
let _x = Test::Foo as *const isize;
17-
//~^ ERROR illegal cast; cast through an integer first: `Test` as `*const isize`
17+
//~^ ERROR illegal cast; cast through a usize first: `Test` as `*const isize`
1818
}

src/test/compile-fail/issue-21554.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
struct Inches(i32);
1212

1313
fn main() {
14-
Inches as f32; //~ ERROR illegal cast; cast through an integer first
14+
Inches as f32; //~ ERROR illegal cast; cast through a usize first
1515
}

src/test/compile-fail/typeck-cast-pointer-to-float.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
fn main() {
1212
let x : i16 = 22;
1313
((&x) as *const i16) as f32;
14-
//~^ ERROR illegal cast; cast through an integer first: `*const i16` as `f32`
14+
//~^ ERROR illegal cast; cast through a usize first: `*const i16` as `f32`
1515
}

src/test/compile-fail/vector-cast-weirdness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
let mut x1 = X { y: [0, 0] };
2929

3030
// This is still an error since we don't allow casts from &mut [T; n] to *mut T.
31-
let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR mismatched types
31+
let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR illegal cast
3232
let t1: *mut [u8; 2] = &mut x1.y as *mut _;
3333
let h1: *mut [u8; 2] = &mut x1.y as *mut [u8; 2];
3434
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
#![allow(dead_code)]
12+
13+
// check dtor calling order when casting enums.
14+
15+
use std::sync::atomic;
16+
use std::sync::atomic::Ordering;
17+
use std::mem;
18+
19+
enum E {
20+
A = 0,
21+
B = 1,
22+
C = 2
23+
}
24+
25+
static FLAG: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
26+
27+
impl Drop for E {
28+
fn drop(&mut self) {
29+
// avoid dtor loop
30+
unsafe { mem::forget(mem::replace(self, E::B)) };
31+
32+
FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst);
33+
}
34+
}
35+
36+
fn main() {
37+
assert_eq!(FLAG.load(Ordering::SeqCst), 0);
38+
{
39+
let e = E::C;
40+
assert_eq!(e as u32, 2);
41+
assert_eq!(FLAG.load(Ordering::SeqCst), 0);
42+
}
43+
assert_eq!(FLAG.load(Ordering::SeqCst), 1);
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
trait Foo<T> {
12+
fn foo(&self, _: T) -> u32 { 42 }
13+
}
14+
15+
trait Bar {
16+
fn bar(&self) { println!("Bar!"); }
17+
}
18+
19+
impl<T> Foo<T> for () {}
20+
impl Foo<u32> for u32 { fn foo(&self, _: u32) -> u32 { self+43 } }
21+
impl Bar for () {}
22+
23+
unsafe fn fool<'a>(t: *const (Foo<u32>+'a)) -> u32 {
24+
let bar : *const Bar = t as *const Bar;
25+
let foo_e : *const Foo<u16> = t as *const _;
26+
let r_1 = foo_e as *mut Foo<u32>;
27+
28+
(&*r_1).foo(0)*(&*(bar as *const Foo<u32>)).foo(0)
29+
}
30+
31+
#[repr(C)]
32+
struct FooS<T:?Sized>(T);
33+
#[repr(C)]
34+
struct BarS<T:?Sized>(T);
35+
36+
fn foo_to_bar<T:?Sized>(u: *const FooS<T>) -> *const BarS<T> {
37+
u as *const BarS<T>
38+
}
39+
40+
fn main() {
41+
let x = 4u32;
42+
let y : &Foo<u32> = &x;
43+
let fl = unsafe { fool(y as *const Foo<u32>) };
44+
assert_eq!(fl, (43+4)*(43+4));
45+
46+
let s = FooS([0,1,2]);
47+
let u: &FooS<[u32]> = &s;
48+
let u: *const FooS<[u32]> = u;
49+
let bar_ref : *const BarS<[u32]> = foo_to_bar(u);
50+
let z : &BarS<[u32]> = unsafe{&*bar_ref};
51+
assert_eq!(&z.0, &[0,1,2]);
52+
}

0 commit comments

Comments
 (0)