Repository navigation
Description
GWT version: 2.12.1, latest
Browser (with version): any
Operating System: any
Description
While most empty clinits seem to be correctly optimized out, sometimes they unnecessarily remain in the compiled output.
As an example, if you build https://github.com/gwtproject/gwt-site-webapp/ but change turn draft mode off, $clinit_Object is correctly removed, but $clinit_Boolean is not, despite being empty:
function $clinit_Boolean(){
$clinit_Boolean = emptyMethod;
}A Java lambda of type elemental2.core.ReadonlyArray.FindPredicateFn
(el, index, parent) -> !"none".equals(((HTMLElement) el).style.display)
is translated as
function GWTProjectEntryPoint$lambda$9$Type(){
}
defineClass(81, $wnd.Function, {}, GWTProjectEntryPoint$lambda$9$Type);
_.onInvoke_2 = function onInvoke_5(arg0, arg1, arg2){
return $clinit_GWTProjectEntryPoint() , $clinit_Boolean() , $equals('none', castToNative(arg0, $wnd.HTMLElement).style.display)?false:true;
}
;
which looks like boxing of boolean to Boolean via inlining Boolean.valueOf(boolean) gone a bit wrong:
gwt/user/super/com/google/gwt/emul/java/lang/Boolean.java
Lines 63 to 65 in c2229e7
| public static Boolean valueOf(boolean b) { | |
| return b ? $create(true) : $create(false); | |
| } |
private boolean shouldEnhanceLink(Element link) {
return
// Enhance only local links
isSameOriginRexp.test(link.getAttribute("href"))
// Do not load links that are marked as full page reload
&& !Boolean.parseBoolean(link.getAttribute("data-full-load"));
}is inlined into two different places, bringing Boolean.parseBoolean with it:
function $lambda$16(this$static, evt_0){
//...
if ($test(isSameOriginRexp, target.getAttribute('href')) && ($clinit_Boolean() , !$equalsIgnoreCase('true', target.getAttribute('data-full-load'))) && $handleAsClick(castToNative(evt_0, $wnd.MouseEvent))) {
//...
}
}function $lambda$19(el_0){
//...
if ($test(isSameOriginRexp, el_0.getAttribute('href')) && ($clinit_Boolean() , !$equalsIgnoreCase('true', el_0.getAttribute('data-full-load')))) {
//...
}
}
gwt/user/super/com/google/gwt/emul/java/lang/Boolean.java
Lines 55 to 57 in c2229e7
| public static boolean parseBoolean(String s) { | |
| return "true".equalsIgnoreCase(s); | |
| } |
Steps to reproduce
Check out gwt-site-webapp @ 27070768ae49c048535358a03c70a5b9d84ddf98
git clone git@github.com:gwtproject/gwt-site-webapp.git
cd gwt-site-webapp
git checkout 27070768ae49c048535358a03c70a5b9d84ddf98Change draft compile to false
diff --git a/pom.xml b/pom.xml
index 747c5b2..f8fc02e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -81,7 +81,7 @@
<compilerArgs>
<arg>-saveSource</arg>
</compilerArgs>
- <draftCompile>true</draftCompile>
+ <draftCompile>false</draftCompile>
<style>PRETTY</style>
<failOnError>true</failOnError>
<launcherDir>${project.build.directory}/www</launcherDir>Build the project, with mvn clean verify
Expected: If $clinit_Boolean is present, it is non-empty.
Actual: $clinit_Boolean is present, and only reassigns itself to emptyMethod
Activity
niloc132 commentedon Oct 21, 2025
This can be traced to the clinit not being optimized out while in Java - though it is optimized out in JS, that isn't good enough, since it still has the
$clinit_Boolean = emptyMethod;assignment.The implementation of Boolean has two fields, TRUE and FALSE, each of which are given a primitive value despite being boxed Booleans. Java requires then that we box via Boolean.valueOf(...), which is in turn implemented (eventually) via JsUtils.uncheckedCast(). That is a JSNI method, which isn't obviously sideeffect-free, so the method call must remain even if the field gets optimized out. So, when the GWT AST optimization is done, the method has gone from
to
The JS optimizer then inherits that code as
and removes the two useless methods (through inlining, which makes it obvious that the method serves no purpose), leaving
which it can't optimize away.
There are a few options for a fix here.
@UncheckedCasthandling to always assume that these methods are trivial and have no side effects. In theory, this annotation is "internal only", so GWT and jsinterop-base should be the only things using it, but practically someone could be using this in their own project in a method with side effects. This probably isn't safe to do.JsUtils.uncheckedCastto be more like jsinterop-base'sJs.uncheckedCast- that is, the implementation can be Java instead of JSNI, but still with the@UncheckedCastto suppress the java type check on the way out. This is a simple fix that obviously works (since jsinterop-base does it), and I have confirmed that this fixes this case. It isn't quite as satisfying, since it won't improve related code automatically.Allow JsUtils.uncheckedCast to be inlined away
Allow JsUtils.uncheckedCast to be inlined away (gwtproject#10165)