diff --git a/src/rewriter.fs b/src/rewriter.fs index 32b54fee..e9f3d6cd 100644 --- a/src/rewriter.fs +++ b/src/rewriter.fs @@ -24,6 +24,10 @@ let rec private sideEffects = function match fct.Declaration with | Declaration.UserFunction fd when not fd.hasExternallyVisibleSideEffects -> args |> List.collect sideEffects | _ -> [e] + | FunCall(Op "?:", [condExpr; thenExpr; elseExpr]) as e -> + if sideEffects thenExpr = [] && sideEffects elseExpr = [] + then sideEffects condExpr + else [e] // We could apply sideEffects to thenExpr and elseExpr, but the result wouldn't necessarily have the same type... | FunCall(Op op, args) when not(Builtin.assignOps.Contains(op)) -> args |> List.collect sideEffects | FunCall(Dot(_, field) as e, args) when field = "length" -> (e :: args) |> List.collect sideEffects | FunCall(Subscript _ as e, args) -> (e :: args) |> List.collect sideEffects diff --git a/tests/unit/ternary.frag b/tests/unit/ternary.frag index 0e620a6a..7529f17b 100644 --- a/tests/unit/ternary.frag +++ b/tests/unit/ternary.frag @@ -12,3 +12,18 @@ float f1() { a = true ? b = 2. : b = 3.; return a; } + +// Fix for #385 + +out vec4 O; +int k; +void main() { + int d=0,e=0; + k==0 ? (sin(O.x),d=1) : (cos(O.x),e=2) ; + O.x=float(d); + O.y=float(e); +} +int f2() { + k++ == 0 ? 1 : 2 ; + return k; +} diff --git a/tests/unit/ternary.frag.expected b/tests/unit/ternary.frag.expected index b5732bbb..8732a92f 100644 --- a/tests/unit/ternary.frag.expected +++ b/tests/unit/ternary.frag.expected @@ -9,3 +9,19 @@ float f1() float a=1.; return a=2.; } +out vec4 O; +int k; +void main() +{ + int d=0,e=0; + k==0? + (sin(O.x),d=1): + (cos(O.x),e=2); + O.x=float(d); + O.y=float(e); +} +int f2() +{ + k++; + return k; +}