Repository navigation
Description
Describe the bug
A ? : expression of tuple type (e.g. vec2) loses its value when it's passed directly as a function argument and one branch uses [] (op_index) on a generic class with a tuple type argument, such as ArrayList<vec2>.
- Lua: compiles with no error or warning. The generated code calls the branch but throws away its result, then passes one never-assigned
nilwhere two values are expected, so the map crashes at runtime. - Jass: the same source fails with an internal error,
Type variable T not eliminated, reported at<source of NoExpr not found>:1.
Found in a real map, where origin.distanceToSq(positions != null ? positions[i] : u.getPos()) crashed in game with v_x nil inside vec2_distanceToSq.
Environment: WurstScript 2.2.0, grill 1.4.0, OpenJDK 27.0.1, wurstStdlib2 e2f93e2, wc3Patch: v3.0.1, Windows 11.
Steps to reproduce
- Create a grill project whose only source file is:
package Repro
class Box<T:>
T value
construct(T value)
this.value = value
function op_index(int i) returns T
return value
function sum(vec2 v) returns real
return v.x + v.y
function broken(boolean b, Box<vec2> box) returns real
return sum(b ? box[0] : vec2(0., 0.)) // conditional result dropped
function okStoredFirst(boolean b, Box<vec2> box) returns real
let v = b ? box[0] : vec2(0., 0.)
return sum(v)
function okDirect(Box<vec2> box) returns real
return sum(box[0])
init
let box = new Box<vec2>(vec2(1., 2.))
print("broken " + broken(true, box)) // expected 3, crashes in Lua
print("okStoredFirst " + okStoredFirst(true, box))
print("okDirect " + okDirect(box))
- Add a
wurst_run.argscontaining-runcompiletimefunctions,-injectobjectsand-stacktraces, one per line. That turns off inlining so the functions stay visible in the output. - With
scriptMode: LUA, rungrill build ExampleMap-folder.w3x --devand look atbrokenin_build/02_compiled.lua. - Switch to
scriptMode: JASSand build again.
Expected: broken compiles like okStoredFirst, with both tuple parts assigned in each branch, and returns 3.
Actual (Lua):
function broken(b1, box, __wurst_stackPos40)
local cond_result2 = nil
local stackTrace_tempReturn2 = nil
if b1 then
Box_op_index_specialized(box, 0, "op_index (Repro:L14)")
end
stackTrace_tempReturn2 = sum(cond_result2, "sum (Repro:L14)")
return stackTrace_tempReturn2
end
-- callee: function sum(v_x, v_y, __wurst_stackPos42)- The branch's two return values are thrown away.
- The
elsebranch is gone entirely. cond_result2is one variable for a two-part tuple and is never assigned.- Every argument after it shifts by one:
v_x = nil,v_y= the trace string, and__wurst_stackPos = nil.
okStoredFirst and okDirect compile correctly. They assign both values, as in …_result_x, …_result_y = Box_op_index_specialized(...).
Actual (Jass):
CompileError: Type variable T not eliminated.
at ...imtojass.ImAttributes.translateType(ImAttributes.java:117)
at ...jassIm.ImTypeVarRefImpl.translateType(ImTypeVarRefImpl.java:187)
at ...imtojass.ImToJassTranslator.getJassVarFor(ImToJassTranslator.java:254)
at ...imtojass.ImToJassTranslator.translateFunction(ImToJassTranslator.java:218)
With broken removed, the Jass build succeeds and passes pjass.
What triggers it (Lua, same flags; each case is f(cond ? A : B) with f taking a vec2):
| Branch | Result |
|---|---|
box[0], op_index on a generic class |
❌ |
list[0] on ArrayList<vec2> |
❌ |
box.get() / list.get(0) |
✅ |
p[0], op_index on a non-generic class returning vec2 |
✅ |
u.getPos() or another function returning vec2 |
✅ |
Same conditional assigned to a let first |
✅ |
box[0] passed directly, no conditional |
✅ |
Likely cause (a guess from behavior, not from reading the source): the conditional's type seems to come from op_index's declared return type T, without substituting vec2. That fits both backends. Lua emits one untyped temp, and Jass's getJassVarFor is asked for a variable of type T.
Side effect: if that conditional is the only call site of a tuple-returning function, the function's own body is also emitted with a bare return, as ArrayList_op_index_specialized and unit_getPos were in the original map. This seems to be downstream of the dropped values.
Not caught by: grill test, because the interpreter doesn't run the generated Lua. Optimized builds (-opt -inline) inline the code away; I didn't check whether the optimized code computes the right value.
Workaround: assign the conditional to a local first, or use .get(i) instead of [i].
Unrelated minor issue: a syntax error in an early draft of the repro (use as a function name) also logged a NullPointerException: Cannot read field "stop" because "p" is null. It came from AntlrWurstParseTreeTransformer.source, via transformExternalLambda, on unrelated later lines. The build still reported the correct syntax error.
Logs:
Activity