From 1f4a097117a75b053d620658df4cb4e81c40fe1a Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 4 Mar 2022 10:06:25 +0200 Subject: [PATCH 1/2] stage2: fix mem{set,cpy} for non comptime mutable pointers --- src/Sema.zig | 20 ++++++++----------- test/behavior.zig | 4 ++-- test/behavior/bugs/718.zig | 5 +++++ .../export_self_referential_type_info.zig | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 99762f4807..e5e3d6e9b9 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -13831,13 +13831,11 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void const src_ptr = try sema.coerce(block, wanted_src_ptr_ty, uncasted_src_ptr, src_src); const len = try sema.coerce(block, Type.usize, sema.resolveInst(extra.byte_count), len_src); - const maybe_dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr); - const maybe_src_ptr_val = try sema.resolveDefinedValue(block, src_src, src_ptr); - const maybe_len_val = try sema.resolveDefinedValue(block, len_src, len); - - const runtime_src = if (maybe_dest_ptr_val) |dest_ptr_val| rs: { - if (maybe_src_ptr_val) |src_ptr_val| { - if (maybe_len_val) |len_val| { + const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |dest_ptr_val| rs: { + if (!dest_ptr_val.isComptimeMutablePtr()) break :rs dest_src; + if (try sema.resolveDefinedValue(block, src_src, src_ptr)) |src_ptr_val| { + if (!src_ptr_val.isComptimeMutablePtr()) break :rs src_src; + if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| { _ = dest_ptr_val; _ = src_ptr_val; _ = len_val; @@ -13876,11 +13874,9 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src); const len = try sema.coerce(block, Type.usize, sema.resolveInst(extra.byte_count), len_src); - const maybe_dest_ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr); - const maybe_len_val = try sema.resolveDefinedValue(block, len_src, len); - - const runtime_src = if (maybe_dest_ptr_val) |ptr_val| rs: { - if (maybe_len_val) |len_val| { + const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |ptr_val| rs: { + if (!ptr_val.isComptimeMutablePtr()) break :rs dest_src; + if (try sema.resolveDefinedValue(block, len_src, len)) |len_val| { if (try sema.resolveMaybeUndefVal(block, value_src, value)) |val| { _ = ptr_val; _ = len_val; diff --git a/test/behavior.zig b/test/behavior.zig index 36674b7666..0138390a7c 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -17,6 +17,7 @@ test { _ = @import("behavior/bugs/656.zig"); _ = @import("behavior/bugs/679.zig"); _ = @import("behavior/bugs/704.zig"); + _ = @import("behavior/bugs/718.zig"); _ = @import("behavior/bugs/1025.zig"); _ = @import("behavior/bugs/1076.zig"); _ = @import("behavior/bugs/1111.zig"); @@ -124,6 +125,7 @@ test { _ = @import("behavior/bugs/10970.zig"); _ = @import("behavior/cast_int.zig"); _ = @import("behavior/eval.zig"); + _ = @import("behavior/export_self_referential_type_info.zig"); _ = @import("behavior/int128.zig"); _ = @import("behavior/merge_error_sets.zig"); _ = @import("behavior/translate_c_macros.zig"); @@ -154,7 +156,6 @@ test { } _ = @import("behavior/await_struct.zig"); _ = @import("behavior/bugs/529.zig"); - _ = @import("behavior/bugs/718.zig"); _ = @import("behavior/bugs/920.zig"); _ = @import("behavior/bugs/1120.zig"); _ = @import("behavior/bugs/1851.zig"); @@ -166,7 +167,6 @@ test { _ = @import("behavior/bugs/10147.zig"); _ = @import("behavior/const_slice_child.zig"); _ = @import("behavior/export.zig"); - _ = @import("behavior/export_self_referential_type_info.zig"); _ = @import("behavior/select.zig"); _ = @import("behavior/shuffle.zig"); _ = @import("behavior/struct_contains_slice_of_itself.zig"); diff --git a/test/behavior/bugs/718.zig b/test/behavior/bugs/718.zig index 078c3604ee..a441b38ef7 100644 --- a/test/behavior/bugs/718.zig +++ b/test/behavior/bugs/718.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const mem = std.mem; const expect = std.testing.expect; const Keys = struct { @@ -9,6 +10,10 @@ const Keys = struct { }; var keys: Keys = undefined; test "zero keys with @memset" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + @memset(@ptrCast([*]u8, &keys), 0, @sizeOf(@TypeOf(keys))); try expect(!keys.up); try expect(!keys.down); diff --git a/test/behavior/export_self_referential_type_info.zig b/test/behavior/export_self_referential_type_info.zig index 683ac19147..f822a81a13 100644 --- a/test/behavior/export_self_referential_type_info.zig +++ b/test/behavior/export_self_referential_type_info.zig @@ -1 +1 @@ -export const foo = @typeInfo(@This()).Struct.decls; +export const foo: c_int = @boolToInt(@typeInfo(@This()).Struct.is_tuple); From 8f037db88552b5171807d4a9f8be6e8942aab8a1 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Mon, 7 Mar 2022 20:41:48 +0200 Subject: [PATCH 2/2] stage2: correct constness of allocs --- src/AstGen.zig | 4 +++- src/Sema.zig | 16 ++++++++++++++++ src/Zir.zig | 6 ++++++ src/print_zir.zig | 1 + test/behavior/bugs/5474.zig | 2 -- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index d3ef670723..a807f6a87d 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2138,6 +2138,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner .alloc_inferred_mut, .alloc_inferred_comptime, .alloc_inferred_comptime_mut, + .make_ptr_const, .array_cat, .array_mul, .array_type, @@ -2754,12 +2755,13 @@ fn varDecl( if (resolve_inferred_alloc != .none) { _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node); } + const const_ptr = try gz.addUnNode(.make_ptr_const, init_scope.rl_ptr, node); const sub_scope = try block_arena.create(Scope.LocalPtr); sub_scope.* = .{ .parent = scope, .gen_zir = gz, .name = ident_name, - .ptr = init_scope.rl_ptr, + .ptr = const_ptr, .token_src = name_token, .maybe_comptime = true, .id_cat = .@"local constant", diff --git a/src/Sema.zig b/src/Sema.zig index e5e3d6e9b9..d74385dbc1 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -607,6 +607,7 @@ fn analyzeBodyInner( .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, Type.initTag(.inferred_alloc_mut)), .alloc_mut => try sema.zirAllocMut(block, inst), .alloc_comptime_mut => try sema.zirAllocComptime(block, inst), + .make_ptr_const => try sema.zirMakePtrConst(block, inst), .anyframe_type => try sema.zirAnyframeType(block, inst), .array_cat => try sema.zirArrayCat(block, inst), .array_mul => try sema.zirArrayMul(block, inst), @@ -2409,6 +2410,21 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr return sema.analyzeComptimeAlloc(block, var_ty, 0, ty_src); } +fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { + const inst_data = sema.code.instructions.items(.data)[inst].un_node; + const ptr = sema.resolveInst(inst_data.operand); + const ptr_ty = sema.typeOf(ptr); + var ptr_info = ptr_ty.ptrInfo().data; + ptr_info.mutable = false; + const const_ptr_ty = try Type.ptr(sema.arena, sema.mod.getTarget(), ptr_info); + + if (try sema.resolveMaybeUndefVal(block, inst_data.src(), ptr)) |val| { + return sema.addConstant(const_ptr_ty, val); + } + try sema.requireRuntimeBlock(block, inst_data.src()); + return block.addBitCast(const_ptr_ty, ptr); +} + fn zirAllocInferredComptime( sema: *Sema, inst: Zir.Inst.Index, diff --git a/src/Zir.zig b/src/Zir.zig index ceaef3ebe0..c800170c12 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -954,6 +954,10 @@ pub const Inst = struct { /// is the allocation that needs to have its type inferred. /// Uses the `un_node` field. The AST node is the var decl. resolve_inferred_alloc, + /// Turns a pointer coming from an `alloc`, `alloc_inferred`, `alloc_inferred_comptime` or + /// `Extended.alloc` into a constant version of the same pointer. + /// Uses the `un_node` union field. + make_ptr_const, /// Implements `resume` syntax. Uses `un_node` field. @"resume", @@ -993,6 +997,7 @@ pub const Inst = struct { .alloc_inferred_mut, .alloc_inferred_comptime, .alloc_inferred_comptime_mut, + .make_ptr_const, .array_cat, .array_mul, .array_type, @@ -1496,6 +1501,7 @@ pub const Inst = struct { .alloc_inferred_comptime = .node, .alloc_inferred_comptime_mut = .node, .resolve_inferred_alloc = .un_node, + .make_ptr_const = .un_node, .@"resume" = .un_node, .@"await" = .un_node, diff --git a/src/print_zir.zig b/src/print_zir.zig index 677248abd3..f7b2c5732e 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -238,6 +238,7 @@ const Writer = struct { .field_base_ptr, .validate_array_init_ty, .validate_struct_init_ty, + .make_ptr_const, => try self.writeUnNode(stream, inst), .ref, diff --git a/test/behavior/bugs/5474.zig b/test/behavior/bugs/5474.zig index 937cfc160c..62d7a52a3f 100644 --- a/test/behavior/bugs/5474.zig +++ b/test/behavior/bugs/5474.zig @@ -54,8 +54,6 @@ test "pointer-to-array constness for zero-size elements, var" { } test "pointer-to-array constness for zero-size elements, const" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - try constant(); comptime try constant(); }