2014-06-25 01:22:21 +00:00
|
|
|
// Issue #14893. Tests that casts from vectors don't behave strangely in the
|
|
|
|
// presence of the `_` type shorthand notation.
|
2021-01-28 17:22:33 +00: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-20 04:15:27 +00:00
|
|
|
// Update: after a change to the way casts are done, we have more type information
|
|
|
|
// around and so the errors here are no longer exactly the same.
|
2021-01-28 17:22:33 +00:00
|
|
|
//
|
|
|
|
// Update: With PR #81479 some of the previously rejected cases are now allowed.
|
|
|
|
// New test cases added.
|
2014-06-25 01:22:21 +00:00
|
|
|
|
|
|
|
struct X {
|
2014-12-20 02:20:51 +00:00
|
|
|
y: [u8; 2],
|
2014-06-25 01:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x1 = X { y: [0, 0] };
|
|
|
|
|
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-20 04:15:27 +00:00
|
|
|
// No longer a type mismatch - the `_` can be fully resolved by type inference.
|
|
|
|
let p1: *const u8 = &x1.y as *const _;
|
2021-01-28 17:22:33 +00:00
|
|
|
let p1: *mut u8 = &x1.y as *mut _;
|
|
|
|
//~^ ERROR: casting `&[u8; 2]` as `*mut u8` is invalid
|
2014-12-20 02:20:51 +00:00
|
|
|
let t1: *const [u8; 2] = &x1.y as *const _;
|
2021-01-28 17:22:33 +00:00
|
|
|
let t1: *mut [u8; 2] = &x1.y as *mut _;
|
|
|
|
//~^ ERROR: casting `&[u8; 2]` as `*mut [u8; 2]` is invalid
|
2014-12-20 02:20:51 +00:00
|
|
|
let h1: *const [u8; 2] = &x1.y as *const [u8; 2];
|
2021-01-28 17:22:33 +00:00
|
|
|
let t1: *mut [u8; 2] = &x1.y as *mut [u8; 2];
|
|
|
|
//~^ ERROR: casting `&[u8; 2]` as `*mut [u8; 2]` is invalid
|
2014-06-25 01:22:21 +00:00
|
|
|
|
|
|
|
let mut x1 = X { y: [0, 0] };
|
|
|
|
|
2021-01-28 17:22:33 +00:00
|
|
|
let p1: *mut u8 = &mut x1.y as *mut _;
|
|
|
|
let p2: *const u8 = &mut x1.y as *const _;
|
2014-12-20 02:20:51 +00:00
|
|
|
let t1: *mut [u8; 2] = &mut x1.y as *mut _;
|
|
|
|
let h1: *mut [u8; 2] = &mut x1.y as *mut [u8; 2];
|
2014-06-25 01:22:21 +00:00
|
|
|
}
|