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(
23182318 else
23192319 Type.initTag(.inferred_alloc_mut);
23202320
2321 if (small.is_comptime) {
2321 if (block.is_comptime or small.is_comptime) {
23222322 if (small.has_type) {
23232323 return sema.analyzeComptimeAlloc(block, var_ty, alignment, ty_src);
23242324 } else {
......@@ -2379,7 +2379,10 @@ fn zirAllocInferredComptime(
23792379 sema.src = src;
23802380 return sema.addConstant(
23812381 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 }),
23832386 );
23842387}
23852388
......@@ -2440,7 +2443,10 @@ fn zirAllocInferred(
24402443 if (block.is_comptime) {
24412444 return sema.addConstant(
24422445 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 }),
24442450 );
24452451 }
24462452
......@@ -11341,7 +11347,14 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1134111347 const operand_ty = sema.typeOf(operand);
1134211348 try sema.checkPtrType(block, dest_ty_src, dest_ty);
1134311349 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);
1134511358}
1134611359
1134711360fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -13081,7 +13094,7 @@ fn fieldVal(
1308113094 try sema.analyzeLoad(block, src, object, object_src)
1308213095 else
1308313096 object;
13084 return sema.analyzeSlicePtr(block, src, slice, inner_ty, object_src);
13097 return sema.analyzeSlicePtr(block, object_src, slice, inner_ty);
1308513098 } else if (mem.eql(u8, field_name, "len")) {
1308613099 const slice = if (is_pointer_to)
1308713100 try sema.analyzeLoad(block, src, object, object_src)
......@@ -15584,19 +15597,17 @@ fn analyzeLoad(
1558415597fn analyzeSlicePtr(
1558515598 sema: *Sema,
1558615599 block: *Block,
15587 src: LazySrcLoc,
15600 slice_src: LazySrcLoc,
1558815601 slice: Air.Inst.Ref,
1558915602 slice_ty: Type,
15590 slice_src: LazySrcLoc,
1559115603) CompileError!Air.Inst.Ref {
1559215604 const buf = try sema.arena.create(Type.SlicePtrFieldTypeBuffer);
1559315605 const result_ty = slice_ty.slicePtrFieldType(buf);
15594
1559515606 if (try sema.resolveMaybeUndefVal(block, slice_src, slice)) |val| {
1559615607 if (val.isUndef()) return sema.addConstUndef(result_ty);
1559715608 return sema.addConstant(result_ty, val.slicePtr());
1559815609 }
15599 try sema.requireRuntimeBlock(block, src);
15610 try sema.requireRuntimeBlock(block, slice_src);
1560015611 return block.addTyOp(.slice_ptr, result_ty, slice);
1560115612}
1560215613
......@@ -15729,7 +15740,7 @@ fn analyzeSlice(
1572915740 }
1573015741
1573115742 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)
1573315744 else
1573415745 ptr_or_slice;
1573515746
test/behavior/slice.zig-2
......@@ -312,8 +312,6 @@ test "empty array to slice" {
312312}
313313
314314test "@ptrCast slice to pointer" {
315 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
316
317315 const S = struct {
318316 fn doTheTest() !void {
319317 var array align(@alignOf(u16)) = [5]u8{ 0xff, 0xff, 0xff, 0xff, 0xff };