Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,10 @@ private void executeCompiletimeExpr(ImCompiletimeExpr cte) {
private final GetAForB<ILconstObject, ImVar> globalForObject = new GetAForB<ILconstObject, ImVar>() {
@Override
public ImVar initFor(ILconstObject obj) {


ImVar res = JassIm.ImVar(obj.getTrace(), obj.getType(), obj.getType() + "_compiletime", false);
// Traced to the program, as the globals for handles are: the object's trace is its allocation, which
// lies in its class, and a global traced into a generic class is taken for a static field of that
// class (EliminateGenerics.identifyGenericGlobals), which this global, holding one object, is not.
ImVar res = JassIm.ImVar(imProg.getTrace(), obj.getType(), obj.getType() + "_compiletime", false);
imProg.getGlobals().add(res);
globalState.setValUntracked(res, obj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,11 @@ public void testPersistCompiletimeClass() {

@Test
public void testPersistCompiletimeNewGenericClass() {
// Translation is the assertion here: executing the synthesized generic
// runtime global through the interpreter still needs a separate attachment fix.
test()
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"class PureMap<T:>",
" T value",
" function put(T value)",
Expand All @@ -606,7 +606,10 @@ public void testPersistCompiletimeNewGenericClass() {
" return value",
"PureMap<int> map = compiletime(new PureMap<int>)",
"@compiletime function populate()",
" map.put(42)");
" map.put(42)",
"init",
" if map.get() == 42",
" testSuccess()");
}

@Test
Expand Down Expand Up @@ -658,6 +661,245 @@ public void testPersistCompiletimeClassTuple() {
" testSuccess()");
}

/** A compile-time object of a new-generic class keeps its fields: its holding global is no static of the class. */
@Test
public void testPersistCompiletimeObjectOfNewGenericClass() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"class Box<T:>",
" T value",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function build() returns Box<int>",
" let b = new Box<int>",
" b.value = 5",
" return b",
"let kept = compiletime(build())",
"init",
" let other = new Box<int>",
" other.value = 7",
" if kept.value == 5 and other.value == 7",
" testSuccess()");
}

/** On Lua a compile-time object of a new-generic class keeps its id after dropped ones, as #1504 keeps it for others. */
@Test
public void testCompiletimeObjectOfNewGenericClassKeepsIdAfterDroppedObjectOnLua() {
// Lua only: Jass gives each instantiation its own allocator after compile time, while the compile-time ids
// and the replay of the allocator are those of the erased class, so there the object gets the next id.
test().testLua(true).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"native testFail(string s)",
"class Box<T:>",
" T value",
" int storedId",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function build() returns Box<int>",
" new Box<int>",
" let keep = new Box<int>",
" keep.value = 5",
" keep.storedId = keep castTo int",
" new Box<int>",
" return keep",
"let kept = compiletime(build())",
"init",
" if kept castTo int != kept.storedId",
" testFail(\"id\")",
" let other = new Box<int>",
" other.value = 7",
" if kept.value != 5 or other.value != 7",
" testFail(\"value\")",
" testSuccess()");
}

/** A new-generic object kept in a @compiletime global, from a class with a real static, replays the global. */
@Test
public void testCompiletimeGlobalHoldingObjectOfNewGenericClassWithStatic() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"native testFail(string s)",
"class Box<T:>",
" static int made = 0",
" T v",
" construct(T v)",
" this.v = v",
" made++",
" function count() returns int",
" return made",
" function get() returns T",
" return v",
"@compiletime Box<int> early = null",
"@compiletime function makeEarly()",
" early = new Box<int>(5)",
"init",
" let b = new Box<int>(6)",
" let s = new Box<string>(\"x\")",
" if early.get() != 5",
" testFail(\"value\")",
" if b.count() != 1 or s.count() != 1 or early.count() != 1",
" testFail(\"count\")",
" testSuccess()");
}

