Skip to content

Commit c76b5c1

Browse files
committed
stage2: correct node offset of nested declarations
1 parent d769fd0 commit c76b5c1

9 files changed

+106
-114
lines changed

src/AstGen.zig

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,7 +4278,7 @@ fn structDeclInner(
42784278
var known_non_opv = false;
42794279
var known_comptime_only = false;
42804280
for (container_decl.ast.members) |member_node| {
4281-
const member = switch (try containerMember(gz, &namespace.base, &wip_members, member_node)) {
4281+
const member = switch (try containerMember(&block_scope, &namespace.base, &wip_members, member_node)) {
42824282
.decl => continue,
42834283
.field => |field| field,
42844284
};
@@ -4445,7 +4445,7 @@ fn unionDeclInner(
44454445
defer wip_members.deinit();
44464446

44474447
for (members) |member_node| {
4448-
const member = switch (try containerMember(gz, &namespace.base, &wip_members, member_node)) {
4448+
const member = switch (try containerMember(&block_scope, &namespace.base, &wip_members, member_node)) {
44494449
.decl => continue,
44504450
.field => |field| field,
44514451
};
@@ -4732,7 +4732,7 @@ fn containerDecl(
47324732
for (container_decl.ast.members) |member_node| {
47334733
if (member_node == counts.nonexhaustive_node)
47344734
continue;
4735-
const member = switch (try containerMember(gz, &namespace.base, &wip_members, member_node)) {
4735+
const member = switch (try containerMember(&block_scope, &namespace.base, &wip_members, member_node)) {
47364736
.decl => continue,
47374737
.field => |field| field,
47384738
};
@@ -4810,13 +4810,26 @@ fn containerDecl(
48104810
};
48114811
defer namespace.deinit(gpa);
48124812

4813+
astgen.advanceSourceCursorToNode(node);
4814+
var block_scope: GenZir = .{
4815+
.parent = &namespace.base,
4816+
.decl_node_index = node,
4817+
.decl_line = astgen.source_line,
4818+
.astgen = astgen,
4819+
.force_comptime = true,
4820+
.in_defer = false,
4821+
.instructions = gz.instructions,
4822+
.instructions_top = gz.instructions.items.len,
4823+
};
4824+
defer block_scope.unstack();
4825+
48134826
const decl_count = try astgen.scanDecls(&namespace, container_decl.ast.members);
48144827

48154828
var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, 0, 0, 0);
48164829
defer wip_members.deinit();
48174830

48184831
for (container_decl.ast.members) |member_node| {
4819-
const res = try containerMember(gz, &namespace.base, &wip_members, member_node);
4832+
const res = try containerMember(&block_scope, &namespace.base, &wip_members, member_node);
48204833
if (res == .field) {
48214834
return astgen.failNode(member_node, "opaque types cannot have fields", .{});
48224835
}

src/Module.zig

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -853,8 +853,6 @@ pub const EmitH = struct {
853853
pub const ErrorSet = struct {
854854
/// The Decl that corresponds to the error set itself.
855855
owner_decl: Decl.Index,
856-
/// Offset from Decl node index, points to the error set AST node.
857-
node_offset: i32,
858856
/// The string bytes are stored in the owner Decl arena.
859857
/// These must be in sorted order. See sortNames.
860858
names: NameMap,
@@ -866,7 +864,7 @@ pub const ErrorSet = struct {
866864
return .{
867865
.file_scope = owner_decl.getFileScope(),
868866
.parent_decl_node = owner_decl.src_node,
869-
.lazy = LazySrcLoc.nodeOffset(self.node_offset),
867+
.lazy = LazySrcLoc.nodeOffset(0),
870868
};
871869
}
872870

@@ -893,8 +891,6 @@ pub const Struct = struct {
893891
namespace: Namespace,
894892
/// The Decl that corresponds to the struct itself.
895893
owner_decl: Decl.Index,
896-
/// Offset from `owner_decl`, points to the struct AST node.
897-
node_offset: i32,
898894
/// Index of the struct_decl ZIR instruction.
899895
zir_index: Zir.Inst.Index,
900896

@@ -953,7 +949,7 @@ pub const Struct = struct {
953949
return .{
954950
.file_scope = owner_decl.getFileScope(),
955951
.parent_decl_node = owner_decl.src_node,
956-
.lazy = LazySrcLoc.nodeOffset(s.node_offset),
952+
.lazy = LazySrcLoc.nodeOffset(0),
957953
};
958954
}
959955

@@ -968,7 +964,7 @@ pub const Struct = struct {
968964
});
969965
return s.srcLoc(mod);
970966
};
971-
const node = owner_decl.relativeToNodeIndex(s.node_offset);
967+
const node = owner_decl.relativeToNodeIndex(0);
972968
const node_tags = tree.nodes.items(.tag);
973969
switch (node_tags[node]) {
974970
.container_decl,
@@ -1060,8 +1056,6 @@ pub const Struct = struct {
10601056
pub const EnumSimple = struct {
10611057
/// The Decl that corresponds to the enum itself.
10621058
owner_decl: Decl.Index,
1063-
/// Offset from `owner_decl`, points to the enum decl AST node.
1064-
node_offset: i32,
10651059
/// Set of field names in declaration order.
10661060
fields: NameMap,
10671061

@@ -1072,7 +1066,7 @@ pub const EnumSimple = struct {
10721066
return .{
10731067
.file_scope = owner_decl.getFileScope(),
10741068
.parent_decl_node = owner_decl.src_node,
1075-
.lazy = LazySrcLoc.nodeOffset(self.node_offset),
1069+
.lazy = LazySrcLoc.nodeOffset(0),
10761070
};
10771071
}
10781072
};
@@ -1083,8 +1077,6 @@ pub const EnumSimple = struct {
10831077
pub const EnumNumbered = struct {
10841078
/// The Decl that corresponds to the enum itself.
10851079
owner_decl: Decl.Index,
1086-
/// Offset from `owner_decl`, points to the enum decl AST node.
1087-
node_offset: i32,
10881080
/// An integer type which is used for the numerical value of the enum.
10891081
/// Whether zig chooses this type or the user specifies it, it is stored here.
10901082
tag_ty: Type,
@@ -1103,7 +1095,7 @@ pub const EnumNumbered = struct {
11031095
return .{
11041096
.file_scope = owner_decl.getFileScope(),
11051097
.parent_decl_node = owner_decl.src_node,
1106-
.lazy = LazySrcLoc.nodeOffset(self.node_offset),
1098+
.lazy = LazySrcLoc.nodeOffset(0),
11071099
};
11081100
}
11091101
};
@@ -1113,8 +1105,6 @@ pub const EnumNumbered = struct {
11131105
pub const EnumFull = struct {
11141106
/// The Decl that corresponds to the enum itself.
11151107
owner_decl: Decl.Index,
1116-
/// Offset from `owner_decl`, points to the enum decl AST node.
1117-
node_offset: i32,
11181108
/// An integer type which is used for the numerical value of the enum.
11191109
/// Whether zig chooses this type or the user specifies it, it is stored here.
11201110
tag_ty: Type,
@@ -1137,7 +1127,7 @@ pub const EnumFull = struct {
11371127
return .{
11381128
.file_scope = owner_decl.getFileScope(),
11391129
.parent_decl_node = owner_decl.src_node,
1140-
.lazy = LazySrcLoc.nodeOffset(self.node_offset),
1130+
.lazy = LazySrcLoc.nodeOffset(0),
11411131
};
11421132
}
11431133
};
@@ -1155,8 +1145,6 @@ pub const Union = struct {
11551145
namespace: Namespace,
11561146
/// The Decl that corresponds to the union itself.
11571147
owner_decl: Decl.Index,
1158-
/// Offset from `owner_decl`, points to the union decl AST node.
1159-
node_offset: i32,
11601148
/// Index of the union_decl ZIR instruction.
11611149
zir_index: Zir.Inst.Index,
11621150

@@ -1203,7 +1191,7 @@ pub const Union = struct {
12031191
return .{
12041192
.file_scope = owner_decl.getFileScope(),
12051193
.parent_decl_node = owner_decl.src_node,
1206-
.lazy = LazySrcLoc.nodeOffset(self.node_offset),
1194+
.lazy = LazySrcLoc.nodeOffset(0),
12071195
};
12081196
}
12091197

@@ -1218,7 +1206,7 @@ pub const Union = struct {
12181206
});
12191207
return u.srcLoc(mod);
12201208
};
1221-
const node = owner_decl.relativeToNodeIndex(u.node_offset);
1209+
const node = owner_decl.relativeToNodeIndex(0);
12221210
const node_tags = tree.nodes.items(.tag);
12231211
var buf: [2]Ast.Node.Index = undefined;
12241212
switch (node_tags[node]) {
@@ -1410,8 +1398,6 @@ pub const Union = struct {
14101398
pub const Opaque = struct {
14111399
/// The Decl that corresponds to the opaque itself.
14121400
owner_decl: Decl.Index,
1413-
/// Offset from `owner_decl`, points to the opaque decl AST node.
1414-
node_offset: i32,
14151401
/// Represents the declarations inside this opaque.
14161402
namespace: Namespace,
14171403

@@ -1420,7 +1406,7 @@ pub const Opaque = struct {
14201406
return .{
14211407
.file_scope = owner_decl.getFileScope(),
14221408
.parent_decl_node = owner_decl.src_node,
1423-
.lazy = LazySrcLoc.nodeOffset(self.node_offset),
1409+
.lazy = LazySrcLoc.nodeOffset(0),
14241410
};
14251411
}
14261412

@@ -4337,7 +4323,6 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
43374323
struct_obj.* = .{
43384324
.owner_decl = undefined, // set below
43394325
.fields = .{},
4340-
.node_offset = 0, // it's the struct for the root file
43414326
.zir_index = undefined, // set below
43424327
.layout = .Auto,
43434328
.status = .none,

0 commit comments

Comments
 (0)