]> git.lizzy.rs Git - rust.git/commitdiff
Add E0607
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 7 Jun 2017 22:13:28 +0000 (00:13 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 24 Jun 2017 19:27:49 +0000 (21:27 +0200)
src/librustc_typeck/check/cast.rs
src/librustc_typeck/diagnostics.rs
src/test/compile-fail/E0607.rs [new file with mode: 0644]
src/test/ui/mismatched_types/cast-rfc0401.stderr

index 5c255364311132eb91492781632481c916d90879..52962e1e478c07adfd0c11e4e7ea68f55f620927 100644 (file)
@@ -225,14 +225,10 @@ fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
                                 .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();
             }
         }
     }
index d3138af978aa7337d417ef329eccb2849e4d65a2..ae13e236743dd525f5f6ee86668100c893e98be0 100644 (file)
@@ -4270,6 +4270,29 @@ trait was performed.
 ```
 "##,
 
+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 (file)
index 0000000..fa761f2
--- /dev/null
@@ -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
+}
index b4c3106253e14fbc98382dba32f0950fc009eca2..50e1815057928537328387b9eb9da1309e8038a1 100644 (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];