diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e45e016b3d66b..7373025200245 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -765,6 +765,9 @@ Bug Fixes in This Version Fixes (`#77583 `_) - Fix an issue where CTAD fails for function-type/array-type arguments. Fixes (`#51710 `_) +- Fix crashes when using the binding decl from an invalid structured binding. + Fixes (`#67495 `_) and + (`#72198 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index b642f38fa628a..b74ff0b9bfe4c 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -13602,6 +13602,15 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) { CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), Args); if (RecoveryExpr.get()) VDecl->setInit(RecoveryExpr.get()); + // In general, for error recovery purposes, the initalizer doesn't play + // part in the valid bit of the declaration. There are a few exceptions: + // 1) if the var decl has a deduced auto type, and the type cannot be + // deduced by an invalid initializer; + // 2) if the var decl is decompsition decl with a non-deduced type, and + // the initialization fails (e.g. `int [a] = {1, 2};`); + // Case 1) was already handled elsewhere. + if (isa(VDecl)) // Case 2) + VDecl->setInvalidDecl(); return; } diff --git a/clang/test/AST/ast-dump-invalid-initialized.cpp b/clang/test/AST/ast-dump-invalid-initialized.cpp index 1c374ae716a9d..7fcbc41a7be40 100644 --- a/clang/test/AST/ast-dump-invalid-initialized.cpp +++ b/clang/test/AST/ast-dump-invalid-initialized.cpp @@ -24,4 +24,19 @@ void test() { auto b4 = A(1); // CHECK: `-VarDecl {{.*}} invalid b5 'auto' auto b5 = A{1}; -} \ No newline at end of file +} + +void GH72198() { + // CHECK: DecompositionDecl {{.*}} invalid 'int' + int [_, b] = {0, 0}; + [b]{}; +} + +namespace GH67495 { +int get_point(); +void f() { + // CHECK: DecompositionDecl {{.*}} invalid 'int &' + auto& [x, y] = get_point(); + [x, y] {}; +} +}