Skip to content

Commit

Permalink
Allow switch expr's type to be replaced when merging compilationunits
Browse files Browse the repository at this point in the history
niloc132 committed Oct 18, 2024
1 parent 9c0955f commit a98a418
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -45,6 +45,10 @@ public boolean hasSideEffects() {
return true;
}

public void setType(JType type) {
this.type = type;
}

@Override
public void traverse(JVisitor visitor, Context ctx) {
if (visitor.visit(this, ctx)) {
6 changes: 6 additions & 0 deletions dev/core/src/com/google/gwt/dev/jjs/impl/UnifyAst.java
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@
import com.google.gwt.dev.jjs.ast.JProgram;
import com.google.gwt.dev.jjs.ast.JReferenceType;
import com.google.gwt.dev.jjs.ast.JStringLiteral;
import com.google.gwt.dev.jjs.ast.JSwitchExpression;
import com.google.gwt.dev.jjs.ast.JThisRef;
import com.google.gwt.dev.jjs.ast.JTryStatement;
import com.google.gwt.dev.jjs.ast.JType;
@@ -342,6 +343,11 @@ public void endVisit(JStringLiteral x, Context ctx) {
instantiate(stringType);
}

@Override
public void endVisit(JSwitchExpression x, Context ctx) {
x.setType(translate(x.getType()));
}

@Override
public void endVisit(JThisRef x, Context ctx) {
assert !x.getType().isExternal();
Original file line number Diff line number Diff line change
@@ -459,11 +459,23 @@ public static final int pick(HasSwitchMethod whichSwitch) {
case ZERO -> 5;
};
}
public static final String select(HasSwitchMethod whichSwitch) {
if (Math.random() > 2) {
return "none";
}
return switch(whichSwitch) {
case A -> "1";
case RED -> "2";
case SUNDAY, JANUARY -> "4";
case ZERO -> "5";
};
}
}

HasSwitchMethod uninlinedValue = Math.random() > 2 ? HasSwitchMethod.A : HasSwitchMethod.RED;
assertEquals(2, HasSwitchMethod.which(uninlinedValue));
assertEquals(4, HasSwitchMethod.pick(uninlinedValue));
assertEquals("hello 2", "hello " + HasSwitchMethod.select(uninlinedValue));
}

private static final String ONE = "1";

0 comments on commit a98a418

Please sign in to comment.