[PATCH] D121824: [clang] Do not crash on arrow operator on dependent type.

Adam Czachorowski via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 18 10:36:52 PDT 2022


adamcz updated this revision to Diff 416561.
adamcz added a comment.

changed to marking VarDecl as invalid if it's ref type and initializer is invalid


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D121824

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/AST/ast-dump-invalid-initialized.cpp
  clang/test/SemaCXX/arrow-operator.cpp


Index: clang/test/SemaCXX/arrow-operator.cpp
===================================================================
--- clang/test/SemaCXX/arrow-operator.cpp
+++ clang/test/SemaCXX/arrow-operator.cpp
@@ -65,3 +65,24 @@
 }
 
 } // namespace arrow_suggest
+
+namespace no_crash_dependent_type {
+
+template<class T>
+struct A {
+  void call();
+  A* operator->();
+};
+
+template <class T>
+void foo() {
+  // x is dependent.
+  A<int>& x = blah[7];  // expected-error {{use of undeclared identifier 'blah'}}
+  x->call();
+}
+
+void test() {
+  foo<int>();
+}
+
+} // namespace no_crash_dependent_type
Index: clang/test/AST/ast-dump-invalid-initialized.cpp
===================================================================
--- clang/test/AST/ast-dump-invalid-initialized.cpp
+++ clang/test/AST/ast-dump-invalid-initialized.cpp
@@ -24,4 +24,6 @@
   auto b4 = A(1);
   // CHECK: `-VarDecl {{.*}} invalid b5 'auto'
   auto b5 = A{1};
-}
\ No newline at end of file
+  // CHECK: `-VarDecl {{.*}} invalid b6 'A &'
+  A& b6 = garbage[1];
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -12794,6 +12794,12 @@
     return;
   }
 
+  // Reference type without initializer doesn't work.
+  if (VD->getType()->isReferenceType()) {
+    VD->setInvalidDecl();
+    return;
+  }
+
   QualType Ty = VD->getType();
   if (Ty->isDependentType()) return;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121824.416561.patch
Type: text/x-patch
Size: 1462 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220318/fd716f31/attachment.bin>


More information about the cfe-commits mailing list