Skip to content

Commit

Permalink
improve analysis around generic type function parameters
Browse files Browse the repository at this point in the history
generic function parameters should now resolve to "unknown"
instead of incorrectly showing `type` when hovering.
see #1601
  • Loading branch information
Techatrix committed Dec 29, 2023
1 parent 22b04f4 commit 842e8fc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
35 changes: 20 additions & 15 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2103,17 +2103,20 @@ pub const TypeWithHandle = struct {
pub fn instanceTypeVal(self: TypeWithHandle, analyser: *Analyser) error{OutOfMemory}!?TypeWithHandle {
if (!self.type.is_type_val) return null;
return switch (self.type.data) {
.ip_index => |payload| TypeWithHandle{
.type = .{
.data = .{
.ip_index = .{
.index = try analyser.ip.getUnknown(analyser.gpa, payload.index),
.node = payload.node,
.ip_index => |payload| {
if (payload.index == .unknown_type) return null;
return TypeWithHandle{
.type = .{
.data = .{
.ip_index = .{
.index = try analyser.ip.getUnknown(analyser.gpa, payload.index),
.node = payload.node,
},
},
.is_type_val = payload.index == .type_type,
},
.is_type_val = false,
},
.handle = self.handle,
.handle = self.handle,
};
},
else => TypeWithHandle{
.type = .{ .data = self.type.data, .is_type_val = false },
Expand Down Expand Up @@ -2207,6 +2210,7 @@ pub const TypeWithHandle = struct {
};
}

/// returns whether the given type is of type `type`.
pub fn isMetaType(self: TypeWithHandle) bool {
if (!self.type.is_type_val) return false;
switch (self.type.data) {
Expand Down Expand Up @@ -3207,16 +3211,17 @@ pub const DeclWithHandle = struct {
return maybe_type_handle;
}

if (isMetaType(tree, param.type_expr)) {
const param_type = try analyser.resolveTypeOfNodeInternal(
.{ .node = param.type_expr, .handle = self.handle },
) orelse return null;

if (param_type.isMetaType()) {
if (analyser.bound_type_params.get(.{ .func = pay.func, .param_index = pay.param_index })) |resolved_type| {
return resolved_type;
}
return try analyser.resolveTypeOfNodeInternal(.{ .node = param.type_expr, .handle = self.handle });
}
const ty = (try analyser.resolveTypeOfNodeInternal(
.{ .node = param.type_expr, .handle = self.handle },
)) orelse return null;
return try ty.instanceTypeVal(analyser);

return try param_type.instanceTypeVal(analyser);
},
.pointer_payload => |pay| {
const ty = (try analyser.resolveTypeOfNodeInternal(.{
Expand Down
9 changes: 9 additions & 0 deletions tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ test "inlayhints - var decl" {
, .Type);
}

test "inlayhints - generic function parameter" {
// TODO there should be an inlay hint that shows `T`
try testInlayHints(
\\fn foo(comptime T: type, param: T) void {
\\ const val = param;
\\}
, .Type);
}

test "inlayhints - capture values" {
try testInlayHints(
\\fn a() void {
Expand Down
21 changes: 20 additions & 1 deletion tests/lsp_features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,25 @@ test "semantic tokens - function" {
.{ "=", .operator, .{} },
.{ "T", .parameter, .{} },
});
try testSemanticTokens(
\\fn foo(comptime T: type, in: T) void {
\\ _ = T;
\\ _ = in;
\\}
, &.{
.{ "fn", .keyword, .{} },
.{ "foo", .function, .{ .declaration = true, .generic = true } },
.{ "comptime", .keyword, .{} },
.{ "T", .typeParameter, .{ .declaration = true } },
.{ "type", .type, .{} },
.{ "in", .parameter, .{ .declaration = true } },
.{ "T", .typeParameter, .{} },
.{ "void", .type, .{} },
.{ "=", .operator, .{} },
.{ "T", .typeParameter, .{} },
.{ "=", .operator, .{} },
.{ "in", .parameter, .{} },
});
}

test "semantic tokens - method" {
Expand Down Expand Up @@ -1240,7 +1259,7 @@ test "semantic tokens - builtin fuctions" {
\\const foo = @as(type, u32);
, &.{
.{ "const", .keyword, .{} },
.{ "foo", .variable, .{ .declaration = true } },
.{ "foo", .type, .{ .declaration = true } },
.{ "=", .operator, .{} },
.{ "@as", .builtin, .{} },
.{ "type", .type, .{} },
Expand Down

0 comments on commit 842e8fc

Please sign in to comment.