Author: [email protected]
Date: Wed Jun 17 12:08:06 2009
New Revision: 5574

Modified:
    trunk/dev/core/src/com/google/gwt/dev/jjs/impl/MethodInliner.java
    trunk/user/test/com/google/gwt/dev/jjs/test/NativeLongTest.java

Log:
Fixes issue 3710.  When inlining a method, add an explicit cast if the
method's return type is different from the type of the inlined expression.
This matches the similar cast added for the parameters of an inlined method.

Review by: scottb


Modified: trunk/dev/core/src/com/google/gwt/dev/jjs/impl/MethodInliner.java
==============================================================================
--- trunk/dev/core/src/com/google/gwt/dev/jjs/impl/MethodInliner.java    
(original)
+++ trunk/dev/core/src/com/google/gwt/dev/jjs/impl/MethodInliner.java   Wed  
Jun 17 12:08:06 2009
@@ -32,6 +32,7 @@
  import com.google.gwt.dev.jjs.ast.JReturnStatement;
  import com.google.gwt.dev.jjs.ast.JStatement;
  import com.google.gwt.dev.jjs.ast.JThisRef;
+import com.google.gwt.dev.jjs.ast.JType;
  import com.google.gwt.dev.jjs.ast.JVisitor;
  import com.google.gwt.dev.jjs.ast.js.JMultiExpression;

@@ -221,6 +222,7 @@
            if (expr != null) {
              if (!ignoringReturnValue || expr.hasSideEffects()) {
                JExpression clone = cloner.cloneExpression(expr);
+              clone = maybeCast(clone, body.getMethod().getType());
                multi.exprs.add(clone);
              }
            }
@@ -451,13 +453,7 @@
        JExpression arg = methodCall.getArgs().get(paramIndex);
        JExpression clone = cloner.cloneExpression(arg);

-      /*
-       * Insert an implicit cast if the types differ; it might get  
optimized out
-       * later, but in some cases it will force correct math evaluation.
-       */
-      if (clone.getType() != x.getType()) {
-        clone = new JCastOperation(clone.getSourceInfo(), x.getType(),  
clone);
-      }
+      clone = maybeCast(clone, x.getType());
        ctx.replaceMe(clone);
      }
    }
@@ -491,6 +487,17 @@

    public static boolean exec(JProgram program) {
      return new MethodInliner(program).execImpl();
+  }
+
+  /**
+   * Insert an implicit cast if the types differ; it might get optimized  
out
+   * later, but in some cases it will force correct math evaluation.
+   */
+  private static JExpression maybeCast(JExpression exp, JType targetType) {
+    if (exp.getType() != targetType) {
+      exp = new JCastOperation(exp.getSourceInfo(), targetType, exp);
+    }
+    return exp;
    }

    private JMethod currentMethod;

Modified: trunk/user/test/com/google/gwt/dev/jjs/test/NativeLongTest.java
==============================================================================
--- trunk/user/test/com/google/gwt/dev/jjs/test/NativeLongTest.java      
(original)
+++ trunk/user/test/com/google/gwt/dev/jjs/test/NativeLongTest.java     Wed Jun 
 
17 12:08:06 2009
@@ -23,6 +23,21 @@
   * LongLibTest.
   */
  public class NativeLongTest extends GWTTestCase {
+  /**
+   * A class that wraps an int. See {...@link  
NativeLongTest#testImplicitCastToLong()}.
+   */
+  private static class IntegerWrapper {
+    private final int i;
+
+    public IntegerWrapper(int i) {
+      this.i = i;
+    }
+
+    public long longValue() {
+      return i; // implicit cast to long
+    }
+  }
+
    private static class RequestIdFactory {
      static RequestIdFactory instance = new RequestIdFactory();

@@ -122,6 +137,10 @@
      l += 5;
      assertEquals(15, l);
      assertTrue(15 == l);
+
+    // Issue 3710
+    IntegerWrapper wrap = new IntegerWrapper(20);
+    assertEquals(400L, wrap.longValue() * wrap.longValue());
    }

    public void testInlinedIntInitializer() {

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to