/** A compile-time object of a new-generic class with a @compiletime static T keeps both per instantiation. */
@Test
public void testPersistCompiletimeObjectOfNewGenericClassWithStaticT() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"native testFail(string s)",
"class Box<T:>",
" @compiletime static T last",
" T value",
" construct(T value)",
" this.value = value",
" last = value",
" static function getLast() returns T",
" return last",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function build() returns Box<int>",
" return new Box<int>(5)",
"let kept = compiletime(build())",
"init",
" if kept.value != 5",
" testFail(\"value\")",
" if Box<int>.getLast() != 5",
" testFail(\"static int\")",
" let s = new Box<string>(\"x\")",
" if Box<string>.getLast() != \"x\" or Box<int>.getLast() != 5 or s.value != \"x\"",
" testFail(\"static string\")",
" testSuccess()");
}

/** Compile-time objects of two instantiations of one new-generic class each keep their own fields. */
@Test
public void testPersistCompiletimeObjectsOfTwoNewGenericInstantiations() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"native testFail(string s)",
"class Box<T:>",
" T value",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function buildInt() returns Box<int>",
" let b = new Box<int>",
" b.value = 5",
" return b",
"@compiletime function buildString() returns Box<string>",
" let b = new Box<string>",
" b.value = \"five\"",
" return b",
"let keptInt = compiletime(buildInt())",
"let keptString = compiletime(buildString())",
"init",
" let other = new Box<int>",
" other.value = 7",
" if keptInt.value != 5 or other.value != 7",
" testFail(\"int\")",
" if keptString.value != \"five\"",
" testFail(\"string\")",
" testSuccess()");
}

/** A compile-time object of a class which extends a new-generic class keeps its inherited field. */
@Test
public void testPersistCompiletimeObjectOfSubclassOfNewGenericClass() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"class Box<T:>",
" T value",
" function get() returns T",
" return value",
"class IntBox extends Box<int>",
" int extra",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function build() returns IntBox",
" let b = new IntBox",
" b.value = 5",
" b.extra = 6",
" return b",
"let kept = compiletime(build())",
"init",
" if kept.get() == 5 and kept.value == 5 and kept.extra == 6",
" testSuccess()");
}

/** A compile-time object of a new-generic class held by an old-generic object comes back with its fields. */
@Test
public void testPersistCompiletimeObjectOfNewGenericClassInOldGenericHolder() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.lines("package Test",
"native testSuccess()",
"class Box<T:>",
" T value",
"class Holder<T>",
" T item",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function build() returns Holder<Box<int>>",
" let b = new Box<int>",
" b.value = 5",
" let h = new Holder<Box<int>>",
" h.item = b",
" return h",
"let kept = compiletime(build())",
"init",
// through a local: on Jass kept.item.value fails without compile time too ("Could not find
// field array for Holder_item[...].Box_value"), a separate defect
" Box<int> item = kept.item",
" if item.value == 5",
" testSuccess()");
}

/** The compile-time object of a new-generic class keeps its fields with -inline -localOptimizations on both targets. */
@Test
public void testPersistCompiletimeObjectOfNewGenericClassOptimized() {
test().testLua(true).luaOnly(false).executeProg(true)
.runCompiletimeFunctions(true)
.executeProgOnlyAfterTransforms()
.inline().localOptimizations()
.lines("package Test",
"native testSuccess()",
"class Box<T:>",
" T value",
" function get() returns T",
" return value",
"function compiletime<T:>(T t) returns T",
" return t",
"@compiletime function build() returns Box<int>",
" let b = new Box<int>",
" b.value = 5",
" return b",
"let kept = compiletime(build())",
"init",
" let other = new Box<int>",
" other.value = 7",
" if kept.get() == 5 and other.get() == 7",
" testSuccess()");
}

// An object read back from an old-generic HashMap at compile time is the object that was put in.
@Test
public void testCompiletimeOldGenericMapGetReturnsTheSameObject() {
Expand Down
Loading