Skip to content

Commit 66b0995

Browse files
authored
Fix generic overload resolve bug (#1117)
1 parent acac4cb commit 66b0995

File tree

3 files changed

+138
-5
lines changed

3 files changed

+138
-5
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static FuncLink calculate(final ExprFuncRef node) {
9898
cands.addAll(methods);
9999
cands.addAll(exts);
100100

101-
var argTypes = AttrFuncDef.argumentTypesPre(node);
101+
var argTypes = AttrFuncDef.argumentTypes(node);
102102

103103
// Pass 1: exact matches
104104
java.util.ArrayList<FuncLink> exactLinks = new java.util.ArrayList<>();
@@ -242,9 +242,11 @@ public static List<WurstType> argumentTypes(StmtCall node) {
242242
pIndex++;
243243
}
244244
if (hasInferredType) {
245-
// if there are unknown parameter types, use an approximated function type for overloading resolution
246-
WurstType resultType = WurstTypeInfer.instance();
247-
argType = new WurstTypeClosure(paramTypes, resultType);
245+
WurstType bodyType = closure.getImplementation().attrTyp();
246+
if (bodyType == null || bodyType instanceof WurstTypeUnknown) {
247+
bodyType = WurstTypeInfer.instance();
248+
}
249+
argType = new WurstTypeClosure(paramTypes, bodyType);
248250
} else {
249251
// if there are no unknown types for the argument, then it should be safe to directly calculate the type
250252
argType = arg.attrTyp();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private void adaptSubmethods(List<ImMethod> oldSubMethods, ImMethod newM) {
342342
newM.getSubMethods().add(specializedSubMethod);
343343
}
344344
});
345-
} else {
345+
}else {
346346
subClass.getSuperClasses().replaceAll(this::specializeType);
347347
ImClassType newClassTspecialized = specializeType(newClassT);
348348
if (subClass.isSubclassOf(newClassTspecialized.getClassDef())) {

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,137 @@ public void linkedListModule_perClassStatics_andTyping_ok() {
16791679
);
16801680
}
16811681

1682+
@Test
1683+
public void testTooltipGenerator() {
1684+
test().withStdLib().lines(
1685+
"package Test",
1686+
"import AbilityObjEditing",
1687+
"import HashMap",
1688+
"",
1689+
"@configurable constant TITLE_COLOR = \"|cff3B97D3\"",
1690+
"@configurable constant TITLE_TTYPE = \"Target Type:\"",
1691+
"@configurable constant MAX_TITLES = 10",
1692+
"",
1693+
"@configurable function buildLearnTooltip(string name) returns string",
1694+
"\treturn \"|cffFFCC00Learn|r \" + name + \" - [|cffffcc00Level %d|r]\"",
1695+
"",
1696+
"@configurable function buildActiveTooltip(string name, int lvl) returns string",
1697+
"\treturn name + \" - Level \" + lvl.toString()",
1698+
"",
1699+
"@configurable function buildPassiveTooltip(string name, int lvl) returns string",
1700+
"\treturn name + \" - Level \" + lvl.toString()",
1701+
"",
1702+
"@configurable function buildDescription(string description) returns string",
1703+
"\treturn \"|cff919191\" + description + \"|r\"",
1704+
"\t",
1705+
"public class AbilityTooltipGenerator implements TooltipGenerator",
1706+
"\tprivate constant propMap = new IterableMap<string, StringLevelClosure>",
1707+
"\tprivate var description = \"\"",
1708+
"",
1709+
"\tprivate var levels = 1",
1710+
"\tprivate var hotkey = \"\"",
1711+
"\tprivate var name = \"\"",
1712+
"",
1713+
"\tconstruct(string description)",
1714+
"\t\tthis.description = description",
1715+
"",
1716+
"\tconstruct()",
1717+
"",
1718+
"\toverride function addProperty(string title, StringLevelClosure lc)",
1719+
"\t\tpropMap.put(title, lc)",
1720+
"",
1721+
"\toverride function applyToDef(AbilityDefinition def)",
1722+
"\t\tlevels = propMap.has(\"Levels\") ? fixValue(propMap.getAndRemove(\"Levels\").run(0)).toInt() : 1",
1723+
"\t\tname = propMap.has(\"Name\") ? propMap.getAndRemove(\"Name\").run(0) : \"unnamed\"",
1724+
"\t\thotkey = propMap.has(\"Hotkey\") ? propMap.getAndRemove(\"Hotkey\").run(0) : \"Q\"",
1725+
"\t\tdef..setTooltipLearn(generateTooltipLearn())",
1726+
"\t\t..setTooltipLearnExtended(generateTooltipExtended(-1, true))",
1727+
"\t\tfor i = 1 to levels",
1728+
"\t\t\tdef..setTooltipNormal(i, generateTooltipNormal(i))",
1729+
"\t\t\t..setTooltipNormalExtended(i, generateTooltipExtended(i, false))",
1730+
"",
1731+
"\tprivate function generateTooltipLearn() returns string",
1732+
"\t\treturn buildLearnTooltip(name)",
1733+
"",
1734+
"\tprivate function generateTooltipNormal(int lvl) returns string",
1735+
"\t\treturn buildActiveTooltip(name, lvl)",
1736+
"",
1737+
"\tprivate function generateTooltipExtended(int lvl, boolean learn) returns string",
1738+
"\t\tvar s = \"\"",
1739+
"",
1740+
"\t\tfor key in propMap",
1741+
"\t\t\tvar tmp = \"\"",
1742+
"\t\t\tvar isConstantValue = true",
1743+
"\t\t\tlet val = fixValue(propMap.get(key).run(1))",
1744+
"\t\t\tfor i = 2 to levels",
1745+
"\t\t\t\tlet tval = fixValue(propMap.get(key).run(i))",
1746+
"\t\t\t\tif val != tval",
1747+
"\t\t\t\t\tisConstantValue = false",
1748+
"\t\t\t\t\tbreak",
1749+
"",
1750+
"\t\t\tif isConstantValue",
1751+
"\t\t\t\ttmp += \"|cffFFCC00\" + val + \"|r \"",
1752+
"\t\t\telse",
1753+
"\t\t\t\tfor i = 1 to levels",
1754+
"\t\t\t\t\ttmp += fixValue(propMap.get(key).run(i)) + (i < levels ? \"/\" : \"\")",
1755+
"",
1756+
"\t\t\ts += TITLE_COLOR + key + \":|r \" + colorLevelValue(tmp, lvl, levels) + \"\\n\"",
1757+
"\t\ts += \"\\n\"",
1758+
"\t\ts += buildDescription(description)",
1759+
"\t\treturn s",
1760+
"",
1761+
"\tprivate static function fixValue(string value) returns string",
1762+
"\t\tif value == \"\"",
1763+
"\t\t\treturn \"\"",
1764+
"\t\tvar s = value",
1765+
"\t\tlet len = s.length()",
1766+
"\t\tlet lastChar = s.substring(len-1, len)",
1767+
"\t\tif lastChar == \".\"",
1768+
"\t\t\ts = s.substring(0, len-1)",
1769+
"\t\telse if len > 1 and s.substring(len-2, len) == \".0\"",
1770+
"\t\t\ts = s.substring(0, len-2)",
1771+
"\t\treturn s",
1772+
"",
1773+
"\tprivate static function colorLevelValue(string oldString, int lvl, int maxLevel) returns string",
1774+
"\t\tvar _newString = \"\"",
1775+
"\t\tvar charCount = 0",
1776+
"\t\tvar charPosCount = 0",
1777+
"\t\tint array charPos",
1778+
"\t\tlet oldLen = oldString.length()",
1779+
"\t\tfor char in oldString",
1780+
"\t\t\tif char == \"/\"",
1781+
"\t\t\t\tcharPos[charPosCount] = charCount",
1782+
"\t\t\t\tcharPosCount++",
1783+
"\t\t\tcharCount++",
1784+
"\t\tif lvl == -1",
1785+
"\t\t\t_newString = oldString",
1786+
"\t\telse if charPosCount <= 0",
1787+
"\t\t\t_newString = \"|cffFFCC00\" + oldString",
1788+
"\t\telse if maxLevel <= 1",
1789+
"\t\t\t_newString = \"|cffFFCC00\" + oldString",
1790+
"\t\telse if lvl == 1",
1791+
"\t\t\t_newString = \"|cffFFCC00\" + oldString.substring(0, charPos[0]) + \"|r\" + oldString.substring(charPos[0], oldLen)",
1792+
"\t\telse if lvl == maxLevel",
1793+
"\t\t\t_newString = oldString.substring(0, charPos[lvl-2] + 1) + \"|cffFFCC00\" + oldString.substring(charPos[lvl-2] + 1, oldLen) + \"|r\"",
1794+
"\t\telse",
1795+
"\t\t\t_newString = oldString.substring(0, charPos[lvl-2] + 1) + \"|cffFFCC00\" + oldString.substring(charPos[lvl-2] + 1, charPos[lvl-1]) + \"|r\" + oldString.substring(charPos[lvl-1], oldLen)",
1796+
"\t\treturn _newString",
1797+
"constant h = compiletime(gen())",
1798+
"",
1799+
"function gen() returns int",
1800+
" let myInt = 20",
1801+
" let tgen = new AbilityTooltipGenerator(\"Test1\")",
1802+
" new AbilityDefinitionItemHealAoe('1234')",
1803+
" ..registerTooltipGenerator(tgen)",
1804+
" ..tooltipStartListen()",
1805+
" ..addTooltipProperty(\"String\", _ -> \"a\")",
1806+
" ..tooltipStopListen(true)",
1807+
"",
1808+
" return 1",
1809+
"init",
1810+
" testSuccess()"
16821811

1812+
);
1813+
}
16831814

16841815
}

0 commit comments

Comments
 (0)