Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #385 #386

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}