Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix orelse type resolution #1896

Merged
merged 6 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,13 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
.deref => try analyser.resolveDerefType(base_type),
.unwrap_optional => try analyser.resolveOptionalUnwrap(base_type),
.array_access => try analyser.resolveBracketAccessType(base_type, .Single),
.@"orelse" => try analyser.resolveOptionalUnwrap(base_type),
.@"orelse" => {
const type_right = try analyser.resolveTypeOfNodeInternal(.{ .node = datas[node].rhs, .handle = handle }) orelse return null;
return switch (type_right.data) {
.optional => type_right,
else => try analyser.resolveOptionalUnwrap(base_type),
};
},
Sekky61 marked this conversation as resolved.
Show resolved Hide resolved
.@"catch" => try analyser.resolveUnwrapErrorUnionType(base_type, .payload),
.@"try" => try analyser.resolveUnwrapErrorUnionType(base_type, .payload),
.address_of => try analyser.resolveAddressOf(base_type),
Expand Down Expand Up @@ -1853,13 +1859,29 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
label_token: ?Ast.TokenIndex,
then_expr: Ast.Node.Index,
else_expr: Ast.Node.Index,
} = if (ast.fullWhile(tree, node)) |while_node|
.{
} = if (ast.fullWhile(tree, node)) |while_node| no_hang: {

// while (true) {} is a noreturn type
Sekky61 marked this conversation as resolved.
Show resolved Hide resolved
if (try analyser.resolveTypeOfNodeInternal(.{ .node = while_node.ast.cond_expr, .handle = handle })) |cond_type| {
switch (cond_type.data) {
.ip_index => |payload| {
switch (payload.index) {
.bool_true => {
return try Type.typeValFromIP(analyser, .noreturn_type);
},
else => {},
}
},
else => {},
}
}

break :no_hang .{
.label_token = while_node.label_token,
.then_expr = while_node.ast.then_expr,
.else_expr = while_node.ast.else_expr,
}
else if (ast.fullFor(tree, node)) |for_node|
};
} else if (ast.fullFor(tree, node)) |for_node|
.{
.label_token = for_node.label_token,
.then_expr = for_node.ast.then_expr,
Expand Down Expand Up @@ -2122,10 +2144,13 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
.switch_case,
.switch_case_inline,
.switch_range,
=> {},
.@"continue",
.@"break",
.@"return",
=> {},
=> {
return try Type.typeValFromIP(analyser, .noreturn_type);
},

.@"await",
.@"suspend",
Expand Down
78 changes: 78 additions & 0 deletions tests/lsp_features/hover.zig
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,84 @@ test "optional" {
\\
\\Go to [S](file:///test.zig#L1)
);

try testHover(
\\const foo: ?i32 = 5;
\\const b<cursor>ar = foo orelse 0;
,
\\```zig
\\const bar = foo orelse 0
\\```
\\```zig
\\(i32)
\\```
);

try testHover(
\\const foo: ?i32 = 5;
\\const b<cursor>ar = foo orelse foo;
,
\\```zig
\\const bar = foo orelse foo
\\```
\\```zig
\\(?i32)
\\```
);

try testHover(
\\const foo: ?i32 = 5;
\\const b<cursor>ar = foo orelse unreachable;
,
\\```zig
\\const bar = foo orelse unreachable
\\```
\\```zig
\\(i32)
\\```
);

try testHover(
\\fn foo(a: ?i32) void {
\\ const b<cursor>ar = a orelse return;
\\}
,
\\```zig
\\const bar = a orelse return
\\```
\\```zig
\\(i32)
\\```
);

try testHover(
\\fn foo() void {
\\ const array: [1]?i32 = [1]?i32{ 4 };
\\ for (array) |elem| {
\\ const b<cursor>ar = elem orelse continue;
\\ }
\\}
,
\\```zig
\\const bar = elem orelse continue
\\```
\\```zig
\\(i32)
\\```
);

try testHover(
\\fn foo(foo: ?i32) void {
\\ const b<cursor>ar = foo orelse while (true) {};
\\}
,
\\```zig
\\const bar = foo orelse while (true) {}
\\```
\\```zig
\\(i32)
\\```
);
}

test "error union" {
Expand Down