[clang] [Clang] Fix crashes in coroutine pack expansions (PR #237969)
Kunal Dubey via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 00:18:09 PST 2026
================
@@ -27,8 +27,10 @@ ExprDependence clang::computeDependence(FullExpr *E) {
ExprDependence clang::computeDependence(OpaqueValueExpr *E) {
auto D = toExprDependenceForImpliedType(E->getType()->getDependence());
+ // An OpaqueValueExpr has no syntactic children, so it does not contain an
+ // unexpanded pack even if its source does.
if (auto *S = E->getSourceExpr())
- D |= S->getDependence();
+ D |= S->getDependence() & ~ExprDependence::UnexpandedPack;
assert(!(D & ExprDependence::UnexpandedPack));
----------------
xakep8 wrote:
@zyn0217 I had to go to GCC to check what they do here and followed their approach for this one.
So I switched the strategy and instead of suppressing the unexpanded-pack dependency on an OpaqueValueExpr I introduced an additional binding node which owns the original expression. OpaqueValueExpr refers to the value generated by this binding only. When instantiating the template, Clang transforms the expression stored in the binding and then redirects all references to the newly created value. In case of pack expansion, the mapping is updated per each expansion element.
This way the expression with the pack stays in the transformable AST and gets transformed as usual, but the value still can be reused without transformation of the source expression multiple times. Also it does not depend on special handling of the conditional operator, as the same representation is used for pseudo-objects, coroutines, initializers, HLSL rewrites and all other users of the opaque values.
In addition this needs updates to other sections like CodeGen, CIR and many others to be aware of this explicit binding. The assertion would also be valid now, as the bare opaque-value reference no longer owns the untransformed expression with the pack anymore.
While this does introduce one extra AST node per binding, the references share the same OpaqueValueExpr and there is no extra runtime evaluation involved. In effect, that binding is playing the role that GCC's SAVE_EXPR plays. It allows you to refer to the source expression that can be modified and represents the value that is evaluated exactly once. While GCC combines both in one node, this solution separates them. However, I believe it better suits existing uses of OpaqueValueExpr in Clang.
I would like to know what you think about this as I think this could help correct the modelling for OVE that we currently have, as IMO OVE was never the problem the source ownership was.
https://github.com/llvm/llvm-project/pull/237969
More information about the cfe-commits
mailing list