Skip to content

Commit b5155aa

Browse files
authored
Allow JsUtils.uncheckedCast to be inlined away (#10165)
This will have minimal impacts in cases where JS inlining and static eval passes are able to make improvements, but will improve cases where Java/GWT passes can benefit from this method being removed, such as removing clinits. The new implementation is effectively what jsinterop-base's Js.uncheckedCast() does, so is unlikely to cause any surprises. Fixes #10055
1 parent cfb4e79 commit b5155aa

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

dev/core/src/com/google/gwt/dev/jjs/impl/MethodInliner.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ public void endVisit(JMethodCall x, Context ctx) {
134134
return;
135135
}
136136

137+
JMethod.Specialization specialization = getCurrentMethod().getSpecialization();
138+
// If we have a specialization, don't inline that away - specializations must be called
139+
// so they aren't pruned or type tightened into uselessness.
140+
if (specialization != null) {
141+
if (specialization.getTargetMethod() == method) {
142+
return;
143+
}
144+
// We might have had a static impl that in turn will have a specialization - ensure we
145+
// also don't inline that specialization away. Note that this check looks for it "backwards"
146+
// by checking if the inlinable method has an instance method, since current method might
147+
// have once had a static method, but it was already removed.
148+
if (specialization.getTargetMethod() == program.instanceMethodForStaticImpl(method)) {
149+
return;
150+
}
151+
}
152+
137153
if (tryInlineMethodCall(x, ctx) == InlineResult.BLACKLIST) {
138154
// Do not try to inline this method again
139155
cannotInline.add(method);

user/super/com/google/gwt/emul/java/util/EnumSet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static javaemul.internal.InternalPreconditions.checkState;
2222

2323
import javaemul.internal.ArrayHelper;
24+
import javaemul.internal.JsUtils;
2425
import javaemul.internal.annotations.SpecializeMethod;
2526

2627
/**
@@ -136,7 +137,7 @@ public EnumSet<E> clone() {
136137
@SpecializeMethod(params = Enum.class, target = "containsEnum")
137138
@Override
138139
public boolean contains(Object o) {
139-
return (o instanceof Enum) && containsEnum((Enum) o);
140+
return (o instanceof Enum) && containsEnum(JsUtils.uncheckedCast(o));
140141
}
141142

142143
private boolean containsEnum(Enum e) {
@@ -151,7 +152,7 @@ public Iterator<E> iterator() {
151152
@SpecializeMethod(params = Enum.class, target = "removeEnum")
152153
@Override
153154
public boolean remove(Object o) {
154-
return (o instanceof Enum) && removeEnum((Enum) o);
155+
return (o instanceof Enum) && removeEnum(JsUtils.uncheckedCast(o));
155156
}
156157

157158
private boolean removeEnum(Enum e) {

user/super/com/google/gwt/emul/javaemul/internal/JsUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ public static native boolean unsafeCastToBoolean(Object bool) /*-{
8282
}-*/;
8383

8484
@UncheckedCast
85-
public static native <T> T uncheckedCast(@DoNotAutobox Object o) /*-{
86-
return o;
87-
}-*/;
85+
public static <T> T uncheckedCast(@DoNotAutobox Object o) {
86+
return (T) o;
87+
}
8888

8989
@UncheckedCast
9090
public static native <T> T getProperty(Object map, String key) /*-{

user/super/com/google/gwt/emul/javaemul/internal/annotations/SpecializeMethod.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* An annotation to mark a given method as being specialized. If the specified
2323
* parameters and return context match of a JMethodCall, then the call
2424
* is retargeted at the specialized version.
25+
* <p/>
26+
* The annotated method must call the target method directly, and the compiler
27+
* must not inline the target method to prevent it from being pruned while it
28+
* still might be used.
2529
*/
2630
@Target(ElementType.METHOD)
2731
@CompilerHint

0 commit comments

Comments
 (0)