Skip to content

Commit 5d8969e

Browse files
committed
Preserve specialized generic static bindings
1 parent 6c8d205 commit 5d8969e

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,15 @@ public void visit(ImVarArrayAccess access) {
14431443
}
14441444

14451445
private ImVar specializedGlobal(ImVar original) {
1446+
ImTranslator.Specialisation existing = translator.specialisationOf(original);
1447+
if (existing != null && translator.genericStaticOwnerOf(original) != null
1448+
&& !existing.typeArguments().isEmpty()) {
1449+
// This access already names a concrete instantiation of the static field.
1450+
// Its own binding wins over the type arguments of the function which happens
1451+
// to contain it; in particular, specialising touch<pair> must not turn an
1452+
// access produced by Box<int> into one for Box<pair>.
1453+
return original;
1454+
}
14461455
ImClass owner = globalToClass.get(original);
14471456
if (owner == null) {
14481457
return original;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,34 @@ public void tupleAssignmentCapturesLvalueBeforeRhs() throws IOException {
446446
assertFalse(compiled.contains("tupleCopy"));
447447
}
448448

449+
@Test
450+
public void tupleSpecializationPreservesExplicitGenericStaticOwner() throws IOException {
451+
test().testLua(true).executeProg().lines(
452+
"package Test",
453+
"native testSuccess()",
454+
"tuple pair(int x, int y)",
455+
"class Box<T:>",
456+
" static int counter",
457+
" static function setCounter(int value)",
458+
" counter = value",
459+
" static function incrementCounter()",
460+
" counter++",
461+
" static function getCounter() returns int",
462+
" return counter",
463+
"function touch<T:>(T value)",
464+
" Box<int>.incrementCounter()",
465+
"init",
466+
" Box<int>.setCounter(10)",
467+
" Box<pair>.setCounter(100)",
468+
" touch<pair>(pair(1, 2))",
469+
" if Box<int>.getCounter() == 11 and Box<pair>.getCounter() == 100",
470+
" testSuccess()"
471+
);
472+
473+
String compiled = compiledLua("tupleSpecializationPreservesExplicitGenericStaticOwner");
474+
assertFalse(compiled.contains("tupleCopy"));
475+
}
476+
449477
@Test
450478
public void compiletimeGenericArrayReplayLeavesAreSplit() {
451479
String compiled = compileLuaWithRunArgs(

0 commit comments

Comments
 (0)