2014-06-28 15:57:36 -05:00
|
|
|
|
// Copyright 2014 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.
|
2014-10-15 01:05:01 -05:00
|
|
|
|
|
2014-06-28 15:57:36 -05:00
|
|
|
|
use core::ptr::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test() {
|
|
|
|
|
unsafe {
|
|
|
|
|
struct Pair {
|
2015-03-25 19:06:52 -05:00
|
|
|
|
fst: isize,
|
|
|
|
|
snd: isize
|
2014-06-28 15:57:36 -05:00
|
|
|
|
};
|
|
|
|
|
let mut p = Pair {fst: 10, snd: 20};
|
|
|
|
|
let pptr: *mut Pair = &mut p;
|
2015-07-23 20:04:55 -05:00
|
|
|
|
let iptr: *mut isize = pptr as *mut isize;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
assert_eq!(*iptr, 10);
|
|
|
|
|
*iptr = 30;
|
|
|
|
|
assert_eq!(*iptr, 30);
|
|
|
|
|
assert_eq!(p.fst, 30);
|
|
|
|
|
|
|
|
|
|
*pptr = Pair {fst: 50, snd: 60};
|
|
|
|
|
assert_eq!(*iptr, 50);
|
|
|
|
|
assert_eq!(p.fst, 50);
|
|
|
|
|
assert_eq!(p.snd, 60);
|
|
|
|
|
|
|
|
|
|
let v0 = vec![32000u16, 32001u16, 32002u16];
|
|
|
|
|
let mut v1 = vec![0u16, 0u16, 0u16];
|
|
|
|
|
|
2015-03-27 13:12:28 -05:00
|
|
|
|
copy(v0.as_ptr().offset(1), v1.as_mut_ptr().offset(1), 1);
|
2014-10-15 01:05:01 -05:00
|
|
|
|
assert!((v1[0] == 0u16 &&
|
|
|
|
|
v1[1] == 32001u16 &&
|
|
|
|
|
v1[2] == 0u16));
|
2015-03-27 13:12:28 -05:00
|
|
|
|
copy(v0.as_ptr().offset(2), v1.as_mut_ptr(), 1);
|
2014-10-15 01:05:01 -05:00
|
|
|
|
assert!((v1[0] == 32002u16 &&
|
|
|
|
|
v1[1] == 32001u16 &&
|
|
|
|
|
v1[2] == 0u16));
|
2015-03-27 13:12:28 -05:00
|
|
|
|
copy(v0.as_ptr(), v1.as_mut_ptr().offset(2), 1);
|
2014-10-15 01:05:01 -05:00
|
|
|
|
assert!((v1[0] == 32002u16 &&
|
|
|
|
|
v1[1] == 32001u16 &&
|
|
|
|
|
v1[2] == 32000u16));
|
2014-06-28 15:57:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_is_null() {
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let p: *const isize = null();
|
2014-06-28 15:57:36 -05:00
|
|
|
|
assert!(p.is_null());
|
|
|
|
|
|
|
|
|
|
let q = unsafe { p.offset(1) };
|
|
|
|
|
assert!(!q.is_null());
|
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let mp: *mut isize = null_mut();
|
2014-06-28 15:57:36 -05:00
|
|
|
|
assert!(mp.is_null());
|
|
|
|
|
|
|
|
|
|
let mq = unsafe { mp.offset(1) };
|
|
|
|
|
assert!(!mq.is_null());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2014-08-20 15:06:34 -05:00
|
|
|
|
fn test_as_ref() {
|
2014-06-28 15:57:36 -05:00
|
|
|
|
unsafe {
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let p: *const isize = null();
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert_eq!(p.as_ref(), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let q: *const isize = &2;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert_eq!(q.as_ref().unwrap(), &2);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let p: *mut isize = null_mut();
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert_eq!(p.as_ref(), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let q: *mut isize = &mut 2;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert_eq!(q.as_ref().unwrap(), &2);
|
|
|
|
|
|
|
|
|
|
// Lifetime inference
|
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 u = 2isize;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
{
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let p = &u as *const isize;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert_eq!(p.as_ref().unwrap(), &2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_as_mut() {
|
|
|
|
|
unsafe {
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let p: *mut isize = null_mut();
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert!(p.as_mut() == None);
|
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let q: *mut isize = &mut 2;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert!(q.as_mut().unwrap() == &mut 2);
|
|
|
|
|
|
|
|
|
|
// Lifetime inference
|
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 mut u = 2isize;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
{
|
2015-03-25 19:06:52 -05:00
|
|
|
|
let p = &mut u as *mut isize;
|
2014-08-20 15:06:34 -05:00
|
|
|
|
assert!(p.as_mut().unwrap() == &mut 2);
|
|
|
|
|
}
|
2014-06-28 15:57:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_ptr_addition() {
|
|
|
|
|
unsafe {
|
2015-07-08 15:52:55 -05:00
|
|
|
|
let xs = vec![5; 16];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
let mut ptr = xs.as_ptr();
|
|
|
|
|
let end = ptr.offset(16);
|
|
|
|
|
|
|
|
|
|
while ptr < end {
|
|
|
|
|
assert_eq!(*ptr, 5);
|
|
|
|
|
ptr = ptr.offset(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut xs_mut = xs;
|
|
|
|
|
let mut m_ptr = xs_mut.as_mut_ptr();
|
|
|
|
|
let m_end = m_ptr.offset(16);
|
|
|
|
|
|
|
|
|
|
while m_ptr < m_end {
|
|
|
|
|
*m_ptr += 5;
|
|
|
|
|
m_ptr = m_ptr.offset(1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 15:52:55 -05:00
|
|
|
|
assert!(xs_mut == vec![10; 16]);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_ptr_subtraction() {
|
|
|
|
|
unsafe {
|
|
|
|
|
let xs = vec![0,1,2,3,4,5,6,7,8,9];
|
2015-03-03 02:42:26 -06:00
|
|
|
|
let mut idx = 9;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
let ptr = xs.as_ptr();
|
|
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
|
while idx >= 0 {
|
2015-03-25 19:06:52 -05:00
|
|
|
|
assert_eq!(*(ptr.offset(idx as isize)), idx as isize);
|
2015-03-03 02:42:26 -06:00
|
|
|
|
idx = idx - 1;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut xs_mut = xs;
|
|
|
|
|
let m_start = xs_mut.as_mut_ptr();
|
|
|
|
|
let mut m_ptr = m_start.offset(9);
|
|
|
|
|
|
|
|
|
|
while m_ptr >= m_start {
|
|
|
|
|
*m_ptr += *m_ptr;
|
|
|
|
|
m_ptr = m_ptr.offset(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 12:15:45 -06:00
|
|
|
|
assert_eq!(xs_mut, [0,2,4,6,8,10,12,14,16,18]);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_set_memory() {
|
2014-12-30 02:19:41 -06:00
|
|
|
|
let mut xs = [0u8; 20];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
let ptr = xs.as_mut_ptr();
|
2015-03-18 21:51:10 -05:00
|
|
|
|
unsafe { write_bytes(ptr, 5u8, xs.len()); }
|
2014-12-30 02:19:41 -06:00
|
|
|
|
assert!(xs == [5u8; 20]);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
}
|
2015-02-10 07:37:44 -06:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_unsized_unique() {
|
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 xs: &mut [i32] = &mut [1, 2, 3];
|
|
|
|
|
let ptr = unsafe { Unique::new(xs as *mut [i32]) };
|
2015-02-12 09:33:21 -06:00
|
|
|
|
let ys = unsafe { &mut **ptr };
|
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 zs: &mut [i32] = &mut [1, 2, 3];
|
2015-02-10 07:37:44 -06:00
|
|
|
|
assert!(ys == zs);
|
|
|
|
|
}
|
2016-07-16 16:15:15 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
2016-08-29 14:31:28 -05:00
|
|
|
|
#[allow(warnings)]
|
|
|
|
|
// Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the
|
|
|
|
|
// ABI, or even point to an actual executable code, because the function itself is never invoked.
|
|
|
|
|
#[no_mangle]
|
|
|
|
|
pub fn test_variadic_fnptr() {
|
2016-07-16 16:15:15 -05:00
|
|
|
|
use core::hash::{Hash, SipHasher};
|
2016-08-29 14:31:28 -05:00
|
|
|
|
extern {
|
|
|
|
|
fn test_variadic_fnptr(_: u64, ...) -> f64;
|
2016-07-16 16:15:15 -05:00
|
|
|
|
}
|
2016-08-29 14:31:28 -05:00
|
|
|
|
let p: unsafe extern fn(u64, ...) -> f64 = test_variadic_fnptr;
|
2016-07-16 16:15:15 -05:00
|
|
|
|
let q = p.clone();
|
|
|
|
|
assert_eq!(p, q);
|
|
|
|
|
assert!(!(p < q));
|
|
|
|
|
let mut s = SipHasher::new();
|
|
|
|
|
assert_eq!(p.hash(&mut s), q.hash(&mut s));
|
|
|
|
|
}
|