Skip to content

Rework logical and/or type rules on contextual bool #1264

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

Merged
merged 1 commit into from
May 14, 2020
Merged

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented May 13, 2020

An attempt to fix #1136 by refining the existing special case for contextual bool on logical LHS && RHS and LHS || RHS.

For a bit of background: We can't model this exactly like JS because it would involve a union typeof(LHS) | typeof(RHS), so the compiler currently compiles LHS with the outer contextual type allowing it to deviate and RHS with LHS's type as the contextual type forcing an implicit conversion to get a single AOT-compatible type.

The important difference this PR introduces is that the compiler no longer attempts to implicitly convert the RHS expression to the LHS type if the outer contextual type is bool or void, instead allowing both LHS and RHS to have different types, which is fine since subsequent codegen converts each to a bool anyway.

Allows us to write

if (someObject && someInt)
if (someObject && someBool)
if (someObject || someInt)
if (someObject || someBool)
...

but also has the (unfortunate?) side-effect that the following magically works in AS where TS would complain:

function test(): bool {
  return someObject || someInt;
}

@dcodeIO dcodeIO merged commit 9a66208 into master May 14, 2020
@dcodeIO dcodeIO deleted the logical-bool branch May 14, 2020 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate logical expression type rules
1 participant