asm-js / validator

A reference validator for asm.js.
Apache License 2.0
1.78k stars 154 forks source link

return type inference problem #89

Closed wtchoi closed 9 years ago

wtchoi commented 9 years ago

I believe following function should type check.

function foo(){ if(1) return 1; else return 1; }

Instead, asm.js gives me the following error message:

TypeError: asm.js type error: void incompatible with previous return of type signed''

whitten commented 9 years ago

What type do you think function foo(){ if(1) return 1; else return 1; } should return?

I can imagine it returning signed or unsigned. How would the system know that the value 1 should return a signed value instead of an unsigned value?

ghost commented 9 years ago

The problem is that asm.js currently requires that every non-void function return with a single return statement. The 'foo' function above does not have a final return statement so the return type is declared to be 'void'. While it is true that both paths return, the spec would need to specify some sort of "all paths dominating the return need to return the same type" which adds complexity to the spec and impl.

wtchoi commented 9 years ago

@whitten Having "return 1|0" raises the same error.

@andhow Thanks for the clarification. The design decision to keep the system simple is understandable (I would do a similar thing at the beginning of a project), though I don't think a function local dominance relation analysis is super complicated. Anyway, I should read the specification again carefully.

ghost commented 9 years ago

@wtchoi You're right, it wouldn't be too hard and we may indeed want to loosen the rules on this in the future (which we can do since all old code will continue to validate).

ghost commented 9 years ago

For now, closing the bug. Feel free to open a new issue for the returns-on-all-paths enhancement.