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

feat(const-prop): propagate type casts #10144

Closed
wants to merge 3 commits into from

Conversation

brandonspark
Copy link
Contributor

@brandonspark brandonspark commented Apr 23, 2024

What:

This PR makes it so that when we do constant propagation on a literal that is being casted, we propagate the literal.

Why:

People asked for this.

How:

I made it so evaluation of something being casted just evaluated the inner contents.

Alternatives:

This behavior may be intentional -- in a C-like language, for instance, where you can do something like (int) false, which to my knowledge should actually be 0, and not a literal false, this could be wrong. But, I think that this is probably a low-probability occurrence. If bothered by this, we can create a whitelist of languages in which it is safe to do this unwrapping of the cast.

Test plan:

Automated tests.

Copy link
Contributor

github-actions bot commented Apr 23, 2024

PR checklist:

  • Purpose of the code is evident to future readers
  • Tests included or PR comment includes a reproducible test plan
  • Documentation is up-to-date
  • A changelog entry was added to changelog.d for any user-facing change
  • Change has no security implications (otherwise, ping security team)

If you're unsure about any of this, please see:

@@ -0,0 +1,10 @@
Constant propagation: Constants that are being type-casted will now
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who asked for it? Didn't they provide any other example?

@@ -0,0 +1,10 @@
Constant propagation: Constants that are being type-casted will now
propagate properly, e.g. in the following example:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that branch name, and without "Closes SAF-NNN" or similar in the PR comment, I think Linear won't link the PR with the ticket.

@@ -230,6 +230,7 @@ let rec eval (env : G.svalue Dataflow_var_env.t) (exp : IL.exp) : G.svalue =
match exp.e with
| Fetch lval -> eval_lval env lval
| Literal li -> G.Lit li
| Cast (_, e) -> eval env e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of caution, I'd prefer this is restricted to TS for now. I think TS is kind of a special case. Even in Java you would need to do something when casting e.g. (double)42. That would mean passing the language to eval, it's a bit more work, but this can be useful later too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rebase, I just merged another PR that adds a field lang : Lang.t to the env here.

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

Successfully merging this pull request may close these issues.

None yet

3 participants