2014-12-12 17:39:27 -06:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-06-28 15:57:36 -05:00
|
|
|
// 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.
|
|
|
|
use core::mem::*;
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn size_of_basic() {
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(size_of::<u8>(), 1);
|
|
|
|
assert_eq!(size_of::<u16>(), 2);
|
|
|
|
assert_eq!(size_of::<u32>(), 4);
|
|
|
|
assert_eq!(size_of::<u64>(), 8);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-13 20:59:45 -06:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn size_of_32() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<usize>(), 4);
|
|
|
|
assert_eq!(size_of::<*const usize>(), 4);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-13 20:59:45 -06:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn size_of_64() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<usize>(), 8);
|
|
|
|
assert_eq!(size_of::<*const usize>(), 8);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn size_of_val_basic() {
|
|
|
|
assert_eq!(size_of_val(&1u8), 1);
|
|
|
|
assert_eq!(size_of_val(&1u16), 2);
|
|
|
|
assert_eq!(size_of_val(&1u32), 4);
|
|
|
|
assert_eq!(size_of_val(&1u64), 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn align_of_basic() {
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(align_of::<u8>(), 1);
|
|
|
|
assert_eq!(align_of::<u16>(), 2);
|
|
|
|
assert_eq!(align_of::<u32>(), 4);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-13 20:59:45 -06:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn align_of_32() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(align_of::<usize>(), 4);
|
|
|
|
assert_eq!(align_of::<*const usize>(), 4);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-13 20:59:45 -06:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn align_of_64() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(align_of::<usize>(), 8);
|
|
|
|
assert_eq!(align_of::<*const usize>(), 8);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn align_of_val_basic() {
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(align_of_val(&1u8), 1);
|
|
|
|
assert_eq!(align_of_val(&1u16), 2);
|
|
|
|
assert_eq!(align_of_val(&1u32), 4);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut x = 31337;
|
|
|
|
let mut y = 42;
|
2014-06-28 15:57:36 -05:00
|
|
|
swap(&mut x, &mut y);
|
|
|
|
assert_eq!(x, 42);
|
|
|
|
assert_eq!(y, 31337);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_replace() {
|
|
|
|
let mut x = Some("test".to_string());
|
|
|
|
let y = replace(&mut x, None);
|
|
|
|
assert!(x.is_none());
|
|
|
|
assert!(y.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transmute_copy() {
|
2015-02-03 07:50:52 -06:00
|
|
|
assert_eq!(1, unsafe { transmute_copy(&1) });
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transmute() {
|
2015-02-12 09:29:52 -06:00
|
|
|
trait Foo { fn dummy(&self) { } }
|
2015-03-25 19:06:52 -05:00
|
|
|
impl Foo for isize {}
|
2014-06-28 15:57:36 -05:00
|
|
|
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-19 23:15:27 -05:00
|
|
|
let a = box 100isize as Box<Foo>;
|
2014-06-28 15:57:36 -05:00
|
|
|
unsafe {
|
|
|
|
let x: ::core::raw::TraitObject = transmute(a);
|
2015-03-25 19:06:52 -05:00
|
|
|
assert!(*(x.data as *const isize) == 100);
|
2014-06-28 15:57:36 -05:00
|
|
|
let _x: Box<Foo> = transmute(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2015-03-10 19:59:23 -05:00
|
|
|
assert_eq!(transmute::<_, Vec<u8>>("L".to_string()), [76]);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME #13642 (these benchmarks should be in another place)
|
|
|
|
/// Completely miscellaneous language-construct benchmarks.
|
|
|
|
// Static/dynamic method dispatch
|
|
|
|
|
|
|
|
struct Struct {
|
2015-03-25 19:06:52 -05:00
|
|
|
field: isize
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Trait {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn method(&self) -> isize;
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Trait for Struct {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn method(&self) -> isize {
|
2014-06-28 15:57:36 -05:00
|
|
|
self.field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn trait_vtable_method_call(b: &mut Bencher) {
|
|
|
|
let s = Struct { field: 10 };
|
|
|
|
let t = &s as &Trait;
|
|
|
|
b.iter(|| {
|
|
|
|
t.method()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn trait_static_method_call(b: &mut Bencher) {
|
|
|
|
let s = Struct { field: 10 };
|
|
|
|
b.iter(|| {
|
|
|
|
s.method()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overhead of various match forms
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn match_option_some(b: &mut Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let x = Some(10);
|
2014-06-28 15:57:36 -05:00
|
|
|
b.iter(|| {
|
|
|
|
match x {
|
|
|
|
Some(y) => y,
|
|
|
|
None => 11
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn match_vec_pattern(b: &mut Bencher) {
|
2015-01-25 15:05:03 -06:00
|
|
|
let x = [1,2,3,4,5,6];
|
2014-06-28 15:57:36 -05:00
|
|
|
b.iter(|| {
|
|
|
|
match x {
|
2015-01-25 15:05:03 -06:00
|
|
|
[1,2,3,..] => 10,
|
|
|
|
_ => 11,
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|