Skip to content

Commit 7b8c6a2

Browse files
Add E0607
1 parent 30effc1 commit 7b8c6a2

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

src/librustc_typeck/check/cast.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,10 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
225225
.emit();
226226
}
227227
CastError::SizedUnsizedCast => {
228-
fcx.type_error_message(self.span,
229-
|actual| {
230-
format!("cannot cast thin pointer `{}` to fat pointer \
231-
`{}`",
232-
actual,
233-
fcx.ty_to_string(self.cast_ty))
234-
},
235-
self.expr_ty)
228+
struct_span_err!(fcx.tcx.sess, self.span, E0607,
229+
"cannot cast thin pointer `{}` to fat pointer `{}`",
230+
self.expr_ty,
231+
fcx.ty_to_string(self.cast_ty)).emit();
236232
}
237233
}
238234
}

src/librustc_typeck/diagnostics.rs

+23
Original file line numberDiff line numberDiff line change
@@ -4270,6 +4270,29 @@ let y: u32 = *x as u32; // We dereference it first and then cast it.
42704270
```
42714271
"##,
42724272

4273+
E0607: r##"
4274+
A cast between a thin and a fat pointer was attempted.
4275+
4276+
Erroneous code example:
4277+
4278+
```compile_fail,E0607
4279+
let v = 0 as *const u8;
4280+
v as *const [u8];
4281+
```
4282+
4283+
First: what are thin and fat pointers?
4284+
4285+
Thin pointers are "simple" pointers that simply reference a memory address.
4286+
4287+
Fat pointers are pointers referencing Dynamically Sized Types (also called DST).
4288+
They don't have a statically known size, therefore they can only exist behind
4289+
some kind of pointers that contain additional information. Slices and trait
4290+
objects are DSTs.
4291+
4292+
So in order to fix this error, don't try to cast directly between thin and fat
4293+
pointers.
4294+
"##,
4295+
42734296
E0609: r##"
42744297
Attempted to access a non-existent field in a struct.
42754298

src/test/compile-fail/E0607.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let v = 0 as *const u8;
13+
v as *const [u8]; //~ ERROR E0607
14+
}

src/test/ui/mismatched_types/cast-rfc0401.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ error[E0606]: casting `usize` as `*const [u8]` is invalid
156156
61 | let _ = 42usize as *const [u8];
157157
| ^^^^^^^^^^^^^^^^^^^^^^
158158

159-
error: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
159+
error[E0607]: cannot cast thin pointer `*const u8` to fat pointer `*const [u8]`
160160
--> $DIR/cast-rfc0401.rs:62:13
161161
|
162162
62 | let _ = v as *const [u8];

0 commit comments

Comments
 (0)