fix: error reporting for global variables

This commit is contained in:
Brett Broadhurst 2026-03-25 23:50:42 -06:00
parent 2dfa6cd842
commit 9ca2200448
Failed to generate hash of commit
5 changed files with 80 additions and 55 deletions

View file

@ -491,6 +491,7 @@ fn setDeclaration(
name: Ir.NullTerminatedString,
value: Ir.Inst.Index,
gi: *GenIr,
node: *const Ast.Node,
},
) !void {
const astgen = args.gi.astgen;
@ -498,17 +499,19 @@ fn setDeclaration(
try astgen.extra.ensureUnusedCapacity(astgen.gpa, extra_len);
const inst_data = &astgen.instructions.items[@intFromEnum(decl_index)].data;
inst_data.payload.extra_index = astgen.addExtraAssumeCapacity(
Ir.Inst.Declaration{
inst_data.payload = .{
.src_offset = @intCast(args.node.loc.start),
.extra_index = astgen.addExtraAssumeCapacity(Ir.Inst.Declaration{
.name = args.name,
.value = args.value,
},
);
}),
};
}
fn setDeclVarPayload(
decl_index: Ir.Inst.Index,
body_block: *GenIr,
node: *const Ast.Node,
) !void {
defer body_block.unstack();
@ -518,11 +521,12 @@ fn setDeclVarPayload(
try astgen.extra.ensureUnusedCapacity(astgen.gpa, extra_len);
const inst_data = &astgen.instructions.items[@intFromEnum(decl_index)].data;
inst_data.payload.extra_index = astgen.addExtraAssumeCapacity(
Ir.Inst.Var{
inst_data.payload = .{
.src_offset = @intCast(node.loc.start),
.extra_index = astgen.addExtraAssumeCapacity(Ir.Inst.Var{
.body_len = @intCast(body.len),
},
);
}),
};
astgen.appendBlockBody(body);
}
@ -1266,11 +1270,12 @@ fn varDecl(gi: *GenIr, scope: *Scope, decl_node: *const Ast.Node) !void {
_ = try expr(&decl_block, scope, expr_node);
const name_str = try astgen.strFromNode(identifier_node);
try setDeclVarPayload(var_inst, &decl_block);
try setDeclVarPayload(var_inst, &decl_block, identifier_node);
try setDeclaration(decl_inst, .{
.name = name_str.index,
.value = var_inst,
.gi = gi,
.node = decl_node,
});
try astgen.globals.append(gpa, decl_inst);
}
@ -1328,13 +1333,14 @@ fn defaultBlock(
.name = decl_str.index,
.value = knot_inst,
.gi = gi,
.node = body_node,
});
}
fn stitchDeclInner(
gi: *GenIr,
scope: *Scope,
_: *const Ast.Node,
node: *const Ast.Node,
prototype_node: *const Ast.Node,
body_node: ?*const Ast.Node,
) InnerError!void {
@ -1377,6 +1383,7 @@ fn stitchDeclInner(
.name = decl_str.index,
.value = stitch_inst,
.gi = gi,
.node = node,
});
}
@ -1450,6 +1457,7 @@ fn knotDecl(gi: *GenIr, parent_scope: *Scope, decl_node: *const Ast.Node) InnerE
.name = name_str.index,
.value = knot_inst,
.gi = gi,
.node = decl_node,
});
}
@ -1461,7 +1469,6 @@ fn file(gi: *GenIr, scope: *Scope, file_node: *const Ast.Node) InnerError!void {
},
});
var start_index: usize = 0;
var file_scope = gi.makeSubBlock();
defer file_scope.unstack();
@ -1472,12 +1479,8 @@ fn file(gi: *GenIr, scope: *Scope, file_node: *const Ast.Node) InnerError!void {
const first_child = nested_decls_list[0];
if (first_child.tag == .block_stmt) {
try defaultBlock(&file_scope, scope, first_child);
if (nested_decls_list.len > 1)
start_index += 1
else
return file_scope.setBlockBody(file_inst);
}
for (nested_decls_list[start_index..]) |child_node| {
for (nested_decls_list[1..]) |child_node| {
switch (child_node.tag) {
.knot_decl => try knotDecl(gi, scope, child_node),
.stitch_decl => try stitchDecl(gi, scope, child_node),