Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit dc19f46

Browse files
Rakete1111vsapsai
authored andcommitted
[SemaCXX] Fix ICE with structure bindings to members of template
Summary: Trying to use structure binding with a structure that doesn't implement std::tuple_size, should unpack the data members. When the struct is a template though, clang might hit an assertion (if the type has not been completed before), because CXXRecordDecl::DefinitionData is nullptr. This commit fixes the problem by completing the type while trying to decompose the structured binding. The ICE happens in real world code, for example, when trying to iterate a protobuf generated map with a range-based for loop and structure bindings (because google::protobuf::MapPair is a template and doesn't support std::tuple_size). Reported-by: [email protected] Patch by Daniele Di Proietto Reviewers: #clang, rsmith Reviewed By: #clang, rsmith Subscribers: cpplearner, Rakete1111, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D56974 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352323 91177308-0d34-0410-b5e6-96231b3b80d8 (cherry picked from commit 085dbeb)
1 parent d527b9e commit dc19f46

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Sema/SemaDeclCXX.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,10 @@ static DeclAccessPair findDecomposableBaseClass(Sema &S, SourceLocation Loc,
13011301
static bool checkMemberDecomposition(Sema &S, ArrayRef<BindingDecl*> Bindings,
13021302
ValueDecl *Src, QualType DecompType,
13031303
const CXXRecordDecl *OrigRD) {
1304+
if (S.RequireCompleteType(Src->getLocation(), DecompType,
1305+
diag::err_incomplete_type))
1306+
return true;
1307+
13041308
CXXCastPath BasePath;
13051309
DeclAccessPair BasePair =
13061310
findDecomposableBaseClass(S, Src->getLocation(), OrigRD, BasePath);

test/SemaCXX/cxx1z-decomposition.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,21 @@ struct PR37352 {
8181
void f() { static auto [a] = *this; } // expected-error {{cannot be declared 'static'}}
8282
};
8383

84+
namespace instantiate_template {
85+
86+
template <typename T1, typename T2>
87+
struct pair {
88+
T1 a;
89+
T2 b;
90+
};
91+
92+
const pair<int, int> &f1();
93+
94+
int f2() {
95+
const auto &[a, b] = f1();
96+
return a + b;
97+
}
98+
99+
} // namespace instantiate_template
100+
84101
// FIXME: by-value array copies

0 commit comments

Comments
 (0)