Skip to content

Commit 4f965c8

Browse files
author
Ariel Ben-Yehuda
committed
Fix test fallout, and add some rather comprehensive tests.
1 parent 145e3e1 commit 4f965c8

14 files changed

+359
-36
lines changed
Lines changed: 15 additions & 0 deletions
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

Lines changed: 70 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 12 deletions
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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Make sure casts between thin pointer <-> fat pointer are illegal.
12-
13-
pub trait Trait {}
14-
1511
fn main() {
1612
let a: &[i32] = &[1, 2, 3];
1713
let b: Box<[i32]> = Box::new([1, 2, 3]);

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

Lines changed: 2 additions & 12 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)