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(
42144214 const have_value = member.ast.value_expr != 0;
42154215 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
42174223 if (!is_comptime) {
42184224 known_non_opv = known_non_opv or
42194225 nodeImpliesMoreThanOnePossibleValue(tree, member.ast.type_expr);
src/Sema.zig+3-2
......@@ -14509,6 +14509,7 @@ fn zirStructInit(
1450914509 var field_i: u32 = 0;
1451014510 var extra_index = extra.end;
1451114511
14512 const is_packed = resolved_ty.containerLayout() == .Packed;
1451214513 while (field_i < extra.data.fields_len) : (field_i += 1) {
1451314514 const item = sema.code.extraData(Zir.Inst.StructInit.Item, extra_index);
1451414515 extra_index = item.end;
......@@ -14535,7 +14536,7 @@ fn zirStructInit(
1453514536 }
1453614537 found_fields[field_index] = item.data.field_type;
1453714538 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| {
1453914540 const init_val = (try sema.resolveMaybeUndefVal(block, field_src, field_inits[field_index])) orelse {
1454014541 return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime known");
1454114542 };
......@@ -14544,7 +14545,7 @@ fn zirStructInit(
1454414545 // TODO add note showing where default value is provided
1454514546 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
1454614547 }
14547 }
14548 };
1454814549 }
1454914550
1455014551 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