pow: Ensure side effects are evaluated in e1^^0 rewrite

This commit is contained in:
Iain Buclaw 2025-03-29 18:55:30 +01:00 committed by The Dlang Bot
parent 95fdc47f5f
commit 5c9c91a651
2 changed files with 22 additions and 3 deletions

View file

@ -12818,11 +12818,10 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
result = ex;
return;
}
// Replace e1 ^^ 0 with 1
// Replace e1 ^^ 0 with (e1, 1)
else if (expo == 0)
{
Expression ex = one;
ex.loc = exp.loc;
Expression ex = new CommaExp(exp.loc, pe.e1, one, exp);
ex = ex.expressionSemantic(sc);
result = ex;
return;

View file

@ -35,10 +35,30 @@ void test3()
static assert(4.0 ^^ -1 == 0.25);
}
void test4()
{
// Test that LHS side effects are evaluated.
int count = 0;
double x()
{
count++;
return 8.0;
}
assert(x ^^ -1 == 1.0 / 8.0);
assert(count == 1);
assert(x ^^ 0 == 1.0);
assert(count == 2);
assert(x ^^ 1 == 8.0);
assert(count == 3);
assert(x ^^ 2 == 64.0);
assert(count == 4);
}
extern(C) void main()
{
test1();
test2();
test3();
test4();
printf("Success\n");
}