| ... | @@ -131,7 +131,6 @@ const CompileError = Module.CompileError; | ... | @@ -131,7 +131,6 @@ const CompileError = Module.CompileError; |
| 131 | const SemaError = Module.SemaError; | 131 | const SemaError = Module.SemaError; |
| 132 | const Decl = Module.Decl; | 132 | const Decl = Module.Decl; |
| 133 | const CaptureScope = Module.CaptureScope; | 133 | const CaptureScope = Module.CaptureScope; |
| 134 | const WipCaptureScope = Module.WipCaptureScope; | | |
| 135 | const LazySrcLoc = Module.LazySrcLoc; | 134 | const LazySrcLoc = Module.LazySrcLoc; |
| 136 | const RangeSet = @import("RangeSet.zig"); | 135 | const RangeSet = @import("RangeSet.zig"); |
| 137 | const target_util = @import("target.zig"); | 136 | const target_util = @import("target.zig"); |
| ... | @@ -308,7 +307,7 @@ pub const Block = struct { | ... | @@ -308,7 +307,7 @@ pub const Block = struct { |
| 308 | /// used to add a `func_instance` into the `InternPool`. | 307 | /// used to add a `func_instance` into the `InternPool`. |
| 309 | params: std.MultiArrayList(Param) = .{}, | 308 | params: std.MultiArrayList(Param) = .{}, |
| 310 | | 309 | |
| 311 | wip_capture_scope: *CaptureScope, | 310 | wip_capture_scope: CaptureScope.Index, |
| 312 | | 311 | |
| 313 | label: ?*Label = null, | 312 | label: ?*Label = null, |
| 314 | inlining: ?*Inlining, | 313 | inlining: ?*Inlining, |
| ... | @@ -951,21 +950,12 @@ fn analyzeBodyInner( | ... | @@ -951,21 +950,12 @@ fn analyzeBodyInner( |
| 951 | // different values for the same Zir.Inst.Index, so in those cases, we will | 950 | // different values for the same Zir.Inst.Index, so in those cases, we will |
| 952 | // have to create nested capture scopes; see the `.repeat` case below. | 951 | // have to create nested capture scopes; see the `.repeat` case below. |
| 953 | const parent_capture_scope = block.wip_capture_scope; | 952 | const parent_capture_scope = block.wip_capture_scope; |
| 954 | parent_capture_scope.incRef(); | | |
| 955 | var wip_captures: WipCaptureScope = .{ | | |
| 956 | .scope = parent_capture_scope, | | |
| 957 | .gpa = sema.gpa, | | |
| 958 | .finalized = true, // don't finalize the parent scope | | |
| 959 | }; | | |
| 960 | defer wip_captures.deinit(); | | |
| 961 | | 953 | |
| 962 | const mod = sema.mod; | 954 | const mod = sema.mod; |
| 963 | const map = &sema.inst_map; | 955 | const map = &sema.inst_map; |
| 964 | const tags = sema.code.instructions.items(.tag); | 956 | const tags = sema.code.instructions.items(.tag); |
| 965 | const datas = sema.code.instructions.items(.data); | 957 | const datas = sema.code.instructions.items(.data); |
| 966 | | 958 | |
| 967 | var orig_captures: usize = parent_capture_scope.captures.count(); | | |
| 968 | | | |
| 969 | var crash_info = crash_report.prepAnalyzeBody(sema, block, body); | 959 | var crash_info = crash_report.prepAnalyzeBody(sema, block, body); |
| 970 | crash_info.push(); | 960 | crash_info.push(); |
| 971 | defer crash_info.pop(); | 961 | defer crash_info.pop(); |
| ... | @@ -1500,16 +1490,11 @@ fn analyzeBodyInner( | ... | @@ -1500,16 +1490,11 @@ fn analyzeBodyInner( |
| 1500 | // Send comptime control flow back to the beginning of this block. | 1490 | // Send comptime control flow back to the beginning of this block. |
| 1501 | const src = LazySrcLoc.nodeOffset(datas[inst].node); | 1491 | const src = LazySrcLoc.nodeOffset(datas[inst].node); |
| 1502 | try sema.emitBackwardBranch(block, src); | 1492 | try sema.emitBackwardBranch(block, src); |
| 1503 | if (wip_captures.scope.captures.count() != orig_captures) { | 1493 | |
| 1504 | // We need to construct new capture scopes for the next loop iteration so it | 1494 | // We need to construct new capture scopes for the next loop iteration so it |
| 1505 | // can capture values without clobbering the earlier iteration's captures. | 1495 | // can capture values without clobbering the earlier iteration's captures. |
| 1506 | // At first, we reused the parent capture scope as an optimization, but for | 1496 | block.wip_capture_scope = try mod.createCaptureScope(parent_capture_scope); |
| 1507 | // successive scopes we have to create new ones as children of the parent | 1497 | |
| 1508 | // scope. | | |
| 1509 | try wip_captures.reset(parent_capture_scope); | | |
| 1510 | block.wip_capture_scope = wip_captures.scope; | | |
| 1511 | orig_captures = 0; | | |
| 1512 | } | | |
| 1513 | i = 0; | 1498 | i = 0; |
| 1514 | continue; | 1499 | continue; |
| 1515 | } else { | 1500 | } else { |
| ... | @@ -1520,16 +1505,11 @@ fn analyzeBodyInner( | ... | @@ -1520,16 +1505,11 @@ fn analyzeBodyInner( |
| 1520 | // Send comptime control flow back to the beginning of this block. | 1505 | // Send comptime control flow back to the beginning of this block. |
| 1521 | const src = LazySrcLoc.nodeOffset(datas[inst].node); | 1506 | const src = LazySrcLoc.nodeOffset(datas[inst].node); |
| 1522 | try sema.emitBackwardBranch(block, src); | 1507 | try sema.emitBackwardBranch(block, src); |
| 1523 | if (wip_captures.scope.captures.count() != orig_captures) { | 1508 | |
| 1524 | // We need to construct new capture scopes for the next loop iteration so it | 1509 | // We need to construct new capture scopes for the next loop iteration so it |
| 1525 | // can capture values without clobbering the earlier iteration's captures. | 1510 | // can capture values without clobbering the earlier iteration's captures. |
| 1526 | // At first, we reused the parent capture scope as an optimization, but for | 1511 | block.wip_capture_scope = try mod.createCaptureScope(parent_capture_scope); |
| 1527 | // successive scopes we have to create new ones as children of the parent | 1512 | |
| 1528 | // scope. | | |
| 1529 | try wip_captures.reset(parent_capture_scope); | | |
| 1530 | block.wip_capture_scope = wip_captures.scope; | | |
| 1531 | orig_captures = 0; | | |
| 1532 | } | | |
| 1533 | i = 0; | 1513 | i = 0; |
| 1534 | continue; | 1514 | continue; |
| 1535 | }, | 1515 | }, |
| ... | @@ -1803,12 +1783,9 @@ fn analyzeBodyInner( | ... | @@ -1803,12 +1783,9 @@ fn analyzeBodyInner( |
| 1803 | } | 1783 | } |
| 1804 | if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some); | 1784 | if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some); |
| 1805 | | 1785 | |
| 1806 | if (!wip_captures.finalized) { | 1786 | // We may have overwritten the capture scope due to a `repeat` instruction where |
| 1807 | // We've updated the capture scope due to a `repeat` instruction where | 1787 | // the body had a capture; restore it now. |
| 1808 | // the body had a capture; finalize our child scope and reset | 1788 | block.wip_capture_scope = parent_capture_scope; |
| 1809 | try wip_captures.finalize(); | | |
| 1810 | block.wip_capture_scope = parent_capture_scope; | | |
| 1811 | } | | |
| 1812 | | 1789 | |
| 1813 | return result; | 1790 | return result; |
| 1814 | } | 1791 | } |
| ... | @@ -3157,15 +3134,12 @@ fn zirEnumDecl( | ... | @@ -3157,15 +3134,12 @@ fn zirEnumDecl( |
| 3157 | sema.func_index = .none; | 3134 | sema.func_index = .none; |
| 3158 | defer sema.func_index = prev_func_index; | 3135 | defer sema.func_index = prev_func_index; |
| 3159 | | 3136 | |
| 3160 | var wip_captures = try WipCaptureScope.init(gpa, new_decl.src_scope); | | |
| 3161 | defer wip_captures.deinit(); | | |
| 3162 | | | |
| 3163 | var enum_block: Block = .{ | 3137 | var enum_block: Block = .{ |
| 3164 | .parent = null, | 3138 | .parent = null, |
| 3165 | .sema = sema, | 3139 | .sema = sema, |
| 3166 | .src_decl = new_decl_index, | 3140 | .src_decl = new_decl_index, |
| 3167 | .namespace = new_namespace_index, | 3141 | .namespace = new_namespace_index, |
| 3168 | .wip_capture_scope = wip_captures.scope, | 3142 | .wip_capture_scope = try mod.createCaptureScope(new_decl.src_scope), |
| 3169 | .instructions = .{}, | 3143 | .instructions = .{}, |
| 3170 | .inlining = null, | 3144 | .inlining = null, |
| 3171 | .is_comptime = true, | 3145 | .is_comptime = true, |
| ... | @@ -3176,8 +3150,6 @@ fn zirEnumDecl( | ... | @@ -3176,8 +3150,6 @@ fn zirEnumDecl( |
| 3176 | try sema.analyzeBody(&enum_block, body); | 3150 | try sema.analyzeBody(&enum_block, body); |
| 3177 | } | 3151 | } |
| 3178 | | 3152 | |
| 3179 | try wip_captures.finalize(); | | |
| 3180 | | | |
| 3181 | if (tag_type_ref != .none) { | 3153 | if (tag_type_ref != .none) { |
| 3182 | const ty = try sema.resolveType(block, tag_ty_src, tag_type_ref); | 3154 | const ty = try sema.resolveType(block, tag_ty_src, tag_type_ref); |
| 3183 | if (ty.zigTypeTag(mod) != .Int and ty.zigTypeTag(mod) != .ComptimeInt) { | 3155 | if (ty.zigTypeTag(mod) != .Int and ty.zigTypeTag(mod) != .ComptimeInt) { |
| ... | @@ -7298,15 +7270,12 @@ fn analyzeCall( | ... | @@ -7298,15 +7270,12 @@ fn analyzeCall( |
| 7298 | | 7270 | |
| 7299 | try mod.declareDeclDependencyType(ics.callee().owner_decl_index, module_fn.owner_decl, .function_body); | 7271 | try mod.declareDeclDependencyType(ics.callee().owner_decl_index, module_fn.owner_decl, .function_body); |
| 7300 | | 7272 | |
| 7301 | var wip_captures = try WipCaptureScope.init(gpa, fn_owner_decl.src_scope); | | |
| 7302 | defer wip_captures.deinit(); | | |
| 7303 | | | |
| 7304 | var child_block: Block = .{ | 7273 | var child_block: Block = .{ |
| 7305 | .parent = null, | 7274 | .parent = null, |
| 7306 | .sema = sema, | 7275 | .sema = sema, |
| 7307 | .src_decl = module_fn.owner_decl, | 7276 | .src_decl = module_fn.owner_decl, |
| 7308 | .namespace = fn_owner_decl.src_namespace, | 7277 | .namespace = fn_owner_decl.src_namespace, |
| 7309 | .wip_capture_scope = wip_captures.scope, | 7278 | .wip_capture_scope = try mod.createCaptureScope(fn_owner_decl.src_scope), |
| 7310 | .instructions = .{}, | 7279 | .instructions = .{}, |
| 7311 | .label = null, | 7280 | .label = null, |
| 7312 | .inlining = &inlining, | 7281 | .inlining = &inlining, |
| ... | @@ -7514,8 +7483,6 @@ fn analyzeCall( | ... | @@ -7514,8 +7483,6 @@ fn analyzeCall( |
| 7514 | break :res2 result; | 7483 | break :res2 result; |
| 7515 | }; | 7484 | }; |
| 7516 | | 7485 | |
| 7517 | try wip_captures.finalize(); | | |
| 7518 | | | |
| 7519 | break :res res2; | 7486 | break :res res2; |
| 7520 | } else res: { | 7487 | } else res: { |
| 7521 | assert(!func_ty_info.is_generic); | 7488 | assert(!func_ty_info.is_generic); |
| ... | @@ -7840,15 +7807,12 @@ fn instantiateGenericCall( | ... | @@ -7840,15 +7807,12 @@ fn instantiateGenericCall( |
| 7840 | }; | 7807 | }; |
| 7841 | defer child_sema.deinit(); | 7808 | defer child_sema.deinit(); |
| 7842 | | 7809 | |
| 7843 | var wip_captures = try WipCaptureScope.init(gpa, sema.owner_decl.src_scope); | | |
| 7844 | defer wip_captures.deinit(); | | |
| 7845 | | | |
| 7846 | var child_block: Block = .{ | 7810 | var child_block: Block = .{ |
| 7847 | .parent = null, | 7811 | .parent = null, |
| 7848 | .sema = &child_sema, | 7812 | .sema = &child_sema, |
| 7849 | .src_decl = generic_owner_func.owner_decl, | 7813 | .src_decl = generic_owner_func.owner_decl, |
| 7850 | .namespace = namespace_index, | 7814 | .namespace = namespace_index, |
| 7851 | .wip_capture_scope = wip_captures.scope, | 7815 | .wip_capture_scope = try mod.createCaptureScope(sema.owner_decl.src_scope), |
| 7852 | .instructions = .{}, | 7816 | .instructions = .{}, |
| 7853 | .inlining = null, | 7817 | .inlining = null, |
| 7854 | .is_comptime = true, | 7818 | .is_comptime = true, |
| ... | @@ -8000,8 +7964,6 @@ fn instantiateGenericCall( | ... | @@ -8000,8 +7964,6 @@ fn instantiateGenericCall( |
| 8000 | const func_ty = callee.ty.toType(); | 7964 | const func_ty = callee.ty.toType(); |
| 8001 | const func_ty_info = mod.typeToFunc(func_ty).?; | 7965 | const func_ty_info = mod.typeToFunc(func_ty).?; |
| 8002 | | 7966 | |
| 8003 | try wip_captures.finalize(); | | |
| 8004 | | | |
| 8005 | // If the call evaluated to a return type that requires comptime, never mind | 7967 | // If the call evaluated to a return type that requires comptime, never mind |
| 8006 | // our generic instantiation. Instead we need to perform a comptime call. | 7968 | // our generic instantiation. Instead we need to perform a comptime call. |
| 8007 | if (try sema.typeRequiresComptime(func_ty_info.return_type.toType())) { | 7969 | if (try sema.typeRequiresComptime(func_ty_info.return_type.toType())) { |
| ... | @@ -11897,11 +11859,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r | ... | @@ -11897,11 +11859,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 11897 | const body = sema.code.extra[extra_index..][0..info.body_len]; | 11859 | const body = sema.code.extra[extra_index..][0..info.body_len]; |
| 11898 | extra_index += info.body_len; | 11860 | extra_index += info.body_len; |
| 11899 | | 11861 | |
| 11900 | var wip_captures = try WipCaptureScope.init(gpa, child_block.wip_capture_scope); | | |
| 11901 | defer wip_captures.deinit(); | | |
| 11902 | | | |
| 11903 | case_block.instructions.shrinkRetainingCapacity(0); | 11862 | case_block.instructions.shrinkRetainingCapacity(0); |
| 11904 | case_block.wip_capture_scope = wip_captures.scope; | 11863 | case_block.wip_capture_scope = try mod.createCaptureScope(child_block.wip_capture_scope); |
| 11905 | | 11864 | |
| 11906 | const item = case_vals.items[scalar_i]; | 11865 | const item = case_vals.items[scalar_i]; |
| 11907 | // `item` is already guaranteed to be constant known. | 11866 | // `item` is already guaranteed to be constant known. |
| ... | @@ -11929,8 +11888,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r | ... | @@ -11929,8 +11888,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 11929 | _ = try case_block.addNoOp(.unreach); | 11888 | _ = try case_block.addNoOp(.unreach); |
| 11930 | } | 11889 | } |
| 11931 | | 11890 | |
| 11932 | try wip_captures.finalize(); | | |
| 11933 | | | |
| 11934 | try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); | 11891 | try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len); |
| 11935 | cases_extra.appendAssumeCapacity(1); // items_len | 11892 | cases_extra.appendAssumeCapacity(1); // items_len |
| 11936 | cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); | 11893 | cases_extra.appendAssumeCapacity(@intCast(case_block.instructions.items.len)); |
| ... | @@ -12177,11 +12134,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r | ... | @@ -12177,11 +12134,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12177 | var cond_body = try case_block.instructions.toOwnedSlice(gpa); | 12134 | var cond_body = try case_block.instructions.toOwnedSlice(gpa); |
| 12178 | defer gpa.free(cond_body); | 12135 | defer gpa.free(cond_body); |
| 12179 | | 12136 | |
| 12180 | var wip_captures = try WipCaptureScope.init(gpa, child_block.wip_capture_scope); | | |
| 12181 | defer wip_captures.deinit(); | | |
| 12182 | | | |
| 12183 | case_block.instructions.shrinkRetainingCapacity(0); | 12137 | case_block.instructions.shrinkRetainingCapacity(0); |
| 12184 | case_block.wip_capture_scope = wip_captures.scope; | 12138 | case_block.wip_capture_scope = try mod.createCaptureScope(child_block.wip_capture_scope); |
| 12185 | | 12139 | |
| 12186 | const body = sema.code.extra[extra_index..][0..info.body_len]; | 12140 | const body = sema.code.extra[extra_index..][0..info.body_len]; |
| 12187 | extra_index += info.body_len; | 12141 | extra_index += info.body_len; |
| ... | @@ -12200,8 +12154,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r | ... | @@ -12200,8 +12154,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12200 | ); | 12154 | ); |
| 12201 | } | 12155 | } |
| 12202 | | 12156 | |
| 12203 | try wip_captures.finalize(); | | |
| 12204 | | | |
| 12205 | if (is_first) { | 12157 | if (is_first) { |
| 12206 | is_first = false; | 12158 | is_first = false; |
| 12207 | first_else_body = cond_body; | 12159 | first_else_body = cond_body; |
| ... | @@ -12407,11 +12359,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r | ... | @@ -12407,11 +12359,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12407 | }), | 12359 | }), |
| 12408 | }; | 12360 | }; |
| 12409 | | 12361 | |
| 12410 | var wip_captures = try WipCaptureScope.init(gpa, child_block.wip_capture_scope); | | |
| 12411 | defer wip_captures.deinit(); | | |
| 12412 | | | |
| 12413 | case_block.instructions.shrinkRetainingCapacity(0); | 12362 | case_block.instructions.shrinkRetainingCapacity(0); |
| 12414 | case_block.wip_capture_scope = wip_captures.scope; | 12363 | case_block.wip_capture_scope = try mod.createCaptureScope(child_block.wip_capture_scope); |
| 12415 | | 12364 | |
| 12416 | if (mod.backendSupportsFeature(.is_named_enum_value) and special.body.len != 0 and block.wantSafety() and | 12365 | if (mod.backendSupportsFeature(.is_named_enum_value) and special.body.len != 0 and block.wantSafety() and |
| 12417 | operand_ty.zigTypeTag(mod) == .Enum and (!operand_ty.isNonexhaustiveEnum(mod) or union_originally)) | 12366 | operand_ty.zigTypeTag(mod) == .Enum and (!operand_ty.isNonexhaustiveEnum(mod) or union_originally)) |
| ... | @@ -12456,8 +12405,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r | ... | @@ -12456,8 +12405,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12456 | } | 12405 | } |
| 12457 | } | 12406 | } |
| 12458 | | 12407 | |
| 12459 | try wip_captures.finalize(); | | |
| 12460 | | | |
| 12461 | if (is_first) { | 12408 | if (is_first) { |
| 12462 | final_else_body = case_block.instructions.items; | 12409 | final_else_body = case_block.instructions.items; |
| 12463 | } else { | 12410 | } else { |
| ... | @@ -16557,51 +16504,53 @@ fn zirThis( | ... | @@ -16557,51 +16504,53 @@ fn zirThis( |
| 16557 | } | 16504 | } |
| 16558 | | 16505 | |
| 16559 | fn zirClosureCapture(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { | 16506 | fn zirClosureCapture(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| | 16507 | const mod = sema.mod; |
| | 16508 | const gpa = sema.gpa; |
| 16560 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; | 16509 | const inst_data = sema.code.instructions.items(.data)[inst].un_tok; |
| 16561 | // Closures are not necessarily constant values. For example, the | 16510 | // Closures are not necessarily constant values. For example, the |
| 16562 | // code might do something like this: | 16511 | // code might do something like this: |
| 16563 | // fn foo(x: anytype) void { const S = struct {field: @TypeOf(x)}; } | 16512 | // fn foo(x: anytype) void { const S = struct {field: @TypeOf(x)}; } |
| 16564 | // ...in which case the closure_capture instruction has access to a runtime | 16513 | // ...in which case the closure_capture instruction has access to a runtime |
| 16565 | // value only. In such case we preserve the type and use a dummy runtime value. | 16514 | // value only. In such case only the type is saved into the scope. |
| 16566 | const operand = try sema.resolveInst(inst_data.operand); | 16515 | const operand = try sema.resolveInst(inst_data.operand); |
| 16567 | const ty = sema.typeOf(operand); | 16516 | const ty = sema.typeOf(operand); |
| 16568 | const capture: CaptureScope.Capture = blk: { | 16517 | const key: CaptureScope.Key = .{ |
| 16569 | if (try sema.resolveMaybeUndefValAllowVariables(operand)) |val| { | 16518 | .zir_index = inst, |
| 16570 | const ip_index = try val.intern(ty, sema.mod); | 16519 | .index = block.wip_capture_scope, |
| 16571 | break :blk .{ .comptime_val = ip_index }; | | |
| 16572 | } | | |
| 16573 | break :blk .{ .runtime_val = ty.toIntern() }; | | |
| 16574 | }; | 16520 | }; |
| 16575 | try block.wip_capture_scope.captures.putNoClobber(sema.gpa, inst, capture); | 16521 | if (try sema.resolveMaybeUndefValAllowVariables(operand)) |val| { |
| | 16522 | try mod.comptime_capture_scopes.put(gpa, key, try val.intern(ty, mod)); |
| | 16523 | } else { |
| | 16524 | try mod.runtime_capture_scopes.put(gpa, key, ty.toIntern()); |
| | 16525 | } |
| 16576 | } | 16526 | } |
| 16577 | | 16527 | |
| 16578 | fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { | 16528 | fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 16579 | const mod = sema.mod; | 16529 | const mod = sema.mod; |
| 16580 | const ip = &mod.intern_pool; | 16530 | //const ip = &mod.intern_pool; |
| 16581 | const inst_data = sema.code.instructions.items(.data)[inst].inst_node; | 16531 | const inst_data = sema.code.instructions.items(.data)[inst].inst_node; |
| 16582 | var scope: *CaptureScope = mod.declPtr(block.src_decl).src_scope.?; | 16532 | var scope: CaptureScope.Index = mod.declPtr(block.src_decl).src_scope; |
| | 16533 | assert(scope != .none); |
| 16583 | // Note: The target closure must be in this scope list. | 16534 | // Note: The target closure must be in this scope list. |
| 16584 | // If it's not here, the zir is invalid, or the list is broken. | 16535 | // If it's not here, the zir is invalid, or the list is broken. |
| 16585 | const capture = while (true) { | 16536 | const capture_ty = while (true) { |
| 16586 | // Note: We don't need to add a dependency here, because | 16537 | // Note: We don't need to add a dependency here, because |
| 16587 | // decls always depend on their lexical parents. | 16538 | // decls always depend on their lexical parents. |
| 16588 | | 16539 | const key: CaptureScope.Key = .{ |
| 16589 | // Fail this decl if a scope it depended on failed. | 16540 | .zir_index = inst_data.inst, |
| 16590 | if (scope.failed()) { | 16541 | .index = scope, |
| 16591 | if (sema.owner_func_index != .none) { | 16542 | }; |
| 16592 | ip.funcAnalysis(sema.owner_func_index).state = .dependency_failure; | 16543 | if (mod.comptime_capture_scopes.get(key)) |val| |
| 16593 | } else { | 16544 | return Air.internedToRef(val); |
| 16594 | sema.owner_decl.analysis = .dependency_failure; | 16545 | if (mod.runtime_capture_scopes.get(key)) |ty| |
| 16595 | } | 16546 | break ty; |
| 16596 | return error.AnalysisFail; | 16547 | scope = scope.parent(mod); |
| 16597 | } | 16548 | assert(scope != .none); |
| 16598 | if (scope.captures.get(inst_data.inst)) |capture| { | | |
| 16599 | break capture; | | |
| 16600 | } | | |
| 16601 | scope = scope.parent.?; | | |
| 16602 | }; | 16549 | }; |
| 16603 | | 16550 | |
| 16604 | if (capture == .runtime_val and !block.is_typeof and sema.func_index == .none) { | 16551 | // The comptime case is handled already above. Runtime case below. |
| | 16552 | |
| | 16553 | if (!block.is_typeof and sema.func_index == .none) { |
| 16605 | const msg = msg: { | 16554 | const msg = msg: { |
| 16606 | const name = name: { | 16555 | const name = name: { |
| 16607 | const file = sema.owner_decl.getFileScope(mod); | 16556 | const file = sema.owner_decl.getFileScope(mod); |
| ... | @@ -16629,7 +16578,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! | ... | @@ -16629,7 +16578,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 16629 | return sema.failWithOwnedErrorMsg(block, msg); | 16578 | return sema.failWithOwnedErrorMsg(block, msg); |
| 16630 | } | 16579 | } |
| 16631 | | 16580 | |
| 16632 | if (capture == .runtime_val and !block.is_typeof and !block.is_comptime and sema.func_index != .none) { | 16581 | if (!block.is_typeof and !block.is_comptime and sema.func_index != .none) { |
| 16633 | const msg = msg: { | 16582 | const msg = msg: { |
| 16634 | const name = name: { | 16583 | const name = name: { |
| 16635 | const file = sema.owner_decl.getFileScope(mod); | 16584 | const file = sema.owner_decl.getFileScope(mod); |
| ... | @@ -16659,16 +16608,9 @@ fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! | ... | @@ -16659,16 +16608,9 @@ fn zirClosureGet(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 16659 | return sema.failWithOwnedErrorMsg(block, msg); | 16608 | return sema.failWithOwnedErrorMsg(block, msg); |
| 16660 | } | 16609 | } |
| 16661 | | 16610 | |
| 16662 | switch (capture) { | 16611 | assert(block.is_typeof); |
| 16663 | .runtime_val => |ty_ip_index| { | 16612 | // We need a dummy runtime instruction with the correct type. |
| 16664 | assert(block.is_typeof); | 16613 | return block.addTy(.alloc, capture_ty.toType()); |
| 16665 | // We need a dummy runtime instruction with the correct type. | | |
| 16666 | return block.addTy(.alloc, ty_ip_index.toType()); | | |
| 16667 | }, | | |
| 16668 | .comptime_val => |val_ip_index| { | | |
| 16669 | return Air.internedToRef(val_ip_index); | | |
| 16670 | }, | | |
| 16671 | } | | |
| 16672 | } | 16614 | } |
| 16673 | | 16615 | |
| 16674 | fn zirRetAddr( | 16616 | fn zirRetAddr( |
| ... | @@ -24988,7 +24930,7 @@ fn zirBuiltinExtern( | ... | @@ -24988,7 +24930,7 @@ fn zirBuiltinExtern( |
| 24988 | | 24930 | |
| 24989 | // TODO check duplicate extern | 24931 | // TODO check duplicate extern |
| 24990 | | 24932 | |
| 24991 | const new_decl_index = try mod.allocateNewDecl(sema.owner_decl.src_namespace, sema.owner_decl.src_node, null); | 24933 | const new_decl_index = try mod.allocateNewDecl(sema.owner_decl.src_namespace, sema.owner_decl.src_node, .none); |
| 24992 | errdefer mod.destroyDecl(new_decl_index); | 24934 | errdefer mod.destroyDecl(new_decl_index); |
| 24993 | const new_decl = mod.declPtr(new_decl_index); | 24935 | const new_decl = mod.declPtr(new_decl_index); |
| 24994 | new_decl.name = options.name; | 24936 | new_decl.name = options.name; |
| ... | @@ -34327,15 +34269,12 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi | ... | @@ -34327,15 +34269,12 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi |
| 34327 | }; | 34269 | }; |
| 34328 | defer sema.deinit(); | 34270 | defer sema.deinit(); |
| 34329 | | 34271 | |
| 34330 | var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope); | | |
| 34331 | defer wip_captures.deinit(); | | |
| 34332 | | | |
| 34333 | var block: Block = .{ | 34272 | var block: Block = .{ |
| 34334 | .parent = null, | 34273 | .parent = null, |
| 34335 | .sema = &sema, | 34274 | .sema = &sema, |
| 34336 | .src_decl = decl_index, | 34275 | .src_decl = decl_index, |
| 34337 | .namespace = struct_obj.namespace, | 34276 | .namespace = struct_obj.namespace, |
| 34338 | .wip_capture_scope = wip_captures.scope, | 34277 | .wip_capture_scope = try mod.createCaptureScope(decl.src_scope), |
| 34339 | .instructions = .{}, | 34278 | .instructions = .{}, |
| 34340 | .inlining = null, | 34279 | .inlining = null, |
| 34341 | .is_comptime = true, | 34280 | .is_comptime = true, |
| ... | @@ -34356,7 +34295,6 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi | ... | @@ -34356,7 +34295,6 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi |
| 34356 | | 34295 | |
| 34357 | try sema.checkBackingIntType(&block, backing_int_src, backing_int_ty, fields_bit_sum); | 34296 | try sema.checkBackingIntType(&block, backing_int_src, backing_int_ty, fields_bit_sum); |
| 34358 | struct_obj.backing_int_ty = backing_int_ty; | 34297 | struct_obj.backing_int_ty = backing_int_ty; |
| 34359 | try wip_captures.finalize(); | | |
| 34360 | for (comptime_mutable_decls.items) |ct_decl_index| { | 34298 | for (comptime_mutable_decls.items) |ct_decl_index| { |
| 34361 | const ct_decl = mod.declPtr(ct_decl_index); | 34299 | const ct_decl = mod.declPtr(ct_decl_index); |
| 34362 | _ = try ct_decl.internValue(mod); | 34300 | _ = try ct_decl.internValue(mod); |
| ... | @@ -35018,15 +34956,12 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void | ... | @@ -35018,15 +34956,12 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void |
| 35018 | }; | 34956 | }; |
| 35019 | defer sema.deinit(); | 34957 | defer sema.deinit(); |
| 35020 | | 34958 | |
| 35021 | var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope); | | |
| 35022 | defer wip_captures.deinit(); | | |
| 35023 | | | |
| 35024 | var block_scope: Block = .{ | 34959 | var block_scope: Block = .{ |
| 35025 | .parent = null, | 34960 | .parent = null, |
| 35026 | .sema = &sema, | 34961 | .sema = &sema, |
| 35027 | .src_decl = decl_index, | 34962 | .src_decl = decl_index, |
| 35028 | .namespace = struct_obj.namespace, | 34963 | .namespace = struct_obj.namespace, |
| 35029 | .wip_capture_scope = wip_captures.scope, | 34964 | .wip_capture_scope = try mod.createCaptureScope(decl.src_scope), |
| 35030 | .instructions = .{}, | 34965 | .instructions = .{}, |
| 35031 | .inlining = null, | 34966 | .inlining = null, |
| 35032 | .is_comptime = true, | 34967 | .is_comptime = true, |
| ... | @@ -35283,7 +35218,6 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void | ... | @@ -35283,7 +35218,6 @@ fn semaStructFields(mod: *Module, struct_obj: *Module.Struct) CompileError!void |
| 35283 | } | 35218 | } |
| 35284 | } | 35219 | } |
| 35285 | } | 35220 | } |
| 35286 | try wip_captures.finalize(); | | |
| 35287 | for (comptime_mutable_decls.items) |ct_decl_index| { | 35221 | for (comptime_mutable_decls.items) |ct_decl_index| { |
| 35288 | const ct_decl = mod.declPtr(ct_decl_index); | 35222 | const ct_decl = mod.declPtr(ct_decl_index); |
| 35289 | _ = try ct_decl.internValue(mod); | 35223 | _ = try ct_decl.internValue(mod); |
| ... | @@ -35361,15 +35295,12 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un | ... | @@ -35361,15 +35295,12 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un |
| 35361 | }; | 35295 | }; |
| 35362 | defer sema.deinit(); | 35296 | defer sema.deinit(); |
| 35363 | | 35297 | |
| 35364 | var wip_captures = try WipCaptureScope.init(gpa, decl.src_scope); | | |
| 35365 | defer wip_captures.deinit(); | | |
| 35366 | | | |
| 35367 | var block_scope: Block = .{ | 35298 | var block_scope: Block = .{ |
| 35368 | .parent = null, | 35299 | .parent = null, |
| 35369 | .sema = &sema, | 35300 | .sema = &sema, |
| 35370 | .src_decl = decl_index, | 35301 | .src_decl = decl_index, |
| 35371 | .namespace = union_type.namespace, | 35302 | .namespace = union_type.namespace, |
| 35372 | .wip_capture_scope = wip_captures.scope, | 35303 | .wip_capture_scope = try mod.createCaptureScope(decl.src_scope), |
| 35373 | .instructions = .{}, | 35304 | .instructions = .{}, |
| 35374 | .inlining = null, | 35305 | .inlining = null, |
| 35375 | .is_comptime = true, | 35306 | .is_comptime = true, |
| ... | @@ -35380,7 +35311,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un | ... | @@ -35380,7 +35311,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un |
| 35380 | try sema.analyzeBody(&block_scope, body); | 35311 | try sema.analyzeBody(&block_scope, body); |
| 35381 | } | 35312 | } |
| 35382 | | 35313 | |
| 35383 | try wip_captures.finalize(); | | |
| 35384 | for (comptime_mutable_decls.items) |ct_decl_index| { | 35314 | for (comptime_mutable_decls.items) |ct_decl_index| { |
| 35385 | const ct_decl = mod.declPtr(ct_decl_index); | 35315 | const ct_decl = mod.declPtr(ct_decl_index); |
| 35386 | _ = try ct_decl.internValue(mod); | 35316 | _ = try ct_decl.internValue(mod); |
| ... | @@ -35823,18 +35753,16 @@ fn generateUnionTagTypeSimple( | ... | @@ -35823,18 +35753,16 @@ fn generateUnionTagTypeSimple( |
| 35823 | } | 35753 | } |
| 35824 | | 35754 | |
| 35825 | fn getBuiltin(sema: *Sema, name: []const u8) CompileError!Air.Inst.Ref { | 35755 | fn getBuiltin(sema: *Sema, name: []const u8) CompileError!Air.Inst.Ref { |
| | 35756 | const mod = sema.mod; |
| 35826 | const gpa = sema.gpa; | 35757 | const gpa = sema.gpa; |
| 35827 | const src = LazySrcLoc.nodeOffset(0); | 35758 | const src = LazySrcLoc.nodeOffset(0); |
| 35828 | | 35759 | |
| 35829 | var wip_captures = try WipCaptureScope.init(gpa, sema.owner_decl.src_scope); | | |
| 35830 | defer wip_captures.deinit(); | | |
| 35831 | | | |
| 35832 | var block: Block = .{ | 35760 | var block: Block = .{ |
| 35833 | .parent = null, | 35761 | .parent = null, |
| 35834 | .sema = sema, | 35762 | .sema = sema, |
| 35835 | .src_decl = sema.owner_decl_index, | 35763 | .src_decl = sema.owner_decl_index, |
| 35836 | .namespace = sema.owner_decl.src_namespace, | 35764 | .namespace = sema.owner_decl.src_namespace, |
| 35837 | .wip_capture_scope = wip_captures.scope, | 35765 | .wip_capture_scope = try mod.createCaptureScope(sema.owner_decl.src_scope), |
| 35838 | .instructions = .{}, | 35766 | .instructions = .{}, |
| 35839 | .inlining = null, | 35767 | .inlining = null, |
| 35840 | .is_comptime = true, | 35768 | .is_comptime = true, |
| ... | @@ -35875,17 +35803,15 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Mod | ... | @@ -35875,17 +35803,15 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Mod |
| 35875 | } | 35803 | } |
| 35876 | | 35804 | |
| 35877 | fn getBuiltinType(sema: *Sema, name: []const u8) CompileError!Type { | 35805 | fn getBuiltinType(sema: *Sema, name: []const u8) CompileError!Type { |
| | 35806 | const mod = sema.mod; |
| 35878 | const ty_inst = try sema.getBuiltin(name); | 35807 | const ty_inst = try sema.getBuiltin(name); |
| 35879 | | 35808 | |
| 35880 | var wip_captures = try WipCaptureScope.init(sema.gpa, sema.owner_decl.src_scope); | | |
| 35881 | defer wip_captures.deinit(); | | |
| 35882 | | | |
| 35883 | var block: Block = .{ | 35809 | var block: Block = .{ |
| 35884 | .parent = null, | 35810 | .parent = null, |
| 35885 | .sema = sema, | 35811 | .sema = sema, |
| 35886 | .src_decl = sema.owner_decl_index, | 35812 | .src_decl = sema.owner_decl_index, |
| 35887 | .namespace = sema.owner_decl.src_namespace, | 35813 | .namespace = sema.owner_decl.src_namespace, |
| 35888 | .wip_capture_scope = wip_captures.scope, | 35814 | .wip_capture_scope = try mod.createCaptureScope(sema.owner_decl.src_scope), |
| 35889 | .instructions = .{}, | 35815 | .instructions = .{}, |
| 35890 | .inlining = null, | 35816 | .inlining = null, |
| 35891 | .is_comptime = true, | 35817 | .is_comptime = true, |