Skip to content

Investigate randomly failing non-null checks #1139

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

Closed
dcodeIO opened this issue Mar 1, 2020 · 4 comments
Closed

Investigate randomly failing non-null checks #1139

dcodeIO opened this issue Mar 1, 2020 · 4 comments

Comments

@dcodeIO
Copy link
Member

dcodeIO commented Mar 1, 2020

While working on #1129 I noticed that flows and/or assignments do sometimes not correctly pick up non-null state after assigning to a local from a function call in a conditional branch:

      if (tn.skip(Token.COLON)) {
        type = this.parseType(tn);
        if (!type) return null;
      } else {
        type = Node.createOmittedType(tn.range(tn.pos)); // returns non-null
        type = type!; // FIXME: WHY?
      }
      // otherwise type is considered nullable here leading to follow-up errors
     someFuncWantingNonNullable(type); // cannot assign Type | null to Type

There are 8 code locations in the compiler currently with that FIXME: WHY? comment that all look similar, but I didn't yet manage to reproduce this in a smaller code sample. If someone else manages to, that would be super helpful :)

@dcodeIO dcodeIO mentioned this issue Mar 1, 2020
8 tasks
@jtenner
Copy link
Contributor

jtenner commented Mar 3, 2020

class Ref {
}

let result: Ref | null = null;

function test(input: bool): void {
  if (input) {
    return;
  } else {
    result = new Ref();
  }
  func(result);
}

function func(input: Ref): void {
  trace("something", 1, <f64>changetype<usize>(input));
}

test(false);

Not exactly minimal, and it probably could be better, but this seems to get the compiler error.

@dcodeIO
Copy link
Member Author

dcodeIO commented Mar 3, 2020

This one seems different, being caused by result being a global, which flows don't yet null-check.

@jtenner
Copy link
Contributor

jtenner commented Mar 3, 2020

Got it!

class Ref {
}

function useRef(aRef: Ref): void {}

function otherExample(condition: bool): void {
  let something: Ref | null;
    if (condition) {
      return;
    } else {
      something = new Ref();
    }
    useRef(something);
}
otherExample(true);
  ERROR TS2322: Type 'test-example/Ref | null' is not assignable to type 'test-example/Ref'.

       useRef(something);
              ~~~~~~~~~
   in test-example.ts(25,11)

@dcodeIO
Copy link
Member Author

dcodeIO commented Mar 3, 2020

This one is interesting, but unlike the cases in the compiler can't be solved with a something = something! right after the assignment. Strange, might be another bug.

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

No branches or pull requests

2 participants