Commit 15f843e70f

Andrew Kelley <superjoe30@gmail.com>
2016-12-26 23:11:36
IR: pass castSliceToU8Slice test
1 parent 66a83d8
Changed files (3)
src/ir.cpp
@@ -5220,10 +5220,18 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s
             source_instr->source_node, ira->codegen->builtin_types.entry_usize, false);
     init_const_usize(ira->codegen, &end->value, array_type->data.array.len);
 
+    bool is_const;
+    if (array->id == IrInstructionIdLoadPtr) {
+        IrInstructionLoadPtr *load_ptr_inst = (IrInstructionLoadPtr *) array;
+        is_const = load_ptr_inst->ptr->value.type->data.pointer.is_const;
+    } else {
+        is_const = true;
+    }
+
     IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope,
-            source_instr->source_node, array, start, end, true, false);
+            source_instr->source_node, array, start, end, is_const, false);
     TypeTableEntry *child_type = array_type->data.array.child_type;
-    result->value.type = get_slice_type(ira->codegen, child_type, true);
+    result->value.type = get_slice_type(ira->codegen, child_type, is_const);
     ir_add_alloca(ira, result, result->value.type);
     return result;
 }
test/cases/misc.zig
@@ -451,6 +451,28 @@ fn cStringConcatenation() {
     assert(b[len] == 0);
 }
 
+fn castSliceToU8Slice() {
+    @setFnTest(this);
+
+    assert(@sizeOf(i32) == 4);
+    var big_thing_array = []i32{1, 2, 3, 4};
+    const big_thing_slice: []i32 = big_thing_array;
+    const bytes = ([]u8)(big_thing_slice);
+    assert(bytes.len == 4 * 4);
+    bytes[4] = 0;
+    bytes[5] = 0;
+    bytes[6] = 0;
+    bytes[7] = 0;
+    assert(big_thing_slice[1] == 0);
+    const big_thing_again = ([]i32)(bytes);
+    assert(big_thing_again[2] == 3);
+    big_thing_again[2] = -1;
+    assert(bytes[8] == @maxValue(u8));
+    assert(bytes[9] == @maxValue(u8));
+    assert(bytes[10] == @maxValue(u8));
+    assert(bytes[11] == @maxValue(u8));
+}
+
 // TODO import from std.cstr
 pub fn cstrlen(ptr: &const u8) -> usize {
     var count: usize = 0;
test/self_hosted.zig
@@ -38,45 +38,3 @@ fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize {
 }
 
 
-// TODO change this test to an issue
-// we're going to change how this works
-fn switchOnErrorUnion() {
-    @setFnTest(this, true);
-
-    const x = switch (returnsTen()) {
-        Ok => |val| val + 1,
-        ItBroke, NoMem => 1,
-        CrappedOut => 2,
-    };
-    assert(x == 11);
-}
-error ItBroke;
-error NoMem;
-error CrappedOut;
-fn returnsTen() -> %i32 {
-    @setFnStaticEval(this, false);
-    10
-}
-
-// TODO not passing
-fn castSliceToU8Slice() {
-    @setFnTest(this);
-
-    assert(@sizeOf(i32) == 4);
-    var big_thing_array = []i32{1, 2, 3, 4};
-    const big_thing_slice: []i32 = big_thing_array;
-    const bytes = ([]u8)(big_thing_slice);
-    assert(bytes.len == 4 * 4);
-    bytes[4] = 0;
-    bytes[5] = 0;
-    bytes[6] = 0;
-    bytes[7] = 0;
-    assert(big_thing_slice[1] == 0);
-    const big_thing_again = ([]i32)(bytes);
-    assert(big_thing_again[2] == 3);
-    big_thing_again[2] = -1;
-    assert(bytes[8] == @maxValue(u8));
-    assert(bytes[9] == @maxValue(u8));
-    assert(bytes[10] == @maxValue(u8));
-    assert(bytes[11] == @maxValue(u8));
-}