Skip to content

Commit 1c273c4

Browse files
committed
[clang][AST] Invalidate DecompositionDecl if it has invalid initializer.
Fix #67495, #72198
1 parent 55172b7 commit 1c273c4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13602,6 +13602,15 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
1360213602
CreateRecoveryExpr(Init->getBeginLoc(), Init->getEndLoc(), Args);
1360313603
if (RecoveryExpr.get())
1360413604
VDecl->setInit(RecoveryExpr.get());
13605+
// In general, for error recovery purposes, the initalizer doesn't play
13606+
// part in the valid bit of the declaration. There are a few exceptions:
13607+
// 1) if the var decl has a deduced auto type, and the type cannot be
13608+
// deduced by an invalid initializer;
13609+
// 2) if the var decl is decompsition decl with a concrete type (e.g.
13610+
// `int [a, b] = 1;`), and the initializer is invalid;
13611+
// Case 1) is already handled earlier in this function.
13612+
if (llvm::isa<DecompositionDecl>(VDecl)) // Case 2)
13613+
VDecl->setInvalidDecl();
1360513614
return;
1360613615
}
1360713616

clang/test/AST/ast-dump-invalid-initialized.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,17 @@ void test() {
2424
auto b4 = A(1);
2525
// CHECK: `-VarDecl {{.*}} invalid b5 'auto'
2626
auto b5 = A{1};
27-
}
27+
}
28+
29+
void pr72198() {
30+
// CHECK: DecompositionDecl {{.*}} invalid 'int'
31+
int [_, b] = {0, 0};
32+
[b]{};
33+
}
34+
35+
int get_point();
36+
void pr67495() {
37+
// CHECK: DecompositionDecl {{.*}} invalid 'int &'
38+
auto& [x, y] = get_point();
39+
[x, y] {};
40+
}

0 commit comments

Comments
 (0)