zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d78532f462b2d0b514bbd3c1c3ed36135dca766c (tree)
parent 20ea44ef107828d76692e610e1ca62ee231fb4a9
Author: Veikka Tuominen <git@vexu.eu>
Date:   Tue, 26 Jul 2022 13:20:29 +0300

Sema: give comptime_field_ptr priority over field_ptr in tuples

Closes #11983

Diffstat:
Msrc/Sema.zig | 26++++++++++++++++++--------
Mtest/cases/compile_errors/invalid_store_to_comptime_field.zig | 5+++++
2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -20499,6 +20499,14 @@ fn tupleFieldPtr( .@"addrspace" = tuple_ptr_ty.ptrAddressSpace(), }); + if (tuple_ty.structFieldValueComptime(field_index)) |default_val| { + const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{ + .field_ty = field_ty, + .field_val = default_val, + }); + return sema.addConstant(ptr_field_ty, val); + } + if (try sema.resolveMaybeUndefVal(block, tuple_ptr_src, tuple_ptr)) |tuple_ptr_val| { return sema.addConstant( ptr_field_ty, @@ -20510,14 +20518,6 @@ fn tupleFieldPtr( ); } - if (tuple_ty.structFieldValueComptime(field_index)) |default_val| { - const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{ - .field_ty = field_ty, - .field_val = default_val, - }); - return sema.addConstant(ptr_field_ty, val); - } - if (!init) { try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src); } @@ -23241,6 +23241,16 @@ fn beginComptimePtrLoad( break :blk deref; }, + .comptime_field_ptr => blk: { + const comptime_field_ptr = ptr_val.castTag(.comptime_field_ptr).?.data; + break :blk ComptimePtrLoadKit{ + .parent = null, + .pointee = .{ .ty = comptime_field_ptr.field_ty, .val = comptime_field_ptr.field_val }, + .is_mutable = false, + .ty_without_well_defined_layout = comptime_field_ptr.field_ty, + }; + }, + .opt_payload_ptr, .eu_payload_ptr, => blk: { diff --git a/test/cases/compile_errors/invalid_store_to_comptime_field.zig b/test/cases/compile_errors/invalid_store_to_comptime_field.zig @@ -40,6 +40,10 @@ pub export fn entry4() void { }; _ = U.foo(.{ .foo = 2, .bar = 2 }); } +pub export fn entry5() void { + comptime var y = .{ 1, 2}; + y = .{ 3, 4 }; +} // pub export fn entry5() void { // var x: u32 = 15; // const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x }); @@ -60,3 +64,4 @@ pub export fn entry4() void { // :31:19: error: value stored in comptime field does not match the default value of the field // :25:29: note: default value set here // :41:16: error: value stored in comptime field does not match the default value of the field +// :45:12: error: value stored in comptime field does not match the default value of the field