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 {
27042704 else => unreachable,
27052705 }
27062706 },
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 },
27072719 }
27082720 }
27092721
......@@ -3021,6 +3033,9 @@ pub const LazySrcLoc = union(enum) {
30213033 /// The source location points to the tag type of an union or an enum.
30223034 /// The Decl is determined contextually.
30233035 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
30253040 pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
30263041
......@@ -3098,6 +3113,7 @@ pub const LazySrcLoc = union(enum) {
30983113 .node_offset_ptr_bitoffset,
30993114 .node_offset_ptr_hostsize,
31003115 .node_offset_container_tag,
3116 .node_offset_field_default,
31013117 => .{
31023118 .file_scope = decl.getFileScope(),
31033119 .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:
17891789 });
17901790}
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
17921810/// We don't return a pointer to the new error note because the pointer
17931811/// becomes invalid when you add another one.
17941812fn errNote(
......@@ -14542,8 +14560,7 @@ fn zirStructInit(
1454214560 };
1454314561
1454414562 if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index), sema.mod)) {
14545 // TODO add note showing where default value is provided
14546 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
14563 return sema.failWithInvalidComptimeFieldStore(block, field_src, resolved_ty, field_index);
1454714564 }
1454814565 };
1454914566 }
......@@ -22379,7 +22396,7 @@ fn storePtrVal(
2237922396 .direct => |val_ptr| {
2238022397 if (mut_kit.decl_ref_mut.runtime_index == .comptime_field_ptr) {
2238122398 if (!operand_val.eql(val_ptr.*, operand_ty, sema.mod)) {
22382 // TODO add note showing where default value is provided
22399 // TODO use failWithInvalidComptimeFieldStore
2238322400 return sema.fail(block, src, "value stored in comptime field does not match the default value of the field", .{});
2238422401 }
2238522402 return;
......@@ -23754,8 +23771,7 @@ fn coerceTupleToStruct(
2375423771 };
2375523772
2375623773 if (!init_val.eql(field.default_val, field.ty, sema.mod)) {
23757 // TODO add note showing where default value is provided
23758 return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
23774 return sema.failWithInvalidComptimeFieldStore(block, field_src, inst_ty, i);
2375923775 }
2376023776 }
2376123777 if (runtime_src == null) {
src/type.zig+5-1
......@@ -5714,6 +5714,10 @@ pub const Type = extern union {
57145714 }
57155715
57165716 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 {
57175721 switch (ty.tag()) {
57185722 .enum_full, .enum_nonexhaustive => {
57195723 const enum_full = ty.cast(Payload.EnumFull).?.data;
......@@ -5753,7 +5757,7 @@ pub const Type = extern union {
57535757 .type_info,
57545758 => unreachable, // These need to be resolved earlier.
57555759
5756 else => unreachable,
5760 else => return null,
57575761 }
57585762 }
57595763