Skip to content

Commit

Permalink
Fixes #385, detection of side effects of ternary operator (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
eldritchconundrum committed May 10, 2024
1 parent 25c5e6b commit 8ae5343
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/ternary.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 16 additions & 0 deletions tests/unit/ternary.frag.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 8ae5343

Please sign in to comment.