authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-03 13:04:04-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-03 13:04:04-05:00
log9c5852aa8674320d2912627708f32fea37d7cd08
tree1c8d9657be238579002ab35e019b5d37f7fd285d
parente3b275fa47e44d0a77e513d03a3b37b0f9ea0c0d
signaturelock-open Commit is signed but in an unrecognized format.

fix slice of C pointer

closes #2002

3 files changed, 15 insertions(+), 3 deletions(-)

src/codegen.cpp+1-1
...@@ -4570,7 +4570,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst...@@ -4570,7 +4570,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45704570
4571 return tmp_struct_ptr;4571 return tmp_struct_ptr;
4572 } else if (array_type->id == ZigTypeIdPointer) {4572 } else if (array_type->id == ZigTypeIdPointer) {
4573 assert(array_type->data.pointer.ptr_len == PtrLenUnknown);4573 assert(array_type->data.pointer.ptr_len != PtrLenSingle);
4574 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);4574 LLVMValueRef start_val = ir_llvm_value(g, instruction->start);
4575 LLVMValueRef end_val = ir_llvm_value(g, instruction->end);4575 LLVMValueRef end_val = ir_llvm_value(g, instruction->end);
45764576
src/ir.cpp+3
...@@ -19757,6 +19757,9 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction...@@ -19757,6 +19757,9 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
19757 return ira->codegen->invalid_instruction;19757 return ira->codegen->invalid_instruction;
19758 }19758 }
19759 } else {19759 } else {
19760 if (array_type->data.pointer.ptr_len == PtrLenC) {
19761 array_type = adjust_ptr_len(ira->codegen, array_type, PtrLenUnknown);
19762 }
19760 return_type = get_slice_type(ira->codegen, array_type);19763 return_type = get_slice_type(ira->codegen, array_type);
19761 if (!end) {19764 if (!end) {
19762 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));19765 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
test/stage1/behavior/slice.zig+11-2
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const expect = @import("std").testing.expect;1const std = @import("std");
2const mem = @import("std").mem;2const expect = std.testing.expect;
3const expectEqualSlices = std.testing.expectEqualSlices;
4const mem = std.mem;
35
4const x = @intToPtr([*]i32, 0x1000)[0..0x500];6const x = @intToPtr([*]i32, 0x1000)[0..0x500];
5const y = x[0x100..];7const y = x[0x100..];
...@@ -38,3 +40,10 @@ test "implicitly cast array of size 0 to slice" {...@@ -38,3 +40,10 @@ test "implicitly cast array of size 0 to slice" {
38fn assertLenIsZero(msg: []const u8) void {40fn assertLenIsZero(msg: []const u8) void {
39 expect(msg.len == 0);41 expect(msg.len == 0);
40}42}
43
44test "C pointer" {
45 var buf: [*c]const u8 = c"kjdhfkjdhfdkjhfkfjhdfkjdhfkdjhfdkjhf";
46 var len: u32 = 10;
47 var slice = buf[0..len];
48 expectEqualSlices(u8, "kjdhfkjdhf", slice);
49}