This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Fix discarding void-typed comma expressions
ClosedPublic

Authored by tbaeder on Apr 21 2023, 7:49 AM.

Details

Summary

First, we need to handle void types in visitExpr, so we don't run into an assertion there when we try to pop a return value from the stack that isn't there.

Secondly, we need to handle it when visiting comma expressions so we don't do the same thing there.

Diff Detail

Event Timeline

tbaeder created this revision.Apr 21 2023, 7:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2023, 7:49 AM
tbaeder requested review of this revision.Apr 21 2023, 7:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2023, 7:49 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

So things cast to 'void' can have side effects, right? Does the call to discard still evaluate them?

https://godbolt.org/z/a3ej9bxKe

constexpr int foo(int start) {
    int i = start;

    (void)i++;
    (void)i++,(void)i++;
    return i;
}
constexpr int Value = foo(5);
static_assert(Value == 8);
tbaeder updated this revision to Diff 515762.Apr 21 2023, 9:14 AM

So things cast to 'void' can have side effects, right? Does the call to discard still evaluate them?

https://godbolt.org/z/a3ej9bxKe

Yes. discard(E) is just visit(E), except that it sets DiscardResult to true for that one level of visiting. Following visit() calls will have DiscardResult = false again.

So things cast to 'void' can have side effects, right? Does the call to discard still evaluate them?

https://godbolt.org/z/a3ej9bxKe

Yes. discard(E) is just visit(E), except that it sets DiscardResult to true for that one level of visiting. Following visit() calls will have DiscardResult = false again.

Cool! Mind adding the tests I proposed? it would be nice to ensure/show side-effects are guaranteed.

tbaeder retitled this revision from Fix discarding void-typed comma expressions to [clang][Interp] Fix discarding void-typed comma expressions.Apr 21 2023, 9:35 AM
tbaeder updated this revision to Diff 515800.Apr 21 2023, 9:38 AM

Cool! Mind adding the tests I proposed? it would be nice to ensure/show side-effects are guaranteed.

Sure, thanks for the test case!

erichkeane accepted this revision.Apr 21 2023, 9:40 AM
This revision is now accepted and ready to land.Apr 21 2023, 9:40 AM
This revision was landed with ongoing or failed builds.Jul 25 2023, 10:45 PM
This revision was automatically updated to reflect the committed changes.