authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-08 20:56:00-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-08 20:56:00-05:00
log935d208ffb955e74864e12f0f7e265f64642a02f
treeb38fe66f493abb214853c9385a14dbd4d50bcf31
parent95fc41b2b433ccfa751c8877ec7edac3b9bffbd6
parentba62853d26f6a3388a7ad099a1d9091d7eca45c9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11090 from mitchellh/inferred-ty

stage2: correct comptime-known inferred alloc type, plus comptime array init sentinels

2 files changed, 23 insertions(+), 17 deletions(-)

src/Sema.zig+8-14
...@@ -2623,10 +2623,9 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com...@@ -2623,10 +2623,9 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
2623 // block so that codegen does not see it.2623 // block so that codegen does not see it.
2624 block.instructions.shrinkRetainingCapacity(block.instructions.items.len - 3);2624 block.instructions.shrinkRetainingCapacity(block.instructions.items.len - 3);
2625 sema.air_values.items[value_index] = try Value.Tag.decl_ref.create(sema.arena, new_decl);2625 sema.air_values.items[value_index] = try Value.Tag.decl_ref.create(sema.arena, new_decl);
2626 // Would be nice if we could just assign `bitcast_ty_ref` to2626 // if bitcast ty ref needs to be made const, make_ptr_const
2627 // `air_datas[ptr_inst].ty_pl.ty`, wouldn't it? Alas, that is almost correct,2627 // ZIR handles it later, so we can just use the ty ref here.
2628 // except that the pointer is mutable and we need to make it constant here.2628 air_datas[ptr_inst].ty_pl.ty = air_datas[bitcast_inst].ty_op.ty;
2629 air_datas[ptr_inst].ty_pl.ty = try sema.addType(final_ptr_ty);
26302629
2631 return;2630 return;
2632 }2631 }
...@@ -11828,21 +11827,16 @@ fn zirArrayInit(...@@ -11828,21 +11827,16 @@ fn zirArrayInit(
11828 });11827 });
11829 };11828 };
1183011829
11831 const elems = if (!is_sent)11830 const opt_runtime_src: ?LazySrcLoc = for (resolved_args) |arg| {
11832 resolved_args
11833 else
11834 resolved_args[0 .. resolved_args.len - 1];
11835
11836 const opt_runtime_src: ?LazySrcLoc = for (elems) |arg| {
11837 const arg_src = src; // TODO better source location11831 const arg_src = src; // TODO better source location
11838 const comptime_known = try sema.isComptimeKnown(block, arg_src, arg);11832 const comptime_known = try sema.isComptimeKnown(block, arg_src, arg);
11839 if (!comptime_known) break arg_src;11833 if (!comptime_known) break arg_src;
11840 } else null;11834 } else null;
1184111835
11842 const runtime_src = opt_runtime_src orelse {11836 const runtime_src = opt_runtime_src orelse {
11843 const elem_vals = try sema.arena.alloc(Value, elems.len);11837 const elem_vals = try sema.arena.alloc(Value, resolved_args.len);
1184411838
11845 for (elems) |arg, i| {11839 for (resolved_args) |arg, i| {
11846 // We checked that all args are comptime above.11840 // We checked that all args are comptime above.
11847 elem_vals[i] = (sema.resolveMaybeUndefVal(block, src, arg) catch unreachable).?;11841 elem_vals[i] = (sema.resolveMaybeUndefVal(block, src, arg) catch unreachable).?;
11848 }11842 }
...@@ -11869,7 +11863,7 @@ fn zirArrayInit(...@@ -11869,7 +11863,7 @@ fn zirArrayInit(
11869 });11863 });
11870 const elem_ptr_ty_ref = try sema.addType(elem_ptr_ty);11864 const elem_ptr_ty_ref = try sema.addType(elem_ptr_ty);
1187111865
11872 for (elems) |arg, i| {11866 for (resolved_args) |arg, i| {
11873 const index = try sema.addIntUnsigned(Type.usize, i);11867 const index = try sema.addIntUnsigned(Type.usize, i);
11874 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);11868 const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref);
11875 _ = try block.addBinOp(.store, elem_ptr, arg);11869 _ = try block.addBinOp(.store, elem_ptr, arg);
...@@ -11877,7 +11871,7 @@ fn zirArrayInit(...@@ -11877,7 +11871,7 @@ fn zirArrayInit(
11877 return alloc;11871 return alloc;
11878 }11872 }
1187911873
11880 return block.addAggregateInit(array_ty, elems);11874 return block.addAggregateInit(array_ty, resolved_args);
11881}11875}
1188211876
11883fn zirArrayInitAnon(11877fn zirArrayInitAnon(
test/behavior/pointers.zig+15-3
...@@ -317,7 +317,11 @@ test "allow any sentinel" {...@@ -317,7 +317,11 @@ test "allow any sentinel" {
317}317}
318318
319test "pointer sentinel with enums" {319test "pointer sentinel with enums" {
320 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO320 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
321 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
322 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
323 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
324 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
321325
322 const S = struct {326 const S = struct {
323 const Number = enum {327 const Number = enum {
...@@ -336,7 +340,11 @@ test "pointer sentinel with enums" {...@@ -336,7 +340,11 @@ test "pointer sentinel with enums" {
336}340}
337341
338test "pointer sentinel with optional element" {342test "pointer sentinel with optional element" {
339 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO343 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
344 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
345 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
346 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
347 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
340348
341 const S = struct {349 const S = struct {
342 fn doTheTest() !void {350 fn doTheTest() !void {
...@@ -349,7 +357,11 @@ test "pointer sentinel with optional element" {...@@ -349,7 +357,11 @@ test "pointer sentinel with optional element" {
349}357}
350358
351test "pointer sentinel with +inf" {359test "pointer sentinel with +inf" {
352 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO360 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
361 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
362 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
363 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
364 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
353365
354 const S = struct {366 const S = struct {
355 fn doTheTest() !void {367 fn doTheTest() !void {