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

This commit is contained in:
Veikka Tuominen 2022-07-22 17:15:15 +03:00
parent 15dddfd84d
commit 881c0cb20b
3 changed files with 42 additions and 6 deletions

View File

@ -2704,6 +2704,18 @@ pub const SrcLoc = struct {
else => unreachable,
}
},
.node_offset_field_default => |node_off| {
const tree = try src_loc.file_scope.getTree(gpa);
const node_tags = tree.nodes.items(.tag);
const parent_node = src_loc.declRelativeToNodeIndex(node_off);
const full: Ast.full.ContainerField = switch (node_tags[parent_node]) {
.container_field => tree.containerField(parent_node),
.container_field_init => tree.containerFieldInit(parent_node),
else => unreachable,
};
return nodeToSpan(tree, full.ast.value_expr);
},
}
}
@ -3021,6 +3033,9 @@ pub const LazySrcLoc = union(enum) {
/// The source location points to the tag type of an union or an enum.
/// The Decl is determined contextually.
node_offset_container_tag: i32,
/// The source location points to the default value of a field.
/// The Decl is determined contextually.
node_offset_field_default: i32,
pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
@ -3098,6 +3113,7 @@ pub const LazySrcLoc = union(enum) {
.node_offset_ptr_bitoffset,
.node_offset_ptr_hostsize,
.node_offset_container_tag,
.node_offset_field_default,
=> .{
.file_scope = decl.getFileScope(),
.parent_decl_node = decl.src_node,

View File

@ -1789,6 +1789,24 @@ fn failWithIntegerOverflow(sema: *Sema, block: *Block, src: LazySrcLoc, int_ty:
});
}
fn failWithInvalidComptimeFieldStore(sema: *Sema, block: *Block, init_src: LazySrcLoc, container_ty: Type, field_index: usize) CompileError {
const msg = msg: {
const msg = try sema.errMsg(block, init_src, "value stored in comptime field does not match the default value of the field", .{});
errdefer msg.destroy(sema.gpa);
const decl_index = container_ty.getOwnerDeclOrNull() orelse break :msg msg;
const tree = try sema.getAstTree(block);
const decl = sema.mod.declPtr(decl_index);
const field_src = enumFieldSrcLoc(decl, tree.*, container_ty.getNodeOffset(), field_index);
const default_value_src: LazySrcLoc = .{ .node_offset_field_default = field_src.node_offset.x };
try sema.errNote(block, default_value_src, msg, "default value set here", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);
}
/// We don't return a pointer to the new error note because the pointer
/// becomes invalid when you add another one.
fn errNote(
@ -14542,8 +14560,7 @@ fn zirStructInit(
};
if (!init_val.eql(default_value, resolved_ty.structFieldType(field_index), sema.mod)) {
// TODO add note showing where default value is provided
return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
return sema.failWithInvalidComptimeFieldStore(block, field_src, resolved_ty, field_index);
}
};
}
@ -22379,7 +22396,7 @@ fn storePtrVal(
.direct => |val_ptr| {
if (mut_kit.decl_ref_mut.runtime_index == .comptime_field_ptr) {
if (!operand_val.eql(val_ptr.*, operand_ty, sema.mod)) {
// TODO add note showing where default value is provided
// TODO use failWithInvalidComptimeFieldStore
return sema.fail(block, src, "value stored in comptime field does not match the default value of the field", .{});
}
return;
@ -23754,8 +23771,7 @@ fn coerceTupleToStruct(
};
if (!init_val.eql(field.default_val, field.ty, sema.mod)) {
// TODO add note showing where default value is provided
return sema.fail(block, field_src, "value stored in comptime field does not match the default value of the field", .{});
return sema.failWithInvalidComptimeFieldStore(block, field_src, inst_ty, i);
}
}
if (runtime_src == null) {

View File

@ -5714,6 +5714,10 @@ pub const Type = extern union {
}
pub fn getOwnerDecl(ty: Type) Module.Decl.Index {
return ty.getOwnerDeclOrNull() orelse unreachable;
}
pub fn getOwnerDeclOrNull(ty: Type) ?Module.Decl.Index {
switch (ty.tag()) {
.enum_full, .enum_nonexhaustive => {
const enum_full = ty.cast(Payload.EnumFull).?.data;
@ -5753,7 +5757,7 @@ pub const Type = extern union {
.type_info,
=> unreachable, // These need to be resolved earlier.
else => unreachable,
else => return null,
}
}