authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-22 13:20:18+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-23 15:40:12+03:00
log15dddfd84d9007689ef1fa6f4abedb88c570973a
tree0d938db7ae94ac24235cad9656a70958832e5ea1
parent0ef4cc738b9c023c1128e3767919b446c959955a

AstGen: make comptime fields in packed and extern structs compile errors


3 files changed, 30 insertions(+), 2 deletions(-)

src/AstGen.zig+6
...@@ -4214,6 +4214,12 @@ fn structDeclInner(...@@ -4214,6 +4214,12 @@ fn structDeclInner(
4214 const have_value = member.ast.value_expr != 0;4214 const have_value = member.ast.value_expr != 0;
4215 const is_comptime = member.comptime_token != null;4215 const is_comptime = member.comptime_token != null;
42164216
4217 if (is_comptime and layout == .Packed) {
4218 return astgen.failTok(member.comptime_token.?, "packed struct fields cannot be marked comptime", .{});
4219 } else if (is_comptime and layout == .Extern) {
4220 return astgen.failTok(member.comptime_token.?, "extern struct fields cannot be marked comptime", .{});
4221 }
4222
4217 if (!is_comptime) {4223 if (!is_comptime) {
4218 known_non_opv = known_non_opv or4224 known_non_opv = known_non_opv or
4219 nodeImpliesMoreThanOnePossibleValue(tree, member.ast.type_expr);4225 nodeImpliesMoreThanOnePossibleValue(tree, member.ast.type_expr);
src/Sema.zig+3-2
...@@ -14509,6 +14509,7 @@ fn zirStructInit(...@@ -14509,6 +14509,7 @@ fn zirStructInit(
14509 var field_i: u32 = 0;14509 var field_i: u32 = 0;
14510 var extra_index = extra.end;14510 var extra_index = extra.end;
1451114511
14512 const is_packed = resolved_ty.containerLayout() == .Packed;
14512 while (field_i < extra.data.fields_len) : (field_i += 1) {14513 while (field_i < extra.data.fields_len) : (field_i += 1) {
14513 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra_index);14514 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra_index);
14514 extra_index = item.end;14515 extra_index = item.end;
...@@ -14535,7 +14536,7 @@ fn zirStructInit(...@@ -14535,7 +14536,7 @@ fn zirStructInit(
14535 }14536 }
14536 found_fields[field_index] = item.data.field_type;14537 found_fields[field_index] = item.data.field_type;
14537 field_inits[field_index] = try sema.resolveInst(item.data.init);14538 field_inits[field_index] = try sema.resolveInst(item.data.init);
14538 if (resolved_ty.structFieldValueComptime(field_index)) |default_value| {14539 if (!is_packed) if (resolved_ty.structFieldValueComptime(field_index)) |default_value| {
14539 const init_val = (try sema.resolveMaybeUndefVal(block, field_src, field_inits[field_index])) orelse {14540 const init_val = (try sema.resolveMaybeUndefVal(block, field_src, field_inits[field_index])) orelse {
14540 return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime known");14541 return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime known");
14541 };14542 };
...@@ -14544,7 +14545,7 @@ fn zirStructInit(...@@ -14544,7 +14545,7 @@ fn zirStructInit(
14544 // TODO add note showing where default value is provided14545 // TODO add note showing where default value is provided
14545 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});14546 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
14546 }14547 }
14547 }14548 };
14548 }14549 }
1454914550
14550 return sema.finishStructInit(block, src, src, field_inits, resolved_ty, is_ref);14551 return sema.finishStructInit(block, src, src, field_inits, resolved_ty, is_ref);
test/cases/compile_errors/invalid_comptime_fields.zig created+21
...@@ -0,0 +1,21 @@
1const U = union {
2 comptime a: u32 = 1,
3};
4const E = enum {
5 comptime a = 1,
6};
7const P = packed struct {
8 comptime a: u32 = 1,
9};
10const X = extern struct {
11 comptime a: u32 = 1,
12};
13
14// error
15// backend=stage2
16// target=native
17//
18// :2:5: error: union fields cannot be marked comptime
19// :5:5: error: enum fields cannot be marked comptime
20// :8:5: error: packed struct fields cannot be marked comptime
21// :11:5: error: extern struct fields cannot be marked comptime