authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-08 22:51:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-08 22:51:46-07:00
log1678825c1450b29a1016bba62511388b3e539cd8
treeaa94ac0ac79ca5c48c15c83cf365d847f01ff2ed
parent59418d1bf6f82866a1c8f3b35fd815bb7add5129

Sema: fix `@ptrCast` from slices

Also, fix allocations in comptime contexts with alignments.

2 files changed, 21 insertions(+), 12 deletions(-)

src/Sema.zig+21-10
...@@ -2318,7 +2318,7 @@ fn zirAllocExtended(...@@ -2318,7 +2318,7 @@ fn zirAllocExtended(
2318 else2318 else
2319 Type.initTag(.inferred_alloc_mut);2319 Type.initTag(.inferred_alloc_mut);
23202320
2321 if (small.is_comptime) {2321 if (block.is_comptime or small.is_comptime) {
2322 if (small.has_type) {2322 if (small.has_type) {
2323 return sema.analyzeComptimeAlloc(block, var_ty, alignment, ty_src);2323 return sema.analyzeComptimeAlloc(block, var_ty, alignment, ty_src);
2324 } else {2324 } else {
...@@ -2379,7 +2379,10 @@ fn zirAllocInferredComptime(...@@ -2379,7 +2379,10 @@ fn zirAllocInferredComptime(
2379 sema.src = src;2379 sema.src = src;
2380 return sema.addConstant(2380 return sema.addConstant(
2381 inferred_alloc_ty,2381 inferred_alloc_ty,
2382 try Value.Tag.inferred_alloc_comptime.create(sema.arena, undefined),2382 try Value.Tag.inferred_alloc_comptime.create(sema.arena, .{
2383 .decl = undefined,
2384 .alignment = 0,
2385 }),
2383 );2386 );
2384}2387}
23852388
...@@ -2440,7 +2443,10 @@ fn zirAllocInferred(...@@ -2440,7 +2443,10 @@ fn zirAllocInferred(
2440 if (block.is_comptime) {2443 if (block.is_comptime) {
2441 return sema.addConstant(2444 return sema.addConstant(
2442 inferred_alloc_ty,2445 inferred_alloc_ty,
2443 try Value.Tag.inferred_alloc_comptime.create(sema.arena, undefined),2446 try Value.Tag.inferred_alloc_comptime.create(sema.arena, .{
2447 .decl = undefined,
2448 .alignment = 0,
2449 }),
2444 );2450 );
2445 }2451 }
24462452
...@@ -11341,7 +11347,14 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -11341,7 +11347,14 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
11341 const operand_ty = sema.typeOf(operand);11347 const operand_ty = sema.typeOf(operand);
11342 try sema.checkPtrType(block, dest_ty_src, dest_ty);11348 try sema.checkPtrType(block, dest_ty_src, dest_ty);
11343 try sema.checkPtrOperand(block, operand_src, operand_ty);11349 try sema.checkPtrOperand(block, operand_src, operand_ty);
11344 return sema.coerceCompatiblePtrs(block, dest_ty, operand, operand_src);11350 if (dest_ty.isSlice()) {
11351 return sema.fail(block, dest_ty_src, "illegal pointer cast to slice", .{});
11352 }
11353 const ptr = if (operand_ty.isSlice())
11354 try sema.analyzeSlicePtr(block, operand_src, operand, operand_ty)
11355 else
11356 operand;
11357 return sema.coerceCompatiblePtrs(block, dest_ty, ptr, operand_src);
11345}11358}
1134611359
11347fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {11360fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
...@@ -13081,7 +13094,7 @@ fn fieldVal(...@@ -13081,7 +13094,7 @@ fn fieldVal(
13081 try sema.analyzeLoad(block, src, object, object_src)13094 try sema.analyzeLoad(block, src, object, object_src)
13082 else13095 else
13083 object;13096 object;
13084 return sema.analyzeSlicePtr(block, src, slice, inner_ty, object_src);13097 return sema.analyzeSlicePtr(block, object_src, slice, inner_ty);
13085 } else if (mem.eql(u8, field_name, "len")) {13098 } else if (mem.eql(u8, field_name, "len")) {
13086 const slice = if (is_pointer_to)13099 const slice = if (is_pointer_to)
13087 try sema.analyzeLoad(block, src, object, object_src)13100 try sema.analyzeLoad(block, src, object, object_src)
...@@ -15584,19 +15597,17 @@ fn analyzeLoad(...@@ -15584,19 +15597,17 @@ fn analyzeLoad(
15584fn analyzeSlicePtr(15597fn analyzeSlicePtr(
15585 sema: *Sema,15598 sema: *Sema,
15586 block: *Block,15599 block: *Block,
15587 src: LazySrcLoc,15600 slice_src: LazySrcLoc,
15588 slice: Air.Inst.Ref,15601 slice: Air.Inst.Ref,
15589 slice_ty: Type,15602 slice_ty: Type,
15590 slice_src: LazySrcLoc,
15591) CompileError!Air.Inst.Ref {15603) CompileError!Air.Inst.Ref {
15592 const buf = try sema.arena.create(Type.SlicePtrFieldTypeBuffer);15604 const buf = try sema.arena.create(Type.SlicePtrFieldTypeBuffer);
15593 const result_ty = slice_ty.slicePtrFieldType(buf);15605 const result_ty = slice_ty.slicePtrFieldType(buf);
15594
15595 if (try sema.resolveMaybeUndefVal(block, slice_src, slice)) |val| {15606 if (try sema.resolveMaybeUndefVal(block, slice_src, slice)) |val| {
15596 if (val.isUndef()) return sema.addConstUndef(result_ty);15607 if (val.isUndef()) return sema.addConstUndef(result_ty);
15597 return sema.addConstant(result_ty, val.slicePtr());15608 return sema.addConstant(result_ty, val.slicePtr());
15598 }15609 }
15599 try sema.requireRuntimeBlock(block, src);15610 try sema.requireRuntimeBlock(block, slice_src);
15600 return block.addTyOp(.slice_ptr, result_ty, slice);15611 return block.addTyOp(.slice_ptr, result_ty, slice);
15601}15612}
1560215613
...@@ -15729,7 +15740,7 @@ fn analyzeSlice(...@@ -15729,7 +15740,7 @@ fn analyzeSlice(
15729 }15740 }
1573015741
15731 const ptr = if (slice_ty.isSlice())15742 const ptr = if (slice_ty.isSlice())
15732 try sema.analyzeSlicePtr(block, src, ptr_or_slice, slice_ty, ptr_src)15743 try sema.analyzeSlicePtr(block, ptr_src, ptr_or_slice, slice_ty)
15733 else15744 else
15734 ptr_or_slice;15745 ptr_or_slice;
1573515746
test/behavior/slice.zig-2
...@@ -312,8 +312,6 @@ test "empty array to slice" {...@@ -312,8 +312,6 @@ test "empty array to slice" {
312}312}
313313
314test "@ptrCast slice to pointer" {314test "@ptrCast slice to pointer" {
315 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
316
317 const S = struct {315 const S = struct {
318 fn doTheTest() !void {316 fn doTheTest() !void {
319 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };317 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };