authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-26 13:20:29+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-26 16:40:24+03:00
logd78532f462b2d0b514bbd3c1c3ed36135dca766c
tree10945487823a276103345053ffd73922a2bce24f
parent20ea44ef107828d76692e610e1ca62ee231fb4a9

Sema: give comptime_field_ptr priority over field_ptr in tuples

Closes #11983

2 files changed, 23 insertions(+), 8 deletions(-)

src/Sema.zig+18-8
......@@ -20499,6 +20499,14 @@ fn tupleFieldPtr(
2049920499 .@"addrspace" = tuple_ptr_ty.ptrAddressSpace(),
2050020500 });
2050120501
20502 if (tuple_ty.structFieldValueComptime(field_index)) |default_val| {
20503 const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
20504 .field_ty = field_ty,
20505 .field_val = default_val,
20506 });
20507 return sema.addConstant(ptr_field_ty, val);
20508 }
20509
2050220510 if (try sema.resolveMaybeUndefVal(block, tuple_ptr_src, tuple_ptr)) |tuple_ptr_val| {
2050320511 return sema.addConstant(
2050420512 ptr_field_ty,
......@@ -20510,14 +20518,6 @@ fn tupleFieldPtr(
2051020518 );
2051120519 }
2051220520
20513 if (tuple_ty.structFieldValueComptime(field_index)) |default_val| {
20514 const val = try Value.Tag.comptime_field_ptr.create(sema.arena, .{
20515 .field_ty = field_ty,
20516 .field_val = default_val,
20517 });
20518 return sema.addConstant(ptr_field_ty, val);
20519 }
20520
2052120521 if (!init) {
2052220522 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src);
2052320523 }
......@@ -23241,6 +23241,16 @@ fn beginComptimePtrLoad(
2324123241 break :blk deref;
2324223242 },
2324323243
23244 .comptime_field_ptr => blk: {
23245 const comptime_field_ptr = ptr_val.castTag(.comptime_field_ptr).?.data;
23246 break :blk ComptimePtrLoadKit{
23247 .parent = null,
23248 .pointee = .{ .ty = comptime_field_ptr.field_ty, .val = comptime_field_ptr.field_val },
23249 .is_mutable = false,
23250 .ty_without_well_defined_layout = comptime_field_ptr.field_ty,
23251 };
23252 },
23253
2324423254 .opt_payload_ptr,
2324523255 .eu_payload_ptr,
2324623256 => blk: {
test/cases/compile_errors/invalid_store_to_comptime_field.zig+5
......@@ -40,6 +40,10 @@ pub export fn entry4() void {
4040 };
4141 _ = U.foo(.{ .foo = 2, .bar = 2 });
4242}
43pub export fn entry5() void {
44 comptime var y = .{ 1, 2};
45 y = .{ 3, 4 };
46}
4347// pub export fn entry5() void {
4448// var x: u32 = 15;
4549// const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
......@@ -60,3 +64,4 @@ pub export fn entry4() void {
6064// :31:19: error: value stored in comptime field does not match the default value of the field
6165// :25:29: note: default value set here
6266// :41:16: error: value stored in comptime field does not match the default value of the field
67// :45:12: error: value stored in comptime field does not match the default value of the field