[clang] Fix assertion failure on direct initialization of an alias with an unexpanded pack from a parenthesized braced-init-list #236838
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Harlen Batagelo (hbatagelo)
<details>
<summary>Changes</summary>
Fixes #<!-- -->113762.
Given
template<typename T> using Int = int;
template<typename ...Ts> void Pack() {
Int<Ts>({});
}
Sema wraps the ill-formed
Int<Ts>({}) in a RecoveryExpr whose type is the sugared Int<Ts>, so containsUnexpandedParameterPack() is true. But the collector invoked by Sema::DiagnoseUnexpandedParameterPack only walks the subexpression (InitListExpr) which doesn't mention the pack. The traversal finds nothing, triggering the assertion https://github.com/llvm/llvm-project/blob/3bf0a2073bd4ef0a744601305d62ab058233a9ed/clang/lib/Sema/SemaTemplateVariadic.cpp#L566-L567Fix by overriding TraverseRecoveryExpr in CollectUnexpandedParameterPacksVisitor to also traverse the RecoveryExpr's type.
---
Full diff: https://github.com/llvm/llvm-project/pull/236838.diff
2 Files Affected:
````````````diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp
index 2d6e75bd77b402..bda26f5d6aeca6 100644
--- a/clang/lib/Sema/SemaTemplateVariadic.cpp
+++ b/clang/lib/Sema/SemaTemplateVariadic.cpp
@@ -384,6 +384,13 @@ class CollectUnexpandedParameterPacksVisitor
return DynamicRecursiveASTVisitor::TraverseUnresolvedLookupExpr(E);
}
+ bool TraverseRecoveryExpr(RecoveryExpr *E) override {
+ if (!DynamicRecursiveASTVisitor::TraverseRecoveryExpr(E))
+ return false;
+ TraverseType(E->getType());
+ return true;
+ }
+
bool TraverseSubstBuiltinTemplatePackType(SubstBuiltinTemplatePackType *T,
bool TraverseQualifier) override {
addUnexpanded(T);
diff --git a/clang/test/SemaTemplate/find-unexpanded-packs.cpp b/clang/test/SemaTemplate/find-unexpanded-packs.cpp
index ae989c68b50e22..5bb9fb1d6cde09 100644
--- a/clang/test/SemaTemplate/find-unexpanded-packs.cpp
+++ b/clang/test/SemaTemplate/find-unexpanded-packs.cpp
@@ -162,6 +162,7 @@ template void HiddenPack<int, float>();
template<typename ...Ts> void HiddenPackInvalid() {
Int<Ts>(); // expected-error {{unexpanded}}
Int<Ts>{}; // expected-error {{unexpanded}}
+ Int<Ts>({}); // expected-error {{unexpanded}} expected-error {{cannot initialize non-class type}}
Class<Ts>(); // expected-error {{unexpanded}}
Class<Ts>{}; // expected-error {{unexpanded}}
}
</details>
Why didn't we crash for Int<Ts>()?
This is incorrect, I think this is another issue where RecoveryExpr sneaks into Sema deeply where it shouldn't. collectUnexpandedPacks shouldn't handle errors, and we're supposed to bail out early.
For Int<Ts>(), the init sequence is well-formed. The result is a CXXScalarValueInitExpr instead of a RecoveryExpr. The default hook for CXXScalarValueInitExpr walks the TypeSourceInfo that contains the written type (Int<Ts>), so the collector finds the pack.
This is incorrect, I think this is another issue where RecoveryExpr sneaks into Sema deeply where it shouldn't. collectUnexpandedPacks shouldn't handle errors, and we're supposed to bail out early.
Bailing out earlier (in ActOnFinishFullExpr) was my first approach, but it drops the diagnostic for the unexpanded pack. It diagnoses only the error in the subexpression. That makes the behavior inconsistent across the sibling forms (Int<Ts>() and Int<Ts>{}) and diverges from GCC, which diagnoses the pack in all cases. GCC happens not to emit the initialization error for Int<Ts>({}), so the patch does not match GCC exactly, but it emits both diagnostics and keeps the pack diagnostic consistent with the siblings. It is also consistent with existing cases that expect both diagnostics, e.g., from fold_expr_typo.cpp:
template <typename... T>
void foo(T &&...Params) {
foo<T>(Unknown); // expected-error {{expression contains unexpanded parameter pack 'T'}}\
expected-error {{use of undeclared identifier 'Unknown'}}
}
An early bail-out would introduce a regression here (only the "undeclared identifier" error would be diagnosed).
FWIW, I also tried to bail out after the collection in DiagnoseUnexpandedParameterPacks when Unexpanded is empty. It works for the previous example (the pack is reachable, so Unexpanded is non-empty), but still drops the pack diagnostic for Int<Ts>({}) because the pack isn't reachable without traversing the RecoveryExpr's type.
I agree the collector handling a RecoveryExpr is awkward, but the diagnostic cost of bailing out seems worse here. Open to other ideas if you have one in mind.
zyn0217
hbatagelo
Fixes #113762.
Given
Sema wraps the ill-formed
Int<Ts>({})in aRecoveryExprwhose type is the sugaredInt<Ts>, socontainsUnexpandedParameterPack()istrue. But the collector invoked bySema::DiagnoseUnexpandedParameterPackonly walks the subexpression (InitListExpr) which doesn't mention the pack. The traversal finds nothing, triggering the assertion https://github.com/llvm/llvm-project/blob/3bf0a2073bd4ef0a744601305d62ab058233a9ed/clang/lib/Sema/SemaTemplateVariadic.cpp#L566-L567Fix by overriding
TraverseRecoveryExprinCollectUnexpandedParameterPacksVisitorto also traverse theRecoveryExpr's type.