Skip to content

Commit f287355

Browse files
authored
fix decl literal analysis when result type contains an optional
1 parent 265503f commit f287355

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

src/analysis.zig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5869,13 +5869,9 @@ pub fn lookupSymbolFieldInit(
58695869
else => false,
58705870
};
58715871

5872-
while (true) {
5873-
const unwrapped =
5874-
try analyser.resolveUnwrapErrorUnionType(container_type, .payload) orelse
5875-
try analyser.resolveOptionalUnwrap(container_type) orelse
5876-
break;
5877-
container_type = unwrapped;
5878-
}
5872+
container_type = try container_type
5873+
.resolveDeclLiteralResultType()
5874+
.instanceTypeVal(analyser) orelse container_type;
58795875

58805876
if (is_struct_init) {
58815877
return try container_type.lookupSymbol(analyser, field_name);

tests/lsp_features/definition.zig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ test "decl literal on generic type" {
135135
);
136136
}
137137

138+
test "decl literal pointer" {
139+
try testDefinition(
140+
\\const S = struct {
141+
\\ const value: S = .{};
142+
\\ const <def><decl>ptr</decl></def>: *const S = &value;
143+
\\};
144+
\\test {
145+
\\ const ptr: *const S = .<>ptr;
146+
\\}
147+
);
148+
try testDefinition(
149+
\\const S = struct {
150+
\\ const value: S = .{};
151+
\\ fn <def><decl>pointerFn</decl></def>() *const S {
152+
\\ return &value;
153+
\\ }
154+
\\};
155+
\\test {
156+
\\ const ptr: *const S = .poi<>nterFn();
157+
\\}
158+
);
159+
}
160+
138161
test "capture" {
139162
try testDefinition(
140163
\\test {

tests/lsp_features/hover.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,44 @@ test "decl literal function" {
452452
);
453453
}
454454

455+
test "decl literal pointer" {
456+
try testHover(
457+
\\const S = struct {
458+
\\ const value: S = .{};
459+
\\ const ptr: *const S = &value;
460+
\\};
461+
\\const s: *const S = .pt<cursor>r;
462+
,
463+
\\```zig
464+
\\const ptr: *const S = &value
465+
\\```
466+
\\```zig
467+
\\(*const S)
468+
\\```
469+
\\
470+
\\Go to [S](untitled:///Untitled-0.zig#L1)
471+
);
472+
473+
try testHover(
474+
\\const S = struct {
475+
\\ const value: S = .{};
476+
\\ fn pointerFn() *const S {
477+
\\ return &value;
478+
\\ }
479+
\\};
480+
\\const s: *const S = .poi<cursor>nterFn();
481+
,
482+
\\```zig
483+
\\fn pointerFn() *const S
484+
\\```
485+
\\```zig
486+
\\(fn () *const S)
487+
\\```
488+
\\
489+
\\Go to [S](untitled:///Untitled-0.zig#L1)
490+
);
491+
}
492+
455493
test "decl literal on generic type" {
456494
try testHover(
457495
\\fn Box(comptime T: type) type {

0 commit comments

Comments
 (0)