]> git.lizzy.rs Git - rust.git/commitdiff
simplify processing of ConstVal objects when not all variants are legal
authorOliver Schneider <git1984941651981@oli-obk.de>
Mon, 13 Jul 2015 08:53:16 +0000 (10:53 +0200)
committerOliver Schneider <git1984941651981@oli-obk.de>
Mon, 13 Jul 2015 08:53:16 +0000 (10:53 +0200)
src/librustc/middle/const_eval.rs
src/librustc/middle/ty.rs
src/test/compile-fail/repeat_count.rs

index 7d54b8c284f1ff8137e4c186d923b6ac52712e47..cf71c53e4032bbf6c541b2ddb3b80f9db6a0b073 100644 (file)
@@ -273,6 +273,22 @@ pub enum ConstVal {
     Tuple(ast::NodeId),
 }
 
+impl ConstVal {
+    pub fn description(&self) -> &'static str {
+        match *self {
+            Float(_) => "float",
+            Int(i) if i < 0 => "negative integer",
+            Int(_) => "positive integer",
+            Uint(_) => "unsigned integer",
+            Str(_) => "string literal",
+            Binary(_) => "binary array",
+            Bool(_) => "boolean",
+            Struct(_) => "struct",
+            Tuple(_) => "tuple",
+        }
+    }
+}
+
 pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<ast::Pat> {
     let pat = match expr.node {
         ast::ExprTup(ref exprs) =>
@@ -352,16 +368,8 @@ pub enum ErrKind {
     InvalidOpForFloats(ast::BinOp_),
     InvalidOpForIntUint(ast::BinOp_),
     InvalidOpForUintInt(ast::BinOp_),
-    NegateOnString,
-    NegateOnBoolean,
-    NegateOnBinary,
-    NegateOnStruct,
-    NegateOnTuple,
-    NotOnFloat,
-    NotOnString,
-    NotOnBinary,
-    NotOnStruct,
-    NotOnTuple,
+    NegateOn(ConstVal),
+    NotOn(ConstVal),
 
     NegateWithOverflow(i64),
     AddiWithOverflow(i64, i64),
@@ -397,16 +405,8 @@ pub fn description(&self) -> Cow<str> {
             InvalidOpForFloats(_) => "can't do this op on floats".into_cow(),
             InvalidOpForIntUint(..) => "can't do this op on an isize and usize".into_cow(),
             InvalidOpForUintInt(..) => "can't do this op on a usize and isize".into_cow(),
-            NegateOnString => "negate on string".into_cow(),
-            NegateOnBoolean => "negate on boolean".into_cow(),
-            NegateOnBinary => "negate on binary literal".into_cow(),
-            NegateOnStruct => "negate on struct".into_cow(),
-            NegateOnTuple => "negate on tuple".into_cow(),
-            NotOnFloat => "not on float or string".into_cow(),
-            NotOnString => "not on float or string".into_cow(),
-            NotOnBinary => "not on binary literal".into_cow(),
-            NotOnStruct => "not on struct".into_cow(),
-            NotOnTuple => "not on tuple".into_cow(),
+            NegateOn(ref const_val) => format!("negate on {}", const_val.description()).into_cow(),
+            NotOn(ref const_val) => format!("not on {}", const_val.description()).into_cow(),
 
             NegateWithOverflow(..) => "attempted to negate with overflow".into_cow(),
             AddiWithOverflow(..) => "attempted to add with overflow".into_cow(),
@@ -754,11 +754,7 @@ fn fromb(b: bool) -> ConstVal { Int(b as i64) }
               }
               try!(const_uint_checked_neg(i, e, expr_uint_type))
           }
-          Str(_) => signal!(e, NegateOnString),
-          Bool(_) => signal!(e, NegateOnBoolean),
-          Binary(_) => signal!(e, NegateOnBinary),
-          Tuple(_) => signal!(e, NegateOnTuple),
-          Struct(..) => signal!(e, NegateOnStruct),
+          const_val => signal!(e, NegateOn(const_val)),
         }
       }
       ast::ExprUnary(ast::UnNot, ref inner) => {
@@ -766,11 +762,7 @@ fn fromb(b: bool) -> ConstVal { Int(b as i64) }
           Int(i) => Int(!i),
           Uint(i) => const_uint_not(i, expr_uint_type),
           Bool(b) => Bool(!b),
-          Str(_) => signal!(e, NotOnString),
-          Float(_) => signal!(e, NotOnFloat),
-          Binary(_) => signal!(e, NotOnBinary),
-          Tuple(_) => signal!(e, NotOnTuple),
-          Struct(..) => signal!(e, NotOnStruct),
+          const_val => signal!(e, NotOn(const_val)),
         }
       }
       ast::ExprBinary(op, ref a, ref b) => {
index b4b8fbf2064adb5bfce97dcd5488785e66860475..705e11f0597f15f069af9408bd31234a915ffcf0 100644 (file)
@@ -6100,13 +6100,7 @@ pub fn eval_repeat_count(&self, count_expr: &ast::Expr) -> usize {
                 let found = match val {
                     ConstVal::Uint(count) => return count as usize,
                     ConstVal::Int(count) if count >= 0 => return count as usize,
-                    ConstVal::Int(_) => "negative integer",
-                    ConstVal::Float(_) => "float",
-                    ConstVal::Str(_) => "string",
-                    ConstVal::Bool(_) => "boolean",
-                    ConstVal::Binary(_) => "binary array",
-                    ConstVal::Struct(..) => "struct",
-                    ConstVal::Tuple(_) => "tuple"
+                    const_val => const_val.description(),
                 };
                 span_err!(self.sess, count_expr.span, E0306,
                     "expected positive integer for repeat count, found {}",
index 121581412202cdc882e168f51372689d7803408e..7a0623ba44f0240c2713d3dd644ffa5917ad6d77 100644 (file)
 
 fn main() {
     let n = 1;
-    let a = [0; n]; //~ ERROR expected constant integer for repeat count, found variable
+    let a = [0; n];
+    //~^ ERROR expected constant integer for repeat count, found variable [E0307]
     let b = [0; ()];
-//~^ ERROR mismatched types
-//~| expected `usize`
-//~| found `()`
-//~| expected usize
-//~| found ()
-//~| ERROR expected positive integer for repeat count, found tuple
+    //~^ ERROR mismatched types
+    //~| expected `usize`
+    //~| found `()`
+    //~| expected usize
+    //~| found ()) [E0308]
+    //~| ERROR expected positive integer for repeat count, found tuple [E0306]
     let c = [0; true];
     //~^ ERROR mismatched types
     //~| expected `usize`
     //~| found `bool`
     //~| expected usize
-    //~| found bool
-    //~| ERROR expected positive integer for repeat count, found boolean
+    //~| found bool) [E0308]
+    //~| ERROR expected positive integer for repeat count, found boolean [E0306]
     let d = [0; 0.5];
     //~^ ERROR mismatched types
     //~| expected `usize`
     //~| found `_`
     //~| expected usize
-    //~| found floating-point variable
-    //~| ERROR expected positive integer for repeat count, found float
+    //~| found floating-point variable) [E0308]
+    //~| ERROR expected positive integer for repeat count, found float [E0306]
     let e = [0; "foo"];
     //~^ ERROR mismatched types
     //~| expected `usize`
     //~| found `&'static str`
     //~| expected usize
-    //~| found &-ptr
-    //~| ERROR expected positive integer for repeat count, found string
+    //~| found &-ptr) [E0308]
+    //~| ERROR expected positive integer for repeat count, found string literal [E0306]
     let f = [0; -4_isize];
     //~^ ERROR mismatched types
     //~| expected `usize`
     //~| found `isize`
     //~| expected usize
-    //~| found isize
-    //~| ERROR expected positive integer for repeat count, found negative integer
+    //~| found isize) [E0308]
+    //~| ERROR expected positive integer for repeat count, found negative integer [E0306]
     let f = [0_usize; -1_isize];
     //~^ ERROR mismatched types
     //~| expected `usize`
     //~| found `isize`
     //~| expected usize
-    //~| found isize
-    //~| ERROR expected positive integer for repeat count, found negative integer
+    //~| found isize) [E0308]
+    //~| ERROR expected positive integer for repeat count, found negative integer [E0306]
+    struct G {
+        g: (),
+    }
+    let g = [0; G { g: () }];
+    //~^ ERROR mismatched types
+    //~| expected `usize`
+    //~| found `main::G`
+    //~| expected usize
+    //~| found struct `main::G`) [E0308]
+    //~| ERROR expected positive integer for repeat count, found struct [E0306]
 }