r352323 - [SemaCXX] Fix ICE with structure bindings to members of template

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 27 12:06:15 PST 2019


Hi Hans,

This is a safe change that fixes a crash; it'd be good to get this into
clang 8.

Thanks!

On Sun, 27 Jan 2019, 11:19 Nicolas Lesser via cfe-commits, <
cfe-commits at lists.llvm.org> wrote:

> Author: rakete1111
> Date: Sun Jan 27 11:19:59 2019
> New Revision: 352323
>
> URL: http://llvm.org/viewvc/llvm-project?rev=352323&view=rev
> Log:
> [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: nicholas.sun at nlsun.com
>
> 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
>
>
> Modified:
>     cfe/trunk/lib/Sema/SemaDeclCXX.cpp
>     cfe/trunk/test/SemaCXX/cxx1z-decomposition.cpp
>
> Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=352323&r1=352322&r2=352323&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Sun Jan 27 11:19:59 2019
> @@ -1300,6 +1300,10 @@ static DeclAccessPair findDecomposableBa
>  static bool checkMemberDecomposition(Sema &S, ArrayRef<BindingDecl*>
> Bindings,
>                                       ValueDecl *Src, QualType DecompType,
>                                       const CXXRecordDecl *OrigRD) {
> +  if (S.RequireCompleteType(Src->getLocation(), DecompType,
> +                            diag::err_incomplete_type))
> +    return true;
> +
>    CXXCastPath BasePath;
>    DeclAccessPair BasePair =
>        findDecomposableBaseClass(S, Src->getLocation(), OrigRD, BasePath);
>
> Modified: cfe/trunk/test/SemaCXX/cxx1z-decomposition.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx1z-decomposition.cpp?rev=352323&r1=352322&r2=352323&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/cxx1z-decomposition.cpp (original)
> +++ cfe/trunk/test/SemaCXX/cxx1z-decomposition.cpp Sun Jan 27 11:19:59 2019
> @@ -81,4 +81,21 @@ struct PR37352 {
>    void f() { static auto [a] = *this; } // expected-error {{cannot be
> declared 'static'}}
>  };
>
> +namespace instantiate_template {
> +
> +template <typename T1, typename T2>
> +struct pair {
> +  T1 a;
> +  T2 b;
> +};
> +
> +const pair<int, int> &f1();
> +
> +int f2() {
> +  const auto &[a, b] = f1();
> +  return a + b;
> +}
> +
> +} // namespace instantiate_template
> +
>  // FIXME: by-value array copies
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190127/d6fe2f34/attachment.html>


More information about the cfe-commits mailing list