vala-language-server icon indicating copy to clipboard operation
vala-language-server copied to clipboard

Support type narrowing

Open Prince781 opened this issue 4 years ago • 3 comments

It seems that this feature was implemented in the compiler in such a way that the data type of the type-narrowed symbol is unchanged. So we have to either change our strategy or change the way this is implemented in upstream.

Example:

class Derived : Object {
    public void do_something () {
        print ("hello\n");
    }
}

void main() {
    Object d = new Derived ();
    if (d is Derived) {
        d. // <-- we should expect `do_something()` in completions
    }
}

Prince781 avatar May 09 '21 00:05 Prince781

Any feature the compiler supports should be supported in VLS, so this is a bug.

Or rather, not having this feature results in a behavior that's contrary to the behavior of the compiler, so that's why this is a bug and not an enhancement.

Prince781 avatar May 09 '21 00:05 Prince781

@Prince781 The value_type of a MemberAccess provides you with an optional context_symbol which should be used, if set, as replacement for type_symbol.

ricotz avatar May 28 '21 11:05 ricotz

There's a couple of problems with implementing this as things stand:

  1. The SymbolExtractor has to be modified to set context_symbol when resolving the type of an expression. We rely on the symbol extractor for most completions because it's much faster than a full parse. However, if the extractor must reimplement the work of narrowing the type, we could just set value_type instead.
  2. I think this is something more appropriate for the compiler to do, since this requires flow analysis for types.

Prince781 avatar Jul 03 '21 17:07 Prince781