Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix test fallout, and add some rather comprehensive tests.
- Loading branch information
Showing
14 changed files
with
359 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} |
Oops, something went wrong.