]> git.lizzy.rs Git - rust.git/commitdiff
Fix ICE with static DST coercion
authorNick Cameron <ncameron@mozilla.com>
Tue, 2 Sep 2014 03:54:22 +0000 (15:54 +1200)
committerNick Cameron <ncameron@mozilla.com>
Tue, 2 Sep 2014 03:54:22 +0000 (15:54 +1200)
Closes #16911

src/librustc/middle/trans/consts.rs
src/test/run-pass/const-vecs-and-slices.rs

index b3798c9f84dd41ab6d2625f1b0864302e95b33dc..fab4b60d242785bd50e67334295369a431e01726 100644 (file)
@@ -170,7 +170,7 @@ fn const_deref(cx: &CrateContext, v: ValueRef, t: ty::t, explicit: bool)
             }
         }
         None => {
-            cx.sess().bug(format!("can't dereference const of type {}",
+            cx.sess().bug(format!("cannot dereference const of type {}",
                                   ty_to_string(cx.tcx(), t)).as_slice())
         }
     }
@@ -225,10 +225,12 @@ pub fn const_expr(cx: &CrateContext, e: &ast::Expr, is_local: bool) -> (ValueRef
                 ty::AutoDerefRef(ref adj) => {
                     let mut ty = ety;
                     // Save the last autoderef in case we can avoid it.
-                    for _ in range(0, adj.autoderefs-1) {
-                        let (dv, dt) = const_deref(cx, llconst, ty, false);
-                        llconst = dv;
-                        ty = dt;
+                    if adj.autoderefs > 0 {
+                        for _ in range(0, adj.autoderefs-1) {
+                            let (dv, dt) = const_deref(cx, llconst, ty, false);
+                            llconst = dv;
+                            ty = dt;
+                        }
                     }
 
                     match adj.autoref {
@@ -263,6 +265,8 @@ pub fn const_expr(cx: &CrateContext, e: &ast::Expr, is_local: bool) -> (ValueRef
                                         // work properly.
                                         let (_, dt) = const_deref(cx, llconst, ty, false);
                                         ty = dt;
+                                    } else {
+                                        llconst = const_addr_of(cx, llconst, ast::MutImmutable)
                                     }
 
                                     match ty::get(ty).sty {
index a1cd4fe425336db0984b8988acaf04e8b3e17e9c..43e4950a2444529f420027d2687729500f4563c9 100644 (file)
 
 static x : [int, ..4] = [1,2,3,4];
 static y : &'static [int] = &[1,2,3,4];
+static z : &'static [int, ..4] = &[1,2,3,4];
+static zz : &'static [int] = [1,2,3,4];
 
 pub fn main() {
     println!("{:?}", x[1]);
     println!("{:?}", y[1]);
+    println!("{:?}", z[1]);
+    println!("{:?}", zz[1]);
     assert_eq!(x[1], 2);
     assert_eq!(x[3], 4);
     assert_eq!(x[3], y[3]);
+    assert_eq!(z[1], 2);
+    assert_eq!(z[3], 4);
+    assert_eq!(z[3], y[3]);
+    assert_eq!(zz[1], 2);
+    assert_eq!(zz[3], 4);
+    assert_eq!(zz[3], y[3]);
 }