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

Weird behavior with inference related to nullable types #109

Closed
Arnavion opened this issue Nov 23, 2014 · 5 comments
Closed

Weird behavior with inference related to nullable types #109

Arnavion opened this issue Nov 23, 2014 · 5 comments

Comments

@Arnavion
Copy link

/**
 * @flow
 */

function foo() {
    var str: ?string = "5"; // Line 6
    if (str === null) {
        throw new Error("str is null");
    }

    var str2 = str;
    str2.toString();
    var str3: string = str2; // Line 13
}
/stuff/src/flow-experiments/test.js:6:15,20: undefined
This type is incompatible with
  /stuff/src/flow-experiments/test.js:13:15,20: string

/stuff/src/flow-experiments/test.js:13:15,20: string
This type is incompatible with null

Found 2 errors

Not sure if this is related to #21 but:

  • It seems typechecker doesn't realize that str cannot be null after the if() that checks for === null (Does it not consider throw as exiting the function?)
  • If typechecker does think that str, and thus str2, is ?string, then why doesn't it complain about the .toString() on a possibly null value?
  • If typechecker does think that str, and thus str2, is string, then why does it complain that str2 is potentially null when assigning it to str3?

If I change the condition to == null, I get:

/stuff/src/flow-experiments/test.js:6:15,20: null
This type is incompatible with
  /stuff/src/flow-experiments/test.js:13:15,20: string

/stuff/src/flow-experiments/test.js:6:15,20: undefined
This type is incompatible with
  /stuff/src/flow-experiments/test.js:13:15,20: string

Found 2 errors

Only by adding an (unreachable) return after the throw and changing the condition to == null do I get no errors.

/**
 * @flow
 */

function foo() {
    var str: ?string = "5";
    if (str == null) {
        throw new Error("str is null");
        return;
    }

    var str2 = str;
    str2.toString();
    var str3: string = str2;
}
@Arnavion Arnavion changed the title Weird behavior with inference related to nullable return type Weird behavior with inference related to nullable types Nov 23, 2014
@Arnavion
Copy link
Author

Might be a duplicate of #13

@sophiebits
Copy link
Contributor

Seems more related to #21 to me, personally.

@Arnavion
Copy link
Author

It's a mix of both, I guess. The fact that throw doesn't short-circuit the branch analyzer is your issue #13 , and the fact that === null doesn't work seems related to #21

@sophiebits
Copy link
Contributor

Ahh, yes. Sorry, I missed the throw completely. Not sure what I was thinking.

@gabelevi
Copy link
Contributor

We fixed the control-flow logic awhile back to understand that an uncaught throw is terminal. So the version of the code with == works with no errors now. Sorry for forgetting to close out this issue!

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

4 participants