Skip to content

Commit ea8b239

Browse files
committed
Normalize generic values used as Lua indices
1 parent 2cf295e commit ea8b239

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,19 @@ static ImExpr wrapLua(Element trace, ImTranslator t, ImExpr translated, WurstTyp
140140
return translated;
141141
}
142142

143-
static ImExpr wrapTranslation(Element trace, ImTranslator t, ImExpr translated, WurstType actualType, WurstType expectedTypRaw) {
143+
static ImExpr wrapTranslation(Expr e, ImTranslator t, ImExpr translated, WurstType actualType, WurstType expectedTypRaw) {
144+
return wrapTranslation(e, t, translated, actualType, expectedTypRaw,
145+
e.getParent() instanceof Indexes);
146+
}
147+
148+
static ImExpr wrapTranslation(Element trace, ImTranslator t, ImExpr translated,
149+
WurstType actualType, WurstType expectedTypRaw) {
150+
return wrapTranslation(trace, t, translated, actualType, expectedTypRaw, false);
151+
}
152+
153+
private static ImExpr wrapTranslation(Element trace, ImTranslator t, ImExpr translated,
154+
WurstType actualType, WurstType expectedTypRaw,
155+
boolean indexContext) {
144156
ImFunction toIndex = null;
145157
ImFunction fromIndex = null;
146158
if (actualType instanceof WurstTypeBoundTypeParam) {
@@ -184,7 +196,7 @@ static ImExpr wrapTranslation(Element trace, ImTranslator t, ImExpr translated,
184196
if (actualType instanceof WurstTypeBoundTypeParam
185197
&& !(expectedTypRaw instanceof WurstTypeBoundTypeParam)
186198
&& !(expectedTypRaw instanceof WurstTypeTypeParam)
187-
&& isPrimitiveType(expectedTypRaw)) {
199+
&& (isPrimitiveType(expectedTypRaw) || indexContext)) {
188200
return wrapLua(trace, t, translated, actualType);
189201
}
190202
return translated;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ public void visit(ImFunctionCall call) {
379379
}
380380
ImExpr normalized;
381381
if (ensure == translator.ensureBoolFunc) {
382-
normalized = JassIm.ImOperatorCall(WurstOperator.NOTEQ,
383-
JassIm.ImExprs(argument.copy(), JassIm.ImNull(JassIm.ImAnyType())));
382+
normalized = JassIm.ImOperatorCall(WurstOperator.EQ,
383+
JassIm.ImExprs(argument.copy(), JassIm.ImBoolVal(true)));
384384
} else {
385385
normalized = JassIm.ImFunctionCall(call.attrTrace(), ensure,
386386
JassIm.ImTypeArguments(), JassIm.ImExprs(argument.copy()), false, CallType.NORMAL);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,12 +2201,14 @@ public void erasedGenericPrimitiveDefaultsAreNormalizedAtConcreteUse() throws IO
22012201
test().testLua(true).executeProg().lines(
22022202
"package Test",
22032203
"native testSuccess()",
2204+
"int array values",
22042205
"class Box<T:>",
22052206
" T value",
22062207
" function get() returns T",
22072208
" return value",
22082209
"init",
22092210
" let box = new Box<int>",
2211+
" values[box.get()] = 7",
22102212
" if box.get() + 1 == 1",
22112213
" testSuccess()"
22122214
);

0 commit comments

Comments
 (0)