← Back to Pull requests

[clang] Fix assertion failure on direct initialization of an alias with an unexpanded pack from a parenthesized braced-init-list #236838

Open
hbatagelo opened this pull request with 12 lines diff across 3 files
View on GitHub
hbatagelo hbatagelo commented on Oct 29, 2026 16:51 UTC
Author

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-L567

Fix by overriding TraverseRecoveryExpr in CollectUnexpandedParameterPacksVisitor to also traverse the RecoveryExpr's type.

Diff: 12 lines (+12 / -0) across 3 files Reviewer: zyn0217 Review duration: 52331m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.28) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.28 · 12 lines diff across 3 files
0.28
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.94 · 4 substantive comment(s)
0.94
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.153
Real PR Conversation Timeline
6 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 29, 2026 16:52 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Harlen Batagelo (hbatagelo)

<details>
<summary>Changes</summary>

Fixes #<!-- -->113762.

Given

template&lt;typename T&gt; using Int = int;
template&lt;typename ...Ts&gt; void Pack() {
  Int&lt;Ts&gt;({});
}

Sema wraps the ill-formed Int&lt;Ts&gt;({}) in a RecoveryExpr whose type is the sugared Int&lt;Ts&gt;, 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-L567

Fix 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:

  • (modified) clang/lib/Sema/SemaTemplateVariadic.cpp (+7)

  • (modified) clang/test/SemaTemplate/find-unexpanded-packs.cpp (+1)
  • ``````

    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>

    zyn0217 zyn0217 commented on code on Oct 30, 2026 02:29 UTC
    Code Review
    clang/test/SemaTemplate/find-unexpanded-packs.cpp:1
    @@ -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}}

    Why didn't we crash for Int<Ts>()?

    zyn0217 zyn0217 requested changes on Oct 30, 2026 02:31 UTC
    Changes Requested Reviewer

    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.

    hbatagelo hbatagelo commented on code on Oct 30, 2026 18:46 UTC
    Code Review
    clang/test/SemaTemplate/find-unexpanded-packs.cpp:1
    @@ -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}}

    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.

    hbatagelo hbatagelo commented on Oct 30, 2026 18:53 UTC
    Author

    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.

    Reviewers
    Assignees
    hbatagelo hbatagelo
    Labels
    risk: low confidence: 90.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 28.3% (LOW) · Higher = larger blast radius
    28.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.7% · Lower = rushed or shallow review
    90.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.6% · Safe to merge
    2.6%