]> git.lizzy.rs Git - rust.git/commitdiff
librustc: Allow vector repeat exprs in statics.
authorLuqman Aden <laden@csclub.uwaterloo.ca>
Tue, 13 Aug 2013 01:10:29 +0000 (21:10 -0400)
committerLuqman Aden <laden@csclub.uwaterloo.ca>
Tue, 13 Aug 2013 07:36:21 +0000 (03:36 -0400)
src/librustc/middle/check_const.rs
src/librustc/middle/const_eval.rs
src/librustc/middle/trans/consts.rs

index f9ef6dabcd64cfc32eb97d514879a14b6e6342f0..160cc23bd19ff812e046a44e44c4608888652d53 100644 (file)
@@ -160,6 +160,7 @@ pub fn check_expr(sess: Session,
           expr_field(*) |
           expr_index(*) |
           expr_tup(*) |
+          expr_repeat(*) |
           expr_struct(*) => { }
           expr_addr_of(*) => {
                 sess.span_err(
index 2de94cdbf4ccd03d2c249c1d6cd9f4f8b968b37b..68e3dfd63be9ff392e57c46f6ae07b74f3f2575c 100644 (file)
@@ -153,6 +153,8 @@ pub fn classify(e: &expr,
                 lookup_constness(tcx, e)
               }
 
+              ast::expr_repeat(*) => general_const,
+
               _ => non_const
             };
         tcx.ccache.insert(did, cn);
index 1992d71427f2053baacc7413c310324c9602202e..b362ba396f16d97f3c858c1b7b6015ac983955f8 100644 (file)
@@ -32,6 +32,7 @@
 
 use std::c_str::ToCStr;
 use std::libc::c_uint;
+use std::vec;
 use syntax::{ast, ast_util, ast_map};
 
 pub fn const_lit(cx: &mut CrateContext, e: &ast::expr, lit: ast::lit)
@@ -540,6 +541,23 @@ fn const_expr_unadjusted(cx: @mut CrateContext, e: &ast::expr) -> ValueRef {
               _ => cx.sess.span_bug(e.span, "bad const-slice expr")
             }
           }
+          ast::expr_repeat(elem, count, _) => {
+            let vec_ty = ty::expr_ty(cx.tcx, e);
+            let unit_ty = ty::sequence_element_type(cx.tcx, vec_ty);
+            let llunitty = type_of::type_of(cx, unit_ty);
+            let n = match const_eval::eval_const_expr(cx.tcx, count) {
+                const_eval::const_int(i)  => i as uint,
+                const_eval::const_uint(i) => i as uint,
+                _ => cx.sess.span_bug(count.span, "count must be integral const expression.")
+            };
+            let vs = vec::from_elem(n, const_expr(cx, elem));
+            let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) {
+                C_struct(vs)
+            } else {
+                C_array(llunitty, vs)
+            };
+            v
+          }
           ast::expr_path(ref pth) => {
             assert_eq!(pth.types.len(), 0);
             let tcx = cx.tcx;