Skip to content

Commit d080be0

Browse files
committed
Properly propagate upper bounds through ternaries
1 parent 2ea0c6c commit d080be0

4 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/org/jetbrains/java/decompiler/modules/decompiler/exps/FunctionExprent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,12 @@ public CheckTypesResult checkExprTypeBounds() {
370370
case TERNARY:
371371
VarType supertype = getExprType();
372372
result.addExprLowerBound(param1, VarType.VARTYPE_BOOLEAN);
373+
result.addExprUpperBound(param1, VarType.VARTYPE_BOOLEAN);
374+
373375
result.addExprLowerBound(param2, VarType.findFamilyBottom(supertype.typeFamily));
374376
result.addExprLowerBound(lstOperands.get(2), VarType.findFamilyBottom(supertype.typeFamily));
377+
result.addExprUpperBound(param2, supertype);
378+
result.addExprUpperBound(lstOperands.get(2), supertype);
375379
break;
376380
case I2L:
377381
case I2F:

src/org/jetbrains/java/decompiler/modules/decompiler/vars/VarTypeProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ private boolean changeVarExprentType(Exprent exprent, VarType newType, Bound bou
249249
// Already have a type? Find the lower of the two; the upper bound falls.
250250
newMaxType = VarType.meet(currentMaxType, newType);
251251
}
252+
ValidationHelper.assertTrue(newMaxType != null, "Trying to lower the maximum type of disjoint variables!");
252253

253254
upperBounds.put(pair, newMaxType);
254255
}

testData/results/pkg/TestReturnTernaryChar.dec

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public class TestReturnTernaryChar {
1212
public int testByteChar(String input) {
1313
return "hello".equals(input) ? 34 : 21;// 13
1414
}
15+
16+
public int get(int v) {
17+
int w = v == 0 ? 36009 : v;// 17
18+
System.out.println(w);// 18
19+
return w;// 19
20+
}
1521
}
1622

1723
class 'pkg/TestReturnTernaryChar' {
@@ -67,9 +73,32 @@ class 'pkg/TestReturnTernaryChar' {
6773
f 12
6874
10 12
6975
}
76+
77+
method 'get (I)I' {
78+
0 16
79+
1 16
80+
2 16
81+
3 16
82+
4 16
83+
5 16
84+
9 16
85+
a 16
86+
b 17
87+
c 17
88+
d 17
89+
e 17
90+
f 17
91+
10 17
92+
11 17
93+
12 18
94+
13 18
95+
}
7096
}
7197

7298
Lines mapping:
7399
5 <-> 5
74100
9 <-> 9
75101
13 <-> 13
102+
17 <-> 17
103+
18 <-> 18
104+
19 <-> 19

testData/src/java8/pkg/TestReturnTernaryChar.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ public int testShortChar(String input) {
1212
public int testByteChar(String input) {
1313
return "hello".equals(input) ? 34 : 21;
1414
}
15+
16+
public int get(int v) {
17+
int w = v == 0 ? 36009 : v;
18+
System.out.println(w);
19+
return w;
20+
}
1521
}

0 commit comments

Comments
 (0)