[clang] [clang][Sema] Fix rejection of non-dependent void{} during template instantiation (PR #228340)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 1 23:27:51 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Ivy Xu (IvyXu420)

<details>
<summary>Changes</summary>

Fixes #<!-- -->226485 and #<!-- -->181448.

When a template is instantiated, `TreeTransform` rebuilds the expression via `RebuildCXXFunctionalCastExpr`, which uses `E->isListInitialization()` (i.e. `LParenLoc.isInvalid()`) to decide whether the expression models list-initialization.

Mark `void{}` as list-initialization by passing an invalid `LParenLoc` in `BuildCXXTypeConstructExpr`, so it is not rebuilt
as `void({})`.

Assisted-by: Claude Sonnet 5.5.

---
Full diff: https://github.com/llvm/llvm-project/pull/228340.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+3) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+1-1) 
- (modified) clang/test/CXX/drs/cwg23xx.cpp (+4) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index d22874d4130ee..04ded7c5b6a2e 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -801,6 +801,9 @@ features cannot lower the translation-unit ABI level;
   take effect, causing them to be dropped from llvm.used and omitted from
   the object file. (#GH226572)
 
+- Fixed rejection of non-dependent `void{}` during template instantiation,
+  which previously rebuilt the expression as `void({})`. (#GH226485, #GH181448)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 1bf6430665ddf..1ed58fcf20af0 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1647,7 +1647,7 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
       return CXXFunctionalCastExpr::Create(
           Context, Ty.getUnqualifiedType(), VK_PRValue, TInfo, CK_ToVoid,
           Exprs[0], /*Path=*/nullptr, CurFPFeatureOverrides(),
-          Exprs[0]->getBeginLoc(), Exprs[0]->getEndLoc());
+          SourceLocation(), Exprs[0]->getEndLoc());
     }
   } else if (RequireCompleteType(TyBeginLoc, ElemTy,
                                  diag::err_invalid_incomplete_type_use,
diff --git a/clang/test/CXX/drs/cwg23xx.cpp b/clang/test/CXX/drs/cwg23xx.cpp
index cfd78465c0dbe..c81fe223a596c 100644
--- a/clang/test/CXX/drs/cwg23xx.cpp
+++ b/clang/test/CXX/drs/cwg23xx.cpp
@@ -240,6 +240,10 @@ namespace cwg2351 { // cwg2351: 20
   template void i<void>();
   template const void i<const void>();
 
+  template <class T>
+  void f(T) { void{}; }
+  void g() { f(1); }
+
   static_assert((void({}), true), "");
   // since-cxx11-error at -1 {{cannot initialize non-class type 'void' with a parenthesized initializer list}}
 #else

``````````

</details>


https://github.com/llvm/llvm-project/pull/228340


More information about the cfe-commits mailing list