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

ConstantFolding should not short-circuit on right value #187

Closed
glchapman opened this issue Jun 6, 2024 · 2 comments
Closed

ConstantFolding should not short-circuit on right value #187

glchapman opened this issue Jun 6, 2024 · 2 comments

Comments

@glchapman
Copy link

FWIW, Constant folding has this for LogicalAnd and LogicalOr:

            if (op.Kind == BoundBinaryOperatorKind.LogicalAnd)
            {
                if (leftConstant != null && !(bool)leftConstant.Value ||
                    rightConstant != null && !(bool)rightConstant.Value)
                {
                    return new BoundConstant(false);
                }
            }

            if (op.Kind == BoundBinaryOperatorKind.LogicalOr)
            {
                if (leftConstant != null && (bool)leftConstant.Value ||
                    rightConstant != null && (bool)rightConstant.Value)
                {
                    return new BoundConstant(true);
                }
            }

The short-circuiting on non-null rightConstant is incorrect. It gives the correct value for the overall binary op expression, but the left hand side still has to be evaluated in case it has side effects. Here's an example of an incorrect evaluation in msi:

» function foo(): bool {print("foo") return false}

» foo() && false

False

Note that "foo" is not printed.

@ltrzesniewski
Copy link
Contributor

That's a duplicate of #125 🙂

@glchapman
Copy link
Author

Sorry -- I searched for "ConstantFolding" but not "constant folding" so I didn't find 125.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants