Skip to content

Commit a5a8d74

Browse files
committed
Fixed type common super type computation when type params are involved
Signed-off-by: Sebastian Zarnekow <sebastian.zarnekow@gmail.com>
1 parent d364c05 commit a5a8d74

File tree

3 files changed

+568
-2
lines changed

3 files changed

+568
-2
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Sebastian Zarnekow (http://www.itemis.eu) and others.
3+
* This program and the accompanying materials are made available under the
4+
* terms of the Eclipse Public License 2.0 which is available at
5+
* http://www.eclipse.org/legal/epl-2.0.
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*******************************************************************************/
9+
package org.eclipse.xtend.core.tests.compiler
10+
11+
import org.junit.Test
12+
13+
/**
14+
* @author Sebastian - Initial contribution and API
15+
*/
16+
class CompilerBug2351Test extends AbstractXtendCompilerTest {
17+
18+
@Test def void test_01() {
19+
assertCompilesTo(
20+
'''
21+
package p
22+
23+
import java.math.BigDecimal
24+
25+
class C {
26+
def Object m(int i) {
27+
val r = switch(i) {
28+
case 1: 1.0 // double
29+
case 2: 1.0bd // BigDecimal
30+
default: weird("")
31+
}
32+
return r
33+
}
34+
35+
private def <T> T weird(Object object) {
36+
return null;
37+
}
38+
}
39+
''',
40+
'''
41+
package p;
42+
43+
import java.math.BigDecimal;
44+
45+
@SuppressWarnings("all")
46+
public class C {
47+
public Object m(final int i) {
48+
Object _switchResult = null;
49+
switch (i) {
50+
case 1:
51+
_switchResult = Double.valueOf(1.0);
52+
break;
53+
case 2:
54+
_switchResult = new BigDecimal("1.0");
55+
break;
56+
default:
57+
_switchResult = this.<Object>weird("");
58+
break;
59+
}
60+
final Object r = _switchResult;
61+
return r;
62+
}
63+
64+
private <T extends Object> T weird(final Object object) {
65+
return null;
66+
}
67+
}
68+
'''
69+
)
70+
}
71+
72+
@Test def void test_02() {
73+
assertCompilesTo(
74+
'''
75+
package p
76+
77+
import java.math.BigDecimal
78+
79+
class C {
80+
def void m() {
81+
val s1 = switch 45 { // int
82+
case 2: 1
83+
case 3: 3.0
84+
case 78: exit('.')
85+
case 4: 4.0 / 5.0
86+
default: 6
87+
}
88+
val s2 = switch 45 { // Number
89+
case 78: exit('.')
90+
case 2: 1
91+
case 3: 3.0
92+
case 4: 4.0/5.0
93+
default: 6
94+
}
95+
s1.toString
96+
s2.toString
97+
}
98+
99+
def <T> T exit(String message) {
100+
throw new UnsupportedOperationException(message)
101+
}
102+
}
103+
''',
104+
'''
105+
package p;
106+
107+
@SuppressWarnings("all")
108+
public class C {
109+
public void m() {
110+
Number _switchResult = null;
111+
final int _switchValue = 45;
112+
switch (_switchValue) {
113+
case 2:
114+
_switchResult = Integer.valueOf(1);
115+
break;
116+
case 3:
117+
_switchResult = Double.valueOf(3.0);
118+
break;
119+
case 78:
120+
_switchResult = ((Number)this.<Number>exit("."));
121+
break;
122+
case 4:
123+
_switchResult = Double.valueOf((4.0 / 5.0));
124+
break;
125+
default:
126+
_switchResult = Integer.valueOf(6);
127+
break;
128+
}
129+
final Number s1 = ((Number)_switchResult);
130+
Number _switchResult_1 = null;
131+
final int _switchValue_1 = 45;
132+
switch (_switchValue_1) {
133+
case 78:
134+
_switchResult_1 = ((Number)this.<Number>exit("."));
135+
break;
136+
case 2:
137+
_switchResult_1 = Integer.valueOf(1);
138+
break;
139+
case 3:
140+
_switchResult_1 = Double.valueOf(3.0);
141+
break;
142+
case 4:
143+
_switchResult_1 = Double.valueOf((4.0 / 5.0));
144+
break;
145+
default:
146+
_switchResult_1 = Integer.valueOf(6);
147+
break;
148+
}
149+
final Number s2 = ((Number)_switchResult_1);
150+
s1.toString();
151+
s2.toString();
152+
}
153+
154+
public <T extends Object> T exit(final String message) {
155+
throw new UnsupportedOperationException(message);
156+
}
157+
}
158+
'''
159+
)
160+
}
161+
}

0 commit comments

Comments
 (0)