]> git.lizzy.rs Git - rust.git/commitdiff
Add E0604
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 7 Jun 2017 18:58:09 +0000 (20:58 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 24 Jun 2017 19:25:31 +0000 (21:25 +0200)
src/librustc_typeck/check/cast.rs
src/librustc_typeck/diagnostics.rs
src/test/compile-fail/E0604.rs [new file with mode: 0644]
src/test/ui/mismatched_types/cast-rfc0401.stderr

index 91aeade65aa4c2b57a5ffd1f7d263845c769cc69..cff61df52cd818f0985af9ad8106c7f8a69d0024 100644 (file)
@@ -205,12 +205,8 @@ fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
                     .emit();
             }
             CastError::CastToChar => {
-                fcx.type_error_message(self.span,
-                                       |actual| {
-                                           format!("only `u8` can be cast as `char`, not `{}`",
-                                                   actual)
-                                       },
-                                       self.expr_ty);
+                struct_span_err!(fcx.tcx.sess, self.span, E0604,
+                                 "only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
             }
             CastError::NonScalar => {
                 fcx.type_error_message(self.span,
index bc5ba4c323dc1576c9a933141c161e091368f35d..e750a8978448d2f04f00f999152d42ae0cdb34a0 100644 (file)
@@ -4208,6 +4208,23 @@ trait was performed.
 ```
 "##,
 
+E0604: r##"
+A cast to `char` was attempted on another type than `u8`.
+
+Erroneous code example:
+
+```compile_fail,E0604
+0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
+```
+
+As the error message indicates, only `u8` can be casted into `char`. Example:
+
+```
+let c = 86u8 as char; // ok!
+assert!(c, 'V');
+```
+"##,
+
 E0609: r##"
 Attempted to access a non-existent field in a struct.
 
diff --git a/src/test/compile-fail/E0604.rs b/src/test/compile-fail/E0604.rs
new file mode 100644 (file)
index 0000000..c5bf3a7
--- /dev/null
@@ -0,0 +1,13 @@
+// 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() {
+    1u32 as char; //~ ERROR E0604
+}
index 58cd130dcc2534d52f3f827e44b4953b83f15824..82672d5d873ae1af3f8339843bcc5976afd968e9 100644 (file)
@@ -92,7 +92,7 @@ error[E0054]: cannot cast as `bool`
    |
    = help: compare with zero instead
 
-error: only `u8` can be cast as `char`, not `u32`
+error[E0604]: only `u8` can be cast as `char`, not `u32`
   --> $DIR/cast-rfc0401.rs:51:13
    |
 51 |     let _ = 0x61u32 as char;