| author | |
| committer | |
| log | 15f843e70f18fe0cd7ee31a67af6eb8d6302dcbc |
| tree | 1d6e98c5c8849690d7ea9e75960f9d0c3814240a |
| parent | 66a83d87383cd8de62898171b6665cb1d70af231 |
3 files changed, 32 insertions(+), 44 deletions(-)
src/ir.cpp+10-2| ... | @@ -5220,10 +5220,18 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s | ... | @@ -5220,10 +5220,18 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s |
| 5220 | source_instr->source_node, ira->codegen->builtin_types.entry_usize, false); | 5220 | source_instr->source_node, ira->codegen->builtin_types.entry_usize, false); |
| 5221 | init_const_usize(ira->codegen, &end->value, array_type->data.array.len); | 5221 | init_const_usize(ira->codegen, &end->value, array_type->data.array.len); |
| 5222 | 5222 | ||
| 5223 | bool is_const; | ||
| 5224 | if (array->id == IrInstructionIdLoadPtr) { | ||
| 5225 | IrInstructionLoadPtr *load_ptr_inst = (IrInstructionLoadPtr *) array; | ||
| 5226 | is_const = load_ptr_inst->ptr->value.type->data.pointer.is_const; | ||
| 5227 | } else { | ||
| 5228 | is_const = true; | ||
| 5229 | } | ||
| 5230 | |||
| 5223 | IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope, | 5231 | IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope, |
| 5224 | source_instr->source_node, array, start, end, true, false); | 5232 | source_instr->source_node, array, start, end, is_const, false); |
| 5225 | TypeTableEntry *child_type = array_type->data.array.child_type; | 5233 | TypeTableEntry *child_type = array_type->data.array.child_type; |
| 5226 | result->value.type = get_slice_type(ira->codegen, child_type, true); | 5234 | result->value.type = get_slice_type(ira->codegen, child_type, is_const); |
| 5227 | ir_add_alloca(ira, result, result->value.type); | 5235 | ir_add_alloca(ira, result, result->value.type); |
| 5228 | return result; | 5236 | return result; |
| 5229 | } | 5237 | } |
test/cases/misc.zig+22| ... | @@ -451,6 +451,28 @@ fn cStringConcatenation() { | ... | @@ -451,6 +451,28 @@ fn cStringConcatenation() { |
| 451 | assert(b[len] == 0); | 451 | assert(b[len] == 0); |
| 452 | } | 452 | } |
| 453 | 453 | ||
| 454 | fn castSliceToU8Slice() { | ||
| 455 | @setFnTest(this); | ||
| 456 | |||
| 457 | assert(@sizeOf(i32) == 4); | ||
| 458 | var big_thing_array = []i32{1, 2, 3, 4}; | ||
| 459 | const big_thing_slice: []i32 = big_thing_array; | ||
| 460 | const bytes = ([]u8)(big_thing_slice); | ||
| 461 | assert(bytes.len == 4 * 4); | ||
| 462 | bytes[4] = 0; | ||
| 463 | bytes[5] = 0; | ||
| 464 | bytes[6] = 0; | ||
| 465 | bytes[7] = 0; | ||
| 466 | assert(big_thing_slice[1] == 0); | ||
| 467 | const big_thing_again = ([]i32)(bytes); | ||
| 468 | assert(big_thing_again[2] == 3); | ||
| 469 | big_thing_again[2] = -1; | ||
| 470 | assert(bytes[8] == @maxValue(u8)); | ||
| 471 | assert(bytes[9] == @maxValue(u8)); | ||
| 472 | assert(bytes[10] == @maxValue(u8)); | ||
| 473 | assert(bytes[11] == @maxValue(u8)); | ||
| 474 | } | ||
| 475 | |||
| 454 | // TODO import from std.cstr | 476 | // TODO import from std.cstr |
| 455 | pub fn cstrlen(ptr: &const u8) -> usize { | 477 | pub fn cstrlen(ptr: &const u8) -> usize { |
| 456 | var count: usize = 0; | 478 | var count: usize = 0; |
test/self_hosted.zig-42| ... | @@ -38,45 +38,3 @@ fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize { | ... | @@ -38,45 +38,3 @@ fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize { |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | // TODO change this test to an issue | ||
| 42 | // we're going to change how this works | ||
| 43 | fn switchOnErrorUnion() { | ||
| 44 | @setFnTest(this, true); | ||
| 45 | |||
| 46 | const x = switch (returnsTen()) { | ||
| 47 | Ok => |val| val + 1, | ||
| 48 | ItBroke, NoMem => 1, | ||
| 49 | CrappedOut => 2, | ||
| 50 | }; | ||
| 51 | assert(x == 11); | ||
| 52 | } | ||
| 53 | error ItBroke; | ||
| 54 | error NoMem; | ||
| 55 | error CrappedOut; | ||
| 56 | fn returnsTen() -> %i32 { | ||
| 57 | @setFnStaticEval(this, false); | ||
| 58 | 10 | ||
| 59 | } | ||
| 60 | |||
| 61 | // TODO not passing | ||
| 62 | fn castSliceToU8Slice() { | ||
| 63 | @setFnTest(this); | ||
| 64 | |||
| 65 | assert(@sizeOf(i32) == 4); | ||
| 66 | var big_thing_array = []i32{1, 2, 3, 4}; | ||
| 67 | const big_thing_slice: []i32 = big_thing_array; | ||
| 68 | const bytes = ([]u8)(big_thing_slice); | ||
| 69 | assert(bytes.len == 4 * 4); | ||
| 70 | bytes[4] = 0; | ||
| 71 | bytes[5] = 0; | ||
| 72 | bytes[6] = 0; | ||
| 73 | bytes[7] = 0; | ||
| 74 | assert(big_thing_slice[1] == 0); | ||
| 75 | const big_thing_again = ([]i32)(bytes); | ||
| 76 | assert(big_thing_again[2] == 3); | ||
| 77 | big_thing_again[2] = -1; | ||
| 78 | assert(bytes[8] == @maxValue(u8)); | ||
| 79 | assert(bytes[9] == @maxValue(u8)); | ||
| 80 | assert(bytes[10] == @maxValue(u8)); | ||
| 81 | assert(bytes[11] == @maxValue(u8)); | ||
| 82 | } |