From 7378ce67dabf996f2d0927138f826dfb3d6fa05f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 23 Mar 2022 18:45:51 -0700 Subject: Sema: introduce a type resolution queue That happens after a function body is analyzed. This prevents circular dependency compile errors and yet a way to mark types that need to be fully resolved before a given function is sent to the codegen backend. --- src/Module.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index 7b27546f52..79d6343949 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -4837,6 +4837,9 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem // Finally we must resolve the return type and parameter types so that backends // have full access to type information. + // Crucially, this happens *after* we set the function state to success above, + // so that dependencies on the function body will now be satisfied rather than + // result in circular dependency errors. const src: LazySrcLoc = .{ .node_offset = 0 }; sema.resolveFnTypes(&inner_block, src, fn_ty_info) catch |err| switch (err) { error.NeededSourceLocation => unreachable, @@ -4847,6 +4850,20 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn, arena: Allocator) Sem else => |e| return e, }; + // Similarly, resolve any queued up types that were requested to be resolved for + // the backends. + for (sema.types_to_resolve.items) |inst_ref| { + const ty = sema.getTmpAir().getRefType(inst_ref); + sema.resolveTypeFully(&inner_block, src, ty) catch |err| switch (err) { + error.NeededSourceLocation => unreachable, + error.GenericPoison => unreachable, + error.ComptimeReturn => unreachable, + error.ComptimeBreak => unreachable, + error.AnalysisFail => {}, + else => |e| return e, + }; + } + return Air{ .instructions = sema.air_instructions.toOwnedSlice(), .extra = sema.air_extra.toOwnedSlice(gpa), -- cgit v1.2.3