flow icon indicating copy to clipboard operation
flow copied to clipboard

probable bug: type unions in generics

Open opensrcken opened this issue 10 years ago • 7 comments

With Arrays

(1) https://tryflow.org/?code=Y29uc3QgbnVtYmVyczogQXJyYXk8bnVtYmVyPiA9IFsxXTsKCmNvbnN0IG1peGVkOiBBcnJheTxudW1iZXJ8c3RyaW5nPiA9IFsxLCAndHdvJ107CgpmdW5jdGlvbiB0ZXN0KHNvbWVBcnJheTogQXJyYXk8bnVtYmVyfHN0cmluZz4pIHsKICBzb21lQXJyYXkuZm9yRWFjaChrID0+IHsKICAJICBpZiAodHlwZW9mIGsgPT09ICdzdHJpbmcnKSB7CgkJICAKCSAgfSBlbHNlIHsKCQkgIE1hdGguZmxvb3Ioayk7CgkgIH0KICB9KTsKfQoKdGVzdChudW1iZXJzKTs=

Note, this works in Typescript, and I think this is the correct behavior: (2) https://www.typescriptlang.org/play/#src=const%20numbers%3A%20Array%3Cnumber%3E%20%3D%20%5B1%5D%3B%0D%0A%0D%0Aconst%20mixed%3A%20Array%3Cnumber%7Cstring%3E%20%3D%20%5B1%2C%20'two'%5D%3B%0D%0A%0D%0Afunction%20test(someArray%3A%20Array%3Cnumber%7Cstring%3E)%20%7B%0D%0A%20%20someArray.forEach(k%20%3D%3E%20%7B%0D%0A%20%20%09%20%20if%20(typeof%20k%20%3D%3D%3D%20'string')%20%7B%0D%0A%09%09%20%20%0D%0A%09%20%20%7D%20else%20%7B%0D%0A%09%09%20%20Math.floor(k)%3B%0D%0A%09%20%20%7D%0D%0A%20%20%7D)%3B%0D%0A%7D%0D%0A%0D%0Atest(numbers)%3B

In Flow, would you call test with numbers?

Adding Array<number> to the type union doesn't work. (3) https://tryflow.org/?code=Y29uc3QgbnVtYmVyczogQXJyYXk8bnVtYmVyPiA9IFsxXTsKCmNvbnN0IG1peGVkOiBBcnJheTxudW1iZXJ8c3RyaW5nPiA9IFsxLCAndHdvJ107CgpmdW5jdGlvbiB0ZXN0KHNvbWVBcnJheTogQXJyYXk8bnVtYmVyPnxBcnJheTxudW1iZXJ8c3RyaW5nPikgewogIHNvbWVBcnJheS5mb3JFYWNoKGsgPT4gewogIAkgIGlmICh0eXBlb2YgayA9PT0gJ3N0cmluZycpIHsKCQkgIAoJICB9IGVsc2UgewoJCSAgTWF0aC5mbG9vcihrKTsKCSAgfQogIH0pOwp9Cgp0ZXN0KG51bWJlcnMpOw==

With Objects

In tryflow.org, the code seems to fail correctly when Arrays are substituted with Objects (adding Object to the type union): (4) https://tryflow.org/?code=Y29uc3QgbnVtYmVyczoge1trZXk6IHN0cmluZ106IG51bWJlcn0gPSB7YTogMSwgYjogMn07Cgpjb25zdCBtaXhlZDoge1trZXk6IHN0cmluZ106IG51bWJlcnxzdHJpbmd9ID0ge2E6IDEsIGI6ICd0d28nfTsKCmZ1bmN0aW9uIHRlc3Qoc29tZXRoaW5nOiB7W2tleTogc3RyaW5nXTogbnVtYmVyfXx7W2tleTogc3RyaW5nXTogbnVtYmVyfHN0cmluZ30pIHsKICBPYmplY3Qua2V5cyhzb21ldGhpbmcpLmZvckVhY2goayA9PiB7CiAgICBNYXRoLmZsb29yKHNvbWV0aGluZ1trXSk7CiAgfSk7Cn0KCnRlc3QobnVtYmVycyk7

