authorgravatar for spexguy070@gmail.comMartin Wickham <spexguy070@gmail.com> 2021-09-23 18:09:35-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-24 13:49:18-04:00
log1e7009a9d982faa466517063452ed7d299b66966
tree1d784134a0a81c8292101427bd440cb4c89aa6d4
parent8f58e2d77951cdb046e365394c5f02c9b3a93a4f

Fix error references across inline and comptime functions


2 files changed, 24 insertions(+), 28 deletions(-)

src/Module.zig+22-26
......@@ -248,6 +248,9 @@ pub const Export = struct {
248248 link: link.File.Export,
249249 /// The Decl that performs the export. Note that this is *not* the Decl being exported.
250250 owner_decl: *Decl,
251 /// The Decl containing the export statement. Inline function calls
252 /// may cause this to be different from the owner_decl.
253 src_decl: *Decl,
251254 /// The Decl being exported. Note this is *not* the Decl performing the export.
252255 exported_decl: *Decl,
253256 status: enum {
......@@ -261,8 +264,8 @@ pub const Export = struct {
261264
262265 pub fn getSrcLoc(exp: Export) SrcLoc {
263266 return .{
264 .file_scope = exp.owner_decl.namespace.file_scope,
265 .parent_decl_node = exp.owner_decl.src_node,
267 .file_scope = exp.src_decl.namespace.file_scope,
268 .parent_decl_node = exp.src_decl.src_node,
266269 .lazy = exp.src,
267270 };
268271 }
......@@ -1014,15 +1017,6 @@ pub const Scope = struct {
10141017 return @fieldParentPtr(T, "base", base);
10151018 }
10161019
1017 /// Get the decl that is currently being analyzed
1018 pub fn ownerDecl(scope: *Scope) ?*Decl {
1019 return switch (scope.tag) {
1020 .block => scope.cast(Block).?.sema.owner_decl,
1021 .file => null,
1022 .namespace => null,
1023 };
1024 }
1025
10261020 /// Get the decl which contains this decl, for the purposes of source reporting
10271021 pub fn srcDecl(scope: *Scope) ?*Decl {
10281022 return switch (scope.tag) {
......@@ -3402,7 +3396,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
34023396 }
34033397 // The scope needs to have the decl in it.
34043398 const options: std.builtin.ExportOptions = .{ .name = mem.spanZ(decl.name) };
3405 try mod.analyzeExport(&block_scope.base, export_src, options, decl);
3399 try mod.analyzeExport(&block_scope, export_src, options, decl);
34063400 }
34073401 return type_changed or is_inline != prev_is_inline;
34083402 }
......@@ -3462,7 +3456,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
34623456 const export_src = src; // TODO point to the export token
34633457 // The scope needs to have the decl in it.
34643458 const options: std.builtin.ExportOptions = .{ .name = mem.spanZ(decl.name) };
3465 try mod.analyzeExport(&block_scope.base, export_src, options, decl);
3459 try mod.analyzeExport(&block_scope, export_src, options, decl);
34663460 }
34673461
34683462 return type_changed;
......@@ -3931,7 +3925,7 @@ pub fn deleteUnusedDecl(mod: *Module, decl: *Decl) void {
39313925
39323926pub fn deleteAnonDecl(mod: *Module, scope: *Scope, decl: *Decl) void {
39333927 log.debug("deleteAnonDecl {*} ({s})", .{ decl, decl.name });
3934 const scope_decl = scope.ownerDecl().?;
3928 const scope_decl = scope.srcDecl().?;
39353929 assert(scope_decl.namespace.anon_decls.swapRemove(decl));
39363930 decl.destroy(mod);
39373931}
......@@ -4209,7 +4203,7 @@ pub fn getErrorValue(mod: *Module, name: []const u8) !std.StringHashMapUnmanaged
42094203
42104204pub fn analyzeExport(
42114205 mod: *Module,
4212 scope: *Scope,
4206 block: *Scope.Block,
42134207 src: LazySrcLoc,
42144208 borrowed_options: std.builtin.ExportOptions,
42154209 exported_decl: *Decl,
......@@ -4217,7 +4211,7 @@ pub fn analyzeExport(
42174211 try mod.ensureDeclAnalyzed(exported_decl);
42184212 switch (exported_decl.ty.zigTypeTag()) {
42194213 .Fn => {},
4220 else => return mod.fail(scope, src, "unable to export type '{}'", .{exported_decl.ty}),
4214 else => return mod.fail(&block.base, src, "unable to export type '{}'", .{exported_decl.ty}),
42214215 }
42224216
42234217 const gpa = mod.gpa;
......@@ -4234,7 +4228,8 @@ pub fn analyzeExport(
42344228 const section: ?[]const u8 = if (borrowed_options.section) |s| try gpa.dupe(u8, s) else null;
42354229 errdefer if (section) |s| gpa.free(s);
42364230
4237 const owner_decl = scope.ownerDecl().?;
4231 const src_decl = block.src_decl;
4232 const owner_decl = block.sema.owner_decl;
42384233
42394234 log.debug("exporting Decl '{s}' as symbol '{s}' from Decl '{s}'", .{
42404235 exported_decl.name, symbol_name, owner_decl.name,
......@@ -4257,6 +4252,7 @@ pub fn analyzeExport(
42574252 .spirv => .{ .spirv = {} },
42584253 },
42594254 .owner_decl = owner_decl,
4255 .src_decl = src_decl,
42604256 .exported_decl = exported_decl,
42614257 .status = .in_progress,
42624258 };
......@@ -4287,38 +4283,38 @@ pub fn createAnonymousDeclNamed(
42874283 typed_value: TypedValue,
42884284 name: [:0]u8,
42894285) !*Decl {
4290 return mod.createAnonymousDeclFromDeclNamed(scope.ownerDecl().?, scope.srcScope(), typed_value, name);
4286 return mod.createAnonymousDeclFromDeclNamed(scope.srcDecl().?, scope.srcScope(), typed_value, name);
42914287}
42924288
42934289pub fn createAnonymousDecl(mod: *Module, scope: *Scope, typed_value: TypedValue) !*Decl {
4294 return mod.createAnonymousDeclFromDecl(scope.ownerDecl().?, scope.srcScope(), typed_value);
4290 return mod.createAnonymousDeclFromDecl(scope.srcDecl().?, scope.srcScope(), typed_value);
42954291}
42964292
4297pub fn createAnonymousDeclFromDecl(mod: *Module, owner_decl: *Decl, src_scope: ?*CaptureScope, tv: TypedValue) !*Decl {
4293pub fn createAnonymousDeclFromDecl(mod: *Module, src_decl: *Decl, src_scope: ?*CaptureScope, tv: TypedValue) !*Decl {
42984294 const name_index = mod.getNextAnonNameIndex();
42994295 const name = try std.fmt.allocPrintZ(mod.gpa, "{s}__anon_{d}", .{
4300 owner_decl.name, name_index,
4296 src_decl.name, name_index,
43014297 });
4302 return mod.createAnonymousDeclFromDeclNamed(owner_decl, src_scope, tv, name);
4298 return mod.createAnonymousDeclFromDeclNamed(src_decl, src_scope, tv, name);
43034299}
43044300
43054301/// Takes ownership of `name` even if it returns an error.
43064302pub fn createAnonymousDeclFromDeclNamed(
43074303 mod: *Module,
4308 owner_decl: *Decl,
4304 src_decl: *Decl,
43094305 src_scope: ?*CaptureScope,
43104306 typed_value: TypedValue,
43114307 name: [:0]u8,
43124308) !*Decl {
43134309 errdefer mod.gpa.free(name);
43144310
4315 const namespace = owner_decl.namespace;
4311 const namespace = src_decl.namespace;
43164312 try namespace.anon_decls.ensureUnusedCapacity(mod.gpa, 1);
43174313
4318 const new_decl = try mod.allocateNewDecl(namespace, owner_decl.src_node, src_scope);
4314 const new_decl = try mod.allocateNewDecl(namespace, src_decl.src_node, src_scope);
43194315
43204316 new_decl.name = name;
4321 new_decl.src_line = owner_decl.src_line;
4317 new_decl.src_line = src_decl.src_line;
43224318 new_decl.ty = typed_value.ty;
43234319 new_decl.val = typed_value.val;
43244320 new_decl.align_val = Value.initTag(.null_value);
src/Sema.zig+2-2
......@@ -2447,7 +2447,7 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErro
24472447 }
24482448 const decl = try sema.lookupIdentifier(block, operand_src, decl_name);
24492449 const options = try sema.resolveExportOptions(block, options_src, extra.options);
2450 try sema.mod.analyzeExport(&block.base, src, options, decl);
2450 try sema.mod.analyzeExport(block, src, options, decl);
24512451}
24522452
24532453fn zirExportValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {
......@@ -2465,7 +2465,7 @@ fn zirExportValue(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Compil
24652465 .function => operand.val.castTag(.function).?.data.owner_decl,
24662466 else => return sema.mod.fail(&block.base, operand_src, "TODO implement exporting arbitrary Value objects", .{}), // TODO put this Value into an anonymous Decl and then export it.
24672467 };
2468 try sema.mod.analyzeExport(&block.base, src, options, decl);
2468 try sema.mod.analyzeExport(block, src, options, decl);
24692469}
24702470
24712471fn zirSetAlignStack(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!void {