C++: Make node.asExpr() instanceof ClassAggregateLiteral
satisfiable
#19501
+71
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The C++ IR represents an aggregate literal
x = {.a = 1, .b = 2}
as a sequence of field writes:and as such there is no instruction for which
getUnconvertedResultExpression()
gives the aggregate literal. And sinceNode.asExpr()
piggybacks ongetUnconvertedResultExpression()
for most nodesnode.asExpr() instanceof AggregateLiteral
also never holds. This gotcha has bitten users several times.This PR solves the problem for class aggregate literals. Since class aggregate literals are represented as a sequence of field writes (see above), we can pick the last field write's post-update node as the node to represent the aggregate literal expression. Semantically this makes sense since the last post-update node represents the state of the object after the last field has been initialized.
Sadly, since we don't model array writes using post-update nodes we can't use the same trick for
ArrayAggregateLiterals
. However, I think we should do that eventually. Once we do that we can apply a similar trick to that case. For now I think this solution at least solves half of the problems that's been reported.