However, when I run flow-bin 0.30 locally, that very same code passes type checking. So there may be another bug there, where the type-checker does not always return the same result.

Back to the topic at hand, I think something like this should work (removing the type union): (5) https://tryflow.org/?code=Y29uc3QgbnVtYmVyczoge1trZXk6IHN0cmluZ106IG51bWJlcn0gPSB7YTogMSwgYjogMn07Cgpjb25zdCBtaXhlZDoge1trZXk6IHN0cmluZ106IG51bWJlcnxzdHJpbmd9ID0ge2E6IDEsIGI6ICd0d28nfTsKCmZ1bmN0aW9uIHRlc3Qoc29tZXRoaW5nOiB7W2tleTogc3RyaW5nXTogbnVtYmVyfHN0cmluZ30pIHsKCn0KCnRlc3QobnVtYmVycyk7

opensrcken avatar Aug 19 '16 22:08 opensrcken

It's not a bug, this code (1 example) is unsafe, so Flow doesn't allow this

vkurchatkin avatar Aug 19 '16 23:08 vkurchatkin

Can you elaborate on why it is unsafe?

Assuming it is unsafe, for the sake of argument, then certainly example 3 should type-check, since this type-checks (the same situation, except with an Object instead of an Array):

https://tryflow.org/?code=Y29uc3QgbnVtYmVyczoge1trZXk6IHN0cmluZ106IG51bWJlcn0gPSB7YTogMSwgYjogMn07Cgpjb25zdCBtaXhlZDoge1trZXk6IHN0cmluZ106IG51bWJlcnxzdHJpbmd9ID0ge2E6IDEsIGI6ICd0d28nfTsKCmZ1bmN0aW9uIHRlc3Qoc29tZXRoaW5nOiB7W2tleTogc3RyaW5nXTogbnVtYmVyfXx7W2tleTogc3RyaW5nXTogbnVtYmVyfHN0cmluZ30pIHsKICBPYmplY3Qua2V5cyhzb21ldGhpbmcpLmZvckVhY2goayA9PiB7fSk7Cn0KCnRlc3QobnVtYmVycyk7

opensrcken avatar Aug 19 '16 23:08 opensrcken

@opensrcken it's unsafe because

const n: Array<number> = [1]

function test(a: Array<number | string>) {
  a.push('unsafe') // <= now n contains a string
}

test(n)

gcanti avatar Aug 20 '16 05:08 gcanti

@gcanti Fair enough. Any reason example 3 can't work?

opensrcken avatar Aug 20 '16 23:08 opensrcken

@opensrcken for the same reason. Widening the type doesn't make it any safer

gcanti avatar Aug 21 '16 05:08 gcanti

@gcanti is right (thanks for helping with the issues!), the errors you see in every example all have the same cause.

Giulio already gave a counterexample for the Array<number> to Array<number|string> case. The union case can be explained in terms of that one.

~~To facilitate the explanation, read A <: B as "A is a subtype of B." For union types, the statement T <: (A|B) is true if T <: A and T <: B. This is why it's insufficient to use the annotation Array<number>|Array<number|string>; in order to pass in an Array<number>, it has to be a subtype of both, not either one.~~

(Edit: Sorry about the confusion. I mixed up the rules for unions and intersections. How embarrassing!)

It looks like this might actually be a legit bug:

/* @flow */

function test(someArray: Array<number>|Array<number|string>) {
  someArray.slice(); // no error without this line
}

test([0]);

Thanks for the repot!

samwgoldman avatar Aug 21 '16 05:08 samwgoldman

The final case now correctly passes

SamChou19815 avatar Jun 02 '23 16:06 SamChou19815