authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-08 13:49:29-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-08 13:49:29-05:00
log6ffa44554ef70dea9e4e58e0c8a4733be7c939b3
treec73fb2939c8861133f27cf16f1fb5045e60f5468
parente3c2cc1443490973b5038e7ce4e347ad1df9b678
parent8f037db88552b5171807d4a9f8be6e8942aab8a1
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11079 from Vexu/stage2

stage2: make references to const allocs const

8 files changed, 42 insertions(+), 18 deletions(-)

src/AstGen.zig+3-1
......@@ -2138,6 +2138,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
21382138 .alloc_inferred_mut,
21392139 .alloc_inferred_comptime,
21402140 .alloc_inferred_comptime_mut,
2141 .make_ptr_const,
21412142 .array_cat,
21422143 .array_mul,
21432144 .array_type,
......@@ -2754,12 +2755,13 @@ fn varDecl(
27542755 if (resolve_inferred_alloc != .none) {
27552756 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
27562757 }
2758 const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node);
27572759 const sub_scope = try block_arena.create(Scope.LocalPtr);
27582760 sub_scope.* = .{
27592761 .parent = scope,
27602762 .gen_zir = gz,
27612763 .name = ident_name,
2762 .ptr = init_scope.rl_ptr,
2764 .ptr = const_ptr,
27632765 .token_src = name_token,
27642766 .maybe_comptime = true,
27652767 .id_cat = .@"local constant",
src/Sema.zig+24-12
......@@ -607,6 +607,7 @@ fn analyzeBodyInner(
607607 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)),
608608 .alloc_mut => try sema.zirAllocMut(block, inst),
609609 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
610 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
610611 .anyframe_type => try sema.zirAnyframeType(block, inst),
611612 .array_cat => try sema.zirArrayCat(block, inst),
612613 .array_mul => try sema.zirArrayMul(block, inst),
......@@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
24092410 return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src);
24102411}
24112412
2413fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2414 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2415 const ptr = sema.resolveInst(inst_data.operand);
2416 const ptr_ty = sema.typeOf(ptr);
2417 var ptr_info = ptr_ty.ptrInfo().data;
2418 ptr_info.mutable = false;
2419 const const_ptr_ty = try Type.ptr(sema.arena, sema.mod.getTarget(), ptr_info);
2420
2421 if (try sema.resolveMaybeUndefVal(block, inst_data.src(), ptr)) |val| {
2422 return sema.addConstant(const_ptr_ty, val);
2423 }
2424 try sema.requireRuntimeBlock(block, inst_data.src());
2425 return block.addBitCast(const_ptr_ty, ptr);
2426}
2427
24122428fn zirAllocInferredComptime(
24132429 sema: *Sema,
24142430 inst: Zir.Inst.Index,
......@@ -13831,13 +13847,11 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1383113847 const src_ptr = try sema.coerce(block, wanted_src_ptr_ty, uncasted_src_ptr, src_src);
1383213848 const len = try sema.coerce(block, Type.usize, sema.resolveInst(extra.byte_count), len_src);
1383313849
13834 const maybe_dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr);
13835 const maybe_src_ptr_val = try sema.resolveDefinedValue(block, src_src, src_ptr);
13836 const maybe_len_val = try sema.resolveDefinedValue(block, len_src, len);
13837
13838 const runtime_src = if (maybe_dest_ptr_val) |dest_ptr_val| rs: {
13839 if (maybe_src_ptr_val) |src_ptr_val| {
13840 if (maybe_len_val) |len_val| {
13850 const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |dest_ptr_val| rs: {
13851 if (!dest_ptr_val.isComptimeMutablePtr()) break :rs dest_src;
13852 if (try sema.resolveDefinedValue(block, src_src, src_ptr)) |src_ptr_val| {
13853 if (!src_ptr_val.isComptimeMutablePtr()) break :rs src_src;
13854 if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| {
1384113855 _ = dest_ptr_val;
1384213856 _ = src_ptr_val;
1384313857 _ = len_val;
......@@ -13876,11 +13890,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
1387613890 const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src);
1387713891 const len = try sema.coerce(block, Type.usize, sema.resolveInst(extra.byte_count), len_src);
1387813892
13879 const maybe_dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr);
13880 const maybe_len_val = try sema.resolveDefinedValue(block, len_src, len);
13881
13882 const runtime_src = if (maybe_dest_ptr_val) |ptr_val| rs: {
13883 if (maybe_len_val) |len_val| {
13893 const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |ptr_val| rs: {
13894 if (!ptr_val.isComptimeMutablePtr()) break :rs dest_src;
13895 if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| {
1388413896 if (try sema.resolveMaybeUndefVal(block, value_src, value)) |val| {
1388513897 _ = ptr_val;
1388613898 _ = len_val;
src/Zir.zig+6
......@@ -954,6 +954,10 @@ pub const Inst = struct {
954954 /// is the allocation that needs to have its type inferred.
955955 /// Uses the `un_node` field. The AST node is the var decl.
956956 resolve_inferred_alloc,
957 /// Turns a pointer coming from an `alloc`, `alloc_inferred`, `alloc_inferred_comptime` or
958 /// `Extended.alloc` into a constant version of the same pointer.
959 /// Uses the `un_node` union field.
960 make_ptr_const,
957961
958962 /// Implements `resume` syntax. Uses `un_node` field.
959963 @"resume",
......@@ -993,6 +997,7 @@ pub const Inst = struct {
993997 .alloc_inferred_mut,
994998 .alloc_inferred_comptime,
995999 .alloc_inferred_comptime_mut,
1000 .make_ptr_const,
9961001 .array_cat,
9971002 .array_mul,
9981003 .array_type,
......@@ -1496,6 +1501,7 @@ pub const Inst = struct {
14961501 .alloc_inferred_comptime = .node,
14971502 .alloc_inferred_comptime_mut = .node,
14981503 .resolve_inferred_alloc = .un_node,
1504 .make_ptr_const = .un_node,
14991505
15001506 .@"resume" = .un_node,
15011507 .@"await" = .un_node,
src/print_zir.zig+1
......@@ -238,6 +238,7 @@ const Writer = struct {
238238 .field_base_ptr,
239239 .validate_array_init_ty,
240240 .validate_struct_init_ty,
241 .make_ptr_const,
241242 => try self.writeUnNode(stream, inst),
242243
243244 .ref,
test/behavior.zig+2-2
......@@ -17,6 +17,7 @@ test {
1717 _ = @import("behavior/bugs/656.zig");
1818 _ = @import("behavior/bugs/679.zig");
1919 _ = @import("behavior/bugs/704.zig");
20 _ = @import("behavior/bugs/718.zig");
2021 _ = @import("behavior/bugs/1025.zig");
2122 _ = @import("behavior/bugs/1076.zig");
2223 _ = @import("behavior/bugs/1111.zig");
......@@ -124,6 +125,7 @@ test {
124125 _ = @import("behavior/bugs/10970.zig");
125126 _ = @import("behavior/cast_int.zig");
126127 _ = @import("behavior/eval.zig");
128 _ = @import("behavior/export_self_referential_type_info.zig");
127129 _ = @import("behavior/int128.zig");
128130 _ = @import("behavior/merge_error_sets.zig");
129131 _ = @import("behavior/translate_c_macros.zig");
......@@ -154,7 +156,6 @@ test {
154156 }
155157 _ = @import("behavior/await_struct.zig");
156158 _ = @import("behavior/bugs/529.zig");
157 _ = @import("behavior/bugs/718.zig");
158159 _ = @import("behavior/bugs/920.zig");
159160 _ = @import("behavior/bugs/1120.zig");
160161 _ = @import("behavior/bugs/1851.zig");
......@@ -166,7 +167,6 @@ test {
166167 _ = @import("behavior/bugs/10147.zig");
167168 _ = @import("behavior/const_slice_child.zig");
168169 _ = @import("behavior/export.zig");
169 _ = @import("behavior/export_self_referential_type_info.zig");
170170 _ = @import("behavior/select.zig");
171171 _ = @import("behavior/shuffle.zig");
172172 _ = @import("behavior/struct_contains_slice_of_itself.zig");
test/behavior/bugs/5474.zig-2
......@@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" {
5454}
5555
5656test "pointer-to-array constness for zero-size elements, const" {
57 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
58
5957 try constant();
6058 comptime try constant();
6159}
test/behavior/bugs/718.zig+5
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const builtin = @import("builtin");
23const mem = std.mem;
34const expect = std.testing.expect;
45const Keys = struct {
......@@ -9,6 +10,10 @@ const Keys = struct {
910};
1011var keys: Keys = undefined;
1112test "zero keys with @memset" {
13 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
14 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
15 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
16
1217 @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));
1318 try expect(!keys.up);
1419 try expect(!keys.down);
test/behavior/export_self_referential_type_info.zig+1-1
......@@ -1 +1 @@
1export const foo = @typeInfo(@This()).Struct.decls;
1export const foo: c_int = @boolToInt(@typeInfo(@This()).Struct.is_tuple);