From d5977df1c1a934f9892e26a17b38065412ba31d1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 7 Jun 2017 20:58:09 +0200 Subject: [PATCH] Add E0604 --- src/librustc_typeck/check/cast.rs | 8 ++------ src/librustc_typeck/diagnostics.rs | 17 +++++++++++++++++ src/test/compile-fail/E0604.rs | 13 +++++++++++++ .../ui/mismatched_types/cast-rfc0401.stderr | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 src/test/compile-fail/E0604.rs diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index 91aeade65aa..cff61df52cd 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -205,12 +205,8 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { .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, diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index bc5ba4c323d..e750a897844 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4208,6 +4208,23 @@ println!("{}", v[2]); ``` "##, +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 index 00000000000..c5bf3a77b6c --- /dev/null +++ b/src/test/compile-fail/E0604.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + 1u32 as char; //~ ERROR E0604 +} diff --git a/src/test/ui/mismatched_types/cast-rfc0401.stderr b/src/test/ui/mismatched_types/cast-rfc0401.stderr index 58cd130dcc2..82672d5d873 100644 --- a/src/test/ui/mismatched_types/cast-rfc0401.stderr +++ b/src/test/ui/mismatched_types/cast-rfc0401.stderr @@ -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;