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...@@ -2138,6 +2138,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
2138 .alloc_inferred_mut,2138 .alloc_inferred_mut,
2139 .alloc_inferred_comptime,2139 .alloc_inferred_comptime,
2140 .alloc_inferred_comptime_mut,2140 .alloc_inferred_comptime_mut,
2141 .make_ptr_const,
2141 .array_cat,2142 .array_cat,
2142 .array_mul,2143 .array_mul,
2143 .array_type,2144 .array_type,
...@@ -2754,12 +2755,13 @@ fn varDecl(...@@ -2754,12 +2755,13 @@ fn varDecl(
2754 if (resolve_inferred_alloc != .none) {2755 if (resolve_inferred_alloc != .none) {
2755 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);2756 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
2756 }2757 }
2758 const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node);
2757 const sub_scope = try block_arena.create(Scope.LocalPtr);2759 const sub_scope = try block_arena.create(Scope.LocalPtr);
2758 sub_scope.* = .{2760 sub_scope.* = .{
2759 .parent = scope,2761 .parent = scope,
2760 .gen_zir = gz,2762 .gen_zir = gz,
2761 .name = ident_name,2763 .name = ident_name,
2762 .ptr = init_scope.rl_ptr,2764 .ptr = const_ptr,
2763 .token_src = name_token,2765 .token_src = name_token,
2764 .maybe_comptime = true,2766 .maybe_comptime = true,
2765 .id_cat = .@"local constant",2767 .id_cat = .@"local constant",
src/Sema.zig+24-12
...@@ -607,6 +607,7 @@ fn analyzeBodyInner(...@@ -607,6 +607,7 @@ fn analyzeBodyInner(
607 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)),607 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)),
608 .alloc_mut => try sema.zirAllocMut(block, inst),608 .alloc_mut => try sema.zirAllocMut(block, inst),
609 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),609 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
610 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
610 .anyframe_type => try sema.zirAnyframeType(block, inst),611 .anyframe_type => try sema.zirAnyframeType(block, inst),
611 .array_cat => try sema.zirArrayCat(block, inst),612 .array_cat => try sema.zirArrayCat(block, inst),
612 .array_mul => try sema.zirArrayMul(block, inst),613 .array_mul => try sema.zirArrayMul(block, inst),
...@@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
2409 return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src);2410 return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src);
2410}2411}
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
2412fn zirAllocInferredComptime(2428fn zirAllocInferredComptime(
2413 sema: *Sema,2429 sema: *Sema,
2414 inst: Zir.Inst.Index,2430 inst: Zir.Inst.Index,
...@@ -13831,13 +13847,11 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -13831,13 +13847,11 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
13831 const src_ptr = try sema.coerce(block, wanted_src_ptr_ty, uncasted_src_ptr, src_src);13847 const src_ptr = try sema.coerce(block, wanted_src_ptr_ty, uncasted_src_ptr, src_src);
13832 const len = try sema.coerce(block, Type.usize, sema.resolveInst(extra.byte_count), len_src);13848 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);13850 const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |dest_ptr_val| rs: {
13835 const maybe_src_ptr_val = try sema.resolveDefinedValue(block, src_src, src_ptr);13851 if (!dest_ptr_val.isComptimeMutablePtr()) break :rs dest_src;
13836 const maybe_len_val = try sema.resolveDefinedValue(block, len_src, len);13852 if (try sema.resolveDefinedValue(block, src_src, src_ptr)) |src_ptr_val| {
1383713853 if (!src_ptr_val.isComptimeMutablePtr()) break :rs src_src;
13838 const runtime_src = if (maybe_dest_ptr_val) |dest_ptr_val| rs: {13854 if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| {
13839 if (maybe_src_ptr_val) |src_ptr_val| {
13840 if (maybe_len_val) |len_val| {
13841 _ = dest_ptr_val;13855 _ = dest_ptr_val;
13842 _ = src_ptr_val;13856 _ = src_ptr_val;
13843 _ = len_val;13857 _ = len_val;
...@@ -13876,11 +13890,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -13876,11 +13890,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
13876 const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src);13890 const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src);
13877 const len = try sema.coerce(block, Type.usize, sema.resolveInst(extra.byte_count), len_src);13891 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);13893 const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |ptr_val| rs: {
13880 const maybe_len_val = try sema.resolveDefinedValue(block, len_src, len);13894 if (!ptr_val.isComptimeMutablePtr()) break :rs dest_src;
1388113895 if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| {
13882 const runtime_src = if (maybe_dest_ptr_val) |ptr_val| rs: {
13883 if (maybe_len_val) |len_val| {
13884 if (try sema.resolveMaybeUndefVal(block, value_src, value)) |val| {13896 if (try sema.resolveMaybeUndefVal(block, value_src, value)) |val| {
13885 _ = ptr_val;13897 _ = ptr_val;
13886 _ = len_val;13898 _ = len_val;
src/Zir.zig+6
...@@ -954,6 +954,10 @@ pub const Inst = struct {...@@ -954,6 +954,10 @@ pub const Inst = struct {
954 /// is the allocation that needs to have its type inferred.954 /// is the allocation that needs to have its type inferred.
955 /// Uses the `un_node` field. The AST node is the var decl.955 /// Uses the `un_node` field. The AST node is the var decl.
956 resolve_inferred_alloc,956 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
958 /// Implements `resume` syntax. Uses `un_node` field.962 /// Implements `resume` syntax. Uses `un_node` field.
959 @"resume",963 @"resume",
...@@ -993,6 +997,7 @@ pub const Inst = struct {...@@ -993,6 +997,7 @@ pub const Inst = struct {
993 .alloc_inferred_mut,997 .alloc_inferred_mut,
994 .alloc_inferred_comptime,998 .alloc_inferred_comptime,
995 .alloc_inferred_comptime_mut,999 .alloc_inferred_comptime_mut,
1000 .make_ptr_const,
996 .array_cat,1001 .array_cat,
997 .array_mul,1002 .array_mul,
998 .array_type,1003 .array_type,
...@@ -1496,6 +1501,7 @@ pub const Inst = struct {...@@ -1496,6 +1501,7 @@ pub const Inst = struct {
1496 .alloc_inferred_comptime = .node,1501 .alloc_inferred_comptime = .node,
1497 .alloc_inferred_comptime_mut = .node,1502 .alloc_inferred_comptime_mut = .node,
1498 .resolve_inferred_alloc = .un_node,1503 .resolve_inferred_alloc = .un_node,
1504 .make_ptr_const = .un_node,
14991505
1500 .@"resume" = .un_node,1506 .@"resume" = .un_node,
1501 .@"await" = .un_node,1507 .@"await" = .un_node,
src/print_zir.zig+1
...@@ -238,6 +238,7 @@ const Writer = struct {...@@ -238,6 +238,7 @@ const Writer = struct {
238 .field_base_ptr,238 .field_base_ptr,
239 .validate_array_init_ty,239 .validate_array_init_ty,
240 .validate_struct_init_ty,240 .validate_struct_init_ty,
241 .make_ptr_const,
241 => try self.writeUnNode(stream, inst),242 => try self.writeUnNode(stream, inst),
242243
243 .ref,244 .ref,
test/behavior.zig+2-2
...@@ -17,6 +17,7 @@ test {...@@ -17,6 +17,7 @@ test {
17 _ = @import("behavior/bugs/656.zig");17 _ = @import("behavior/bugs/656.zig");
18 _ = @import("behavior/bugs/679.zig");18 _ = @import("behavior/bugs/679.zig");
19 _ = @import("behavior/bugs/704.zig");19 _ = @import("behavior/bugs/704.zig");
20 _ = @import("behavior/bugs/718.zig");
20 _ = @import("behavior/bugs/1025.zig");21 _ = @import("behavior/bugs/1025.zig");
21 _ = @import("behavior/bugs/1076.zig");22 _ = @import("behavior/bugs/1076.zig");
22 _ = @import("behavior/bugs/1111.zig");23 _ = @import("behavior/bugs/1111.zig");
...@@ -124,6 +125,7 @@ test {...@@ -124,6 +125,7 @@ test {
124 _ = @import("behavior/bugs/10970.zig");125 _ = @import("behavior/bugs/10970.zig");
125 _ = @import("behavior/cast_int.zig");126 _ = @import("behavior/cast_int.zig");
126 _ = @import("behavior/eval.zig");127 _ = @import("behavior/eval.zig");
128 _ = @import("behavior/export_self_referential_type_info.zig");
127 _ = @import("behavior/int128.zig");129 _ = @import("behavior/int128.zig");
128 _ = @import("behavior/merge_error_sets.zig");130 _ = @import("behavior/merge_error_sets.zig");
129 _ = @import("behavior/translate_c_macros.zig");131 _ = @import("behavior/translate_c_macros.zig");
...@@ -154,7 +156,6 @@ test {...@@ -154,7 +156,6 @@ test {
154 }156 }
155 _ = @import("behavior/await_struct.zig");157 _ = @import("behavior/await_struct.zig");
156 _ = @import("behavior/bugs/529.zig");158 _ = @import("behavior/bugs/529.zig");
157 _ = @import("behavior/bugs/718.zig");
158 _ = @import("behavior/bugs/920.zig");159 _ = @import("behavior/bugs/920.zig");
159 _ = @import("behavior/bugs/1120.zig");160 _ = @import("behavior/bugs/1120.zig");
160 _ = @import("behavior/bugs/1851.zig");161 _ = @import("behavior/bugs/1851.zig");
...@@ -166,7 +167,6 @@ test {...@@ -166,7 +167,6 @@ test {
166 _ = @import("behavior/bugs/10147.zig");167 _ = @import("behavior/bugs/10147.zig");
167 _ = @import("behavior/const_slice_child.zig");168 _ = @import("behavior/const_slice_child.zig");
168 _ = @import("behavior/export.zig");169 _ = @import("behavior/export.zig");
169 _ = @import("behavior/export_self_referential_type_info.zig");
170 _ = @import("behavior/select.zig");170 _ = @import("behavior/select.zig");
171 _ = @import("behavior/shuffle.zig");171 _ = @import("behavior/shuffle.zig");
172 _ = @import("behavior/struct_contains_slice_of_itself.zig");172 _ = @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" {...@@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" {
54}54}
5555
56test "pointer-to-array constness for zero-size elements, const" {56test "pointer-to-array constness for zero-size elements, const" {
57 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
58
59 try constant();57 try constant();
60 comptime try constant();58 comptime try constant();
61}59}
test/behavior/bugs/718.zig+5
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");
2const mem = std.mem;3const mem = std.mem;
3const expect = std.testing.expect;4const expect = std.testing.expect;
4const Keys = struct {5const Keys = struct {
...@@ -9,6 +10,10 @@ const Keys = struct {...@@ -9,6 +10,10 @@ const Keys = struct {
9};10};
10var keys: Keys = undefined;11var keys: Keys = undefined;
11test "zero keys with @memset" {12test "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
12 @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));17 @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys)));
13 try expect(!keys.up);18 try expect(!keys.up);
14 try expect(!keys.down);19 try expect(!keys.down);
test/behavior/export_self_referential_type_info.zig+1-1
...@@ -1 +1 @@...@@ -1 +1 @@
1export const foo = @typeInfo(@This()).Struct.decls;1export const foo: c_int = @boolToInt(@typeInfo(@This()).Struct.is_tuple);