[llvm-branch-commits] [clang] a82f94b - [clang][sema] Fix crash on decomposition decl missing initializer (#210151)

Douglas Yung via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jul 22 06:52:03 PDT 2026


Author: Patryk Stefanski
Date: 2026-07-22T13:51:49Z
New Revision: a82f94b8f46766504a4f7ee0e9ddb65ebb6cf472

URL: https://github.com/llvm/llvm-project/commit/a82f94b8f46766504a4f7ee0e9ddb65ebb6cf472
DIFF: https://github.com/llvm/llvm-project/commit/a82f94b8f46766504a4f7ee0e9ddb65ebb6cf472.diff

LOG: [clang][sema] Fix crash on decomposition decl missing initializer (#210151)

ActOnUninitializedDecl dereferenced the std::optional<Token> from
Lexer::findNextToken() unconditionally when diagnosing a structured
binding with no initializer. Guard the optional and fall back to the
declaration's location.

(cherry picked from commit 0e3852f73ab75639b76bad2c8a735d46241768f3)

Added: 
    

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/Parser/cxx1z-decomposition.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c5920f03ed6e1..9b2efd96cb62d 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14521,12 +14521,15 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
     }
     // C++1z [dcl.dcl]p1 grammar implies that an initializer is mandatory.
     if (isa<DecompositionDecl>(RealDecl)) {
-      // Point the caret to the token immediately after the closing bracket.
-      auto NextLoc = dyn_cast<DecompositionDecl>(RealDecl)->getRSquareLoc();
-      NextLoc =
-          Lexer::findNextToken(NextLoc, PP.getSourceManager(), PP.getLangOpts())
-              ->getLocation();
-      Diag(NextLoc, diag::err_decomp_decl_requires_init) << Var;
+      // Point the caret to the token immediately after the closing bracket if
+      // it can be found; otherwise fall back to the declaration's location.
+      SourceLocation Loc = Var->getLocation();
+      SourceLocation RSquareLoc =
+          dyn_cast<DecompositionDecl>(RealDecl)->getRSquareLoc();
+      if (std::optional<Token> Next = Lexer::findNextToken(
+              RSquareLoc, PP.getSourceManager(), PP.getLangOpts()))
+        Loc = Next->getLocation();
+      Diag(Loc, diag::err_decomp_decl_requires_init) << Var;
       Var->setInvalidDecl();
       return;
     }

diff  --git a/clang/test/Parser/cxx1z-decomposition.cpp b/clang/test/Parser/cxx1z-decomposition.cpp
index fb22364ddb802..607a628506e8a 100644
--- a/clang/test/Parser/cxx1z-decomposition.cpp
+++ b/clang/test/Parser/cxx1z-decomposition.cpp
@@ -152,6 +152,7 @@ namespace Template {
 }
 
 #define MYC C
+#define CLOSE_NO_INIT ] ;
 
 namespace Init {
   template<typename T> T f(T t) {
@@ -171,6 +172,8 @@ namespace Init {
     T t1 = t; // check that uninitialized structured binding declaration error works with templates and macros
     auto [t0, t2] MYC = {t, t1}; // expected-error{{structured binding declaration '[t0, t2]' requires an initializer; expected '=' or braced initializer list}} expected-error{{expected ';' at end of declaration}}
                                  // CHECK: :[[@LINE-1]]:19: error: structured binding declaration '[t0, t2]' requires an initializer; expected '=' or braced initializer list
+    auto [bad4 CLOSE_NO_INIT // expected-error {{structured binding declaration '[bad4]' requires an initializer; expected '=' or braced initializer list}}
+                             // CHECK: :[[@LINE-1]]:10: error: structured binding declaration '[bad4]' requires an initializer; expected '=' or braced initializer list
   }
 }
 


        


More information about the llvm-branch-commits mailing list