Skip to content

Commit c44b052

Browse files
committed
review fix
1 parent be6c203 commit c44b052

3 files changed

Lines changed: 54 additions & 7 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/attributes/AttrPossibleFunctionSignatures.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static ImmutableCollection<FunctionSignature> calculate(FunctionCall fc)
3737
}
3838
} // TODO else check?
3939

40-
VariableBinding mapping = givenBinding(fc, sig.getDefinitionTypeVariables());
40+
VariableBinding mapping = sig.getMapping().union(givenBinding(fc, sig.getDefinitionTypeVariables()));
4141
sig = sig.setTypeArgs(fc, mapping);
4242

4343
resultBuilder.add(sig);
@@ -229,7 +229,7 @@ public static ImmutableCollection<FunctionSignature> calculate(ExprMemberMethod
229229
}
230230

231231
// Apply explicit type args from the call-site (e.g., c.foo<T,...>(...))
232-
VariableBinding explicit = GenericsHelper.givenBinding(mm, sig.getDefinitionTypeVariables());
232+
VariableBinding explicit = sig.getMapping().union(GenericsHelper.givenBinding(mm, sig.getDefinitionTypeVariables()));
233233
sig = sig.setTypeArgs(mm, explicit);
234234

235235
prepared.add(sig);

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/ImTranslator.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,36 @@ private static boolean hasTypeVarNamed(ImTypeVars vars, String name) {
113113
return false;
114114
}
115115

116+
private static String uniqueTypeVarName(ImTypeVars vars, TypeParamDef tp) {
117+
String name = tp.getName();
118+
if (!hasTypeVarNamed(vars, name)) {
119+
return name;
120+
}
121+
122+
String baseName = name + "$" + typeParamOwnerName(tp);
123+
String candidate = baseName;
124+
int suffix = 2;
125+
while (hasTypeVarNamed(vars, candidate)) {
126+
candidate = baseName + suffix;
127+
suffix++;
128+
}
129+
return candidate;
130+
}
131+
132+
private static String typeParamOwnerName(TypeParamDef tp) {
133+
de.peeeq.wurstscript.ast.Element e = tp.getParent();
134+
while (e != null) {
135+
if (e instanceof FuncDef) {
136+
return ((FuncDef) e).getName();
137+
}
138+
if (e instanceof NamedScope) {
139+
return ((NamedScope) e).getName();
140+
}
141+
e = e.getParent();
142+
}
143+
return "type";
144+
}
145+
116146
public Map<TypeParamDef, ImTypeVar> getTypeVarOverridesForClass(ClassDef cd) {
117147
Map<TypeParamDef, ImTypeVar> m = capturedOwnerTypeVarsByStaticClass.get(cd);
118148
return (m == null) ? Collections.emptyMap() : m;
@@ -1027,11 +1057,7 @@ private void handleTypeParameters(TypeParamDefs tps) {
10271057

10281058
private void handleTypeParameter(TypeParamDef tp) {
10291059
if (tp.getTypeParamConstraints() instanceof TypeExprList) {
1030-
ImTypeVar base = typeVariable.getFor(tp);
1031-
if (hasTypeVarNamed(typeVars, base.getName())) {
1032-
return;
1033-
}
1034-
ImTypeVar v = JassIm.ImTypeVar(base.getName());
1060+
ImTypeVar v = JassIm.ImTypeVar(uniqueTypeVarName(typeVars, tp));
10351061
typeVariableReverse.put(v, tp);
10361062
typeVars.add(v);
10371063
}

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsWithTypeclassesTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,27 @@ public void genericStaticArray_staticMethods_qualifiedSpecializedTypeReceiver_ru
14611461
);
14621462
}
14631463

1464+
@Test
1465+
public void genericStaticArray_staticMethodShadowedTypeParam_runtime() {
1466+
testAssertOkLines(true,
1467+
"package test",
1468+
" native testSuccess()",
1469+
" class Box<T:>",
1470+
" private static T array store",
1471+
" static function put(int i, T v)",
1472+
" store[i] = v",
1473+
" static function get(int i) returns T",
1474+
" return store[i]",
1475+
" static function same<T:>(T v) returns T",
1476+
" return v",
1477+
" init",
1478+
" Box<int>.put(1, 42)",
1479+
" if Box<int>.get(1) == 42 and Box<int>.same<real>(1.5) == 1.5",
1480+
" testSuccess()",
1481+
"endpackage"
1482+
);
1483+
}
1484+
14641485
@Test
14651486
public void genericStaticTuple_runtime() {
14661487
testAssertOkLines(true,

0 commit comments

Comments
 (0)