Skip to content

Commit b94d7e6

Browse files
committed
Propagate range types into expression children
1 parent c99af94 commit b94d7e6

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public class AttrExprExpectedType {
7676
if (nearestFuncDef != null) {
7777
return nearestFuncDef.attrReturnTyp();
7878
}
79+
} else if (parent instanceof StmtForRange) {
80+
StmtForRange forRange = (StmtForRange) parent;
81+
if (forRange.getTo() == expr || forRange.getStep() == expr) {
82+
return WurstTypeInt.instance();
83+
}
7984
} else if (parent instanceof SwitchCase) {
8085
SwitchCase sc = (SwitchCase) parent;
8186
SwitchStmt s = (SwitchStmt) sc.getParent().getParent();

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,29 @@ public void erasedGenericPrimitiveDefaultsAreNormalizedInTypedStatementBlocks()
22702270
compiled.contains("value = __wurst_ensureInt(Box_Box_get(box))"));
22712271
}
22722272

2273+
@Test
2274+
public void erasedGenericPrimitiveDefaultsAreNormalizedInCompositeRangeBounds() throws IOException {
2275+
test().testLua(true).executeProg().lines(
2276+
"package Test",
2277+
"native testSuccess()",
2278+
"class Box<T:>",
2279+
" T value",
2280+
" function get() returns T",
2281+
" return value",
2282+
"init",
2283+
" let box = new Box<int>",
2284+
" bool useBox = true",
2285+
" int iterations = 0",
2286+
" for i = 1 to (useBox ? box.get() : 0)",
2287+
" iterations++",
2288+
" if iterations == 0",
2289+
" testSuccess()"
2290+
);
2291+
String compiled = compiledLua("erasedGenericPrimitiveDefaultsAreNormalizedInCompositeRangeBounds");
2292+
assertTrue("composite range bounds must normalize erased primitive branches",
2293+
compiled.contains("__wurst_ensureInt(Box_Box_get(box))"));
2294+
}
2295+
22732296
/**
22742297
* Seeded boundary corpus for the type-assurance change. Each case varies
22752298
* the primitive type, literal value, and array slot while checking the two

0 commit comments

Comments
 (0)