r357298 - [Sema] Fix assertion when `auto` parameter in lambda has an attribute.

Volodymyr Sapsai via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 29 11:47:07 PDT 2019


Author: vsapsai
Date: Fri Mar 29 11:47:07 2019
New Revision: 357298

URL: http://llvm.org/viewvc/llvm-project?rev=357298&view=rev
Log:
[Sema] Fix assertion when `auto` parameter in lambda has an attribute.

Fixes the assertion
> no Attr* for AttributedType*
> UNREACHABLE executed at llvm-project/clang/lib/Sema/SemaType.cpp:298!

In `TypeProcessingState::getAttributedType` we put into `AttrsForTypes`
types with `auto` but later in
`TypeProcessingState::takeAttrForAttributedType` we use transformed
types and that's why cannot find `Attr` corresponding to
`AttributedType`.

Fix by keeping `AttrsForTypes` up to date after replacing `AutoType`.

rdar://problem/47689465

Reviewers: rsmith, arphaman, aaron.ballman

Reviewed By: aaron.ballman

Subscribers: jkorous, dexonsmith, jdoerfert, cfe-commits

Differential Revision: https://reviews.llvm.org/D58659


Modified:
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/SemaCXX/auto-cxx0x.cpp

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=357298&r1=357297&r2=357298&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Fri Mar 29 11:47:07 2019
@@ -255,6 +255,23 @@ namespace {
       return T;
     }
 
+    /// Completely replace the \c auto in \p TypeWithAuto by
+    /// \p Replacement. Also replace \p TypeWithAuto in \c TypeAttrPair if
+    /// necessary.
+    QualType ReplaceAutoType(QualType TypeWithAuto, QualType Replacement) {
+      QualType T = sema.ReplaceAutoType(TypeWithAuto, Replacement);
+      if (auto *AttrTy = TypeWithAuto->getAs<AttributedType>()) {
+        // Attributed type still should be an attributed type after replacement.
+        auto *NewAttrTy = cast<AttributedType>(T.getTypePtr());
+        for (TypeAttrPair &A : AttrsForTypes) {
+          if (A.first == AttrTy)
+            A.first = NewAttrTy;
+        }
+        AttrsForTypesSorted = false;
+      }
+      return T;
+    }
+
     /// Extract and remove the Attr* for a given attributed type.
     const Attr *takeAttrForAttributedType(const AttributedType *AT) {
       if (!AttrsForTypesSorted) {
@@ -2938,7 +2955,7 @@ static QualType GetDeclSpecTypeForDeclar
         // template type parameter.
         // FIXME: Retain some type sugar to indicate that this was written
         // as 'auto'.
-        T = SemaRef.ReplaceAutoType(
+        T = state.ReplaceAutoType(
             T, QualType(CorrespondingTemplateParam->getTypeForDecl(), 0));
       }
       break;

Modified: cfe/trunk/test/SemaCXX/auto-cxx0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/auto-cxx0x.cpp?rev=357298&r1=357297&r2=357298&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/auto-cxx0x.cpp (original)
+++ cfe/trunk/test/SemaCXX/auto-cxx0x.cpp Fri Mar 29 11:47:07 2019
@@ -15,3 +15,11 @@ void g() {
   // expected-error at -2 {{'auto' not allowed in lambda parameter}}
 #endif
 }
+
+void rdar47689465() {
+  int x = 0;
+  [](auto __attribute__((noderef)) *){}(&x);
+#if __cplusplus == 201103L
+  // expected-error at -2 {{'auto' not allowed in lambda parameter}}
+#endif
+}




More information about the cfe-commits mailing list