| ... | @@ -14515,6 +14515,16 @@ fn zirStructInit( | ... | @@ -14515,6 +14515,16 @@ fn zirStructInit( |
| 14515 | } | 14515 | } |
| 14516 | found_fields[field_index] = item.data.field_type; | 14516 | found_fields[field_index] = item.data.field_type; |
| 14517 | field_inits[field_index] = try sema.resolveInst(item.data.init); | 14517 | field_inits[field_index] = try sema.resolveInst(item.data.init); |
| | 14518 | if (resolved_ty.structFieldValueComptime(field_index)) |default_value| { |
| | 14519 | const init_val = (try sema.resolveMaybeUndefVal(block, field_src, field_inits[field_index])) orelse { |
| | 14520 | return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime known"); |
| | 14521 | }; |
| | 14522 | |
| | 14523 | if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index), sema.mod)) { |
| | 14524 | // TODO add note showing where default value is provided |
| | 14525 | return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{}); |
| | 14526 | } |
| | 14527 | } |
| 14518 | } | 14528 | } |
| 14519 | | 14529 | |
| 14520 | return sema.finishStructInit(block, src, src, field_inits, resolved_ty, is_ref); | 14530 | return sema.finishStructInit(block, src, src, field_inits, resolved_ty, is_ref); |
| ... | @@ -23688,6 +23698,7 @@ fn coerceTupleToStruct( | ... | @@ -23688,6 +23698,7 @@ fn coerceTupleToStruct( |
| 23688 | const struct_ty = try sema.resolveTypeFields(block, dest_ty_src, dest_ty); | 23698 | const struct_ty = try sema.resolveTypeFields(block, dest_ty_src, dest_ty); |
| 23689 | | 23699 | |
| 23690 | if (struct_ty.isTupleOrAnonStruct()) { | 23700 | if (struct_ty.isTupleOrAnonStruct()) { |
| | 23701 | // NOTE remember to handle comptime fields |
| 23691 | return sema.fail(block, dest_ty_src, "TODO: implement coercion from tuples to tuples", .{}); | 23702 | return sema.fail(block, dest_ty_src, "TODO: implement coercion from tuples to tuples", .{}); |
| 23692 | } | 23703 | } |
| 23693 | | 23704 | |
| ... | @@ -23708,12 +23719,19 @@ fn coerceTupleToStruct( | ... | @@ -23708,12 +23719,19 @@ fn coerceTupleToStruct( |
| 23708 | try std.fmt.allocPrint(sema.arena, "{d}", .{i}); | 23719 | try std.fmt.allocPrint(sema.arena, "{d}", .{i}); |
| 23709 | const field_index = try sema.structFieldIndex(block, struct_ty, field_name, field_src); | 23720 | const field_index = try sema.structFieldIndex(block, struct_ty, field_name, field_src); |
| 23710 | const field = fields.values()[field_index]; | 23721 | const field = fields.values()[field_index]; |
| 23711 | if (field.is_comptime) { | | |
| 23712 | return sema.fail(block, dest_ty_src, "TODO: implement coercion from tuples to structs when one of the destination struct fields is comptime", .{}); | | |
| 23713 | } | | |
| 23714 | const elem_ref = try tupleField(sema, block, inst_src, inst, field_src, i); | 23722 | const elem_ref = try tupleField(sema, block, inst_src, inst, field_src, i); |
| 23715 | const coerced = try sema.coerce(block, field.ty, elem_ref, field_src); | 23723 | const coerced = try sema.coerce(block, field.ty, elem_ref, field_src); |
| 23716 | field_refs[field_index] = coerced; | 23724 | field_refs[field_index] = coerced; |
| | 23725 | if (field.is_comptime) { |
| | 23726 | const init_val = (try sema.resolveMaybeUndefVal(block, field_src, coerced)) orelse { |
| | 23727 | return sema.failWithNeededComptime(block, field_src, "value stored in comptime field must be comptime known"); |
| | 23728 | }; |
| | 23729 | |
| | 23730 | if (!init_val.eql(field.default_val, field.ty, sema.mod)) { |
| | 23731 | // TODO add note showing where default value is provided |
| | 23732 | return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{}); |
| | 23733 | } |
| | 23734 | } |
| 23717 | if (runtime_src == null) { | 23735 | if (runtime_src == null) { |
| 23718 | if (try sema.resolveMaybeUndefVal(block, field_src, coerced)) |field_val| { | 23736 | if (try sema.resolveMaybeUndefVal(block, field_src, coerced)) |field_val| { |
| 23719 | field_vals[field_index] = field_val; | 23737 | field_vals[field_index] = field_val; |