about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-06-08 00:13:28 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2017-06-24 21:27:49 +0200
commit7b8c6a2d30f7e6acee89a735ad8fd86c1e1c2370 (patch)
tree9b0b222278cead9e0d8164fe78115cf39ac7dd8f
parent30effc14e4ae23a682495ceadf7f56039b02e747 (diff)
Add E0607
-rw-r--r--src/librustc_typeck/check/cast.rs12
-rw-r--r--src/librustc_typeck/diagnostics.rs23
-rw-r--r--src/test/compile-fail/E0607.rs14
-rw-r--r--src/test/ui/mismatched_types/cast-rfc0401.stderr2
4 files changed, 42 insertions, 9 deletions
diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs
index 5c255364311..52962e1e478 100644
--- a/src/librustc_typeck/check/cast.rs
+++ b/src/librustc_typeck/check/cast.rs
@@ -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();
             }
         }
     }
diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs
index d3138af978a..ae13e236743 100644
--- a/src/librustc_typeck/diagnostics.rs
+++ b/src/librustc_typeck/diagnostics.rs
@@ -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.
 
diff --git a/src/test/compile-fail/E0607.rs b/src/test/compile-fail/E0607.rs
new file mode 100644
index 00000000000..fa761f2c178
--- /dev/null
+++ b/src/test/compile-fail/E0607.rs
@@ -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
+}
diff --git a/src/test/ui/mismatched_types/cast-rfc0401.stderr b/src/test/ui/mismatched_types/cast-rfc0401.stderr
index b4c3106253e..50e18150579 100644
--- a/src/test/ui/mismatched_types/cast-rfc0401.stderr
+++ b/src/test/ui/mismatched_types/cast-rfc0401.stderr
@@ -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];