authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-22 17:15:15+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-23 15:40:12+03:00
log881c0cb20b8cbde252ab38dff2c76886c4b72f1d
treeb373d7ace1576822b357f6ed5694065ecaa5523b
parent15dddfd84d9007689ef1fa6f4abedb88c570973a

Sema: add default value here note to invalid comptime field store error


3 files changed, 42 insertions(+), 6 deletions(-)

src/Module.zig+16
...@@ -2704,6 +2704,18 @@ pub const SrcLoc = struct {...@@ -2704,6 +2704,18 @@ pub const SrcLoc = struct {
2704 else => unreachable,2704 else => unreachable,
2705 }2705 }
2706 },2706 },
2707 .node_offset_field_default => |node_off| {
2708 const tree = try src_loc.file_scope.getTree(gpa);
2709 const node_tags = tree.nodes.items(.tag);
2710 const parent_node = src_loc.declRelativeToNodeIndex(node_off);
2711
2712 const full: Ast.full.ContainerField = switch (node_tags[parent_node]) {
2713 .container_field => tree.containerField(parent_node),
2714 .container_field_init => tree.containerFieldInit(parent_node),
2715 else => unreachable,
2716 };
2717 return nodeToSpan(tree, full.ast.value_expr);
2718 },
2707 }2719 }
2708 }2720 }
27092721
...@@ -3021,6 +3033,9 @@ pub const LazySrcLoc = union(enum) {...@@ -3021,6 +3033,9 @@ pub const LazySrcLoc = union(enum) {
3021 /// The source location points to the tag type of an union or an enum.3033 /// The source location points to the tag type of an union or an enum.
3022 /// The Decl is determined contextually.3034 /// The Decl is determined contextually.
3023 node_offset_container_tag: i32,3035 node_offset_container_tag: i32,
3036 /// The source location points to the default value of a field.
3037 /// The Decl is determined contextually.
3038 node_offset_field_default: i32,
30243039
3025 pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;3040 pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
30263041
...@@ -3098,6 +3113,7 @@ pub const LazySrcLoc = union(enum) {...@@ -3098,6 +3113,7 @@ pub const LazySrcLoc = union(enum) {
3098 .node_offset_ptr_bitoffset,3113 .node_offset_ptr_bitoffset,
3099 .node_offset_ptr_hostsize,3114 .node_offset_ptr_hostsize,
3100 .node_offset_container_tag,3115 .node_offset_container_tag,
3116 .node_offset_field_default,
3101 => .{3117 => .{
3102 .file_scope = decl.getFileScope(),3118 .file_scope = decl.getFileScope(),
3103 .parent_decl_node = decl.src_node,3119 .parent_decl_node = decl.src_node,
src/Sema.zig+21-5
...@@ -1789,6 +1789,24 @@ fn failWithIntegerOverflow(sema: *Sema, block: *Block, src: LazySrcLoc, int_ty:...@@ -1789,6 +1789,24 @@ fn failWithIntegerOverflow(sema: *Sema, block: *Block, src: LazySrcLoc, int_ty:
1789 });1789 });
1790}1790}
17911791
1792fn failWithInvalidComptimeFieldStore(sema: *Sema, block: *Block, init_src: LazySrcLoc, container_ty: Type, field_index: usize) CompileError {
1793 const msg = msg: {
1794 const msg = try sema.errMsg(block, init_src, "value stored in comptime field does not match the default value of the field", .{});
1795 errdefer msg.destroy(sema.gpa);
1796
1797 const decl_index = container_ty.getOwnerDeclOrNull() orelse break :msg msg;
1798
1799 const tree = try sema.getAstTree(block);
1800 const decl = sema.mod.declPtr(decl_index);
1801 const field_src = enumFieldSrcLoc(decl, tree.*, container_ty.getNodeOffset(), field_index);
1802 const default_value_src: LazySrcLoc = .{ .node_offset_field_default = field_src.node_offset.x };
1803
1804 try sema.errNote(block, default_value_src, msg, "default value set here", .{});
1805 break :msg msg;
1806 };
1807 return sema.failWithOwnedErrorMsg(block, msg);
1808}
1809
1792/// We don't return a pointer to the new error note because the pointer1810/// We don't return a pointer to the new error note because the pointer
1793/// becomes invalid when you add another one.1811/// becomes invalid when you add another one.
1794fn errNote(1812fn errNote(
...@@ -14542,8 +14560,7 @@ fn zirStructInit(...@@ -14542,8 +14560,7 @@ fn zirStructInit(
14542 };14560 };
1454314561
14544 if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index), sema.mod)) {14562 if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index), sema.mod)) {
14545 // TODO add note showing where default value is provided14563 return sema.failWithInvalidComptimeFieldStore(block, field_src, resolved_ty, field_index);
14546 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
14547 }14564 }
14548 };14565 };
14549 }14566 }
...@@ -22379,7 +22396,7 @@ fn storePtrVal(...@@ -22379,7 +22396,7 @@ fn storePtrVal(
22379 .direct => |val_ptr| {22396 .direct => |val_ptr| {
22380 if (mut_kit.decl_ref_mut.runtime_index == .comptime_field_ptr) {22397 if (mut_kit.decl_ref_mut.runtime_index == .comptime_field_ptr) {
22381 if (!operand_val.eql(val_ptr.*, operand_ty, sema.mod)) {22398 if (!operand_val.eql(val_ptr.*, operand_ty, sema.mod)) {
22382 // TODO add note showing where default value is provided22399 // TODO use failWithInvalidComptimeFieldStore
22383 return sema.fail(block, src, "value stored in comptime field does not match the default value of the field", .{});22400 return sema.fail(block, src, "value stored in comptime field does not match the default value of the field", .{});
22384 }22401 }
22385 return;22402 return;
...@@ -23754,8 +23771,7 @@ fn coerceTupleToStruct(...@@ -23754,8 +23771,7 @@ fn coerceTupleToStruct(
23754 };23771 };
2375523772
23756 if (!init_val.eql(field.default_val, field.ty, sema.mod)) {23773 if (!init_val.eql(field.default_val, field.ty, sema.mod)) {
23757 // TODO add note showing where default value is provided23774 return sema.failWithInvalidComptimeFieldStore(block, field_src, inst_ty, i);
23758 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
23759 }23775 }
23760 }23776 }
23761 if (runtime_src == null) {23777 if (runtime_src == null) {
src/type.zig+5-1
...@@ -5714,6 +5714,10 @@ pub const Type = extern union {...@@ -5714,6 +5714,10 @@ pub const Type = extern union {
5714 }5714 }
57155715
5716 pub fn getOwnerDecl(ty: Type) Module.Decl.Index {5716 pub fn getOwnerDecl(ty: Type) Module.Decl.Index {
5717 return ty.getOwnerDeclOrNull() orelse unreachable;
5718 }
5719
5720 pub fn getOwnerDeclOrNull(ty: Type) ?Module.Decl.Index {
5717 switch (ty.tag()) {5721 switch (ty.tag()) {
5718 .enum_full, .enum_nonexhaustive => {5722 .enum_full, .enum_nonexhaustive => {
5719 const enum_full = ty.cast(Payload.EnumFull).?.data;5723 const enum_full = ty.cast(Payload.EnumFull).?.data;
...@@ -5753,7 +5757,7 @@ pub const Type = extern union {...@@ -5753,7 +5757,7 @@ pub const Type = extern union {
5753 .type_info,5757 .type_info,
5754 => unreachable, // These need to be resolved earlier.5758 => unreachable, // These need to be resolved earlier.
57555759
5756 else => unreachable,5760 else => return null,
5757 }5761 }
5758 }5762 }
57595763