]> git.lizzy.rs Git - rust.git/commitdiff
improve const eval error reporting on "" and b"" casts
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 22 Jul 2016 07:34:44 +0000 (09:34 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 22 Jul 2016 07:34:44 +0000 (09:34 +0200)
src/librustc_const_eval/eval.rs
src/test/run-pass/const-byte-str-cast.rs

index a3c707e82a0ff45e45be5c74e951c184296f8cb5..4643686786be64c861c6a8a34140421928a7cc16 100644 (file)
@@ -1105,11 +1105,25 @@ fn cast_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstVal, ty: ty::Ty)
         Float(f) => cast_const_float(tcx, f, ty),
         Char(c) => cast_const_int(tcx, Infer(c as u64), ty),
         Function(_) => Err(UnimplementedConstVal("casting fn pointers")),
-        ByteStr(_) => match ty.sty {
+        ByteStr(b) => match ty.sty {
             ty::TyRawPtr(_) => {
                 Err(ErrKind::UnimplementedConstVal("casting a bytestr to a raw ptr"))
             },
-            ty::TyRef(..) => Err(ErrKind::UnimplementedConstVal("casting a bytestr to slice")),
+            ty::TyRef(_, ty::TypeAndMut { ref ty, mutbl: hir::MutImmutable }) => match ty.sty {
+                ty::TyArray(ty, n) if ty == tcx.types.u8 && n == b.len() => Ok(ByteStr(b)),
+                ty::TySlice(_) => {
+                    Err(ErrKind::UnimplementedConstVal("casting a bytestr to slice"))
+                },
+                _ => Err(CannotCast),
+            },
+            _ => Err(CannotCast),
+        },
+        Str(s) => match ty.sty {
+            ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting a str to a raw ptr")),
+            ty::TyRef(_, ty::TypeAndMut { ref ty, mutbl: hir::MutImmutable }) => match ty.sty {
+                ty::TyStr => Ok(Str(s)),
+                _ => Err(CannotCast),
+            },
             _ => Err(CannotCast),
         },
         _ => Err(CannotCast),
index 2f265b9112b9809be21c450095f950cead56a0bc..7297c71a6d668b7aa2844d4f9714858166f53cd3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -12,4 +12,7 @@
 
 pub fn main() {
     let _ = b"x" as &[u8];
+    let _ = b"y" as &[u8; 1];
+    let _ = b"z" as *const u8;
+    let _ = "รค" as *const str;
 }