From 8bbf62c3b98da474dbbde907819a74133da9f430 Mon Sep 17 00:00:00 2001 From: Krzysztof Wolicki Date: Wed, 12 Apr 2023 19:50:50 +0200 Subject: [PATCH] autodoc: Handling of explicit values for enum fields Fixes to frontend handling of structs --- lib/docs/main.js | 13 +++++++++---- src/Autodoc.zig | 14 +++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/docs/main.js b/lib/docs/main.js index f8e2fe94b9..d59c80f57a 100644 --- a/lib/docs/main.js +++ b/lib/docs/main.js @@ -623,7 +623,7 @@ const NAV_MODES = { function typeIsStructWithNoFields(typeIndex) { let typeObj = getType(typeIndex); if (typeObj.kind !== typeKinds.Struct) return false; - return typeObj.fields.length == 0; + return typeObj.field_types.length == 0; } function typeIsGenericFn(typeIndex) { @@ -2705,6 +2705,7 @@ const NAV_MODES = { let declValue = resolveValue(uns.value); if (!("type" in declValue.expr)) continue; let uns_container = getType(declValue.expr.type); + if (!isContainerType(uns_container)) continue; categorizeDecls( uns_container.pubDecls, typesList, @@ -2858,7 +2859,10 @@ const NAV_MODES = { escapeHtml(fieldName); if (container.kind === typeKinds.Enum) { - html += ' = ' + fieldName + ""; + let value = container.values[i]; + if (value !== null) { + html += " = " + exprName(value, { wantHtml: true, wantLink: true }); + } } else { let fieldTypeExpr = container.field_types[i]; if (container.kind !== typeKinds.Struct || !container.is_tuple) { @@ -4106,7 +4110,8 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) { privDecls: ty[3], pubDecls: ty[4], tag: ty[5], - nonexhaustive: ty[6], + values: ty[6], + nonexhaustive: ty[7], }; case 20: // Union return { @@ -4115,7 +4120,7 @@ function addDeclToSearchResults(decl, declIndex, pkgNames, item, list, stack) { src: ty[2], privDecls: ty[3], pubDecls: ty[4], - fields: ty[5], + field_types: ty[5], tag: ty[6], auto_tag: ty[7], }; diff --git a/src/Autodoc.zig b/src/Autodoc.zig index 69177068d1..23731338fb 100644 --- a/src/Autodoc.zig +++ b/src/Autodoc.zig @@ -586,8 +586,8 @@ const DocData = struct { src: usize, // index into astNodes privDecls: []usize = &.{}, // index into decls pubDecls: []usize = &.{}, // index into decls - field_types: ?[]Expr = null, // (use src->fields to find names) - field_defaults: ?[]?Expr = null, + field_types: []Expr = &.{}, // (use src->fields to find names) + field_defaults: []?Expr = &.{}, // default values is specified is_tuple: bool, line_number: usize, outer_decl: usize, @@ -615,6 +615,7 @@ const DocData = struct { pubDecls: []usize = &.{}, // index into decls // (use src->fields to find field names) tag: ?Expr = null, // tag type if specified + values: []?Expr = &.{}, // tag values if specified nonexhaustive: bool, }, Union: struct { @@ -2706,6 +2707,7 @@ fn walkInstruction( extra_index += body_len; var field_name_indexes: std.ArrayListUnmanaged(usize) = .{}; + var field_values: std.ArrayListUnmanaged(?DocData.Expr) = .{}; { var bit_bag_idx = extra_index; var cur_bit_bag: u32 = undefined; @@ -2727,12 +2729,13 @@ fn walkInstruction( const doc_comment_index = file.zir.extra[extra_index]; extra_index += 1; - const value_ref: ?Ref = if (has_value) blk: { + const value_expr: ?DocData.Expr = if (has_value) blk: { const value_ref = file.zir.extra[extra_index]; extra_index += 1; - break :blk @intToEnum(Ref, value_ref); + const value = try self.walkRef(file, &scope, src_info, @intToEnum(Ref, value_ref), false); + break :blk value.expr; } else null; - _ = value_ref; + try field_values.append(self.arena, value_expr); const field_name = file.zir.nullTerminatedString(field_name_index); @@ -2757,6 +2760,7 @@ fn walkInstruction( .privDecls = priv_decl_indexes.items, .pubDecls = decl_indexes.items, .tag = tag_type, + .values = field_values.items, .nonexhaustive = small.nonexhaustive, }, };