Skip to content

Commit

Permalink
fix pointer unwrapping when detecting self parameters (#1730)
Browse files Browse the repository at this point in the history
fixes #1729
  • Loading branch information
Techatrix authored Jan 29, 2024
1 parent a8a83b6 commit 686c491
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,21 @@ pub fn firstParamIs(
const param = ast.nextFnParam(&it).?;
if (param.type_expr == 0) return false;

if (try analyser.resolveTypeOfNodeInternal(.{
const resolved_type = try analyser.resolveTypeOfNodeInternal(.{
.node = param.type_expr,
.handle = handle,
})) |resolved_type| {
if (resolved_type.eql(expected))
return true;
}
}) orelse return false;
if (!resolved_type.is_type_val) return false;

if (ast.fullPtrType(tree, param.type_expr)) |ptr_type| {
if (try analyser.resolveTypeOfNodeInternal(.{
.node = ptr_type.ast.child_type,
.handle = handle,
})) |resolved_prefix_op| {
if (resolved_prefix_op.eql(expected))
return true;
}
}
return false;
const deref_type = switch (resolved_type.data) {
.pointer => |info| switch (info.size) {
.One => info.elem_ty.*,
.Many, .Slice, .C => return false,
},
else => resolved_type,
};

return deref_type.eql(expected);
}

pub fn getVariableSignature(allocator: std.mem.Allocator, tree: Ast, var_decl: Ast.full.VarDecl) error{OutOfMemory}![]const u8 {
Expand Down
18 changes: 18 additions & 0 deletions tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ test "inlayhints - function self parameter" {
\\const foo: Foo = .{};
\\const _ = foo.bar(<alpha>5);
, .Parameter);
try testInlayHints(
\\const Foo = struct { pub fn bar(self: *Foo, alpha: u32) void {} };
\\const foo: *Foo = undefined;
\\const _ = foo.bar(<alpha>5);
, .Parameter);
try testInlayHints(
\\const Foo = struct { pub fn bar(_: Foo, alpha: u32, beta: []const u8) void {} };
\\const foo: Foo = .{};
Expand All @@ -54,6 +59,11 @@ test "inlayhints - function self parameter" {
\\const foo: Foo = .{};
\\const _ = foo.bar(<alpha>5,<beta>4);
, .Parameter);
try testInlayHints(
\\const Foo = struct { pub fn bar(self: Foo, alpha: u32, beta: anytype) void {} };
\\const foo: *Foo = undefined;
\\const _ = foo.bar(<alpha>5,<beta>4);
, .Parameter);
try testInlayHints(
\\const Foo = struct { pub fn bar(self: Foo, alpha: u32, beta: []const u8) void {} };
\\const _ = Foo.bar(<self>undefined,<alpha>5,<beta>"");
Expand All @@ -68,6 +78,14 @@ test "inlayhints - function self parameter" {
, .Parameter);
}

test "inlayhints - function self parameter with pointer type in type declaration" {
try testInlayHints(
\\const Foo = *opaque { pub fn bar(self: Foo, alpha: u32) void {} };
\\const foo: Foo = undefined;
\\const _ = foo.bar(<alpha>5);
, .Parameter);
}

test "inlayhints - resolve alias" {
try testInlayHints(
\\fn foo(alpha: u32) void {}
Expand Down

0 comments on commit 686c491

Please sign in to comment.