untoldwind / KontrolSystem2

Autopilot scripting system for KSP2
Other
54 stars 15 forks source link

LSP Server: Type analysis should consider types that are not explicitely imported #107

Closed kbleeke closed 5 months ago

kbleeke commented 9 months ago

Consider the following structure

test2.to

pub type ResultType = (
    a: int
)

pub fn foo() -> ResultType = {
    (a: 123)
}

test.to

use { foo } from test2

fn bar() -> Unit = {
    let a = foo()
    let b = a.a
}

The type for variable a is unkown. This can be worked around by importing ResultType as well.

image

However, if the local module defines a type of the same name, the inferred type of a becomes test::ResultType instead of test2::ResultType

image

kbleeke commented 9 months ago

Actually, is seems like the return type of the imported function is already wrong in second case

image

untoldwind commented 9 months ago

Problem was that the type alias ResultType was not resolved in the right module context.

untoldwind commented 8 months ago

The lsp-server 0.0.12 should handle this correctly now.

lefouvert commented 8 months ago

version 0.5.2.2 (Ckan)

Hi, Sorry for my ignorance, but I don't know what is a LSP-server, neither I know if this fix has been published on the version 0.5.2.2 available in Ckan.

However, it also seems to apply to Records elements :

gitreport/typeanalysis0.to2

pub type FancyConst = int

pub const FancyConstHolder: (Foo: FancyConst, Bar: FancyConst) = (
    Foo: 0,
    Bar: 1
)

pub fn foobar(p: FancyConst) -> Option<bool> = {
    if (p == FancyConstHolder.Foo)
        return Some(true)
    else if (p == FancyConstHolder.Bar)
        return Some(false)
    return None()
}

gitreport/typeanalysis1.to2

use {FancyConstHolder, foobar} from gitreport::typeanalysis0

fn fancyFunc() -> Unit = {
    foobar(FancyConstHolder.Foo)
}

give this error, which I think is linked with this topic :

Rebooted in 00:00:03.9095632 ERROR: [gitreport\typeanalysis0.to2(4, 35)] InvalidType Unable to lookup type FancyConst

Edit : I forgot to mention an easy workaround is obviously to add FancyConst in the import although I assume it shouldn't be mandatory.

untoldwind commented 8 months ago

The lsp-server is part of the visual studio code plugin. I release that part separately so that it could be used in other editors as well.

Your example was actually a bug in the compiler itself

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 60 days with no activity.

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.