authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-09-30 13:34:12-05:00
committergravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-10-02 15:21:48-05:00
log269e54877051791790f5dffc4d4f1476834e4e43
treef1a050defea8bff3d8f39934fdca4111afb1af0f
parent4916e26be434309209585a7c8a7918ed58c79466

Fix namespace references for deeply nested structs


2 files changed, 21 insertions(+), 39 deletions(-)

src/Module.zig+7-12
......@@ -1107,7 +1107,7 @@ pub const Scope = struct {
11071107 /// Asserts the scope has a parent which is a Namespace and returns it.
11081108 pub fn namespace(scope: *Scope) *Namespace {
11091109 switch (scope.tag) {
1110 .block => return scope.cast(Block).?.sema.owner_decl.namespace,
1110 .block => return scope.cast(Block).?.src_decl.namespace,
11111111 .file => return scope.cast(File).?.root_decl.?.namespace,
11121112 .namespace => return scope.cast(Namespace).?,
11131113 }
......@@ -3244,11 +3244,11 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
32443244 .file_scope = file,
32453245 },
32463246 };
3247 const new_decl = try mod.allocateNewDecl(&struct_obj.namespace, 0, null);
3247 const decl_name = try file.fullyQualifiedNameZ(gpa);
3248 const new_decl = try mod.allocateNewDecl(decl_name, &struct_obj.namespace, 0, null);
32483249 file.root_decl = new_decl;
32493250 struct_obj.owner_decl = new_decl;
32503251 new_decl.src_line = 0;
3251 new_decl.name = try file.fullyQualifiedNameZ(gpa);
32523252 new_decl.is_pub = true;
32533253 new_decl.is_exported = false;
32543254 new_decl.has_align = false;
......@@ -3276,7 +3276,6 @@ pub fn semaFile(mod: *Module, file: *Scope.File) SemaError!void {
32763276 .perm_arena = &new_decl_arena.allocator,
32773277 .code = file.zir,
32783278 .owner_decl = new_decl,
3279 .namespace = &struct_obj.namespace,
32803279 .func = null,
32813280 .fn_ret_ty = Type.initTag(.void),
32823281 .owner_func = null,
......@@ -3342,7 +3341,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
33423341 .perm_arena = &decl_arena.allocator,
33433342 .code = zir,
33443343 .owner_decl = decl,
3345 .namespace = decl.namespace,
33463344 .func = null,
33473345 .fn_ret_ty = Type.initTag(.void),
33483346 .owner_func = null,
......@@ -3818,13 +3816,12 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
38183816 // We create a Decl for it regardless of analysis status.
38193817 const gop = try namespace.decls.getOrPut(gpa, decl_name);
38203818 if (!gop.found_existing) {
3821 const new_decl = try mod.allocateNewDecl(namespace, decl_node, iter.parent_decl.src_scope);
3819 const new_decl = try mod.allocateNewDecl(decl_name, namespace, decl_node, iter.parent_decl.src_scope);
38223820 if (is_usingnamespace) {
38233821 namespace.usingnamespace_set.putAssumeCapacity(new_decl, is_pub);
38243822 }
38253823 log.debug("scan new {*} ({s}) into {*}", .{ new_decl, decl_name, namespace });
38263824 new_decl.src_line = line;
3827 new_decl.name = decl_name;
38283825 gop.value_ptr.* = new_decl;
38293826 // Exported decls, comptime decls, usingnamespace decls, and
38303827 // test decls if in test mode, get analyzed.
......@@ -4089,7 +4086,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: *Allocator) Se
40894086 .perm_arena = &decl_arena.allocator,
40904087 .code = decl.namespace.file_scope.zir,
40914088 .owner_decl = decl,
4092 .namespace = decl.namespace,
40934089 .func = func,
40944090 .fn_ret_ty = func.owner_decl.ty.fnReturnType(),
40954091 .owner_func = func,
......@@ -4226,7 +4222,7 @@ fn markOutdatedDecl(mod: *Module, decl: *Decl) !void {
42264222 decl.analysis = .outdated;
42274223}
42284224
4229pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.Node.Index, src_scope: ?*CaptureScope) !*Decl {
4225pub fn allocateNewDecl(mod: *Module, name: [:0]const u8, namespace: *Scope.Namespace, src_node: Ast.Node.Index, src_scope: ?*CaptureScope) !*Decl {
42304226 // If we have emit-h then we must allocate a bigger structure to store the emit-h state.
42314227 const new_decl: *Decl = if (mod.emit_h != null) blk: {
42324228 const parent_struct = try mod.gpa.create(DeclPlusEmitH);
......@@ -4238,7 +4234,7 @@ pub fn allocateNewDecl(mod: *Module, namespace: *Scope.Namespace, src_node: Ast.
42384234 } else try mod.gpa.create(Decl);
42394235
42404236 new_decl.* = .{
4241 .name = "",
4237 .name = name,
42424238 .namespace = namespace,
42434239 .src_node = src_node,
42444240 .src_line = undefined,
......@@ -4414,9 +4410,8 @@ pub fn createAnonymousDeclFromDeclNamed(
44144410 const namespace = src_decl.namespace;
44154411 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);
44164412
4417 const new_decl = try mod.allocateNewDecl(namespace, src_decl.src_node, src_scope);
4413 const new_decl = try mod.allocateNewDecl(name, namespace, src_decl.src_node, src_scope);
44184414
4419 new_decl.name = name;
44204415 new_decl.src_line = src_decl.src_line;
44214416 new_decl.ty = typed_value.ty;
44224417 new_decl.val = typed_value.val;
src/Sema.zig+14-27
......@@ -24,8 +24,6 @@ inst_map: InstMap = .{},
2424/// and `src_decl` of `Scope.Block` is the `Decl` of the callee.
2525/// This `Decl` owns the arena memory of this `Sema`.
2626owner_decl: *Decl,
27/// How to look up decl names.
28namespace: *Scope.Namespace,
2927/// For an inline or comptime function call, this will be the root parent function
3028/// which contains the callsite. Corresponds to `owner_decl`.
3129owner_func: ?*Module.Fn,
......@@ -1048,6 +1046,8 @@ pub fn analyzeStructDecl(
10481046 } else 0;
10491047
10501048 _ = try sema.mod.scanNamespace(&struct_obj.namespace, extra_index, decls_len, new_decl);
1049
1050 new_decl.namespace = &struct_obj.namespace;
10511051}
10521052
10531053fn zirStructDecl(
......@@ -1084,7 +1084,7 @@ fn zirStructDecl(
10841084 .status = .none,
10851085 .known_has_bits = undefined,
10861086 .namespace = .{
1087 .parent = sema.owner_decl.namespace,
1087 .parent = block.src_decl.namespace,
10881088 .ty = struct_ty,
10891089 .file_scope = block.getFileScope(),
10901090 },
......@@ -1194,7 +1194,7 @@ fn zirEnumDecl(
11941194 .values = .{},
11951195 .node_offset = src.node_offset,
11961196 .namespace = .{
1197 .parent = sema.owner_decl.namespace,
1197 .parent = block.src_decl.namespace,
11981198 .ty = enum_ty,
11991199 .file_scope = block.getFileScope(),
12001200 },
......@@ -1205,6 +1205,8 @@ fn zirEnumDecl(
12051205
12061206 extra_index = try mod.scanNamespace(&enum_obj.namespace, extra_index, decls_len, new_decl);
12071207
1208 new_decl.namespace = &enum_obj.namespace;
1209
12081210 const body = sema.code.extra[extra_index..][0..body_len];
12091211 if (fields_len == 0) {
12101212 assert(body.len == 0);
......@@ -1227,10 +1229,6 @@ fn zirEnumDecl(
12271229 sema.owner_decl = new_decl;
12281230 defer sema.owner_decl = prev_owner_decl;
12291231
1230 const prev_namespace = sema.namespace;
1231 sema.namespace = &enum_obj.namespace;
1232 defer sema.namespace = prev_namespace;
1233
12341232 const prev_owner_func = sema.owner_func;
12351233 sema.owner_func = null;
12361234 defer sema.owner_func = prev_owner_func;
......@@ -1385,7 +1383,7 @@ fn zirUnionDecl(
13851383 .layout = small.layout,
13861384 .status = .none,
13871385 .namespace = .{
1388 .parent = sema.owner_decl.namespace,
1386 .parent = block.src_decl.namespace,
13891387 .ty = union_ty,
13901388 .file_scope = block.getFileScope(),
13911389 },
......@@ -1396,6 +1394,8 @@ fn zirUnionDecl(
13961394
13971395 _ = try sema.mod.scanNamespace(&union_obj.namespace, extra_index, decls_len, new_decl);
13981396
1397 new_decl.namespace = &union_obj.namespace;
1398
13991399 try sema.types_pending_resolution.ensureUnusedCapacity(sema.gpa, 1);
14001400 try new_decl.finalizeNewArena(&new_decl_arena);
14011401 sema.types_pending_resolution.appendAssumeCapacity(union_ty);
......@@ -2692,7 +2692,7 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr
26922692}
26932693
26942694fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {
2695 var namespace = sema.namespace;
2695 var namespace = block.src_decl.namespace;
26962696 while (true) {
26972697 if (try sema.lookupInNamespace(block, src, namespace, name, false)) |decl| {
26982698 return decl;
......@@ -3010,10 +3010,6 @@ fn analyzeCall(
30103010 sema.inst_map = parent_inst_map;
30113011 }
30123012
3013 const parent_namespace = sema.namespace;
3014 sema.namespace = module_fn.owner_decl.namespace;
3015 defer sema.namespace = parent_namespace;
3016
30173013 const parent_func = sema.func;
30183014 sema.func = module_fn;
30193015 defer sema.func = parent_func;
......@@ -3276,12 +3272,12 @@ fn analyzeCall(
32763272
32773273 // Create a Decl for the new function.
32783274 const src_decl = namespace.getDecl();
3279 const new_decl = try mod.allocateNewDecl(namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
32803275 // TODO better names for generic function instantiations
32813276 const name_index = mod.getNextAnonNameIndex();
3282 new_decl.name = try std.fmt.allocPrintZ(gpa, "{s}__anon_{d}", .{
3277 const decl_name = try std.fmt.allocPrintZ(gpa, "{s}__anon_{d}", .{
32833278 module_fn.owner_decl.name, name_index,
32843279 });
3280 const new_decl = try mod.allocateNewDecl(decl_name, namespace, module_fn.owner_decl.src_node, src_decl.src_scope);
32853281 new_decl.src_line = module_fn.owner_decl.src_line;
32863282 new_decl.is_pub = module_fn.owner_decl.is_pub;
32873283 new_decl.is_exported = module_fn.owner_decl.is_exported;
......@@ -3311,7 +3307,6 @@ fn analyzeCall(
33113307 .perm_arena = &new_decl_arena.allocator,
33123308 .code = fn_zir,
33133309 .owner_decl = new_decl,
3314 .namespace = namespace,
33153310 .func = null,
33163311 .fn_ret_ty = Type.initTag(.void),
33173312 .owner_func = null,
......@@ -11809,7 +11804,7 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
1180911804 switch (ty.tag()) {
1181011805 .@"struct" => {
1181111806 const struct_obj = ty.castTag(.@"struct").?.data;
11812 if (struct_obj.owner_decl.namespace != sema.owner_decl.namespace) return;
11807 if (struct_obj.owner_decl.namespace.parent != sema.owner_decl.namespace) return;
1181311808 switch (struct_obj.status) {
1181411809 .none => {},
1181511810 .field_types_wip => {
......@@ -11817,10 +11812,6 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
1181711812 },
1181811813 .have_field_types, .have_layout, .layout_wip => return,
1181911814 }
11820 const prev_namespace = sema.namespace;
11821 sema.namespace = &struct_obj.namespace;
11822 defer sema.namespace = prev_namespace;
11823
1182411815 const old_src = block.src_decl;
1182511816 defer block.src_decl = old_src;
1182611817 block.src_decl = struct_obj.owner_decl;
......@@ -11831,7 +11822,7 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
1183111822 },
1183211823 .@"union", .union_tagged => {
1183311824 const union_obj = ty.cast(Type.Payload.Union).?.data;
11834 if (union_obj.owner_decl.namespace != sema.owner_decl.namespace) return;
11825 if (union_obj.owner_decl.namespace.parent != sema.owner_decl.namespace) return;
1183511826 switch (union_obj.status) {
1183611827 .none => {},
1183711828 .field_types_wip => {
......@@ -11839,10 +11830,6 @@ pub fn resolveDeclFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty:
1183911830 },
1184011831 .have_field_types, .have_layout, .layout_wip => return,
1184111832 }
11842 const prev_namespace = sema.namespace;
11843 sema.namespace = &union_obj.namespace;
11844 defer sema.namespace = prev_namespace;
11845
1184611833 const old_src = block.src_decl;
1184711834 defer block.src_decl = old_src;
1184811835 block.src_decl = union_obj.owner_decl;