diff --git a/src/Sema.zig b/src/Sema.zig index ffe57c4a1b..57de32396d 100644 --- 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 index 7fdb08facb..6c528e0799 100644 --- 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