1
Fork 0

Add E0607

This commit is contained in:
Guillaume Gomez 2017-06-08 00:13:28 +02:00
parent 30effc14e4
commit 7b8c6a2d30
4 changed files with 42 additions and 9 deletions

View file

@ -225,14 +225,10 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
.emit();
}
CastError::SizedUnsizedCast => {
fcx.type_error_message(self.span,
|actual| {
format!("cannot cast thin pointer `{}` to fat pointer \
`{}`",
actual,
fcx.ty_to_string(self.cast_ty))
},
self.expr_ty)
struct_span_err!(fcx.tcx.sess, self.span, E0607,
"cannot cast thin pointer `{}` to fat pointer `{}`",
self.expr_ty,
fcx.ty_to_string(self.cast_ty)).emit();
}
}
}

View file

@ -4270,6 +4270,29 @@ let y: u32 = *x as u32; // We dereference it first and then cast it.
```
"##,
E0607: r##"
A cast between a thin and a fat pointer was attempted.
Erroneous code example:
```compile_fail,E0607
let v = 0 as *const u8;
v as *const [u8];
```
First: what are thin and fat pointers?
Thin pointers are "simple" pointers that simply reference a memory address.
Fat pointers are pointers referencing Dynamically Sized Types (also called DST).
They don't have a statically known size, therefore they can only exist behind
some kind of pointers that contain additional information. Slices and trait
objects are DSTs.
So in order to fix this error, don't try to cast directly between thin and fat
pointers.
"##,
E0609: r##"
Attempted to access a non-existent field in a struct.

View file

@ -0,0 +1,14 @@
// Copyright 2017 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() {
let v = 0 as *const u8;
v as *const [u8]; //~ ERROR E0607
}

View file

@ -156,7 +156,7 @@ error[E0606]: casting `usize` as `*const [u8]` is invalid
61 | let _ = 42usize as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^
error: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
error[E0607]: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
--> $DIR/cast-rfc0401.rs:62:13
|
62 | let _ = v as *const [u8];