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

Wrong variable type info when orelse assigns variable with an optional type #1330

Closed
lucascool12 opened this issue Jul 16, 2023 · 0 comments · Fixed by #1896
Closed

Wrong variable type info when orelse assigns variable with an optional type #1330

lucascool12 opened this issue Jul 16, 2023 · 0 comments · Fixed by #1896
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@lucascool12
Copy link

Zig Version

0.11.0-dev.4002+7dd1cf26f

Zig Language Server Version

8da21e1

Steps to Reproduce

Paste this code into an editor with zls running as lsp.
Send a textDocument/hover request of variable c.

pub fn opt_test(a: ?i32, b: ?i32) void {
    const c = a orelse b;
    std.debug.print("{s}\n", .{@typeName(@TypeOf(c))});
}

Running this code snippet prints ?i32 as type of c.

const std = @import("std");

pub fn main() !void {
    const a = null;
    const b = null;
    opt_test(a, b);
}

pub fn opt_test(a: ?i32, b: ?i32) void {
    const c = a orelse b;
    std.debug.print("{s}\n", .{@typeName(@TypeOf(c))});
}

Expected Behavior

Type of c being ?i32 .

Actual Behavior

zls reports the type of c being i32.

I took a stab at trying to come up with a fix by replacing this switch case with the one below:

                .@"orelse" => orelse_type: {
                    const base_right = .{ .node = datas[node].rhs, .handle = handle };
                    const base_type_right = (try analyser.resolveTypeOfNodeInternal(base_right));
                    if (base_type_right) |type_right| {
                        const ok = switch (type_right.type.data) {
                            .other => |node_type| switch (node_tags[node_type]) {
                                .optional_type => true,
                                else => false,
                            },
                            else => false,
                        };
                        if (ok) {
                            break :orelse_type base_type;
                        }
                    }
                    break :orelse_type try analyser.resolveUnwrapOptionalType(base_type);
                },

This worked for the example above, but when trying to open hover.zig with this change zls crashes.

@lucascool12 lucascool12 added the bug Something isn't working label Jul 16, 2023
@Techatrix Techatrix added the good first issue Good for newcomers label Oct 11, 2023
Sekky61 added a commit to Sekky61/zls that referenced this issue May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants