authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-09-25 00:30:15+02:00
committergravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2022-09-26 08:56:34+02:00
logb6bda5183e44e88b53d3e51c3599d8889fb1bdb8
treea93d14f828db1e4e610eaa7dd1b8d71faf9c42d9
parent6cc2b2616365c2a216f3dd73837f405df094daab

sema: load the correct AST in failWithInvalidComptimeFieldStore

The container we want to get the fields from might not be declared in the same file as the block we are analyzing, so we should get the AST from the decl's file instead.

2 files changed, 30 insertions(+), 2 deletions(-)

src/Sema.zig+4-2
......@@ -1868,9 +1868,11 @@ fn failWithInvalidComptimeFieldStore(sema: *Sema, block: *Block, init_src: LazyS
18681868 errdefer msg.destroy(sema.gpa);
18691869
18701870 const decl_index = container_ty.getOwnerDeclOrNull() orelse break :msg msg;
1871
1872 const tree = try sema.getAstTree(block);
18731871 const decl = sema.mod.declPtr(decl_index);
1872 const tree = decl.getFileScope().getTree(sema.gpa) catch |err| {
1873 log.err("unable to load AST to report compile error: {s}", .{@errorName(err)});
1874 return error.AnalysisFail;
1875 };
18741876 const field_src = enumFieldSrcLoc(decl, tree.*, 0, field_index);
18751877 const default_value_src: LazySrcLoc = .{ .node_offset_field_default = field_src.node_offset.x };
18761878
test/compile_errors.zig+26
......@@ -225,6 +225,32 @@ pub fn addCases(ctx: *TestContext) !void {
225225 });
226226 }
227227
228 {
229 const case = ctx.obj("invalid store to comptime field", .{});
230 case.backend = .stage2;
231
232 case.addSourceFile("a.zig",
233 \\pub const S = struct {
234 \\ comptime foo: u32 = 1,
235 \\ bar: u32,
236 \\ pub fn foo(x: @This()) void {
237 \\ _ = x;
238 \\ }
239 \\};
240 );
241
242 case.addError(
243 \\const a = @import("a.zig");
244 \\
245 \\export fn entry() void {
246 \\ _ = a.S.foo(a.S{ .foo = 2, .bar = 2 });
247 \\}
248 , &[_][]const u8{
249 ":4:23: error: value stored in comptime field does not match the default value of the field",
250 ":2:25: note: default value set here",
251 });
252 }
253
228254 // TODO test this in stage2, but we won't even try in stage1
229255 //ctx.objErrStage1("inline fn calls itself indirectly",
230256 // \\export fn foo() void {