[PATCH] D115248: [Clang] Fix PR28101

PoYao Chang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 18:46:53 PDT 2022


rZhBoYao updated this revision to Diff 417461.
rZhBoYao retitled this revision from "[clang] Fix PR28101" to "[Clang] Fix PR28101".
rZhBoYao edited the summary of this revision.
rZhBoYao added a reviewer: cor3ntin.
rZhBoYao set the repository for this revision to rG LLVM Github Monorepo.
rZhBoYao added a comment.
Herald added a project: All.

The previous diff was indeed very specific and doesn't handle

  template <typename T>
  struct A {
    A(void*) {}
    T A<T>{}; // expected-error{{member 'A' cannot have template arguments}}
  };
  
  A<int> instantiate() { return {nullptr}; }

I have since realized the invalid decorator is the problem and the parentheses are insignificant. It's like:

  template <typename T> struct S {};
  int S<int>{};

which gcc diagnoses to

> error: invalid declarator before '{' token

Then I found there was a new diagnostic added by D120881 <https://reviews.llvm.org/D120881>. So the fix is pretty straightforward at this point. I simply stand on the shoulder of the giant and set the invalid flag of the declarator.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115248/new/

https://reviews.llvm.org/D115248

Files:
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/PR28101.cpp


Index: clang/test/SemaCXX/PR28101.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/PR28101.cpp
@@ -0,0 +1,56 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_1 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_2 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_3 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -DCASE_4 -std=c++17 %s
+
+// Don't crash.
+
+#ifdef CASE_1
+
+template <typename T>
+struct A {
+  A(void *) {}
+  T(A<T>){}; // expected-error{{member 'A' cannot have template arguments}}
+};
+
+A<int> instantiate() { return {nullptr}; }
+
+#elifdef CASE_2
+
+template <typename T>
+struct A {
+  A(void *) {}
+  T A<T>{}; // expected-error{{member 'A' cannot have template arguments}}
+};
+
+A<int> instantiate() { return {nullptr}; }
+
+#elifdef CASE_3
+
+template <typename T>
+struct S {};
+
+template <typename T>
+struct A {
+  A(void *) {}
+  T S<T>{}; // expected-error{{member 'S' cannot have template arguments}}
+};
+
+A<int> instantiate() { return {nullptr}; }
+
+#elifdef CASE_4
+
+template <typename T, template <typename> typename U>
+class A {
+public:
+  A(void *) {}
+  T(A<T, U<T>>) {} // expected-error{{member 'A' cannot have template arguments}}\
+  // expected-error{{expected ';' at end of declaration list}}
+};
+
+template <typename T>
+struct S {};
+
+A<int, S> foo() { return A<int, S>(nullptr); }
+
+#endif
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -3427,6 +3427,7 @@
           << SourceRange(D.getName().TemplateId->LAngleLoc,
                          D.getName().TemplateId->RAngleLoc)
           << D.getName().TemplateId->LAngleLoc;
+      D.setInvalidType();
     }
 
     if (SS.isSet() && !SS.isInvalid()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115248.417461.patch
Type: text/x-patch
Size: 1860 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220323/f575dde8/attachment.bin>


More information about the cfe-commits mailing list