authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-05 07:22:47+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-06 21:26:38+00:00
log2c4ac44f25743f5b7ae9db6bc570ab71f15fd83b
tree5936a2c47c13ea1fcd5bd37ce523754517be38cf
parentd0c022f7347b5cda34751a986a535aee3b1f45dc
signaturelock-open Commit is signed but in an unrecognized format.

compiler: treat decl_val/decl_ref of potentially generic decls as captures

This fixes an issue with the implementation of #18816. Consider the following code: ```zig pub fn Wrap(comptime T: type) type { return struct { pub const T1 = T; inner: struct { x: T1 }, }; } ``` Previously, the type of `inner` was not considered to be "capturing" any value, as `T1` is a decl. However, since it is declared within a generic function, this decl reference depends on the context, and thus should be treated as a capture. AstGen has been augmented to tunnel references to decls through closure when the decl was declared in a potentially-generic context (i.e. within a function).

6 files changed, 194 insertions(+), 65 deletions(-)

lib/std/zig/AstGen.zig+90-26
......@@ -44,6 +44,9 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
4444/// The topmost block of the current function.
4545fn_block: ?*GenZir = null,
4646fn_var_args: bool = false,
47/// Whether we are somewhere within a function. If `true`, any container decls may be
48/// generic and thus must be tunneled through closure.
49within_fn: bool = false,
4750/// The return type of the current function. This may be a trivial `Ref`, or
4851/// otherwise it refers to a `ret_type` instruction.
4952fn_ret_ty: Zir.Inst.Ref = .none,
......@@ -4050,6 +4053,11 @@ fn fnDecl(
40504053 };
40514054 defer fn_gz.unstack();
40524055
4056 // Set this now, since parameter types, return type, etc may be generic.
4057 const prev_within_fn = astgen.within_fn;
4058 defer astgen.within_fn = prev_within_fn;
4059 astgen.within_fn = true;
4060
40534061 const is_pub = fn_proto.visib_token != null;
40544062 const is_export = blk: {
40554063 const maybe_export_token = fn_proto.extern_export_inline_token orelse break :blk false;
......@@ -4311,6 +4319,10 @@ fn fnDecl(
43114319
43124320 const prev_fn_block = astgen.fn_block;
43134321 const prev_fn_ret_ty = astgen.fn_ret_ty;
4322 defer {
4323 astgen.fn_block = prev_fn_block;
4324 astgen.fn_ret_ty = prev_fn_ret_ty;
4325 }
43144326 astgen.fn_block = &fn_gz;
43154327 astgen.fn_ret_ty = if (is_inferred_error or ret_ref.toIndex() != null) r: {
43164328 // We're essentially guaranteed to need the return type at some point,
......@@ -4319,10 +4331,6 @@ fn fnDecl(
43194331 // return type now so the rest of the function can use it.
43204332 break :r try fn_gz.addNode(.ret_type, decl_node);
43214333 } else ret_ref;
4322 defer {
4323 astgen.fn_block = prev_fn_block;
4324 astgen.fn_ret_ty = prev_fn_ret_ty;
4325 }
43264334
43274335 const prev_var_args = astgen.fn_var_args;
43284336 astgen.fn_var_args = is_var_args;
......@@ -4768,11 +4776,14 @@ fn testDecl(
47684776 };
47694777 defer fn_block.unstack();
47704778
4779 const prev_within_fn = astgen.within_fn;
47714780 const prev_fn_block = astgen.fn_block;
47724781 const prev_fn_ret_ty = astgen.fn_ret_ty;
4782 astgen.within_fn = true;
47734783 astgen.fn_block = &fn_block;
47744784 astgen.fn_ret_ty = .anyerror_void_error_union_type;
47754785 defer {
4786 astgen.within_fn = prev_within_fn;
47764787 astgen.fn_block = prev_fn_block;
47774788 astgen.fn_ret_ty = prev_fn_ret_ty;
47784789 }
......@@ -4871,6 +4882,7 @@ fn structDeclInner(
48714882 .node = node,
48724883 .inst = decl_inst,
48734884 .declaring_gz = gz,
4885 .maybe_generic = astgen.within_fn,
48744886 };
48754887 defer namespace.deinit(gpa);
48764888
......@@ -5195,6 +5207,7 @@ fn unionDeclInner(
51955207 .node = node,
51965208 .inst = decl_inst,
51975209 .declaring_gz = gz,
5210 .maybe_generic = astgen.within_fn,
51985211 };
51995212 defer namespace.deinit(gpa);
52005213
......@@ -5543,6 +5556,7 @@ fn containerDecl(
55435556 .node = node,
55445557 .inst = decl_inst,
55455558 .declaring_gz = gz,
5559 .maybe_generic = astgen.within_fn,
55465560 };
55475561 defer namespace.deinit(gpa);
55485562
......@@ -5709,6 +5723,7 @@ fn containerDecl(
57095723 .node = node,
57105724 .inst = decl_inst,
57115725 .declaring_gz = gz,
5726 .maybe_generic = astgen.within_fn,
57125727 };
57135728 defer namespace.deinit(gpa);
57145729
......@@ -8247,9 +8262,14 @@ fn localVarRef(
82478262 const name_str_index = try astgen.identAsString(ident_token);
82488263 var s = scope;
82498264 var found_already: ?Ast.Node.Index = null; // we have found a decl with the same name already
8265 var found_needs_tunnel: bool = undefined; // defined when `found_already != null`
8266 var found_namespaces_out: u32 = undefined; // defined when `found_already != null`
8267
8268 // The number of namespaces above `gz` we currently are
82508269 var num_namespaces_out: u32 = 0;
8251 // defined when `num_namespaces_out != 0`
8270 // defined by `num_namespaces_out != 0`
82528271 var capturing_namespace: *Scope.Namespace = undefined;
8272
82538273 while (true) switch (s.tag) {
82548274 .local_val => {
82558275 const local_val = s.cast(Scope.LocalVal).?;
......@@ -8267,9 +8287,8 @@ fn localVarRef(
82678287 gz,
82688288 ident,
82698289 num_namespaces_out,
8270 capturing_namespace,
8271 local_val.inst,
8272 local_val.token_src,
8290 .{ .ref = local_val.inst },
8291 .{ .token = local_val.token_src },
82738292 ) else local_val.inst;
82748293
82758294 return rvalueNoCoercePreRef(gz, ri, value_inst, ident);
......@@ -8298,9 +8317,8 @@ fn localVarRef(
82988317 gz,
82998318 ident,
83008319 num_namespaces_out,
8301 capturing_namespace,
8302 local_ptr.ptr,
8303 local_ptr.token_src,
8320 .{ .ref = local_ptr.ptr },
8321 .{ .token = local_ptr.token_src },
83048322 ) else local_ptr.ptr;
83058323
83068324 switch (ri.rl) {
......@@ -8329,6 +8347,8 @@ fn localVarRef(
83298347 }
83308348 // We found a match but must continue looking for ambiguous references to decls.
83318349 found_already = i;
8350 found_needs_tunnel = ns.maybe_generic;
8351 found_namespaces_out = num_namespaces_out;
83328352 }
83338353 num_namespaces_out += 1;
83348354 capturing_namespace = ns;
......@@ -8343,6 +8363,29 @@ fn localVarRef(
83438363
83448364 // Decl references happen by name rather than ZIR index so that when unrelated
83458365 // decls are modified, ZIR code containing references to them can be unmodified.
8366
8367 if (found_namespaces_out > 0 and found_needs_tunnel) {
8368 switch (ri.rl) {
8369 .ref, .ref_coerced_ty => return tunnelThroughClosure(
8370 gz,
8371 ident,
8372 found_namespaces_out,
8373 .{ .decl_ref = name_str_index },
8374 .{ .node = found_already.? },
8375 ),
8376 else => {
8377 const result = try tunnelThroughClosure(
8378 gz,
8379 ident,
8380 found_namespaces_out,
8381 .{ .decl_val = name_str_index },
8382 .{ .node = found_already.? },
8383 );
8384 return rvalueNoCoercePreRef(gz, ri, result, ident);
8385 },
8386 }
8387 }
8388
83468389 switch (ri.rl) {
83478390 .ref, .ref_coerced_ty => return gz.addStrTok(.decl_ref, name_str_index, ident_token),
83488391 else => {
......@@ -8361,17 +8404,22 @@ fn tunnelThroughClosure(
83618404 inner_ref_node: Ast.Node.Index,
83628405 /// The number of namespaces being tunnelled through. At least 1.
83638406 num_tunnels: u32,
8364 /// The namespace being captured from.
8365 ns: *Scope.Namespace,
83668407 /// The value being captured.
8367 value: Zir.Inst.Ref,
8368 /// The token of the value's declaration.
8369 token: Ast.TokenIndex,
8408 value: union(enum) {
8409 ref: Zir.Inst.Ref,
8410 decl_val: Zir.NullTerminatedString,
8411 decl_ref: Zir.NullTerminatedString,
8412 },
8413 /// The location of the value's declaration.
8414 decl_src: union(enum) {
8415 token: Ast.TokenIndex,
8416 node: Ast.Node.Index,
8417 },
83708418) !Zir.Inst.Ref {
8371 const value_inst = value.toIndex() orelse {
8372 // For trivial values, we don't need a tunnel; just return the ref.
8373 return value;
8374 };
8419 switch (value) {
8420 .ref => |v| if (v.toIndex() == null) return v, // trivia value; do not need tunnel
8421 .decl_val, .decl_ref => {},
8422 }
83758423
83768424 const astgen = gz.astgen;
83778425 const gpa = astgen.gpa;
......@@ -8382,7 +8430,7 @@ fn tunnelThroughClosure(
83828430 var sfba = std.heap.stackFallback(@sizeOf(usize) * 2, astgen.arena);
83838431 var intermediate_tunnels = try sfba.get().alloc(*Scope.Namespace, num_tunnels - 1);
83848432
8385 {
8433 const root_ns = ns: {
83868434 var i: usize = num_tunnels - 1;
83878435 var scope: *Scope = gz.parent;
83888436 while (i > 0) {
......@@ -8392,15 +8440,27 @@ fn tunnelThroughClosure(
83928440 }
83938441 scope = scope.parent().?;
83948442 }
8395 }
8443 while (true) {
8444 if (scope.cast(Scope.Namespace)) |ns| break :ns ns;
8445 scope = scope.parent().?;
8446 }
8447 };
83968448
83978449 // Now that we know the scopes we're tunneling through, begin adding
83988450 // captures as required, starting with the outermost namespace.
8451 const root_capture = Zir.Inst.Capture.wrap(switch (value) {
8452 .ref => |v| .{ .instruction = v.toIndex().? },
8453 .decl_val => |str| .{ .decl_val = str },
8454 .decl_ref => |str| .{ .decl_ref = str },
8455 });
83998456 var cur_capture_index = std.math.cast(
84008457 u16,
8401 (try ns.captures.getOrPut(gpa, Zir.Inst.Capture.wrap(.{ .inst = value_inst }))).index,
8402 ) orelse return astgen.failNodeNotes(ns.node, "this compiler implementation only supports up to 65536 captures per namespace", .{}, &.{
8403 try astgen.errNoteTok(token, "captured value here", .{}),
8458 (try root_ns.captures.getOrPut(gpa, root_capture)).index,
8459 ) orelse return astgen.failNodeNotes(root_ns.node, "this compiler implementation only supports up to 65536 captures per namespace", .{}, &.{
8460 switch (decl_src) {
8461 .token => |t| try astgen.errNoteTok(t, "captured value here", .{}),
8462 .node => |n| try astgen.errNoteNode(n, "captured value here", .{}),
8463 },
84048464 try astgen.errNoteNode(inner_ref_node, "value used here", .{}),
84058465 });
84068466
......@@ -8409,7 +8469,10 @@ fn tunnelThroughClosure(
84098469 u16,
84108470 (try tunnel_ns.captures.getOrPut(gpa, Zir.Inst.Capture.wrap(.{ .nested = cur_capture_index }))).index,
84118471 ) orelse return astgen.failNodeNotes(tunnel_ns.node, "this compiler implementation only supports up to 65536 captures per namespace", .{}, &.{
8412 try astgen.errNoteTok(token, "captured value here", .{}),
8472 switch (decl_src) {
8473 .token => |t| try astgen.errNoteTok(t, "captured value here", .{}),
8474 .node => |n| try astgen.errNoteNode(n, "captured value here", .{}),
8475 },
84138476 try astgen.errNoteNode(inner_ref_node, "value used here", .{}),
84148477 });
84158478 }
......@@ -11752,6 +11815,7 @@ const Scope = struct {
1175211815 decls: std.AutoHashMapUnmanaged(Zir.NullTerminatedString, Ast.Node.Index) = .{},
1175311816 node: Ast.Node.Index,
1175411817 inst: Zir.Inst.Index,
11818 maybe_generic: bool,
1175511819
1175611820 /// The astgen scope containing this namespace.
1175711821 /// Only valid during astgen.
lib/std/zig/Zir.zig+36-12
......@@ -3057,26 +3057,50 @@ pub const Inst = struct {
30573057 };
30583058
30593059 /// Represents a single value being captured in a type declaration's closure.
3060 /// If high bit is 0, this represents a `Zir.Inst,Index`.
3061 /// If high bit is 1, this represents an index into the last closure.
3062 pub const Capture = enum(u32) {
3063 _,
3060 pub const Capture = packed struct(u32) {
3061 tag: enum(u2) {
3062 /// `data` is a `u16` index into the parent closure.
3063 nested,
3064 /// `data` is a `Zir.Inst.Index` to an instruction whose value is being captured.
3065 instruction,
3066 /// `data` is a `NullTerminatedString` to a decl name.
3067 decl_val,
3068 /// `data` is a `NullTerminatedString` to a decl name.
3069 decl_ref,
3070 },
3071 data: u30,
30643072 pub const Unwrapped = union(enum) {
3065 inst: Zir.Inst.Index,
30663073 nested: u16,
3074 instruction: Zir.Inst.Index,
3075 decl_val: NullTerminatedString,
3076 decl_ref: NullTerminatedString,
30673077 };
30683078 pub fn wrap(cap: Unwrapped) Capture {
30693079 return switch (cap) {
3070 .inst => |inst| @enumFromInt(@intFromEnum(inst)),
3071 .nested => |idx| @enumFromInt((1 << 31) | @as(u32, idx)),
3080 .nested => |idx| .{
3081 .tag = .nested,
3082 .data = idx,
3083 },
3084 .instruction => |inst| .{
3085 .tag = .instruction,
3086 .data = @intCast(@intFromEnum(inst)),
3087 },
3088 .decl_val => |str| .{
3089 .tag = .decl_val,
3090 .data = @intCast(@intFromEnum(str)),
3091 },
3092 .decl_ref => |str| .{
3093 .tag = .decl_ref,
3094 .data = @intCast(@intFromEnum(str)),
3095 },
30723096 };
30733097 }
30743098 pub fn unwrap(cap: Capture) Unwrapped {
3075 const raw = @intFromEnum(cap);
3076 const tag: u1 = @intCast(raw >> 31);
3077 return switch (tag) {
3078 0 => .{ .inst = @enumFromInt(raw) },
3079 1 => .{ .nested = @truncate(raw) },
3099 return switch (cap.tag) {
3100 .nested => .{ .nested = @intCast(cap.data) },
3101 .instruction => .{ .instruction = @enumFromInt(cap.data) },
3102 .decl_val => .{ .decl_val = @enumFromInt(cap.data) },
3103 .decl_ref => .{ .decl_ref = @enumFromInt(cap.data) },
30803104 };
30813105 }
30823106 };
src/Autodoc.zig+19-3
......@@ -459,11 +459,21 @@ const Scope = struct {
459459 NotRequested: u32, // instr_index
460460 };
461461
462 fn getCapture(scope: Scope, idx: u16) struct { Zir.Inst.Index, *Scope } {
462 fn getCapture(scope: Scope, idx: u16) struct {
463 union(enum) { inst: Zir.Inst.Index, decl: Zir.NullTerminatedString },
464 *Scope,
465 } {
463466 const parent = scope.parent.?;
464467 return switch (scope.captures[idx].unwrap()) {
465 .inst => |inst| .{ inst, parent },
466468 .nested => |parent_idx| parent.getCapture(parent_idx),
469 .instruction => |inst| .{
470 .{ .inst = inst },
471 parent,
472 },
473 .decl_val, .decl_ref => |str| .{
474 .{ .decl = str },
475 parent,
476 },
467477 };
468478 }
469479
......@@ -4048,7 +4058,13 @@ fn walkInstruction(
40484058 },
40494059 .closure_get => {
40504060 const captured, const scope = parent_scope.getCapture(extended.small);
4051 return self.walkInstruction(file, scope, parent_src, captured, need_type, call_ctx);
4061 switch (captured) {
4062 .inst => |cap_inst| return self.walkInstruction(file, scope, parent_src, cap_inst, need_type, call_ctx),
4063 .decl => |str| {
4064 const decl_status = parent_scope.resolveDeclName(str, file, inst.toOptional());
4065 return .{ .expr = .{ .declRef = decl_status } };
4066 },
4067 }
40524068 },
40534069 }
40544070 },
src/InternPool.zig+13-4
......@@ -503,22 +503,29 @@ pub const OptionalNullTerminatedString = enum(u32) {
503503};
504504
505505/// A single value captured in the closure of a namespace type. This is not a plain
506/// `Index` because we must differentiate between runtime-known values (where we
507/// store the type) and comptime-known values (where we store the value).
506/// `Index` because we must differentiate between the following cases:
507/// * runtime-known value (where we store the type)
508/// * comptime-known value (where we store the value)
509/// * decl val (so that we can analyze the value lazily)
510/// * decl ref (so that we can analyze the reference lazily)
508511pub const CaptureValue = packed struct(u32) {
509 tag: enum { @"comptime", runtime },
510 idx: u31,
512 tag: enum { @"comptime", runtime, decl_val, decl_ref },
513 idx: u30,
511514
512515 pub fn wrap(val: Unwrapped) CaptureValue {
513516 return switch (val) {
514517 .@"comptime" => |i| .{ .tag = .@"comptime", .idx = @intCast(@intFromEnum(i)) },
515518 .runtime => |i| .{ .tag = .runtime, .idx = @intCast(@intFromEnum(i)) },
519 .decl_val => |i| .{ .tag = .decl_val, .idx = @intCast(@intFromEnum(i)) },
520 .decl_ref => |i| .{ .tag = .decl_ref, .idx = @intCast(@intFromEnum(i)) },
516521 };
517522 }
518523 pub fn unwrap(val: CaptureValue) Unwrapped {
519524 return switch (val.tag) {
520525 .@"comptime" => .{ .@"comptime" = @enumFromInt(val.idx) },
521526 .runtime => .{ .runtime = @enumFromInt(val.idx) },
527 .decl_val => .{ .decl_val = @enumFromInt(val.idx) },
528 .decl_ref => .{ .decl_ref = @enumFromInt(val.idx) },
522529 };
523530 }
524531
......@@ -527,6 +534,8 @@ pub const CaptureValue = packed struct(u32) {
527534 @"comptime": Index,
528535 /// Index refers to the type.
529536 runtime: Index,
537 decl_val: DeclIndex,
538 decl_ref: DeclIndex,
530539 };
531540
532541 pub const Slice = struct {
src/Sema.zig+21-11
......@@ -2671,26 +2671,34 @@ fn analyzeAsInt(
26712671
26722672/// Given a ZIR extra index which points to a list of `Zir.Inst.Capture`,
26732673/// resolves this into a list of `InternPool.CaptureValue` allocated by `arena`.
2674fn getCaptures(sema: *Sema, parent_namespace: ?InternPool.NamespaceIndex, extra_index: usize, captures_len: u32) ![]InternPool.CaptureValue {
2674fn getCaptures(sema: *Sema, block: *Block, extra_index: usize, captures_len: u32) ![]InternPool.CaptureValue {
26752675 const zcu = sema.mod;
26762676 const ip = &zcu.intern_pool;
2677 const parent_captures: InternPool.CaptureValue.Slice = if (parent_namespace) |p| parent: {
2678 break :parent zcu.namespacePtr(p).ty.getCaptures(zcu);
2679 } else undefined; // never used so `undefined` is safe
2677 const parent_captures: InternPool.CaptureValue.Slice = zcu.namespacePtr(block.namespace).ty.getCaptures(zcu);
26802678
26812679 const captures = try sema.arena.alloc(InternPool.CaptureValue, captures_len);
26822680
26832681 for (sema.code.extra[extra_index..][0..captures_len], captures) |raw, *capture| {
2684 const zir_capture: Zir.Inst.Capture = @enumFromInt(raw);
2682 const zir_capture: Zir.Inst.Capture = @bitCast(raw);
26852683 capture.* = switch (zir_capture.unwrap()) {
2686 .inst => |inst| InternPool.CaptureValue.wrap(capture: {
2684 .nested => |parent_idx| parent_captures.get(ip)[parent_idx],
2685 .instruction => |inst| InternPool.CaptureValue.wrap(capture: {
26872686 const air_ref = try sema.resolveInst(inst.toRef());
26882687 if (try sema.resolveValueResolveLazy(air_ref)) |val| {
26892688 break :capture .{ .@"comptime" = val.toIntern() };
26902689 }
26912690 break :capture .{ .runtime = sema.typeOf(air_ref).toIntern() };
26922691 }),
2693 .nested => |parent_idx| parent_captures.get(ip)[parent_idx],
2692 .decl_val => |str| capture: {
2693 const decl_name = try ip.getOrPutString(sema.gpa, sema.code.nullTerminatedString(str));
2694 const decl = try sema.lookupIdentifier(block, .unneeded, decl_name); // TODO: could we need this src loc?
2695 break :capture InternPool.CaptureValue.wrap(.{ .decl_val = decl });
2696 },
2697 .decl_ref => |str| capture: {
2698 const decl_name = try ip.getOrPutString(sema.gpa, sema.code.nullTerminatedString(str));
2699 const decl = try sema.lookupIdentifier(block, .unneeded, decl_name); // TODO: could we need this src loc?
2700 break :capture InternPool.CaptureValue.wrap(.{ .decl_ref = decl });
2701 },
26942702 };
26952703 }
26962704
......@@ -2727,7 +2735,7 @@ fn zirStructDecl(
27272735 break :blk decls_len;
27282736 } else 0;
27292737
2730 const captures = try sema.getCaptures(block.namespace, extra_index, captures_len);
2738 const captures = try sema.getCaptures(block, extra_index, captures_len);
27312739 extra_index += captures_len;
27322740
27332741 if (small.has_backing_int) {
......@@ -2944,7 +2952,7 @@ fn zirEnumDecl(
29442952 break :blk decls_len;
29452953 } else 0;
29462954
2947 const captures = try sema.getCaptures(block.namespace, extra_index, captures_len);
2955 const captures = try sema.getCaptures(block, extra_index, captures_len);
29482956 extra_index += captures_len;
29492957
29502958 const decls = sema.code.bodySlice(extra_index, decls_len);
......@@ -3209,7 +3217,7 @@ fn zirUnionDecl(
32093217 break :blk decls_len;
32103218 } else 0;
32113219
3212 const captures = try sema.getCaptures(block.namespace, extra_index, captures_len);
3220 const captures = try sema.getCaptures(block, extra_index, captures_len);
32133221 extra_index += captures_len;
32143222
32153223 const wip_ty = switch (try ip.getUnionType(gpa, .{
......@@ -3315,7 +3323,7 @@ fn zirOpaqueDecl(
33153323 break :blk decls_len;
33163324 } else 0;
33173325
3318 const captures = try sema.getCaptures(block.namespace, extra_index, captures_len);
3326 const captures = try sema.getCaptures(block, extra_index, captures_len);
33193327 extra_index += captures_len;
33203328
33213329 const wip_ty = switch (try ip.getOpaqueType(gpa, .{
......@@ -17268,6 +17276,8 @@ fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1726817276 const capture_ty = switch (captures.get(ip)[extended.small].unwrap()) {
1726917277 .@"comptime" => |index| return Air.internedToRef(index),
1727017278 .runtime => |index| index,
17279 .decl_val => |decl_index| return sema.analyzeDeclVal(block, src, decl_index),
17280 .decl_ref => |decl_index| return sema.analyzeDeclRef(decl_index),
1727117281 };
1727217282
1727317283 // The comptime case is handled already above. Runtime case below.
src/print_zir.zig+15-9
......@@ -1427,11 +1427,11 @@ const Writer = struct {
14271427 try stream.writeAll("{}, ");
14281428 } else {
14291429 try stream.writeAll("{ ");
1430 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1430 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
14311431 extra_index += 1;
14321432 for (1..captures_len) |_| {
14331433 try stream.writeAll(", ");
1434 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1434 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
14351435 extra_index += 1;
14361436 }
14371437 try stream.writeAll(" }, ");
......@@ -1652,11 +1652,11 @@ const Writer = struct {
16521652 try stream.writeAll("{}, ");
16531653 } else {
16541654 try stream.writeAll("{ ");
1655 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1655 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
16561656 extra_index += 1;
16571657 for (1..captures_len) |_| {
16581658 try stream.writeAll(", ");
1659 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1659 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
16601660 extra_index += 1;
16611661 }
16621662 try stream.writeAll(" }, ");
......@@ -1817,11 +1817,11 @@ const Writer = struct {
18171817 try stream.writeAll("{}, ");
18181818 } else {
18191819 try stream.writeAll("{ ");
1820 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1820 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
18211821 extra_index += 1;
18221822 for (1..captures_len) |_| {
18231823 try stream.writeAll(", ");
1824 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1824 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
18251825 extra_index += 1;
18261826 }
18271827 try stream.writeAll(" }, ");
......@@ -1930,11 +1930,11 @@ const Writer = struct {
19301930 try stream.writeAll("{}, ");
19311931 } else {
19321932 try stream.writeAll("{ ");
1933 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1933 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
19341934 extra_index += 1;
19351935 for (1..captures_len) |_| {
19361936 try stream.writeAll(", ");
1937 try self.writeCapture(stream, @enumFromInt(self.code.extra[extra_index]));
1937 try self.writeCapture(stream, @bitCast(self.code.extra[extra_index]));
19381938 extra_index += 1;
19391939 }
19401940 try stream.writeAll(" }, ");
......@@ -2808,8 +2808,14 @@ const Writer = struct {
28082808
28092809 fn writeCapture(self: *Writer, stream: anytype, capture: Zir.Inst.Capture) !void {
28102810 switch (capture.unwrap()) {
2811 .inst => |inst| return self.writeInstIndex(stream, inst),
28122811 .nested => |i| return stream.print("[{d}]", .{i}),
2812 .instruction => |inst| return self.writeInstIndex(stream, inst),
2813 .decl_val => |str| try stream.print("decl_val \"{}\"", .{
2814 std.zig.fmtEscapes(self.code.nullTerminatedString(str)),
2815 }),
2816 .decl_ref => |str| try stream.print("decl_ref \"{}\"", .{
2817 std.zig.fmtEscapes(self.code.nullTerminatedString(str)),
2818 }),
28132819 }
28142820 }
28152821