“500feceb8f45e51102e10f47b285aa337504923f”上不存在“docs-en/21-tdinternal/01-arch.md”
提交 568453e3 编写于 作者: V vromero

7167125: Two variables after the same operation in a inner class return different results

Reviewed-by: jjg, mcimadamore
上级 4b392991
...@@ -3066,55 +3066,38 @@ public class Lower extends TreeTranslator { ...@@ -3066,55 +3066,38 @@ public class Lower extends TreeTranslator {
} }
public void visitAssignop(final JCAssignOp tree) { public void visitAssignop(final JCAssignOp tree) {
if (!tree.lhs.type.isPrimitive() && final boolean boxingReq = !tree.lhs.type.isPrimitive() &&
tree.operator.type.getReturnType().isPrimitive()) { tree.operator.type.getReturnType().isPrimitive();
// boxing required; need to rewrite as x = (unbox typeof x)(x op y);
// or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y) // boxing required; need to rewrite as x = (unbox typeof x)(x op y);
// (but without recomputing x) // or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y)
JCTree newTree = abstractLval(tree.lhs, new TreeBuilder() { // (but without recomputing x)
public JCTree build(final JCTree lhs) { JCTree newTree = abstractLval(tree.lhs, new TreeBuilder() {
JCTree.Tag newTag = tree.getTag().noAssignOp(); public JCTree build(final JCTree lhs) {
// Erasure (TransTypes) can change the type of JCTree.Tag newTag = tree.getTag().noAssignOp();
// tree.lhs. However, we can still get the // Erasure (TransTypes) can change the type of
// unerased type of tree.lhs as it is stored // tree.lhs. However, we can still get the
// in tree.type in Attr. // unerased type of tree.lhs as it is stored
Symbol newOperator = rs.resolveBinaryOperator(tree.pos(), // in tree.type in Attr.
newTag, Symbol newOperator = rs.resolveBinaryOperator(tree.pos(),
attrEnv, newTag,
tree.type, attrEnv,
tree.rhs.type); tree.type,
JCExpression expr = (JCExpression)lhs; tree.rhs.type);
if (expr.type != tree.type) JCExpression expr = (JCExpression)lhs;
expr = make.TypeCast(tree.type, expr); if (expr.type != tree.type)
JCBinary opResult = make.Binary(newTag, expr, tree.rhs); expr = make.TypeCast(tree.type, expr);
opResult.operator = newOperator; JCBinary opResult = make.Binary(newTag, expr, tree.rhs);
opResult.type = newOperator.type.getReturnType(); opResult.operator = newOperator;
JCTypeCast newRhs = make.TypeCast(types.unboxedType(tree.type), opResult.type = newOperator.type.getReturnType();
opResult); JCExpression newRhs = boxingReq ?
return make.Assign((JCExpression)lhs, newRhs).setType(tree.type); make.TypeCast(types.unboxedType(tree.type),
} opResult) :
}); opResult;
result = translate(newTree); return make.Assign((JCExpression)lhs, newRhs).setType(tree.type);
return; }
} });
tree.lhs = translate(tree.lhs, tree); result = translate(newTree);
tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
// If translated left hand side is an Apply, we are
// seeing an access method invocation. In this case, append
// right hand side as last argument of the access method.
if (tree.lhs.hasTag(APPLY)) {
JCMethodInvocation app = (JCMethodInvocation)tree.lhs;
// if operation is a += on strings,
// make sure to convert argument to string
JCExpression rhs = (((OperatorSymbol)tree.operator).opcode == string_add)
? makeString(tree.rhs)
: tree.rhs;
app.args = List.of(rhs).prependList(app.args);
result = app;
} else {
result = tree;
}
} }
/** Lower a tree of the form e++ or e-- where e is an object type */ /** Lower a tree of the form e++ or e-- where e is an object type */
......
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7167125
* @summary Two variables after the same operation in a inner class return
* different results
* @run main DiffResultAfterSameOperationInnerClasses
*/
public class DiffResultAfterSameOperationInnerClasses {
public int i = 1;
private int j = 1;
public String s1 = "Hi, ";
private String s2 = "Hi, ";
public static void main(String[] args) {
InnerClass inner =
new DiffResultAfterSameOperationInnerClasses().new InnerClass();
if (!inner.test()) {
throw new AssertionError("Different results after same calculation");
}
}
class InnerClass {
public boolean test() {
i += i += 1;
j += j += 1;
s1 += s1 += "dude";
s2 += s2 += "dude";
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
return (i == j && i == 3 &&
s1.equals(s2) && s1.endsWith("Hi, Hi, dude"));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册