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

Incorrect type shown in language server's hover over suffixed expressions #7126

Open
snobee opened this issue Sep 28, 2024 · 3 comments
Open

Comments

@snobee
Copy link
Contributor

snobee commented Sep 28, 2024

When hovering over the values in the file below the types shown are incorrect for everything in main, except for test2 and Stdin.line!

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}

import pf.Stdin

main =
    test1 = "testing"
    test2 = Stdin.line!
    test3 = 1_2_3
    Task.ok {}

Expected behavior

The types of the values when hovering should be
test1, "testing": Str
test2: Str
Stdin.line!: Task {} [...]
test3, 1_2_3: Num *
Task.ok: {} -> Task {} *
{}: {}

Actual behaviour

test2: Str
Stdin.line!: Task {} [...]
everything else in main: Str -> Task {} [...]

I believe the problem is in roc_can::traverse::TypeAtPositionVisitor and roc_can::traverse::find_closest_type_at which finds the type of the closure generated by the canonicalization of ! instead of the proper definition.

The same thing happens with ? suffix as well.

@lukewilliamboswell
Copy link
Collaborator

My guess is that we are messing up the regions with desugaring of the suffixes, and that is somehow related.

@snobee
Copy link
Contributor Author

snobee commented Sep 30, 2024

My guess is that we are messing up the regions with desugaring of the suffixes, and that is somehow related.

That could be it. I checked the output ast from this example by generating a .snap file from can/tests

snapshot
Defs {
    tags: [
        Index(2147483648),
    ],
    regions: [
        @0-85,
    ],
    space_before: [
        Slice(start = 0, length = 0),
    ],
    space_after: [
        Slice(start = 0, length = 1),
    ],
    spaces: [
        Newline,
    ],
    type_defs: [],
    value_defs: [
        Body(
            @0-4 Identifier {
                ident: "main",
            },
            @11-85 Defs(
                Defs {
                    tags: [
                        Index(2147483648),
                    ],
                    regions: [
                        @19-28,
                    ],
                    space_before: [
                        Slice(start = 0, length = 0),
                    ],
                    space_after: [
                        Slice(start = 0, length = 0),
                    ],
                    spaces: [],
                    type_defs: [],
                    value_defs: [
                        Body(
                            @11-16 Identifier {
                                ident: "test1",
                            },
                            @19-28 Str(
                                PlainLine(
                                    "testing",
                                ),
                            ),
                        ),
                    ],
                },
                @11-85 Apply(
                    @11-85 Var {
                        module_name: "Task",
                        ident: "await",
                    },
                    [
                        @41-52 Var {
                            module_name: "Stdin",
                            ident: "line",
                        },
                        @11-85 Closure(
                            [
                                @33-38 Identifier {
                                    ident: "test2",
                                },
                            ],
                            @41-52 Defs(
                                Defs {
                                    tags: [
                                        Index(2147483648),
                                    ],
                                    regions: [
                                        @65-70,
                                    ],
                                    space_before: [
                                        Slice(start = 0, length = 1),
                                    ],
                                    space_after: [
                                        Slice(start = 1, length = 0),
                                    ],
                                    spaces: [
                                        Newline,
                                    ],
                                    type_defs: [],
                                    value_defs: [
                                        Body(
                                            @57-62 Identifier {
                                                ident: "test3",
                                            },
                                            @65-70 Num(
                                                "1_2_3",
                                            ),
                                        ),
                                    ],
                                },
                                @75-85 Apply(
                                    @75-82 Var {
                                        module_name: "Task",
                                        ident: "ok",
                                    },
                                    [
                                        @83-85 Record(
                                            [],
                                        ),
                                    ],
                                    Space,
                                ),
                            ),
                        ),
                    ],
                    BangSuffix,
                ),
            ),
        ),
    ],
}

I'm not very familiar with this but it looks like the Defs node with test3 and Task.ok has the region @41-52 which explains hovering on test3 at 60 doesn't reach it. Is it supposed to be @57-85 instead? That would contain all the regions of the nodes inside it.

Also, is there a better way to print out parts of the ast than creating a snapshot file? I couldn't find anything

@lukewilliamboswell
Copy link
Collaborator

I think making a test is the easiest way. You could also add a test for the parser if you want the AST before it's been desugared which may help see the regions easier see crates/compiler/test_syntax/tests/snapshots for those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants