From: Reid Kleckner Date: Thu, 15 Aug 2019 19:45:28 +0000 (+0000) Subject: [Sema] Implement DR2386 for C++17 structured binding X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6cdb050a2604468310530f1706784b8fdd6ba963;p=clang [Sema] Implement DR2386 for C++17 structured binding Allow implementations to provide complete definitions of std::tuple_size, but to omit the 'value' member to signal that T is not tuple-like. The Microsoft standard library implements std::tuple_size this way. If the value member exists, clang still validates that it is an ICE, but if it does not, then the type is considered to not be tuple-like. Fixes PR33236 Reviewers: rsmith Differential Revision: https://reviews.llvm.org/D66040 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@369043 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index b5d46681b5..2442986eff 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -1030,8 +1030,10 @@ static IsTupleLike isTupleLike(Sema &S, SourceLocation Loc, QualType T, TemplateArgumentListInfo Args(Loc, Loc); Args.addArgument(getTrivialTypeTemplateArgument(S, Loc, T)); - // If there's no tuple_size specialization, it's not tuple-like. - if (lookupStdTypeTraitMember(S, R, Loc, "tuple_size", Args, /*DiagID*/0)) + // If there's no tuple_size specialization or the lookup of 'value' is empty, + // it's not tuple-like. + if (lookupStdTypeTraitMember(S, R, Loc, "tuple_size", Args, /*DiagID*/ 0) || + R.empty()) return IsTupleLike::NotTupleLike; // If we get this far, we've committed to the tuple interpretation, but @@ -1048,11 +1050,6 @@ static IsTupleLike isTupleLike(Sema &S, SourceLocation Loc, QualType T, } } Diagnoser(R, Args); - if (R.empty()) { - Diagnoser.diagnoseNotICE(S, Loc, SourceRange()); - return IsTupleLike::Error; - } - ExprResult E = S.BuildDeclarationNameExpr(CXXScopeSpec(), R, /*NeedsADL*/false); if (E.isInvalid()) diff --git a/test/CXX/dcl.decl/dcl.decomp/p3.cpp b/test/CXX/dcl.decl/dcl.decomp/p3.cpp index b3f0cf1874..9b030c1a2e 100644 --- a/test/CXX/dcl.decl/dcl.decomp/p3.cpp +++ b/test/CXX/dcl.decl/dcl.decomp/p3.cpp @@ -12,7 +12,7 @@ void no_tuple_size_2() { auto [x, y] = A(); } // ok, decompose elementwise struct Bad1 { int a, b; }; template<> struct std::tuple_size {}; -void no_tuple_size_3() { auto [x, y] = Bad1(); } // expected-error {{cannot decompose this type; 'std::tuple_size::value' is not a valid integral constant expression}} +void no_tuple_size_3() { auto [x, y] = Bad1(); } // ok, omitting value is valid after DR2386 struct Bad2 {}; template<> struct std::tuple_size { const int value = 5; }; diff --git a/test/CXX/drs/dr23xx.cpp b/test/CXX/drs/dr23xx.cpp index 8e7a9a880b..763abd5368 100644 --- a/test/CXX/drs/dr23xx.cpp +++ b/test/CXX/drs/dr23xx.cpp @@ -40,6 +40,27 @@ namespace dr2353 { // dr2353: 9 #pragma clang __debug dump not_use_2 } +#if __cplusplus >= 201707L +// Otherwise, if the qualified-id std::tuple_size names a complete class +// type **with a member value**, the expression std::tuple_size::value shall +// be a well-formed integral constant expression +namespace dr2386 { // dr2386: 9 +struct Bad1 { int a, b; }; +struct Bad2 { int a, b; }; +} // namespace dr2386 +namespace std { +template struct tuple_size; +template <> struct std::tuple_size {}; +template <> struct std::tuple_size { + static const int value = 42; +}; +} // namespace std +namespace dr2386 { +void no_value() { auto [x, y] = Bad1(); } +void wrong_value() { auto [x, y] = Bad2(); } // expected-error {{decomposes into 42 elements}} +} // namespace dr2386 +#endif + namespace dr2387 { // dr2387: 9 #if __cplusplus >= 201402L template int a = 0;