Skip to content

Commit 224b83e

Browse files
authored
several fixes around decl or enum literals (#2552)
- opaque types are now also searched for decl literals - enum literal types are now resolved if there's a result location type - inlay hints are now printed for decl literal function calls
1 parent d732b05 commit 224b83e

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

src/analysis.zig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,7 +2770,16 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, options: ResolveOptions) error
27702770
return if (value) |v| Type.fromIP(analyser, ty, v) else Type.fromIP(analyser, ty, null);
27712771
},
27722772

2773-
.enum_literal => return Type.fromIP(analyser, .enum_literal_type, null),
2773+
.enum_literal => {
2774+
const source_token = tree.tokenStart(tree.nodeMainToken(node));
2775+
const lineage = try ast.nodesOverlappingIndex(analyser.arena, tree, source_token);
2776+
defer analyser.arena.free(lineage);
2777+
2778+
const tag = offsets.identifierTokenToNameSlice(tree, tree.nodeMainToken(node));
2779+
const decl = (try analyser.lookupSymbolFieldInit(handle, tag, node, lineage[1..])) orelse return Type.fromIP(analyser, .enum_literal_type, null);
2780+
return decl.resolveType(analyser);
2781+
},
2782+
27742783
.unreachable_literal => return Type.fromIP(analyser, .noreturn_type, null),
27752784
.anyframe_literal => return Type.fromIP(analyser, .anyframe_type, null),
27762785

@@ -5896,7 +5905,7 @@ pub fn lookupSymbolFieldInit(
58965905
}
58975906

58985907
switch (container_type.getContainerKind() orelse return null) {
5899-
.keyword_struct => {},
5908+
.keyword_struct, .keyword_opaque => {},
59005909
.keyword_enum => if (try (try container_type.typeOf(analyser)).lookupSymbol(analyser, field_name)) |ty| return ty,
59015910
.keyword_union => if (try container_type.lookupSymbol(analyser, field_name)) |ty| return ty,
59025911
else => return null,

src/features/inlay_hints.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn writeCallNodeHint(builder: *Builder, call: Ast.full.Call) !void {
399399
const tree = &handle.tree;
400400

401401
switch (tree.nodeTag(call.ast.fn_expr)) {
402-
.identifier, .field_access => try writeCallHint(builder, call),
402+
.identifier, .field_access, .enum_literal => try writeCallHint(builder, call),
403403
else => {
404404
log.debug("cannot deduce fn expression with tag '{}'", .{tree.nodeTag(call.ast.fn_expr)});
405405
},

tests/analysis/decl_literal.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const TaggedUnionType = union(EnumType) {
2222
const baz: TaggedUnionType = .{ .foo = 1 };
2323
};
2424

25+
const OpaqueType = opaque {
26+
pub fn init() *OpaqueType {
27+
var value: void = {};
28+
return @ptrCast(&value);
29+
}
30+
};
31+
2532
const some_struct: StructType = .baz;
2633
// ^^^^ (StructType)()
2734

@@ -33,3 +40,6 @@ const some_union: UnionType = .baz;
3340

3441
const some_tagged_union: TaggedUnionType = .baz;
3542
// ^^^^ (TaggedUnionType)()
43+
44+
const some_opaque: *OpaqueType = .init();
45+
// ^^^^^ (fn () *OpaqueType)()

tests/lsp_features/inlay_hints.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,15 @@ test "tuple fields" {
584584
, .{ .kind = .Type });
585585
}
586586

587+
test "declaration literals" {
588+
try testInlayHints(
589+
\\const X = opaque { fn init(a_thing: u32, b_thing: u32) *X { _ = .{a_thing, b_thing}; const ignore: void = {}; return @ptrCast(&ignore); } };
590+
\\test {
591+
\\ const x: *X = .init(<a_thing>0, <b_thing>1);
592+
\\}
593+
, .{ .kind = .Parameter });
594+
}
595+
587596
const Options = struct {
588597
kind: types.InlayHint.Kind,
589598
show_builtin: bool = true,

0 commit comments

Comments
 (0)