gwtproject / gwtproject/gwt

Identify when consecutive branches of if/else have the same contents and merge them

Open
#10,285 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.6k
Forks
397
Avg merge
4d 23h
Merged PRs (30d)
7

Description

Corollary of #10283.

It isn't uncommon for a method body to look something like this:

if (!ready) {
  return;
}

if (input.isEmpty()) {
  return;
}

for (Item i : input) {...}

#10240 will get this code to something like

if (!ready) {
  return;
} else if (input.isEmpty()) {
  return;
} else {
  for (Item i : input) {...}
}

When we see consecutive branches with the same body, their conditions can be merged with ||, and short circuiting will avoid evaluating more than we should:

if ((!ready) || (input.isEmpty())) {
  return;
} else {
  for (Item i : input) {...}
}

We don't generally have a "are these two JNodes equal" as far as I'm aware - #10283 will also need something like this (or also a "prefix"/"suffix" check for blocks).


With #10248, we then would have the "then" be an empty block, so the conditiona can be flipped, and the method body is (with the ! distributed)

if (ready && !input.isEmpty() {
  for (Item i : input) {...}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the transformations discussed in #10283, #10240, and #10248, then trace how the compiler represents and compares JNodes and branch blocks. Done means consecutive if/else branches with equivalent bodies can be merged using short-circuiting while preserving evaluation order and behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.